diff --git a/bin/swh-git-producer b/bin/swh-git-producer index 9524e74..ce70de1 100755 --- a/bin/swh-git-producer +++ b/bin/swh-git-producer @@ -1,70 +1,55 @@ #!/usr/bin/env python3 # Copyright (C) 2015 Stefano Zacchiroli , # Antoine R. Dumont # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information + import argparse -import configparser import logging import os +from swh.core import config from swh.cloner.git.producer import clones # Default configuration file DEFAULT_CONF_FILE = '~/.config/swh/clones-producer.ini' # default configuration (can be overriden by the DEFAULT_CONF_FILE) DEFAULT_CONF = { - 'table_clones': 'sample', - 'limit': None, - 'offset': None, - 'log_dir': 'swh-git-cloner/log/', - 'db_url': 'dbname=github', - 'repository_scheme': 'https://github.com/%s', - 'debug': None + 'table_clones': ('string', 'sample'), + 'limit': ('int', None), + 'offset': ('int', None), + 'log_dir': ('string', 'swh-git-cloner/log/'), + 'db_url': ('string', 'dbname=github'), + 'repository_scheme': ('string', 'https://github.com/%s'), + 'debug': ('bool', None) } def parse_args(): """Parse the configuration for the cli. """ cli = argparse.ArgumentParser( description='Clone git repository on fs.') cli.add_argument('--verbose', '-v', action='store_true', help='Verbosity level in log file.') cli.add_argument('--config', '-c', help='configuration file path') args = cli.parse_args() return args -def read_conf(args): - """Read the user's configuration file. - - args contains the repo to parse. - Transmit to the result. - """ - config = configparser.ConfigParser(defaults=DEFAULT_CONF) - conf_file = args.config or DEFAULT_CONF_FILE - config.read(os.path.expanduser(conf_file)) - conf = config._sections['main'] - - # ensure the default keys are set if some are missing - for key in DEFAULT_CONF: - conf[key] = conf.get(key, DEFAULT_CONF[key]) - - return conf - if __name__ == '__main__': - args = parse_args() - conf = read_conf(args) - log_filename = os.path.join(conf['log_dir'], 'cloner.log') - logging.basicConfig(filename=log_filename, - level=logging.DEBUG if args.verbose else logging.INFO) + args = parse_args + conf = config.read(args.config or DEFAULT_CONF_FILE, DEFAULT_CONF) + config.prepare_folders(conf, 'log_dir') + + logging.basicConfig(filename=os.path.join(conf['log_dir'], 'cloner.log'), + level=logging.DEBUG if args.verbose else logging.INFO) - clones.produce(conf) + clones.produce(conf)