From d37fb2ae726446b79cb142e4b86a8ef7942043f4 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 12 Aug 2019 10:20:53 +0300 Subject: move options to a config file --- examples/cgit-repos.conf | 24 ++++++++++++++ examples/my_repos.py | 12 +++++++ pull/cgit.py | 17 ++++++---- pull/main.py | 84 ++++++++++++++++++++++++++++++++++++------------ pull/repo.py | 61 +++++++++++++++++++++++------------ 5 files changed, 150 insertions(+), 48 deletions(-) create mode 100644 examples/cgit-repos.conf create mode 100644 examples/my_repos.py diff --git a/examples/cgit-repos.conf b/examples/cgit-repos.conf new file mode 100644 index 0000000..9729fd3 --- /dev/null +++ b/examples/cgit-repos.conf @@ -0,0 +1,24 @@ +# All settings are optional. + +[DEFAULT] + +# /etc/cgit-repos/my_repos.py by default. +my_repos = /path/to/my_repos.py + +# "$( pwd )/output/" by default. +output = /path/to/output/ + +# URL to clone from the output directory. +# {repo_id} is substituted with the repository ID (in the NAME or SECTION/NAME +# format, etc.). +clone_url = http://example.com:8080/git/{repo_id} + +owner = Your Name + +[GITHUB] + +username = your-username + +[BITBUCKET] + +username = your-username diff --git a/examples/my_repos.py b/examples/my_repos.py new file mode 100644 index 0000000..2047b8d --- /dev/null +++ b/examples/my_repos.py @@ -0,0 +1,12 @@ +from pull.repo import BitbucketRepo, GithubRepo, Repo + + +MY_REPOS = ( + GithubRepo('xyz'), + GithubRepo('foo/bar', user='test', via_ssh=False), + + BitbucketRepo('xyz'), + BitbucketRepo('foo/bar', desc='Foo (Bar)'), + + Repo('tmp/tmp', clone_url='https://example.com/tmp.git', owner='John Doe'), +) diff --git a/pull/cgit.py b/pull/cgit.py index ec7876c..a6b4312 100644 --- a/pull/cgit.py +++ b/pull/cgit.py @@ -21,14 +21,13 @@ def _check_output(*args, **kwargs): class CGit: - def __init__(self, user, host, port): - self.user = user - self.host = host - self.ip = socket.gethostbyname(self.host) - self.port = port + def __init__(self, clone_url): + self.clone_url = clone_url def get_clone_url(self, repo): - return f'http://{self.user}@{self.ip}:{self.port}/git/{repo.repo_id}' + if self.clone_url is None: + return None + return self.clone_url.format(repo_id=repo.repo_id) class CGitRC: @@ -52,7 +51,11 @@ class CGitRC: clone_urls = [] if repo.clone_url is not None: clone_urls.append(repo.clone_url) - clone_urls.append(self.cgit.get_clone_url(repo)) + cgit_clone_url = self.cgit.get_clone_url(repo) + if cgit_clone_url is not None: + clone_urls.append(cgit_clone_url) + if not clone_urls: + return None clone_urls = ' '.join(clone_urls) return clone_urls 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 diff --git a/pull/repo.py b/pull/repo.py index 43b55e4..cfd5901 100644 --- a/pull/repo.py +++ b/pull/repo.py @@ -1,12 +1,9 @@ import os.path -DEFAULT_OWNER = 'Egor Tensin' -DEFAULT_GITHUB_USER = 'egor-tensin' -DEFAULT_BITBUCKET_USER = 'egor-tensin' - - class Repo: + DEFAULT_OWNER = None + @staticmethod def extract_repo_name(repo_id): return os.path.basename(repo_id) @@ -17,7 +14,7 @@ class Repo: self.repo_name = self.extract_repo_name(repo_id) self.clone_url = clone_url if owner is None: - owner = DEFAULT_OWNER + owner = Repo.DEFAULT_OWNER self.owner = owner if desc is None: if homepage is not None: @@ -31,42 +28,66 @@ class Repo: class GithubRepo(Repo): + DEFAULT_USER = None + def __init__(self, repo_id, clone_url=None, owner=None, desc=None, - homepage=None, github_user=DEFAULT_GITHUB_USER): + homepage=None, user=DEFAULT_USER, via_ssh=True): + if user is None: + if GithubRepo.DEFAULT_USER is None: + raise RuntimeError('neither explicit or default GitHub username was provided') + user = GithubRepo.DEFAULT_USER + name = Repo.extract_repo_name(repo_id) if clone_url is None: - clone_url = self.build_clone_url(github_user, repo_id) + if via_ssh: + clone_url = self.build_clone_url_ssh(user, name) + else: + clone_url = self.build_clone_url_https(user, name) if homepage is None: - homepage = self.build_homepage_url(github_user, repo_id) + homepage = self.build_homepage_url(user, name) super().__init__(repo_id, clone_url, owner=owner, desc=desc, homepage=homepage) @staticmethod - def build_clone_url(user, repo_id): - name = Repo.extract_repo_name(repo_id) + def build_clone_url_ssh(user, name): return f'ssh://git@github.com/{user}/{name}.git' @staticmethod - def build_homepage_url(user, repo_id): - name = Repo.extract_repo_name(repo_id) + def build_clone_url_https(user, name): + return f'https://github.com/{user}/{name}.git' + + @staticmethod + def build_homepage_url(user, name): return f'https://github.com/{user}/{name}' class BitbucketRepo(Repo): + DEFAULT_USER = None + def __init__(self, repo_id, clone_url=None, owner=None, desc=None, - homepage=None, bitbucket_user=DEFAULT_BITBUCKET_USER): + homepage=None, user=DEFAULT_USER, via_ssh=True): + if user is None: + if BitbucketRepo.DEFAULT_USER is None: + raise RuntimeError('neither explicit or default Bitbucket username was provided') + user = BitbucketRepo.DEFAULT_USER + name = Repo.extract_repo_name(repo_id) if clone_url is None: - clone_url = self.build_clone_url(bitbucket_user, repo_id) + if via_ssh: + clone_url = self.build_clone_url_ssh(user, name) + else: + clone_url = self.build_clone_url_https(user, name) if homepage is None: - homepage = self.build_homepage_url(bitbucket_user, repo_id) + homepage = self.build_homepage_url(user, name) super().__init__(repo_id, clone_url, owner=owner, desc=desc, homepage=homepage) @staticmethod - def build_clone_url(user, repo_id): - name = Repo.extract_repo_name(repo_id) + def build_clone_url_ssh(user, name): return f'ssh://git@bitbucket.org/{user}/{name}.git' @staticmethod - def build_homepage_url(user, repo_id): - name = Repo.extract_repo_name(repo_id) + def build_clone_url_https(user, name): + return f'https://bitbucket.org/{user}/{name}.git' + + @staticmethod + def build_homepage_url(user, name): return f'https://bitbucket.org/{user}/{name.lower()}' -- cgit v1.2.3