Remove stray 'not' in builder.py
[invirt/packages/invirt-dev.git] / python / invirt / builder.py
index f2cb421..2e2e405 100644 (file)
@@ -6,9 +6,9 @@ and the remctl submission scripts that insert items into its queue.
 
 
 import os
+import subprocess
 
 from debian_bundle import changelog
-from debian_bundle import deb822
 
 import invirt.common as c
 from invirt.config import structs as config
@@ -17,7 +17,7 @@ from invirt.config import structs as config
 _QUEUE_DIR = '/var/lib/invirt-dev/queue'
 _REPO_DIR = '/srv/git'
 _LOG_DIR = '/var/log/invirt/builds'
-_HOOKS_DIR = '/usr/share/invirt-dev/build.d'
+_HOOKS_DIR = '/usr/share/invirt-dev/build-hooks'
 
 
 class InvalidBuild(ValueError):
@@ -28,7 +28,7 @@ def getRepo(package):
     """Return the path to the git repo for a given package."""
     return os.path.join(_REPO_DIR, 'invirt/packages', '%s.git' % package)
 
-def ensureValidRepo(package):
+def ensureValidPackage(package):
     """Perform some basic sanity checks that the requested repo is in a
     subdirectory of _REPO_DIR/invirt/packages.  This prevents weirdness
     such as submitting a package like '../prod/...git'.  Also ensures that
@@ -42,6 +42,14 @@ def ensureValidRepo(package):
     elif not os.path.exists(repo):
         raise InvalidBuild('Nonexisting package %s' % package)
 
+def canonicalize_commit(package, commit, shorten=False):
+    if shorten:
+        flags = ['--short']
+    else:
+        flags = []
+    return c.captureOutput(['git', 'rev-parse'] + flags + [commit],
+                           cwd=getRepo(package)).strip()
+
 def pocketToGit(pocket):
     """Map a pocket in the configuration to a git branch."""
     return getattr(getattr(config.build.pockets, pocket), 'git', pocket)
@@ -66,6 +74,13 @@ def getChangelog(package, ref):
     """
     return changelog.Changelog(getGitFile(package, ref, 'debian/changelog'))
 
+def runHook(hook, args=[], stdin_str=None):
+    """Run a named hook."""
+    hook = os.path.join(_HOOKS_DIR, hook)
+    try:
+        c.captureOutput([hook] + args, stdin_str=stdin_str)
+    except OSError:
+        pass
 
 def getVersion(package, ref):
     """Get the version of a given package at a particular ref."""
@@ -98,7 +113,7 @@ def validateBuild(pocket, package, commit):
     another pocket, then this function returns that pocket. Otherwise,
     it returns True.
     """
-    ensureValidRepo(package)
+    ensureValidPackage(package)
     package_repo = getRepo(package)
     new_version = getVersion(package, commit)
 
@@ -109,8 +124,13 @@ def validateBuild(pocket, package, commit):
             continue
 
         b = pocketToGit(p)
-        current_commit = c.captureOutput(['git', 'rev-parse', b],
-                                         cwd=package_repo).strip()
+        try:
+            current_commit = c.captureOutput(['git', 'rev-parse', b],
+                                             cwd=package_repo).strip()
+        except subprocess.CalledProcessError:
+            # Guess we haven't created this pocket yet
+            continue
+
         current_version = getVersion(package, b)
 
         if current_version == new_version:
@@ -131,7 +151,8 @@ def validateBuild(pocket, package, commit):
 
         # Almost by definition, A is a fast-forward of B if B..A is
         # empty
-        if not c.captureOutput(['git', 'rev-list', '%s..%s' % (commit, branch)]):
+        if c.captureOutput(['git', 'rev-list', '%s..%s' % (commit, branch)],
+                           cwd=package_repo):
             raise InvalidBuild('New commit %s of %s is not a fast-forward of'
                                'commit currently in pocket %s' %
                                (commit, package, pocket))