From 72b6e0081739ff9936ed52b1830aa53de6e8a4d5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 21 Jul 2018 20:28:01 +0300 Subject: Add: GitMirrorAction --- ick2/actions.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/ick2/actions.py b/ick2/actions.py index 77f4fed..ffdb62a 100644 --- a/ick2/actions.py +++ b/ick2/actions.py @@ -429,6 +429,70 @@ class GitAction(Action): # pragma: no cover return exit_code +class GitMirrorAction(Action): # pragma: no cover + + def encode_parameters(self, params): + pass + + def execute(self, params, step): + env = self.get_env() + workspace = env.get_workspace_directory() + mirrors = os.path.join(workspace, '.mirrors') + + if step.get('where') != 'host': + env.report(1, '"where" must be "host"\n') + return 1 + + sources = params.get('sources') + if sources is None: + env.report(1, '"sources" parameter not provided\n') + return 1 + + try: + exit_code = self.git_mirror(env, sources, mirrors) + except Exception as e: + env.report(1, 'Caught exception: {}\n'.format(e)) + return 1 + + env.report(exit_code, 'git mirror action finished (exit code %d)\n' % exit_code) + return exit_code + + def git_mirror(self, env, sources, mirrors): + if not os.path.exists(mirrors): + os.mkdir(mirrors) + checked = self.check_sources(sources) + for name, url in checked: + exit_code = self.mirror(env, mirrors, name, url) + if exit_code != 0: + return exit_code + return 0 + + def check_sources(self, sources): + checked = [] + for source in sources: + name = source.get('name') + repo = source.get('repo') + if name is None: + raise Exception('source lacks "name" field: {}'.format(source)) + if repo is None: + raise Exception('source lacks "repo" field: {}'.format(source)) + checked.append((name, repo)) + return checked + + def mirror(self, env, mirrors, name, url): + dirname = os.path.join(mirrors, name) + if os.path.exists(dirname): + argv = ['git', 'remote', 'update', '--prune'] + cwd = dirname + else: + argv = ['git', 'clone', '--mirror', url, name] + cwd = mirrors + os.mkdir(dirname) + + env.report(None, 'Running: {}\n'.format(' '.join(argv))) + return env.host_runcmd(argv, cwd=cwd) + + class RsyncAction(Action): # pragma: no cover def encode_parameters(self, params): -- cgit v1.2.1