summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-07-21 20:28:01 +0300
committerLars Wirzenius <liw@liw.fi>2018-07-21 20:28:01 +0300
commit72b6e0081739ff9936ed52b1830aa53de6e8a4d5 (patch)
tree3b6ee8f3dda13ee0ac6d54572e640889b39487dd
parent9f7e84fd817781a6343600c1571e44be841b8b01 (diff)
downloadick2-72b6e0081739ff9936ed52b1830aa53de6e8a4d5.tar.gz
Add: GitMirrorAction
-rw-r--r--ick2/actions.py64
1 files changed, 64 insertions, 0 deletions
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):