Fixed conflicts from version vs dev
[invirt/packages/invirt-dev.git] / python / invirt / builder.py
index c1730df..f864feb 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,17 +17,55 @@ 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'
+
+_DEFAULT_DISTRIBUTION = 'hardy'
 
 
 class InvalidBuild(ValueError):
     pass
 
+_DISTRO_TO_SUFFIX = {
+    'etch': '~debian4.0',
+    'lenny': '~debian5.0',
+    'squeeze': '~debian6.0',
+
+    'hardy': '~ubuntu8.04',
+    'lucid': '~ubuntu10.04',
+    'maverick': '~ubuntu10.10',
+    'natty': '~ubuntu11.04',
+    'oneiric': '~ubuntu11.10',
+    'precise': '~ubuntu12.04',
+    }
+
+def distroToSuffix(distro):
+    return _DISTRO_TO_SUFFIX.get(distro, '~'+distro)
 
 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 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
+    the repo exists."""
+    # TODO: this might be easier just to regex
+    repo = os.path.abspath(getRepo(package))
+    parent_dir = os.path.dirname(repo)
+    prefix = os.path.join(_REPO_DIR, 'invirt/packages')
+    if not parent_dir.startswith(prefix):
+        raise InvalidBuild('Invalid package name %s' % 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."""
@@ -38,11 +76,14 @@ def pocketToApt(pocket):
     """Map a pocket in the configuration to an apt repo pocket."""
     return getattr(getattr(config.build.pockets, pocket), 'apt', pocket)
 
+def pocketToDistro(pocket):
+    """Map a pocket in the configuration to the distro we build for."""
+    return getattr(getattr(config.build.pockets, pocket), 'distro', _DEFAULT_DISTRIBUTION)
 
 def getGitFile(package, ref, path):
     """Return the contents of a path from a git ref in a package."""
     return c.captureOutput(['git', 'cat-file', 'blob', '%s:%s' % (ref, path)],
-                         cwd=getRepo(package))
+                           cwd=getRepo(package))
 
 
 def getChangelog(package, ref):
@@ -53,6 +94,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."""
@@ -85,8 +133,10 @@ def validateBuild(pocket, package, commit):
     another pocket, then this function returns that pocket. Otherwise,
     it returns True.
     """
+    ensureValidPackage(package)
     package_repo = getRepo(package)
     new_version = getVersion(package, commit)
+    new_distro = pocketToDistro(pocket)
 
     ret = True
 
@@ -95,19 +145,28 @@ def validateBuild(pocket, package, commit):
             continue
 
         b = pocketToGit(p)
-        current_commit = c.captureOutput(['git', 'rev-parse', b],
-                                       cwd=package_repo)
+        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)
+        current_distro = pocketToDistro(p)
+
+        # NB: Neither current_version nor new_version will have the
+        # distro-specific prefix.
 
-        if current_version == new_version:
+        if current_version == new_version and current_distro == new_distro:
             if current_commit == commit:
                 ret = p
             else:
-                raise InvalidBuild('Version %s of %s already available in '
+                raise InvalidBuild('Version %s of %s already available is in '
                                    'pocket %s from commit %s' %
                                    (new_version, package, p, current_commit))
 
-    if config.build.pockets[pocket].get('allow_backtracking', False):
+    if not config.build.pockets[pocket].get('allow_backtracking', False):
         branch = pocketToGit(pocket)
         current_version = getVersion(package, branch)
         if new_version <= current_version:
@@ -117,7 +176,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))