diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2019-08-12 10:20:53 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2019-08-12 10:20:53 +0300 |
commit | d37fb2ae726446b79cb142e4b86a8ef7942043f4 (patch) | |
tree | 247e7213ccbaae8186e4aba46125131c72edba66 /pull/main.py | |
parent | remove pull.sh (diff) | |
download | cgitize-d37fb2ae726446b79cb142e4b86a8ef7942043f4.tar.gz cgitize-d37fb2ae726446b79cb142e4b86a8ef7942043f4.zip |
move options to a config file
Diffstat (limited to 'pull/main.py')
-rw-r--r-- | pull/main.py | 84 |
1 files changed, 63 insertions, 21 deletions
diff --git a/pull/main.py b/pull/main.py index d111da7..a4eeeab 100644 --- a/pull/main.py +++ b/pull/main.py @@ -1,16 +1,17 @@ from argparse import ArgumentParser +import configparser +import importlib import logging +import os.path import sys from pull.cgit import CGit, Output -from pull.my_repos import MY_REPOS +from pull.repo import BitbucketRepo, GithubRepo, Repo DEFAULT_OUTPUT_DIR = 'output' - -DEFAULT_CGIT_CLONE_USER = 'egor' -DEFAULT_CGIT_CLONE_HOST = 'tensin-ext1.home' -DEFAULT_CGIT_CLONE_PORT = 8080 +DEFAULT_CONFIG_PATH = '/etc/cgit-repos/cgit-repos.conf' +DEFAULT_MY_REPOS_PATH = '/etc/cgit-repos/my_repos.py' def set_up_logging(): @@ -24,31 +25,72 @@ def parse_args(argv=None): if argv is None: argv = sys.argv[1:] parser = ArgumentParser() - parser.add_argument('--output', metavar='PATH', - default=DEFAULT_OUTPUT_DIR, - help='output directory path') - parser.add_argument('--cgit-user', metavar='USERNAME', - default=DEFAULT_CGIT_CLONE_USER, - help='cgit clone username') - parser.add_argument('--cgit-host', metavar='HOST', - default=DEFAULT_CGIT_CLONE_HOST, - help='cgit clone host') - parser.add_argument('--cgit-port', metavar='PORT', type=int, - default=DEFAULT_CGIT_CLONE_PORT, - help='cgit clone port number') - parser.add_argument('--repo', metavar='REPO_ID', nargs='*', dest='repos', + parser.add_argument('--config', metavar='PATH', + default=DEFAULT_CONFIG_PATH, + help='config file path') + parser.add_argument('--repo', metavar='REPO_ID', + nargs='*', dest='repos', help='repos to pull') return parser.parse_args(argv) +class Config: + @staticmethod + def read(path): + config = configparser.ConfigParser() + config.read(path) + return Config(config) + + def __init__(self, impl): + self.impl = impl + + @property + def output(self): + return self.impl.get('DEFAULT', 'output', fallback=DEFAULT_OUTPUT_DIR) + + @property + def clone_url(self): + return self.impl.get('DEFAULT', 'clone_url', fallback=None) + + @property + def default_owner(self): + return self.impl.get('DEFAULT', 'owner', fallback=None) + + @property + def github_username(self): + return self.impl.get('GITHUB', 'username', fallback=None) + + @property + def bitbucket_username(self): + return self.impl.get('BITBUCKET', 'username', fallback=None) + + def set_defaults(self): + Repo.DEFAULT_OWNER = self.default_owner + GithubRepo.DEFAULT_USER = self.github_username + BitbucketRepo.DEFAULT_USER = self.bitbucket_username + + @property + def my_repos(self): + return self.impl.get('DEFAULT', 'my_repos', fallback=DEFAULT_MY_REPOS_PATH) + + def import_my_repos(self): + sys.path.append(os.path.dirname(self.my_repos)) + module_name = os.path.splitext(os.path.basename(self.my_repos))[0] + module = importlib.import_module(module_name) + return module.MY_REPOS + + def main(args=None): set_up_logging() try: args = parse_args(args) - cgit = CGit(args.cgit_user, args.cgit_host, args.cgit_port) - output = Output(args.output, cgit) + config = Config.read(args.config) + config.set_defaults() + my_repos = config.import_my_repos() + cgit = CGit(config.clone_url) + output = Output(config.output, cgit) success = True - for repo in MY_REPOS: + for repo in my_repos: if args.repos is None or repo.repo_id in args.repos: if not output.pull(repo): success = False |