提交 80c516ea 编写于 作者: L Lucas Meneghel Rodrigues 提交者: Lucas Meneghel Rodrigues

Merge pull request #8 from lmr/settings

Settings
"""
Reads the avocado settings from a .ini file (from python ConfigParser).
"""
class Settings(object):
pass
......@@ -7,37 +7,55 @@ import sys
import glob
import shutil
from avocado.settings import settings
_ROOT_PATH = os.path.join(sys.modules[__name__].__file__, "..", "..", "..")
DEFAULT_ROOT_DIR = os.path.abspath(_ROOT_PATH)
DEFAULT_TMP_DIR = os.path.join(DEFAULT_ROOT_DIR, 'tmp')
IN_TREE_ROOT_DIR = os.path.abspath(_ROOT_PATH)
IN_TREE_TMP_DIR = os.path.join(IN_TREE_ROOT_DIR, 'tmp')
def get_root_dir():
return DEFAULT_ROOT_DIR
if settings.intree:
return IN_TREE_ROOT_DIR
else:
return settings.get_value('runner', 'root_dir')
def get_test_dir():
if settings.intree:
return os.path.join(get_root_dir(), 'tests')
else:
return settings.get_value('runner', 'test_dir')
def get_logs_dir():
if settings.intree:
return os.path.join(get_root_dir(), 'logs')
else:
return os.path.expanduser(settings.get_value('runner', 'logs_dir'))
def get_tmp_dir():
if not os.path.isdir(DEFAULT_TMP_DIR):
os.makedirs(DEFAULT_TMP_DIR)
return DEFAULT_TMP_DIR
if settings.intree:
if not os.path.isdir(IN_TREE_TMP_DIR):
os.makedirs(IN_TREE_TMP_DIR)
return IN_TREE_TMP_DIR
else:
return settings.get_value('runner', 'tmp_dir')
def clean_tmp_files():
if os.path.isdir(DEFAULT_TMP_DIR):
hidden_paths = glob.glob(os.path.join(DEFAULT_TMP_DIR, ".??*"))
paths = glob.glob(os.path.join(DEFAULT_TMP_DIR, "*"))
tmp_dir = get_tmp_dir()
if os.path.isdir(tmp_dir):
hidden_paths = glob.glob(os.path.join(tmp_dir, ".??*"))
paths = glob.glob(os.path.join(tmp_dir, "*"))
for path in paths + hidden_paths:
try:
shutil.rmtree(path, ignore_errors=True)
except OSError:
pass
if __name__ == '__main__':
print "root dir: " + DEFAULT_ROOT_DIR
print "tmp dir: " + DEFAULT_TMP_DIR
print "root dir: " + get_root_dir()
print "tmp dir: " + get_tmp_dir()
"""
Reads the avocado settings from a .ini file (from python ConfigParser).
"""
import ConfigParser
import os
import sys
config_filename = 'settings.ini'
_config_dir_system = os.path.join('/etc', 'avocado')
config_path_system = os.path.join(_config_dir_system, config_filename)
_config_dir_local = os.path.join(os.path.expanduser("~"), '.config', 'avocado')
config_path_local = os.path.join(_config_dir_local, config_filename)
_source_tree_root = os.path.join(sys.modules[__name__].__file__, "..", "..")
_config_path_intree = os.path.join(os.path.abspath(_source_tree_root), 'etc')
config_path_intree = os.path.join(_config_path_intree, config_filename)
class SettingsError(Exception):
pass
class SettingsValueError(SettingsError):
pass
class ConfigFileNotFound(SettingsError):
def __init__(self, path_list):
super(ConfigFileNotFound, self).__init__()
self.path_list = path_list
def __str__(self):
return ("Could not find the avocado config file after looking in: %s" %
self.path_list)
def convert_value_type(key, section, value, value_type):
"""
Convert a string to another data type.
"""
# strip off leading and trailing white space
sval = value.strip()
# if length of string is zero then return None
if len(sval) == 0:
if value_type == str:
return ""
elif value_type == bool:
return False
elif value_type == int:
return 0
elif value_type == float:
return 0.0
elif value_type == list:
return []
else:
return None
if value_type == bool:
if sval.lower() == "false":
return False
else:
return True
if value_type == list:
# Split the string using ',' and return a list
return [val.strip() for val in sval.split(',')]
try:
conv_val = value_type(sval)
return conv_val
except Exception:
msg = ("Could not convert %s value %r in section %s to type %s" %
(key, sval, section, value_type))
raise SettingsValueError(msg)
class Settings(object):
no_default = object()
def __init__(self):
self.config = ConfigParser.ConfigParser()
config_system = os.path.exists(config_path_system)
config_local = os.path.exists(config_path_local)
config_intree = os.path.exists(config_path_intree)
self.intree = False
if not config_local and not config_system:
if not config_intree:
raise ConfigFileNotFound([config_path_system,
config_path_local,
config_path_intree])
self.config_path = config_path_intree
self.intree = True
else:
if config_local:
self.config_path = config_path_local
else:
self.config_path = config_path_system
self.parse_file()
def parse_file(self):
self.config.read(self.config_path)
def _handle_no_value(self, section, key, default):
if default is self.no_default:
msg = ("Value '%s' not found in section '%s'" %
(key, section))
raise SettingsError(msg)
else:
return default
def get_value(self, section, key, key_type=str, default=no_default,
allow_blank=False):
try:
val = self.config.get(section, key)
except ConfigParser.Error:
return self._handle_no_value(section, key, default)
if not val.strip() and not allow_blank:
return self._handle_no_value(section, key, default)
return convert_value_type(key, section, val, key_type)
def get_section_values(self, sections):
"""
Return a config parser object containing a single section of the
global configuration, that can be later written to a file object.
:param section: Tuple with sections we want to turn into a config parser
object.
:return: ConfigParser() object containing all the contents of sections.
"""
if isinstance(sections, str):
sections = [sections]
cfgparser = ConfigParser.ConfigParser()
for section in sections:
cfgparser.add_section(section)
for option, value in self.config.items(section):
cfgparser.set(section, option, value)
return cfgparser
settings = Settings()
[runner]
root_dir = /usr/share/avocado
test_dir = /usr/share/avocado/tests
logs_dir = ~/avocado/logs
tmp_dir = /tmp
import glob
from distutils.core import setup
import avocado.version
......@@ -10,9 +11,13 @@ setup(name='avocado',
url='http://autotest.github.com',
packages=['avocado',
'avocado.cli',
'avocado.conf',
'avocado.core',
'avocado.linux',
'avocado.utils',
'avocado.plugins'],
data_files=[('/etc/avocado', ['etc/settings.ini']),
('/usr/share/avocado/tests/sleeptest', glob.glob('tests/sleeptest/*')),
('/usr/share/avocado/tests/failtest', glob.glob('tests/failtest/*')),
('/usr/share/avocado/tests/synctest', glob.glob('tests/synctest/synctest.py')),
('/usr/share/avocado/tests/synctest/deps', glob.glob('tests/synctest/deps/synctest.tar.bz2'))],
scripts=['scripts/avocado'])
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册