Make invirtibuilder work
[invirt/packages/invirt-dev.git] / invirtibuilder
index f5c3821..d2a571c 100755 (executable)
@@ -29,15 +29,22 @@ principal is the Kerberos principal that requested the build.
 from __future__ import with_statement
 
 import contextlib
+import glob
 import os
 import re
 import shutil
 import subprocess
+import tempfile
+import traceback
 
 import pyinotify
 
+from debian_bundle import deb822
+
 import invirt.builder as b
+import invirt.common as c
 from invirt import database
+from invirt.config import structs as config
 
 
 DISTRIBUTION = 'hardy'
@@ -51,7 +58,7 @@ def getControl(package, ref):
     acts roughly like a dict.
     """
     return deb822.Deb822.iter_paragraphs(
-        getGitFile(package, ref, 'debian/control').split('\n'))
+        b.getGitFile(package, ref, 'debian/control').split('\n'))
 
 
 def getBinaries(package, ref):
@@ -71,7 +78,7 @@ def getArches(package, ref):
 
 def getDscName(package, ref):
     """Return the .dsc file that will be generated for this package."""
-    v = getVersion(package, ref)
+    v = b.getVersion(package, ref)
     if v.debian_version:
         v_str = '%s-%s' % (v.upstream_version,
                            v.debian_version)
@@ -95,13 +102,13 @@ def sanitizeVersion(version):
     return v.replace('~', '.')
 
 
-def aptCopy(packages, dst_pocket, src_pocket):
+def aptCopy(package, commit, dst_pocket, src_pocket):
     """Copy a package from one pocket to another."""
     binaries = getBinaries(package, commit)
-    cpatureOutput(['reprepro-env', 'copy',
-                   b.pocketToApt(dst_pocket),
-                   b.pocketToApt(src_pocket),
-                   package] + binaries)
+    c.captureOutput(['reprepro-env', 'copy',
+                     b.pocketToApt(dst_pocket),
+                     b.pocketToApt(src_pocket),
+                     package] + binaries)
 
 
 def sbuild(package, ref, arch, workdir, arch_all=False):
@@ -122,7 +129,7 @@ def sbuildAll(package, ref, workdir):
         sbuild(package, ref, 'i386', workdir)
 
 
-def tagSubmodule(pocket, package, ref, principal):
+def tagSubmodule(pocket, package, principal, version, env):
     """Tag a new version of a submodule.
 
     If this pocket does not allow_backtracking, then this will create
@@ -138,12 +145,7 @@ def tagSubmodule(pocket, package, ref, principal):
     legitimacy of my reasoning.
     """
     if not config.build.pockets[pocket].get('allow_backtracking', False):
-        env = dict(os.environ)
         branch = b.pocketToGit(pocket)
-        version = b.getVersion(package, ref)
-
-        env['GIT_COMMITTER_NAME'] = config.build.tagger.name
-        env['GIT_COMMITTER_EMAIL'] = config.build.tagger.email
         tag_msg = ('Tag %s of %s\n\n'
                    'Requested by %s' % (version.full_version,
                                         package,
@@ -159,7 +161,7 @@ def updateSubmoduleBranch(pocket, package, ref):
     """Update the appropriately named branch in the submodule."""
     branch = b.pocketToGit(pocket)
     c.captureOutput(
-        ['git', 'update-ref', 'refs/heads/%s' % branch, ref])
+        ['git', 'update-ref', 'refs/heads/%s' % branch, ref], cwd=b.getRepo(package))
 
 
 def uploadBuild(pocket, workdir):
@@ -167,13 +169,13 @@ def uploadBuild(pocket, workdir):
     apt = b.pocketToApt(pocket)
     for changes in glob.glob(os.path.join(workdir, '*.changes')):
         c.captureOutput(['reprepro-env',
-                       'include',
                        '--ignore=wrongdistribution',
+                       'include',
                        apt,
                        changes])
 
 
-def updateSuperproject(pocket, package, commit, principal):
+def updateSuperproject(pocket, package, commit, principal, version, env):
     """Update the superproject.
 
     This will create a new commit on the branch for the given pocket
@@ -185,32 +187,35 @@ def updateSuperproject(pocket, package, commit, principal):
     superproject = os.path.join(b._REPO_DIR, 'invirt/packages.git')
     branch = b.pocketToGit(pocket)
     tree = c.captureOutput(['git', 'ls-tree', branch],
-                         cwd=superproject)
+                           cwd=superproject).strip()
 
     new_tree = re.compile(
         r'^(160000 commit )[0-9a-f]*(\t%s)$' % package, re.M).sub(
-        r'\1%s\2' % commit,
+        r'\g<1>%s\g<2>' % commit,
         tree)
 
-    new_tree_id = c.captureOutput(['git', 'mktree'],
-                                cwd=superproject,
-                                stdin_str=new_tree)
+    new_tree_id = c.captureOutput(['git', 'mktree', '--missing'],
+                                  cwd=superproject,
+                                  stdin_str=new_tree).strip()
 
     commit_msg = ('Update %s to version %s\n\n'
                   'Requested by %s' % (package,
                                        version.full_version,
                                        principal))
     new_commit = c.captureOutput(
-        ['git', 'commit-tree', new_tree_hash, '-p', branch],
+        ['git', 'commit-tree', new_tree_id, '-p', branch],
         cwd=superproject,
         env=env,
-        stdin_str=commit_msg)
+        stdin_str=commit_msg).strip()
 
     c.captureOutput(
         ['git', 'update-ref', 'refs/heads/%s' % branch, new_commit],
         cwd=superproject)
 
 
+def makeReadable(workdir):
+    os.chmod(workdir, 0755)
+
 @contextlib.contextmanager
 def packageWorkdir(package, commit):
     """Checkout the package in a temporary working directory.
@@ -227,7 +232,7 @@ def packageWorkdir(package, commit):
         p_archive = subprocess.Popen(
             ['git', 'archive',
              '--remote=file://%s' % b.getRepo(package),
-             '--prefix=%s' % package,
+             '--prefix=%s/' % package,
              commit,
              ],
             stdout=subprocess.PIPE,
@@ -277,13 +282,17 @@ def build():
         db.commit = commit
         db.principal = principal
         database.session.save_or_update(db)
-        database.commit()
+        database.session.commit()
 
-        database.begin()
+        database.session.begin()
 
         try:
             db.failed_stage = 'validating job'
-            src = validateBuild(pocket, package, commit)
+            src = b.validateBuild(pocket, package, commit)
+            # Don't expand the commit in the DB until we're sure the user
+            # isn't trying to be tricky.
+            db.commit = commit = c.captureOutput(['git', 'rev-parse', commit],
+                                                 cwd=b.getRepo(package)).strip()
 
             db.version = str(b.getVersion(package, commit))
 
@@ -294,7 +303,7 @@ def build():
             # raised an exception)
             if src != True:
                 db.failed_stage = 'copying package from another pocket'
-                aptCopy(packages, pocket, src)
+                aptCopy(package, commit, pocket, src)
             # If we can't copy the package from somewhere, but
             # validateBuild didn't raise an exception, then we need to
             # do the build ourselves
@@ -319,18 +328,27 @@ def build():
                         db.failed_stage = 'building binary packages'
                         sbuildAll(package, commit, workdir)
                     finally:
-                        logdir = os.path.join(b._LOG_DIR, db.build_id)
+                        logdir = os.path.join(b._LOG_DIR, str(db.build_id))
                         if not os.path.exists(logdir):
                             os.makedirs(logdir)
 
                         for log in glob.glob(os.path.join(workdir, '*.build')):
                             os.copy2(log, logdir)
+
+                    db.failed_stage = 'processing metadata'
+                    env = dict(os.environ)
+                    env['GIT_COMMITTER_NAME'] = config.build.tagger.name
+                    env['GIT_COMMITTER_EMAIL'] = config.build.tagger.email
+                    version = b.getVersion(package, commit)
+
                     db.failed_stage = 'tagging submodule'
-                    tagSubmodule(pocket, package, commit, principal)
+                    tagSubmodule(pocket, package, principal, version, env)
                     db.failed_stage = 'updating submodule branches'
                     updateSubmoduleBranch(pocket, package, commit)
                     db.failed_stage = 'updating superproject'
-                    updateSuperproject(pocket, package, commit, principal)
+                    updateSuperproject(pocket, package, commit, principal, version, env)
+                    db.failed_stage = 'relaxing permissions on workdir'
+                    makeReadable(workdir)
                     db.failed_stage = 'uploading packages to apt repo'
                     uploadBuild(pocket, workdir)