def write_db(self): tmpdir = tempfile.mkdtemp(prefix='laymantmp_') test_xml = os.path.join(tmpdir, 'test.xml') test_json = os.path.join(tmpdir, 'test.json') config = BareConfig() a = DbBase(config, [ HERE + '/testfiles/global-overlays.xml', ]) b = DbBase({ 'output': Message(), 'db_type': 'xml' }, [ test_xml, ]) b.overlays['wrobel-stable'] = a.overlays['wrobel-stable'] b.write(test_xml) c = DbBase({ 'output': Message(), 'db_type': 'xml' }, [ test_xml, ]) keys = sorted(c.overlays) self.assertEqual(keys, ['wrobel-stable']) config.set_option('db_type', 'json') a = DbBase(config, [ HERE + '/testfiles/global-overlays.json', ]) b = DbBase({ 'output': Message(), 'db_type': 'json' }, [ test_json, ]) b.overlays['twitch153'] = a.overlays['twitch153'] b.write(test_json) c = DbBase({ 'output': Message(), 'db_type': 'json' }, [ test_json, ]) keys = sorted(c.overlays) self.assertEqual(keys, ['twitch153']) # Clean up: os.unlink(test_xml) os.unlink(test_json) shutil.rmtree(tmpdir)
def read_db(self): output = Message() # First test if XML databasing works. config = { 'output': output, 'db_type': 'xml', } db = DbBase(config, [ HERE + '/testfiles/global-overlays.xml', ]) keys = sorted(db.overlays) self.assertEqual(keys, ['wrobel', 'wrobel-stable']) url = ['rsync://gunnarwrobel.de/wrobel-stable'] self.assertEqual(list(db.overlays['wrobel-stable'].source_uris()), url) # Test JSON databasing after. config['db_type'] = 'json' db = DbBase(config, [ HERE + '/testfiles/global-overlays.json', ]) keys = sorted(db.overlays) self.assertEqual(keys, ['twitch153', 'wrobel-stable']) url = ['git://github.com/twitch153/ebuilds.git'] self.assertEqual(list(db.overlays['twitch153'].source_uris()), url)
def __init__(self, stdout=sys.stdout, stdin=sys.stdin, stderr=sys.stderr, config=None, read_configfile=True, quiet=False, quietness=4, verbose=False, nocolor=False, width=0, root=None): """Input parameters are optional to override the defaults. sets up our LaymanAPI with defaults or passed in values and returns an instance of it""" self.message = Message(out=stdout, err=stderr) self.config = BareConfig(output=self.message, stdout=stdout, stdin=stdin, stderr=stderr, config=config, read_configfile=read_configfile, quiet=quiet, quietness=quietness, verbose=verbose, nocolor=nocolor, width=width, root=root) LaymanAPI.__init__(self, self.config, report_errors=True, output=self.config['output']) return
def get_input(msg, color='green', output=None): ''' py2, py3 compatibility function to obtain user input. @params msg: message prompt for user @rtype str: input from user ''' if not output: output = Message() try: value = raw_input(' %s %s' % (output.color_func(color, '*'), msg)) except NameError: value = input(' %s %s' % (output.color_func(color, '*'), msg)) return value
def getshortlist(self): document = ET.parse(HERE + '/testfiles/global-overlays.xml') overlays = document.findall('overlay') + document.findall('repo') output = Message() ovl = Overlay({'output': output, 'db_type': 'xml'}, xml=overlays[0]) test_short_list = 'wrobel [Subversion] '\ '(https://o.g.o/svn/dev/wrobel )' self.assertEqual(ovl.short_list(80).decode('utf-8'), test_short_list) print(ovl.short_list(80).decode('utf-8'))
def delete_empty_directory(mdir, output=None): # test for a usable output parameter, # and make it usable if not if output is None: output = Message() if os.path.exists(mdir) and not os.listdir(mdir): # Check for sufficient privileges if os.access(mdir, os.W_OK): output.info('Deleting _empty_ directory "%s"' % mdir, 2) try: os.rmdir(mdir) except OSError as error: output.warn(str(error)) else: output.warn( 'Insufficient permissions to delete _empty_ folder "%s".' % mdir) import getpass if getpass.getuser() != 'root': output.warn('Hint: You are not root.')
def getinfostr(self): document = ET.parse(HERE + '/testfiles/global-overlays.xml') overlays = document.findall('overlay') + document.findall('repo') output = Message() ovl = Overlay({'output': output, 'db_type': 'xml'}, xml=overlays[0]) test_infostr = 'wrobel\n~~~~~~\nSource : '\ 'https://overlays.gentoo.org/svn/dev/wrobel\nContact '\ ': [email protected]\nType : Subversion; Priority: '\ '10\nQuality : experimental\n\nDescription:\n Test\n' self.assertEqual(ovl.get_infostr().decode('utf-8'), test_infostr) print(ovl.get_infostr().decode('utf-8'))
def delete_empty_directory(mdir, output=None): # test for a usable output parameter, # and make it usable if not if output is None: output = Message() if os.path.exists(mdir) and not os.listdir(mdir): # Check for sufficient privileges if os.access(mdir, os.W_OK): output.info('Deleting _empty_ directory "%s"' % mdir, 2) try: os.rmdir(mdir) except OSError as error: output.warn(str(error)) else: output.warn('Insufficient permissions to delete _empty_ folder "%s".' % mdir) import getpass if getpass.getuser() != 'root': output.warn('Hint: You are not root.')
def objattribs(self): document = ET.parse(HERE + '/testfiles/global-overlays.xml') overlays = document.findall('overlay') + document.findall('repo') output = Message() ovl_a = Overlay({'output': output, 'db_type': 'xml'}, xml=overlays[0]) self.assertEqual(ovl_a.name, 'wrobel') self.assertEqual(ovl_a.is_official(), True) url = ['https://overlays.gentoo.org/svn/dev/wrobel'] self.assertEqual(list(ovl_a.source_uris()), url) self.assertEqual(ovl_a.owners[0]['email'], '*****@*****.**') self.assertEqual(ovl_a.descriptions, ['Test']) self.assertEqual(ovl_a.priority, 10) ovl_b = Overlay({'output': output, 'db_type': 'xml'}, xml=overlays[1]) self.assertEqual(ovl_b.is_official(), False)
def __init__(self, options=None, defaults=None, root=None): """ @param options: dictionary of {'option': value, ...} @rtype OptionConfig class instance. """ BareConfig.__init__(self, root=root) if options and 'output' in options: self.output = options['output'] else: self.output = Message() self.update_defaults(defaults) self.update(options) return
def __init__(self, path=None, namepath=None, output=None): if path: self._module_path = path else: self._module_path = os.path.join( (os.path.dirname(os.path.realpath(__file__))), 'modules') if namepath: self._namepath = namepath else: self._namepath = '.'.join( os.path.dirname(os.path.realpath(__file__)), 'modules') if output: self.output = output else: self.output = Message() self._modules = self._get_all_modules() self.module_names = sorted(self._modules)
def _get_layman_api(self): ''' Initializes layman api. @rtype layman.api.LaymanAPI instance ''' # Make it so that we aren't initializing the # LaymanAPI instance if it already exists and # if the current storage location hasn't been # changed for the new repository. self.storage = self.repo.location.replace(self.repo.name, '') if self._layman and self.storage in self.current_storage: return self._layman config = BareConfig() configdir = {'configdir': config.get_option('configdir')} self.message = Message(out=sys.stdout, err=sys.stderr) self.current_storage = self.storage options = { 'config': config.get_option('config') % (configdir), 'quiet': self.settings.get('PORTAGE_QUIET'), 'quietness': config.get_option('quietness'), 'overlay_defs': config.get_option('overlay_defs') % (configdir), 'output': self.message, 'nocolor': self.settings.get('NOCOLOR'), 'root': self.settings.get('EROOT'), 'storage': self.current_storage, 'verbose': self.settings.get('PORTAGE_VERBOSE'), 'width': self.settings.get('COLUMNWIDTH'), } self.config = OptionConfig(options=options, root=options['root']) # Reloads config to read custom overlay # xml files. reload_config(self.config) layman_api = LaymanAPI(self.config, report_errors=True, output=self.config['output']) self._layman = layman_api return layman_api
def select_db(self): output = Message() config = { 'output': output, 'db_type': 'xml', } db = DbBase(config, [ HERE + '/testfiles/global-overlays.xml', ]) url = ['rsync://gunnarwrobel.de/wrobel-stable'] self.assertEqual(list(db.select('wrobel-stable').source_uris()), url) config['db_type'] = 'json' db = DbBase(config, [ HERE + '/testfiles/global-overlays.json', ]) url = ['git://github.com/twitch153/ebuilds.git'] self.assertEqual(list(db.select('twitch153').source_uris()), url)
def list_db(self): output = Message() config = { 'output': output, 'db_type': 'xml', 'svn_command': '/usr/bin/svn', 'rsync_command': '/usr/bin/rsync' } db = DbBase(config, [ HERE + '/testfiles/global-overlays.xml', ]) test_info = ('wrobel\n~~~~~~\nSource : '\ 'https://overlays.gentoo.org/svn/dev/wrobel\nContact : '\ '[email protected]\nType : Subversion; Priority: 10\n'\ 'Quality : experimental\n\nDescription:\n Test\n', 'wrobel-stable\n~~~~~~~~~~~~~\nSource : '\ 'rsync://gunnarwrobel.de/wrobel-stable\nContact : '\ '[email protected]\nType : Rsync; Priority: 50\n'\ 'Quality : experimental\n\nDescription:\n A collection '\ 'of ebuilds from Gunnar Wrobel [[email protected]].\n') info = db.list(verbose=True) for i in range(0, len(info)): self.assertEqual(info[i][0].decode('utf-8'), test_info[i]) print(info[i][0].decode('utf-8')) test_info = ('wrobel [Subversion] '\ '(https://o.g.o/svn/dev/wrobel )', 'wrobel-stable [Rsync ] '\ '(rsync://gunnarwrobel.de/wrobel-stable)') info = db.list(verbose=False, width=80) for i in range(0, len(info)): self.assertEqual(info[i][0].decode('utf-8'), test_info[i]) print(info[i][0].decode('utf-8'))
def __init__(self, output=None, stdout=None, stdin=None, stderr=None, config=None, read_configfile=False, quiet=False, quietness=4, verbose=False, nocolor=False, width=0, root=None): ''' Creates a bare config with defaults and a few output options. ''' if root is None: self.root = '' else: self.root = root self._defaults = { 'configdir': path([self.root, EPREFIX, '/etc/layman']), 'config': '%(configdir)s/layman.cfg', 'storage': path([self.root, EPREFIX, '/var/lib/layman']), 'cache': '%(storage)s/cache', 'local_list': '%(storage)s/overlays.xml', 'installed': '%(storage)s/installed.xml', 'protocol_filter': [], 'auto_sync': 'No', 'check_official': 'Yes', 'conf_type': 'repos.conf', 'db_type': 'xml', 'require_repoconfig': 'Yes', 'clean_archive': 'yes', 'make_conf': '%(storage)s/make.conf', 'repos_conf': path([self.root, EPREFIX, '/etc/portage/repos.conf/layman.conf']), 'conf_module': ['make_conf', 'repos_conf'], 'nocheck': 'yes', 'http_proxy': '', 'https_proxy': '', 'umask': '0022', 'news_reporter': 'portage', 'custom_news_pkg': '', 'gpg_detached_lists': 'https://api.gentoo.org/overlays/repositories.xml https://api.gentoo.org/overlays/repositories.xml.asc', 'gpg_signed_lists': '', 'overlays': 'https://api.gentoo.org/overlays/repositories.xml', 'overlay_defs': '%(configdir)s/overlays', 'bzr_command': path([self.root, EPREFIX, '/usr/bin/bzr']), 'cvs_command': path([self.root, EPREFIX, '/usr/bin/cvs']), 'darcs_command': path([self.root, EPREFIX, '/usr/bin/darcs']), 'git_command': path([self.root, EPREFIX, '/usr/bin/git']), 'g-common_command': path([self.root, EPREFIX, '/usr/bin/g-common']), 'g-sorcery_command': path([self.root, EPREFIX, '/usr/bin/g-sorcery']), 'mercurial_command': path([self.root, EPREFIX, '/usr/bin/hg']), # left blank due to squashfs only needing to mount the image. 'squashfs_command': '', 'rsync_command': path([self.root, EPREFIX, '/usr/bin/rsync']), 'svn_command': path([self.root, EPREFIX, '/usr/bin/svn']), 'tar_command': path([self.root, EPREFIX, '/bin/tar']), 't/f_options': [ 'check_official', 'clean_archive', 'nocheck', 'require_repoconfig' ], 'bzr_addopts': '', 'bzr_syncopts': '', 'cvs_addopts': '', 'cvs_syncopts': '', 'darcs_addopts': '', 'darcs_syncopts': '', 'git_addopts': '', 'git_syncopts': '', 'mercurial_addopts': '', 'mercurial_syncopts': '', 'rsync_syncopts': '', 'squashfs_addopts': '', 'squashfs_syncopts': '', 'svn_addopts': '', 'svn_syncopts': '', 'g-common_generateopts': '', 'g-common_syncopts': '', 'g-sorcery_generateopts': '', 'g-sorcery_syncopts': '', 'bzr_postsync': '', 'cvs_postsync': '', 'darcs_postsync': '', 'git_postsync': '', 'mercurial_postsync': '', 'rsync_postsync': '', 'squashfs_postsync': '', 'svn_postsync': '', 'tar_postsync': '', 'g-common_postsync': '', 'g-sorcery_postsync': '', 'git_user': '******', 'git_email': 'layman@localhost', 'support_url_updates': ['Bzr', 'cvs', 'Git', 'Mercurial', 'Subversion'], } self._options = { 'config': config if config else self._defaults['config'], 'stdout': stdout if stdout else sys.stdout, 'stdin': stdin if stdin else sys.stdin, 'stderr': stderr if stderr else sys.stderr, 'output': output if output else Message(), 'quietness': quietness, 'nocolor': nocolor, 'width': width, 'verbose': verbose, 'quiet': quiet, 'custom_news_func': None, } self._set_quietness(quietness) self.config = None if read_configfile: defaults = self.get_defaults() if "%(configdir)s" in defaults['config']: # fix the config path defaults['config'] = defaults['config'] \ % {'configdir': defaults['configdir']} self.read_config(defaults)