diff options
-rw-r--r-- | g_octave/config.py | 59 | ||||
-rw-r--r-- | g_octave/ebuild.py | 11 | ||||
-rw-r--r-- | g_octave/overlay.py | 56 | ||||
-rw-r--r-- | tests/test_overlay.py | 62 |
4 files changed, 139 insertions, 49 deletions
diff --git a/g_octave/config.py b/g_octave/config.py index 8609df6..05fe74c 100644 --- a/g_octave/config.py +++ b/g_octave/config.py @@ -1,6 +1,17 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +""" + config.py + ~~~~~~~~~ + + This module implements a Python object to handle the configuration + of g-octave. + + :copyright: (c) 2009-2010 by Rafael Goncalves Martins + :license: GPL-2, see LICENSE for more details. +""" + __all__ = ['Config'] import ConfigParser @@ -11,59 +22,59 @@ from exception import ConfigException class Config(object): - __defaults = { + _defaults = { 'db': '/var/cache/g-octave', 'overlay': '/usr/local/portage/g-octave', 'categories': 'main,extra,language', 'db_mirror': 'http://files.rafaelmartins.eng.br/octave-forge', } - __section_name = 'main' + _section_name = 'main' def __init__(self, fetch_phase=False, config_file=None, create_dirs=True): # Config Parser - self.__config = ConfigParser.ConfigParser(self.__defaults) + self._config = ConfigParser.ConfigParser(self._defaults) + + self._fetch_phase = fetch_phase if config_file is not None: - self.__config_file = config_file + self._config_file = config_file elif os.path.exists('../etc/g-octave.cfg.devel'): - self.__config_file = '../etc/g-octave.cfg.devel' + self._config_file = '../etc/g-octave.cfg.devel' else: - self.__config_file = '/etc/g-octave.cfg' + self._config_file = '/etc/g-octave.cfg' - self.__config.read(self.__config_file) + self._config.read(self._config_file) - __db = self.__config.get(self.__section_name, 'db') - __overlay = self.__config.get(self.__section_name, 'overlay') + _db = self._config.get(self._section_name, 'db') + _overlay = self._config.get(self._section_name, 'overlay') - for dir in [__db, __overlay]: + for dir in [_db, _overlay]: if not os.path.exists(dir) and create_dirs: os.makedirs(dir, 0755) if not fetch_phase: # Cache (JSON) - cache_file = os.path.join(__db, 'cache.json') - fp = open(cache_file) - self.__cache = json.load(fp) - fp.close() + cache_file = os.path.join(_db, 'cache.json') + with open(cache_file) as fp: + self._cache = json.load(fp) # JSON - json_file = os.path.join(__db, self.__cache['files']['info.json']) - fp = open(json_file) - self.__info = json.load(fp) - fp.close() + json_file = os.path.join(_db, self._cache['files']['info.json']) + with open(json_file) as fp: + self._info = json.load(fp) def __getattr__(self, attr): - if self.__defaults.has_key(attr): - return self.__config.get(self.__section_name, attr) - elif self.__info.has_key(attr): - return self.__info[attr] - elif attr == 'cache': - return self.__cache['files'] + if attr in self._defaults: + return self._config.get(self._section_name, attr) + elif not self._fetch_phase and attr in self._info: + return self._info[attr] + elif not self._fetch_phase and attr == 'cache': + return self._cache['files'] else: raise ConfigException('Invalid option: %s' % attr) diff --git a/g_octave/ebuild.py b/g_octave/ebuild.py index b2e9ece..683cc01 100644 --- a/g_octave/ebuild.py +++ b/g_octave/ebuild.py @@ -1,6 +1,17 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +""" + ebuild.py + ~~~~~~~~~ + + This module implements a Python class responsible to create the ebuilds + for the octave-forge packages and track the dependencies correctly. + + :copyright: (c) 2009-2010 by Rafael Goncalves Martins + :license: GPL-2, see LICENSE for more details. +""" + __all__ = [ 'Ebuild', 're_keywords', diff --git a/g_octave/overlay.py b/g_octave/overlay.py index f8904c6..4f3b542 100644 --- a/g_octave/overlay.py +++ b/g_octave/overlay.py @@ -1,27 +1,41 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -__all__ = ['create_overlay'] +""" + overlay.py + ~~~~~~~~~~ + + This module implements a function to create an overlay to host the + ebuilds generated by g-octave. + + :copyright: (c) 2009-2010 by Rafael Goncalves Martins + :license: GPL-2, see LICENSE for more details. +""" -from config import Config -conf = Config() +__all__ = ['create_overlay'] import os import sys import shutil import portage.output +from config import Config + out = portage.output.EOutput() -def create_overlay(force=False): +def create_overlay(force=False, conf=None, quiet=False): - if force: - if os.path.exists(conf.overlay): - shutil.rmtree(conf.overlay) + # the function parameter conf is used by the tests + if conf is None: + conf = Config() + + if force and os.path.exists(conf.overlay): + shutil.rmtree(conf.overlay) if not os.path.exists(os.path.join(conf.overlay, 'profiles', 'repo_name')): - out.ebegin('Creating overlay: %s' % conf.overlay) + if not quiet: + out.ebegin('Creating overlay: %s' % conf.overlay) try: # creating dirs @@ -39,23 +53,15 @@ def create_overlay(force=False): } for _file in files: if not os.path.exists(_file) or force: - __create_file(_file, files[_file]) + with open(_file, 'w', 0644) as fp: + content = files[_file] + if isinstanceof(content, file): + content = content.read() + fp.write(content) except Exception, error: - out.eend(1) - # TODO: remove this from here! - out.eerror('Failed to create the overlay.') + if not quiet: + out.eend(1) sys.exit(1) else: - out.eend(0) - - -def __create_file(_file, content): - - if type(content) == file: - content = content.read() - - fp = open(_file, 'w', 0644) - fp.write(content) - fp.close() - - type(content) == file and content.close() + if not quiet: + out.eend(0) diff --git a/tests/test_overlay.py b/tests/test_overlay.py new file mode 100644 index 0000000..c5d854f --- /dev/null +++ b/tests/test_overlay.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + test_overlay.py + ~~~~~~~~~~~~~~~ + + test suite for the *g_octave.overlay* module + + :copyright: (c) 2010 by Rafael Goncalves Martins + :license: GPL-2, see LICENSE for more details. +""" + +import ConfigParser +import os +import shutil +import tempfile +import unittest + +from g_octave import config, overlay + + +class TestOverlay(unittest.TestCase): + + def setUp(self): + self._config_file = tempfile.mkstemp(suffix='.cfg')[1] + self._dir = tempfile.mkdtemp() + + # the directories doesn't need to be created. the config module + # will do this + self._db = os.path.join(self._dir, 'db') + self._cache = os.path.join(self._dir, 'cache') + self._overlay = os.path.join(self._dir, 'overlay') + + cp = ConfigParser.ConfigParser() + cp.add_section('main') + cp.set('main', 'db', self._db) + cp.set('main', 'cache', self._cache) + cp.set('main', 'overlay', self._overlay) + + with open(self._config_file, 'w') as fp: + cp.write(fp) + + self._config = config.Config( + fetch_phase = True, + config_file = self._config_file, + create_dirs = True + ) + + def test_overlay(self): + overlay.create_overlay(conf = self._config, quiet = True) + + def tearDown(self): + shutil.rmtree(self._dir) + os.unlink(self._config_file) + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(TestOverlay('test_overlay')) + return suite + |