diff --git a/getmystuph_in_phab.py b/getmystuph_in_phab.py index 550f8cc..55957f8 100755 --- a/getmystuph_in_phab.py +++ b/getmystuph_in_phab.py @@ -1,177 +1,180 @@ #!/usr/bin/env python3 import argparse import yaml import keyring import getpass import collections import phabricator as phab import getmystuph from getmystuph import colored from getmystuph.utils import get_password import getmystuph.importers as getimp import logging import logging.config -try: - with open('.logging.conf', 'r') as fh: - config = yaml.load(fh) - logging.config.dictConfig(config) -except: - logging.basicConfig(level=logging.ERROR) - _logger = logging.getLogger('getmystuph').getChild('main') - class PhabImporter(object): """ Parses a YAML configuration file: """ _config = None def __init__(self, args): self._config = yaml.load(args.config) self._dry_run = args.dry_run _logger.debug('ConfigurationParser: {0}'.format(self._config)) if 'phabricator' not in self._config: # this will use default infos from ~/.arcrc self._config['phabricator'] = {} try: self._phab = phab.Phabricator(**self._config['phabricator']) self._phab.update_interfaces() # this request is just to make an actual connection self._phab.user.whoami() except Exception as e: _logger.error( 'Could not connect to phabricator, either give the' + ' connection information in the \'phabricator\' ' + 'section of the config file:\n' + ' phabricator:\n' + ' username: richart\n' + ' host: https://c4science.ch\n' + ' token: cli-g3amff25kdpnnv2tqvigmr4omnn7\n') raise e self._keyring = None if 'use_keyring' in self._config['global'] and \ self._config['global']['use_keyring']: self._keyring = keyring.get_keyring() self._imported_groups = {} self._out_directory = getmystuph.Directory(phabricator=self._phab, backend='c4science', dry_run=self._dry_run) self._users_db = getimp.ImportUserDB(self._out_directory) def _import_repository(self, name, info): _logger.info('Getting repo {0}'.format( getmystuph.Repo.color_name(name))) _logger.debug(' --> repo info {0}'.format(colored(info, attrs=['bold']))) password = get_password(info['backend'], info['username'], keyring=self._keyring) repo = getmystuph.Repo(name, password=password, **info) _permissions = repo.permissions _logger.debug("Permissions for repo {0}: {1}".format( repo.color_name(name), colored(_permissions, attrs=['bold']))) _users = self._complete_users(repo.directory, _permissions.all_users) for u in _users: print(u) if 'import_scheme' in info and \ info['import_scheme'] != 'all': try: query = repo.get_query() with query: print(query.list_branches()) print(query.list_tags()) except: _logger.warning( "No fine grain operation possible for repo {0}".format( repo.color_name(name))) def __get_full_info(self, info, _type): if info is None: info = {} if 'global' in self._config: global_conf = self._config['global'] if _type in global_conf: for key, value in global_conf[_type].items(): if key not in info: info[key] = value elif type(value) == dict: info[key] = dict(value, **info[key]) import_always = ['backend', 'username'] for key in import_always: if key in global_conf and key not in info: info[key] = global_conf[key] if 'username' not in info: info['username'] = getpass.getuser() if 'import-scheme' not in info: info['import-scheme'] = {} if 'backend' not in info['import-scheme']: info['import-scheme']['backend'] = 'c4science' info['import-scheme']['phabricator'] = self._phab return info def import_all(self): methods = collections.OrderedDict( [('groups', 'GroupImporter'), ('repositories', 'RepoImporter')]) for _type in methods.keys(): if _type in self._config: all_info = self._config[_type] if type(all_info) == list: all_info = {key: {} for key in all_info} for name, info in all_info.items(): info = self.__get_full_info(info, _type) importer = getattr(getimp, methods[_type])(name, info, self._users_db) importer.transfer() # Set up cli arguments parser = argparse.ArgumentParser( description='Import projects into c4science' ) parser.add_argument( '--config', help='configuration file (YAML)', type=argparse.FileType(), required=True ) parser.add_argument( '--dry-run', help='Do not do the creations', action='store_true' ) +parser.add_argument( + '-v', '--verbose', + help='Make the tool verbose', + action='store_true' +) args = parser.parse_args() +if args.verbose: + with open('.logging.conf', 'r') as fh: + config = yaml.load(fh) + logging.config.dictConfig(config) +else: + logging.basicConfig(level=logging.ERROR) imp = PhabImporter(args) imp.import_all() # LocalWords: keyring