]> git.decadent.org.uk Git - dak.git/blobdiff - dak/generate_releases.py
Add by-hash support
[dak.git] / dak / generate_releases.py
index 2814b24a61b7ea78eea776367fe518d7fbf1d6dd..82ff6394bffa3b92765d6f403a121c3b939c10cc 100755 (executable)
@@ -37,6 +37,7 @@ import stat
 import time
 import gzip
 import bz2
+import errno
 import apt_pkg
 import subprocess
 from tempfile import mkstemp, mkdtemp
@@ -139,7 +140,9 @@ class ReleaseWriter(object):
                     ('Label',       'label'),
                     ('Suite',       'release_suite_output'),
                     ('Version',     'version'),
-                    ('Codename',    'codename') )
+                    ('Codename',    'codename'),
+                    ('Changelogs',  'changelog_url'),
+                  )
 
         # A "Sub" Release file has slightly different fields
         subattribs = ( ('Archive',  'suite_name'),
@@ -149,7 +152,9 @@ class ReleaseWriter(object):
 
         # Boolean stuff. If we find it true in database, write out "yes" into the release file
         boolattrs = ( ('NotAutomatic',         'notautomatic'),
-                      ('ButAutomaticUpgrades', 'butautomaticupgrades') )
+                      ('ButAutomaticUpgrades', 'butautomaticupgrades'),
+                      ('Acquire-By-Hash',      'byhash'),
+                    )
 
         cnf = Config()
 
@@ -161,7 +166,7 @@ class ReleaseWriter(object):
         for key, dbfield in attribs:
             # Hack to skip NULL Version fields as we used to do this
             # We should probably just always ignore anything which is None
-            if key == "Version" and getattr(suite, dbfield) is None:
+            if key in ("Version", "Changelogs") and getattr(suite, dbfield) is None:
                 continue
 
             out.write("%s: %s\n" % (key, getattr(suite, dbfield)))
@@ -222,9 +227,8 @@ class ReleaseWriter(object):
 
         os.chdir(os.path.join(suite.archive.path, "dists", suite.suite_name, suite_suffix))
 
-        hashfuncs = { 'MD5Sum' : apt_pkg.md5sum,
-                      'SHA1' : apt_pkg.sha1sum,
-                      'SHA256' : apt_pkg.sha256sum }
+        hashfuncs = dict(zip([x.upper().replace('UM', 'um') for x in suite.checksums],
+                             [getattr(apt_pkg, "%s" % (x)) for x in [x.replace("sum", "") + "sum" for x in suite.checksums]]))
 
         fileinfo = {}
 
@@ -283,6 +287,47 @@ class ReleaseWriter(object):
         out.close()
         os.rename(outfile + '.new', outfile)
 
+        if suite.byhash:
+            query = """
+                UPDATE hashfile SET unreferenced = CURRENT_TIMESTAMP
+                WHERE suite_id = :id AND unreferenced IS NULL"""
+            session.execute(query, {'id': suite.suite_id})
+
+            for filename in fileinfo:
+                if not os.path.exists(filename):
+                    # probably an uncompressed index we didn't generate
+                    continue
+
+                for h in hashfuncs:
+                    hashfile = os.path.join(os.path.dirname(filename), 'by-hash', h, fileinfo[filename][h])
+                    query = "SELECT 1 FROM hashfile WHERE path = :p AND suite_id = :id"
+                    q = session.execute(
+                            query,
+                            {'p': hashfile, 'id': suite.suite_id})
+                    if q.rowcount:
+                        session.execute('''
+                            UPDATE hashfile SET unreferenced = NULL
+                            WHERE path = :p and suite_id = :id''',
+                            {'p': hashfile, 'id': suite.suite_id})
+                    else:
+                        session.execute('''
+                            INSERT INTO hashfile (path, suite_id)
+                            VALUES (:p, :id)''',
+                            {'p': hashfile, 'id': suite.suite_id})
+
+                    try:
+                        os.makedirs(os.path.dirname(hashfile))
+                    except OSError as exc:
+                        if exc.errno != errno.EEXIST:
+                            raise
+                    try:
+                        os.link(filename, hashfile)
+                    except OSError as exc:
+                        if exc.errno != errno.EEXIST:
+                            raise
+
+                session.commit()
+
         sign_release_dir(suite, os.path.dirname(outfile))
 
         os.chdir(oldcwd)