From 10edd0e04571842c0991e2d9cbf83cb26b3960f6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 26 Apr 2018 21:14:39 +0300 Subject: Add: action: git --- NEWS | 1 + ick2/__init__.py | 1 + ick2/actions.py | 66 +++++++++++++++++++++++++++++++++++++++++---------- ick2/actions_tests.py | 10 +++++++- 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/NEWS b/NEWS index d2d0991..9a522b7 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,7 @@ along with this program. If not, see . Version 0.47+git, not yet released ---------------------------------- +* New action to do `git clone` or `git remote update`. Version 0.47, released 2018-04-25 ---------------------------------- diff --git a/ick2/__init__.py b/ick2/__init__.py index e350753..f212946 100644 --- a/ick2/__init__.py +++ b/ick2/__init__.py @@ -78,4 +78,5 @@ from .actions import ( CreateWorkspaceAction, ArchiveWorkspaceAction, PopulateSystreeAction, + GitAction, ) diff --git a/ick2/actions.py b/ick2/actions.py index 37ebd32..96382ef 100644 --- a/ick2/actions.py +++ b/ick2/actions.py @@ -85,18 +85,28 @@ class ActionFactory: return action def _create_action_object(self, env, spec): - if 'shell' in spec: - return ShellAction(env) - if 'python' in spec: - return PythonAction(env) - if 'debootstrap' in spec: - return DebootstrapAction(env) - if 'archive' in spec: - return ArchiveWorkspaceAction(env) - if spec.get('action') == 'populate_systree': - return PopulateSystreeAction(env) - if spec.get('action') == 'create_workspace': - return CreateWorkspaceAction(env) + rules = [ + ('shell', ShellAction), + ('python', PythonAction), + ('debootstrap', DebootstrapAction), + ('archive', ArchiveWorkspaceAction), + ] + + for key, klass in rules: + if key in spec: + return klass(env) + + if 'action' in spec: + rules2 = { + 'populate_systree': PopulateSystreeAction, + 'create_workspace': CreateWorkspaceAction, + 'git': GitAction, + } + kind = spec['action'] + klass = rules2.get(kind) + if klass: + return klass(env) + raise UnknownStepError('Unknown action %r' % spec) @@ -290,6 +300,38 @@ class PopulateSystreeAction(Action): # pragma: no cover return exit_code +class GitAction(Action): # pragma: no cover + + def encode_parameters(self, params): + pass + + def execute(self, params, step): + env = self.get_env() + workspace = env.get_workspace_directory() + + git_dir = params.get('git_dir') + if git_dir is None: + env.report(1, 'git_dir not provided\n') + if git_dir.startswith('/') or '..' in git_dir: + env.report(1, 'git_dir not acceptable\n') + + git_url = params.get('git_url') + if git_url is None: + env.report(1, 'git_url not provided\n') + + pathname = os.path.join(workspace, git_dir) + if os.path.exists(pathname): + argv = ['git', 'remote', '-v', 'update', '--prune'] + cwd = pathname + else: + argv = ['git', 'clone', '-v', git_url, git_dir] + cwd = workspace + + exit_code = env.host_runcmd(argv, cwd=cwd) + env.report(exit_code, 'git finished (exit code %d)\n' % exit_code) + return exit_code + + def make_directory_empty(env, dirname): return env.runcmd( ['sudo', 'find', dirname, '-mindepth', '1', '-delete']) diff --git a/ick2/actions_tests.py b/ick2/actions_tests.py index 8220fe2..06c07f5 100644 --- a/ick2/actions_tests.py +++ b/ick2/actions_tests.py @@ -123,7 +123,7 @@ class ActionFactoryTests(unittest.TestCase): self.assertTrue(isinstance(action, ick2.ArchiveWorkspaceAction)) self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment)) - def test_creates_populate_sysgtre_action_on_host(self): + def test_creates_populate_systree_action_on_host(self): spec = { 'action': 'populate_systree', } @@ -131,6 +131,14 @@ class ActionFactoryTests(unittest.TestCase): self.assertTrue(isinstance(action, ick2.PopulateSystreeAction)) self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment)) + def test_creates_git_action_on_host(self): + spec = { + 'action': 'git', + } + action = self.af.create_action(spec, self.project) + self.assertTrue(isinstance(action, ick2.GitAction)) + self.assertTrue(isinstance(action.get_env(), ick2.HostEnvironment)) + def test_raises_exception_for_unknown_step(self): with self.assertRaises(ick2.UnknownStepError): self.af.create_action({}, self.project) -- cgit v1.2.1