grim/local-pipelines

6504522f8b27
Merged in seanfarley/local-pipelines (pull request #5)

Refactor vcs to have backend classes that allow easier extensibility
--- a/pipelines/core.py Thu Jul 28 14:21:11 2016 -0500
+++ b/pipelines/core.py Thu Jul 28 15:07:43 2016 -0500
@@ -118,7 +118,7 @@
path, config = _load_config(args.pipeline_filename)
- branch = vcs.get_branch(path)
+ repo = vcs.repo(path)
env = _load_env(args.env_filenames)
@@ -127,6 +127,6 @@
k, v = _split_env_var(var)
env[k] = v
- pipeline = Pipeline(config, path, branch, env)
+ pipeline = Pipeline(config, path, repo, env)
sys.exit(pipeline.run())
--- a/pipelines/pipeline.py Thu Jul 28 14:21:11 2016 -0500
+++ b/pipelines/pipeline.py Thu Jul 28 15:07:43 2016 -0500
@@ -26,10 +26,10 @@
class Pipeline(object):
- def __init__(self, config, path, branch, env=None):
+ def __init__(self, config, path, repo, env=None):
self.config = config
self.build_path = os.path.abspath(path)
- self.branch = branch
+ self.repo = repo
self._env = env or {}
@property
@@ -39,7 +39,7 @@
branches = self.config["pipelines"]["branches"]
for pattern, steps in branches.iteritems():
- if fnmatch.fnmatch(self.branch, pattern):
+ if fnmatch.fnmatch(self.repo.branch, pattern):
ret = steps
break
--- a/pipelines/vcs.py Thu Jul 28 14:21:11 2016 -0500
+++ b/pipelines/vcs.py Thu Jul 28 15:07:43 2016 -0500
@@ -22,14 +22,7 @@
import subprocess
-_VCSS = {
- 'git': 'git rev-parse --abbrev-ref HEAD -C {}',
- 'hg': 'hg branch -R {}',
-}
-
-
-def get_branch(path):
- branch = None
+def run_command(cmd, path):
env = os.environ.copy()
# ignore user's config
@@ -37,14 +30,56 @@
env['HOME'] = '/tmp'
env['GIT_CONFIG_NOSYSTEM'] = '1'
- for _, cmd in _VCSS.iteritems():
- try:
- command = cmd.format(path)
- branch = subprocess.check_output(command.split(),
- stderr=open(os.devnull, 'w'),
- env=env)
- branch = branch.strip()
- except subprocess.CalledProcessError:
- pass
+ try:
+ return subprocess.check_output(cmd.split(),
+ env=env,
+ stderr=open(os.devnull, 'w'),
+ cwd=path).strip()
+ except subprocess.CalledProcessError:
+ return ''
+
+
+class git_backend(object):
+ def __init__(self, path):
+ self._path = path
+
+ @property
+ def branch(self):
+ return run_command('git rev-parse --abbrev-ref HEAD',
+ self._path)
+
+
+class hg_backend(object):
+ def __init__(self, path):
+ self._path = path
- return branch
+ @property
+ def branch(self):
+ return run_command('hg log -r . -T {branch}',
+ self._path)
+
+
+class repo(object):
+ def __init__(self, search_path):
+ self._repo = None
+
+ # search for .git or .hg to determine our backend class
+ prev_dir = None
+ next_dir = os.path.realpath(search_path)
+ while prev_dir != next_dir:
+ if os.path.exists(os.path.join(next_dir, '.git')):
+ self._repo = git_backend(next_dir)
+ break
+ elif os.path.exists(os.path.join(next_dir, '.hg')):
+ self._repo = hg_backend(next_dir)
+ break
+ prev_dir = next_dir
+ next_dir = os.path.dirname(prev_dir)
+
+ # sanity check
+ if self._repo is None:
+ raise IOError("Could not find .git nor .hg")
+
+ @property
+ def branch(self):
+ return self._repo.branch