]> git.decadent.org.uk Git - dak.git/blobdiff - daklib/upload.py
Add by-hash support
[dak.git] / daklib / upload.py
index c55c4090496818d9c5f5cf07f64ec6445d58e7af..4c10f45f4e433e36bfd40a40fcff31673f2fc5b9 100644 (file)
@@ -23,45 +23,71 @@ It provides methods to access the included binary and source packages.
 
 import apt_inst
 import apt_pkg
+import errno
 import os
 import re
 
 from daklib.gpg import SignedFile
 from daklib.regexes import *
+import daklib.packagelist
 
-class InvalidChangesException(Exception):
+class UploadException(Exception):
     pass
 
-class InvalidBinaryException(Exception):
+class InvalidChangesException(UploadException):
     pass
 
-class InvalidSourceException(Exception):
+class InvalidBinaryException(UploadException):
     pass
 
-class InvalidHashException(Exception):
+class InvalidSourceException(UploadException):
+    pass
+
+class InvalidHashException(UploadException):
     def __init__(self, filename, hash_name, expected, actual):
         self.filename = filename
         self.hash_name = hash_name
         self.expected = expected
         self.actual = actual
     def __str__(self):
-        return "Invalid {0} hash for {1}: expected {2}, but got {3}.".format(self.hash_name, self.filename, self.expected, self.actual)
-
-class InvalidFilenameException(Exception):
+        return ("Invalid {0} hash for {1}:\n"
+                "According to the control file the {0} hash should be {2},\n"
+                "but {1} has {3}.\n"
+                "\n"
+                "If you did not include {1} in your upload, a different version\n"
+                "might already be known to the archive software.") \
+                .format(self.hash_name, self.filename, self.expected, self.actual)
+
+class InvalidFilenameException(UploadException):
     def __init__(self, filename):
         self.filename = filename
     def __str__(self):
         return "Invalid filename '{0}'.".format(self.filename)
 
+class FileDoesNotExist(UploadException):
+    def __init__(self, filename):
+        self.filename = filename
+    def __str__(self):
+        return "Refers to non-existing file '{0}'".format(self.filename)
+
 class HashedFile(object):
     """file with checksums
     """
-    def __init__(self, filename, size, md5sum, sha1sum, sha256sum, section=None, priority=None):
+    def __init__(self, filename, size, md5sum, sha1sum, sha256sum, section=None, priority=None, input_filename=None):
         self.filename = filename
         """name of the file
         @type: str
         """
 
+        if input_filename is None:
+            input_filename = filename
+        self.input_filename = input_filename
+        """name of the file on disk
+
+        Used for temporary files that should not be installed using their on-disk name.
+        @type: str
+        """
+
         self.size = size
         """size in bytes
         @type: long
@@ -92,6 +118,33 @@ class HashedFile(object):
         @type: str of C{None}
         """
 
+    @classmethod
+    def from_file(cls, directory, filename, section=None, priority=None):
+        """create with values for an existing file
+
+        Create a C{HashedFile} object that refers to an already existing file.
+
+        @type  directory: str
+        @param directory: directory the file is located in
+
+        @type  filename: str
+        @param filename: filename
+
+        @type  section: str or C{None}
+        @param section: optional section as given in .changes files
+
+        @type  priority: str or C{None}
+        @param priority: optional priority as given in .changes files
+
+        @rtype:  L{HashedFile}
+        @return: C{HashedFile} object for the given file
+        """
+        path = os.path.join(directory, filename)
+        with open(path, 'r') as fh:
+            size = os.fstat(fh.fileno()).st_size
+            hashes = apt_pkg.Hashes(fh)
+        return cls(filename, size, hashes.md5, hashes.sha1, hashes.sha256, section, priority)
+
     def check(self, directory):
         """Validate hashes
 
@@ -102,28 +155,33 @@ class HashedFile(object):
 
         @raise InvalidHashException: hash mismatch
         """
-        path = os.path.join(directory, self.filename)
-        fh = open(path, 'r')
+        path = os.path.join(directory, self.input_filename)
+        try:
+            with open(path) as fh:
+                self.check_fh(fh)
+        except IOError as e:
+            if e.errno == errno.ENOENT:
+                raise FileDoesNotExist(self.input_filename)
+            raise
+
+    def check_fh(self, fh):
+        size = os.fstat(fh.fileno()).st_size
+        fh.seek(0)
+        hashes = apt_pkg.Hashes(fh)
 
-        size = os.stat(path).st_size
         if size != self.size:
             raise InvalidHashException(self.filename, 'size', self.size, size)
 
-        md5sum = apt_pkg.md5sum(fh)
-        if md5sum != self.md5sum:
-            raise InvalidHashException(self.filename, 'md5sum', self.md5sum, md5sum)
+        if hashes.md5 != self.md5sum:
+            raise InvalidHashException(self.filename, 'md5sum', self.md5sum, hashes.md5)
 
-        fh.seek(0)
-        sha1sum = apt_pkg.sha1sum(fh)
-        if sha1sum != self.sha1sum:
-            raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, sha1sum)
+        if hashes.sha1 != self.sha1sum:
+            raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, hashes.sha1)
 
-        fh.seek(0)
-        sha256sum = apt_pkg.sha256sum(fh)
-        if sha256sum != self.sha256sum:
-            raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, sha256sum)
+        if hashes.sha256 != self.sha256sum:
+            raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, hashes.sha256)
 
-def parse_file_list(control, has_priority_and_section):
+def parse_file_list(control, has_priority_and_section, safe_file_regexp = re_file_safe, fields = ('Files', 'Checksums-Sha1', 'Checksums-Sha256')):
     """Parse Files and Checksums-* fields
 
     @type  control: dict-like
@@ -140,7 +198,7 @@ def parse_file_list(control, has_priority_and_section):
     """
     entries = {}
 
-    for line in control["Files"].split('\n'):
+    for line in control.get(fields[0], "").split('\n'):
         if len(line) == 0:
             continue
 
@@ -153,24 +211,26 @@ def parse_file_list(control, has_priority_and_section):
 
         entries[filename] = entry
 
-    for line in control["Checksums-Sha1"].split('\n'):
+    for line in control.get(fields[1], "").split('\n'):
         if len(line) == 0:
             continue
         (sha1sum, size, filename) = line.split()
         entry = entries.get(filename, None)
-        if entry.get('size', None) != long(size):
-            raise InvalidChangesException('Size for {0} in Files and Checksum-Sha1 fields differ.'.format(filename))
+        if entry is None:
+            raise InvalidChangesException('{0} is listed in {1}, but not in {2}.'.format(filename, fields[1], fields[0]))
+        if entry is not None and entry.get('size', None) != long(size):
+            raise InvalidChangesException('Size for {0} in {1} and {2} fields differ.'.format(filename, fields[0], fields[1]))
         entry['sha1sum'] = sha1sum
 
-    for line in control["Checksums-Sha256"].split('\n'):
+    for line in control.get(fields[2], "").split('\n'):
         if len(line) == 0:
             continue
         (sha256sum, size, filename) = line.split()
         entry = entries.get(filename, None)
         if entry is None:
-            raise InvalidChangesException('No sha256sum for {0}.'.format(filename))
-        if entry.get('size', None) != long(size):
-            raise InvalidChangesException('Size for {0} in Files and Checksum-Sha256 fields differ.'.format(filename))
+            raise InvalidChangesException('{0} is listed in {1}, but not in {2}.'.format(filename, fields[2], fields[0]))
+        if entry is not None and entry.get('size', None) != long(size):
+            raise InvalidChangesException('Size for {0} in {1} and {2} fields differ.'.format(filename, fields[0], fields[2]))
         entry['sha256sum'] = sha256sum
 
     files = {}
@@ -184,7 +244,7 @@ def parse_file_list(control, has_priority_and_section):
             raise InvalidChangesException('No sha1sum for {0}.'.format(filename))
         if 'sha256sum' not in entry:
             raise InvalidChangesException('No sha256sum for {0}.'.format(filename))
-        if not re_file_safe.match(filename):
+        if safe_file_regexp is not None and not safe_file_regexp.match(filename):
             raise InvalidChangesException("{0}: References file with unsafe filename {1}.".format(self.filename, filename))
         f = files[filename] = HashedFile(**entry)
 
@@ -241,12 +301,20 @@ class Changes(object):
         """
         return self._signed_file.valid
 
+    @property
+    def signature_timestamp(self):
+        return self._signed_file.signature_timestamp
+
+    @property
+    def contents_sha1(self):
+        return self._signed_file.contents_sha1
+
     @property
     def architectures(self):
         """list of architectures included in the upload
         @type: list of str
         """
-        return self.changes['Architecture'].split()
+        return self.changes.get('Architecture', '').split()
 
     @property
     def distributions(self):
@@ -269,6 +337,13 @@ class Changes(object):
                 self._source = Source(self.directory, source_files, self._keyrings, self._require_signature)
         return self._source
 
+    @property
+    def sourceful(self):
+        """C{True} if the upload includes source
+        @type: bool
+        """
+        return "source" in self.architectures
+
     @property
     def source_name(self):
         """source package name
@@ -299,6 +374,8 @@ class Changes(object):
         for f in self.files.itervalues():
             if re_file_dsc.match(f.filename) or re_file_source.match(f.filename) or re_file_binary.match(f.filename):
                 continue
+            if re_file_buildinfo.match(f.filename):
+                continue
             if f.section != 'byhand' and f.section[:4] != 'raw-':
                 raise InvalidChangesException("{0}: {1} looks like a byhand package, but is in section {2}".format(self.filename, f.filename, f.section))
             byhand.append(f)
@@ -379,7 +456,7 @@ class Binary(object):
         @type: HashedFile
         """
 
-        path = os.path.join(directory, hashed_file.filename)
+        path = os.path.join(directory, hashed_file.input_filename)
         data = apt_inst.DebFile(path).control.extractdata("control")
 
         self.control = apt_pkg.TagSection(data)
@@ -387,6 +464,11 @@ class Binary(object):
         @type: dict-like
         """
 
+    @classmethod
+    def from_file(cls, directory, filename):
+        hashed_file = HashedFile.from_file(directory, filename)
+        return cls(directory, hashed_file)
+
     @property
     def source(self):
         """get tuple with source package name and version
@@ -403,6 +485,10 @@ class Binary(object):
             version = self.control['Version']
         return (match.group('package'), version)
 
+    @property
+    def name(self):
+        return self.control['Package']
+
     @property
     def type(self):
         """package type ('deb' or 'udeb')
@@ -439,7 +525,11 @@ class Source(object):
                     raise InvalidSourceException("Multiple .dsc found ({0} and {1})".format(self._dsc_file.filename, f.filename))
                 else:
                     self._dsc_file = f
-        dsc_file_path = os.path.join(directory, self._dsc_file.filename)
+
+        # make sure the hash for the dsc is valid before we use it
+        self._dsc_file.check(directory)
+
+        dsc_file_path = os.path.join(directory, self._dsc_file.input_filename)
         data = open(dsc_file_path, 'r').read()
         self._signed_file = SignedFile(data, keyrings, require_signature)
         self.dsc = apt_pkg.TagSection(self._signed_file.contents)
@@ -447,8 +537,18 @@ class Source(object):
         @type: dict-like
         """
 
+        self.package_list = daklib.packagelist.PackageList(self.dsc)
+        """Information about packages built by the source.
+        @type: daklib.packagelist.PackageList
+        """
+
         self._files = None
 
+    @classmethod
+    def from_file(cls, directory, filename, keyrings, require_signature=True):
+        hashed_file = HashedFile.from_file(directory, filename)
+        return cls(directory, [hashed_file], keyrings, require_signature)
+
     @property
     def files(self):
         """dict mapping filenames to L{HashedFile} objects for additional source files
@@ -489,3 +589,10 @@ class Source(object):
         if len(fields) > 1:
             return fields[0]
         return "main"
+
+    @property
+    def filename(self):
+        """filename of .dsc file
+        @type: str
+        """
+        return self._dsc_file.filename