← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jelmer/launchpad/publisher-use-debian-1 into lp:launchpad/devel

 

Jelmer Vernooij has proposed merging lp:~jelmer/launchpad/publisher-use-debian-1 into lp:launchpad/devel.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers): code


Refactor lp.archivepublisher.publishing to use the functionality in the new "debian" Python module to generate Release files, rather than using hardcoded strings.
-- 
https://code.launchpad.net/~jelmer/launchpad/publisher-use-debian-1/+merge/32245
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jelmer/launchpad/publisher-use-debian-1 into lp:launchpad/devel.
=== modified file 'lib/lp/archivepublisher/publishing.py'
--- lib/lp/archivepublisher/publishing.py	2010-07-07 06:28:03 +0000
+++ lib/lp/archivepublisher/publishing.py	2010-08-10 19:24:54 +0000
@@ -14,6 +14,8 @@
 import os
 import shutil
 
+from debian.deb822 import Release
+
 from datetime import datetime
 
 from zope.component import getUtility
@@ -40,25 +42,6 @@
 
 suffixpocket = dict((v, k) for (k, v) in pocketsuffix.items())
 
-DISTRORELEASE_STANZA = """Origin: %s
-Label: %s
-Suite: %s
-Version: %s
-Codename: %s
-Date: %s
-Architectures: %s
-Components: %s
-Description: %s
-"""
-
-DISTROARCHRELEASE_STANZA = """Archive: %s
-Version: %s
-Component: %s
-Origin: %s
-Label: %s
-Architecture: %s
-"""
-
 def reorder_components(components):
     """Return a list of the components provided.
 
@@ -487,33 +470,38 @@
         else:
             drsummary += pocket.name.capitalize()
 
+        release_file = Release()
+        release_file["Origin"] = self._getOrigin()
+        release_file["Label"] = self._getLabel()
+        release_file["Suite"] = full_name
+        release_file["Version"] = distroseries.version
+        release_file["Codename"] = distroseries.name
+        release_file["Date"] = datetime.utcnow().strftime(
+            "%a, %d %b %Y %k:%M:%S UTC")
+        release_file["Architectures"] = " ".join(sorted(list(all_architectures)))
+        release_file["Components"] = " ".join(reorder_components(all_components))
+        release_file["Description"] = drsummary
+
+        for f in sorted(list(all_files), key=os.path.dirname):
+            entry = self._createSumLine(full_name, f)
+            if entry is None:
+                continue
+            release_file.setdefault("MD5Sum", []).append(
+                { "md5sum": hashlib.md5(entry).hexdigest(),
+                  "name": f, "size": len(entry)})
+            release_file.setdefault("SHA1", []).append(
+                { "sha1": hashlib.sha1(entry).hexdigest(),
+                  "name": f, "size": len(entry)})
+            release_file.setdefault("SHA256", []).append(
+                { "sha256": hashlib.sha256(entry).hexdigest(),
+                  "name": f, "size": len(entry)})
+
         f = open(os.path.join(
             self._config.distsroot, full_name, "Release"), "w")
-
-        stanza = (DISTRORELEASE_STANZA % (
-                    self._getOrigin(),
-                    self._getLabel(),
-                    full_name,
-                    distroseries.version,
-                    distroseries.name,
-                    datetime.utcnow().strftime("%a, %d %b %Y %k:%M:%S UTC"),
-                    " ".join(sorted(list(all_architectures))),
-                    " ".join(reorder_components(all_components)),
-                    drsummary)).encode("utf-8")
-        f.write(stanza)
-
-        f.write("MD5Sum:\n")
-        all_files = sorted(list(all_files), key=os.path.dirname)
-        for file_name in all_files:
-            self._writeSumLine(full_name, f, file_name, hashlib.md5)
-        f.write("SHA1:\n")
-        for file_name in all_files:
-            self._writeSumLine(full_name, f, file_name, hashlib.sha1)
-        f.write("SHA256:\n")
-        for file_name in all_files:
-            self._writeSumLine(full_name, f, file_name, hashlib.sha256)
-
-        f.close()
+        try:
+            release_file.dump(f, "utf-8")
+        finally:
+            f.close()
 
         # Skip signature if the archive signing key is undefined.
         if self.archive.signing_key is None:
@@ -562,23 +550,25 @@
 
         all_files.add(os.path.join(component, architecture, "Release"))
 
+        release_file = Release()
+        release_file["Archive"] = full_name
+        release_file["Version"] = distroseries.version
+        release_file["Component"] = component
+        release_file["Origin"] = self._getOrigin()
+        release_file["Label"] = self._getLabel()
+        release_file["Architecture"] = clean_architecture
+
         f = open(os.path.join(self._config.distsroot, full_name,
                               component, architecture, "Release"), "w")
-
-        stanza = (DISTROARCHRELEASE_STANZA % (
-                full_name,
-                distroseries.version,
-                component,
-                self._getOrigin(),
-                self._getLabel(),
-                unicode(clean_architecture))).encode("utf-8")
-        f.write(stanza)
-        f.close()
+        try:
+            release_file.dump(f, "utf-8")
+        finally:
+            f.close()
 
         return clean_architecture
 
-    def _writeSumLine(self, distroseries_name, out_file, file_name, sum_form):
-        """Write out a checksum line.
+    def _createSumLine(self, distroseries_name, file_name):
+        """Generate out a checksum entry.
 
         Writes a checksum to the given file for the given filename in
         the given form.
@@ -590,21 +580,17 @@
             # Most likely we have an incomplete archive (E.g. no sources
             # for a given distroseries). This is a non-fatal issue
             self.log.debug("Failed to find " + full_name)
-            return
+            return None
 
         in_file = open(full_name, 'r')
         try:
-            contents = in_file.read()
-            length = len(contents)
-            checksum = sum_form(contents).hexdigest()
+            return in_file.read()
         finally:
             in_file.close()
 
-        out_file.write(" %s % 16d %s\n" % (checksum, length, file_name))
-
     def deleteArchive(self):
         """Delete the archive.
-        
+
         Physically remove the entire archive from disk and set the archive's 
         status to DELETED.