summaryrefslogtreecommitdiffstats
path: root/dashboard/backend/dovetail/utils
diff options
context:
space:
mode:
Diffstat (limited to 'dashboard/backend/dovetail/utils')
-rwxr-xr-xdashboard/backend/dovetail/utils/__init__.py8
-rwxr-xr-xdashboard/backend/dovetail/utils/flags.py82
-rwxr-xr-xdashboard/backend/dovetail/utils/logsetting.py98
-rwxr-xr-xdashboard/backend/dovetail/utils/setting_wrapper.py18
-rwxr-xr-xdashboard/backend/dovetail/utils/util.py71
5 files changed, 0 insertions, 277 deletions
diff --git a/dashboard/backend/dovetail/utils/__init__.py b/dashboard/backend/dovetail/utils/__init__.py
deleted file mode 100755
index 6dbd8d79..00000000
--- a/dashboard/backend/dovetail/utils/__init__.py
+++ /dev/null
@@ -1,8 +0,0 @@
-##############################################################################
-# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
diff --git a/dashboard/backend/dovetail/utils/flags.py b/dashboard/backend/dovetail/utils/flags.py
deleted file mode 100755
index dd10670b..00000000
--- a/dashboard/backend/dovetail/utils/flags.py
+++ /dev/null
@@ -1,82 +0,0 @@
-##############################################################################
-# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-import sys
-
-from optparse import OptionParser
-
-
-class Flags(object):
- """Class to store flags."""
-
- PARSER = OptionParser()
- PARSED_OPTIONS = None
-
- @classmethod
- def parse_args(cls):
- """parse args."""
- (options, argv) = Flags.PARSER.parse_args()
- sys.argv = [sys.argv[0]] + argv
- Flags.PARSED_OPTIONS = options
-
- def __getattr__(self, name):
- if Flags.PARSED_OPTIONS and hasattr(Flags.PARSED_OPTIONS, name):
- return getattr(Flags.PARSED_OPTIONS, name)
-
- for option in Flags.PARSER.option_list:
- if option.dest == name:
- return option.default
-
- raise AttributeError('Option instance has no attribute %s' % name)
-
- def __setattr__(self, name, value):
- if Flags.PARSED_OPTIONS and hasattr(Flags.PARSED_OPTIONS, name):
- setattr(Flags.PARSED_OPTIONS, name, value)
- return
-
- for option in Flags.PARSER.option_list:
- if option.dest == name:
- option.default = value
- return
-
- object.__setattr__(self, name, value)
-
-
-OPTIONS = Flags()
-
-
-def init():
- """Init flag parsing."""
- OPTIONS.parse_args()
-
-
-def add(flagname, **kwargs):
- """Add a flag name and its setting.
-
- :param flagname: flag name declared in cmd as --<flagname>=...
- :type flagname: str
- """
- Flags.PARSER.add_option('--%s' % flagname,
- dest=flagname, **kwargs)
-
-
-def add_bool(flagname, default=True, **kwargs):
- """Add a bool flag name and its setting.
-
- :param flagname: flag name declared in cmd as --[no]<flagname>.
- :type flagname: str
- :param default: default value
- :type default: bool
- """
- Flags.PARSER.add_option('--%s' % flagname,
- dest=flagname, default=default,
- action="store_true", **kwargs)
- Flags.PARSER.add_option('--no%s' % flagname,
- dest=flagname,
- action="store_false", **kwargs)
diff --git a/dashboard/backend/dovetail/utils/logsetting.py b/dashboard/backend/dovetail/utils/logsetting.py
deleted file mode 100755
index 27255688..00000000
--- a/dashboard/backend/dovetail/utils/logsetting.py
+++ /dev/null
@@ -1,98 +0,0 @@
-##############################################################################
-# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-import logging
-import logging.handlers
-import os
-import os.path
-import sys
-
-from dovetail.utils import flags
-from dovetail.utils import setting_wrapper as setting
-
-
-flags.add('loglevel',
- help='logging level', default=setting.DEFAULT_LOGLEVEL)
-flags.add('logdir',
- help='logging directory', default=setting.DEFAULT_LOGDIR)
-flags.add('logfile',
- help='logging filename', default=None)
-flags.add('log_interval', type='int',
- help='log interval', default=setting.DEFAULT_LOGINTERVAL)
-flags.add('log_interval_unit',
- help='log interval unit', default=setting.DEFAULT_LOGINTERVAL_UNIT)
-flags.add('log_format',
- help='log format', default=setting.DEFAULT_LOGFORMAT)
-flags.add('log_backup_count', type='int',
- help='log backup count', default=setting.DEFAULT_LOGBACKUPCOUNT)
-
-
-# mapping str setting in flag --loglevel to logging level.
-LOGLEVEL_MAPPING = {
- 'finest': logging.DEBUG - 2, # more detailed log.
- 'fine': logging.DEBUG - 1, # detailed log.
- 'debug': logging.DEBUG,
- 'info': logging.INFO,
- 'warning': logging.WARNING,
- 'error': logging.ERROR,
- 'critical': logging.CRITICAL,
-}
-
-
-logging.addLevelName(LOGLEVEL_MAPPING['fine'], 'fine')
-logging.addLevelName(LOGLEVEL_MAPPING['finest'], 'finest')
-
-
-# disable logging when logsetting.init not called
-logging.getLogger().setLevel(logging.CRITICAL)
-
-
-def getLevelByName(level_name):
- """Get log level by level name."""
- return LOGLEVEL_MAPPING[level_name]
-
-
-def init():
- """Init loggsetting. It should be called after flags.init."""
- loglevel = flags.OPTIONS.loglevel.lower()
- logdir = flags.OPTIONS.logdir
- logfile = flags.OPTIONS.logfile
- logger = logging.getLogger()
- if logger.handlers:
- for handler in logger.handlers:
- logger.removeHandler(handler)
-
- if logdir:
- if not logfile:
- logfile = './%s.log' % os.path.basename(sys.argv[0])
-
- handler = logging.handlers.TimedRotatingFileHandler(
- os.path.join(logdir, logfile),
- when=flags.OPTIONS.log_interval_unit,
- interval=flags.OPTIONS.log_interval,
- backupCount=flags.OPTIONS.log_backup_count)
- else:
- if not logfile:
- handler = logging.StreamHandler(sys.stderr)
- else:
- handler = logging.handlers.TimedRotatingFileHandler(
- logfile,
- when=flags.OPTIONS.log_interval_unit,
- interval=flags.OPTIONS.log_interval,
- backupCount=flags.OPTIONS.log_backup_count)
-
- if loglevel in LOGLEVEL_MAPPING:
- logger.setLevel(LOGLEVEL_MAPPING[loglevel])
- handler.setLevel(LOGLEVEL_MAPPING[loglevel])
-
- formatter = logging.Formatter(
- flags.OPTIONS.log_format)
-
- handler.setFormatter(formatter)
- logger.addHandler(handler)
diff --git a/dashboard/backend/dovetail/utils/setting_wrapper.py b/dashboard/backend/dovetail/utils/setting_wrapper.py
deleted file mode 100755
index bb390ada..00000000
--- a/dashboard/backend/dovetail/utils/setting_wrapper.py
+++ /dev/null
@@ -1,18 +0,0 @@
-##############################################################################
-# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-
-DEFAULT_LOGLEVEL = 'debug'
-DEFAULT_LOGDIR = '/var/log/dovetail/'
-DEFAULT_LOGINTERVAL = 30
-DEFAULT_LOGINTERVAL_UNIT = 'M'
-DEFAULT_LOGFORMAT = (
- '%(asctime)s - %(filename)s - %(lineno)d - %(levelname)s - %(message)s')
-DEFAULT_LOGBACKUPCOUNT = 10
-WEB_LOGFILE = 'dovetail_web.log'
diff --git a/dashboard/backend/dovetail/utils/util.py b/dashboard/backend/dovetail/utils/util.py
deleted file mode 100755
index bfd257d7..00000000
--- a/dashboard/backend/dovetail/utils/util.py
+++ /dev/null
@@ -1,71 +0,0 @@
-##############################################################################
-# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-
-import datetime
-import re
-import sys
-
-
-def format_datetime(date_time):
- """Generate string from datetime object."""
- return date_time.strftime("%Y-%m-%d %H:%M:%S")
-
-
-def parse_time_interval(time_interval_str):
- """parse string of time interval to time interval.
-
- supported time interval unit: ['d', 'w', 'h', 'm', 's']
- Examples:
- time_interval_str: '3d 2h' time interval to 3 days and 2 hours.
- """
- if not time_interval_str:
- return 0
-
- time_interval_tuple = [
- time_interval_element
- for time_interval_element in time_interval_str.split(' ')
- if time_interval_element
- ]
- time_interval_dict = {}
- time_interval_unit_mapping = {
- 'd': 'days',
- 'w': 'weeks',
- 'h': 'hours',
- 'm': 'minutes',
- 's': 'seconds'
- }
- for time_interval_element in time_interval_tuple:
- mat = re.match(r'^([+-]?\d+)(w|d|h|m|s).*', time_interval_element)
- if not mat:
- continue
-
- time_interval_value = int(mat.group(1))
- time_interval_unit = time_interval_unit_mapping[mat.group(2)]
- time_interval_dict[time_interval_unit] = (
- time_interval_dict.get(time_interval_unit, 0) + time_interval_value
- )
-
- time_interval = datetime.timedelta(**time_interval_dict)
- if sys.version_info[0:2] > (2, 6):
- return time_interval.total_seconds()
- else:
- return (
- time_interval.microseconds + (
- time_interval.seconds + time_interval.days * 24 * 3600
- ) * 1e6
- ) / 1e6
-
-
-def pretty_print(*contents):
- """pretty print contents."""
- if len(contents) == 0:
- print ""
- else:
- print "\n".join(content for content in contents)