aboutsummaryrefslogtreecommitdiffstats
path: root/minidinstall/ChangeFile.py
diff options
context:
space:
mode:
authorLibravatarKrytarik Raido <krytarik@tuxgarage.com>2018-04-03 06:50:04 +0200
committerLibravatarKrytarik Raido <krytarik@tuxgarage.com>2018-04-03 06:50:04 +0200
commitdc580be8f9ef38a1c0903820b04e1b5c7217da16 (patch)
tree4a214d88d3e094efdb9e4ff70920537a4d33ae9b /minidinstall/ChangeFile.py
parent23ac25c0b388b5ffebf66154b12a3950b89b977a (diff)
Various improvements in coding style.
Diffstat (limited to 'minidinstall/ChangeFile.py')
-rw-r--r--minidinstall/ChangeFile.py77
1 files changed, 35 insertions, 42 deletions
diff --git a/minidinstall/ChangeFile.py b/minidinstall/ChangeFile.py
index 3b0cf48..4a65af6 100644
--- a/minidinstall/ChangeFile.py
+++ b/minidinstall/ChangeFile.py
@@ -1,8 +1,8 @@
-# ChangeFile
+# ChangeFile -*- mode: python; coding: utf-8 -*-
# A class which represents a Debian change file.
-# Copyright 2002 Colin Walters <walters@gnu.org>
+# Copyright (c) 2002 Colin Walters <walters@gnu.org>
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
@@ -18,11 +18,11 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-import os, re, sys, string, stat
-import threading, queue
+import os, re, stat
import logging
-from minidinstall import DpkgControl, SignedFile
-from minidinstall import misc
+from .DpkgControl import *
+from .SignedFile import *
+from . import misc
class ChangeFileException(Exception):
def __init__(self, value):
@@ -30,19 +30,19 @@ class ChangeFileException(Exception):
def __str__(self):
return repr(self._value)
-class ChangeFile(DpkgControl.DpkgParagraph):
- md5_re = r'^(?P<md5>[0-9a-f]{32})[ \t]+(?P<size>\d+)[ \t]+(?P<section>[-/a-zA-Z0-9]+)[ \t]+(?P<priority>[-a-zA-Z0-9]+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$'
- sha1_re = r'^(?P<sha1>[0-9a-f]{40})[ \t]+(?P<size>\d+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$'
- sha256_re = r'^(?P<sha256>[0-9a-f]{64})[ \t]+(?P<size>\d+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$'
+class ChangeFile(DpkgParagraph):
+ md5_re = r'^(?P<hashsum>[0-9a-f]{32})[ \t]+(?P<size>\d+)[ \t]+(?P<section>[-/a-zA-Z0-9]+)[ \t]+(?P<priority>[-a-zA-Z0-9]+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$'
+ sha1_re = r'^(?P<hashsum>[0-9a-f]{40})[ \t]+(?P<size>\d+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$'
+ sha256_re = r'^(?P<hashsum>[0-9a-f]{64})[ \t]+(?P<size>\d+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$'
def __init__(self):
- DpkgControl.DpkgParagraph.__init__(self)
+ DpkgParagraph.__init__(self)
self._logger = logging.getLogger("mini-dinstall")
self._file = ''
def load_from_file(self, filename):
self._file = filename
- f = SignedFile.SignedFile(open(self._file))
+ f = SignedFile(open(self._file))
self.load(f)
f.close()
@@ -52,57 +52,50 @@ class ChangeFile(DpkgControl.DpkgParagraph):
def _get_checksum_from_changes(self):
""" extract checksums and size from changes file """
output = {}
- hashes = { 'md5': ['files', re.compile(self.md5_re)],
- 'sha1': ['checksums-sha1', re.compile(self.sha1_re)],
- 'sha256': ['checksums-sha256', re.compile(self.sha256_re)]
- }
- hashes_checked = hashes.copy()
+ hashes = {
+ 'md5': ['files', re.compile(self.md5_re)],
+ 'sha1': ['checksums-sha1', re.compile(self.sha1_re)],
+ 'sha256': ['checksums-sha256', re.compile(self.sha256_re)]
+ }
- try:
- self['files']
- except KeyError:
+ if 'files' not in self:
return []
- for hash in hashes:
- try:
- self[hashes[hash][0]]
- except KeyError:
+ for (hash, (field, regex)) in list(hashes.items()):
+ if field not in self:
self._logger.warn("Can't find %s checksum in changes file '%s'" % (hash, os.path.basename(self._file)))
- hashes_checked.pop(hash)
-
- for hash in hashes_checked:
+ continue
output[hash] = []
- for line in self[hashes[hash][0]]:
- if line == '':
+ for line in self[field].splitlines():
+ if not line:
continue
- match = hashes[hash][1].match(line)
- if (match is None):
- raise ChangeFileException("Couldn't parse file entry \"%s\" in Files field of .changes" % (line,))
- output[hash].append([match.group(hash), match.group('size'), match.group('file') ])
+ match = regex.match(line)
+ if not match:
+ raise ChangeFileException("Couldn't parse file entry \"%s\" in %s field of .changes" % (line, self.trueFieldCasing[field]))
+ output[hash].append([match.group('hashsum'), match.group('size'), match.group('file')])
return output
def verify(self, sourcedir):
""" verify size and hash values from changes file """
checksum = self._get_checksum_from_changes()
- for hash in list(checksum.keys()):
- for (hashsum, size, filename) in checksum[hash]:
+ for (hash, value) in list(checksum.items()):
+ for (hashsum, size, filename) in value:
self._verify_file_integrity(os.path.join(sourcedir, filename), int(size), hash, hashsum)
-
def _verify_file_integrity(self, filename, expected_size, hash, expected_hashsum):
""" check uploaded file integrity """
- self._logger.debug('Checking integrity of %s' % (filename,))
+ self._logger.debug('Checking integrity of %s' % filename)
try:
statbuf = os.stat(filename)
if not stat.S_ISREG(statbuf[stat.ST_MODE]):
- raise ChangeFileException("%s is not a regular file" % (filename,))
+ raise ChangeFileException("%s is not a regular file" % filename)
size = statbuf[stat.ST_SIZE]
except OSError as e:
- raise ChangeFileException("Can't stat %s: %s" % (filename,e.strerror))
+ raise ChangeFileException("Can't stat %s: %s" % (filename, e.strerror))
if size != expected_size:
- raise ChangeFileException("File size for %s does not match that specified in .dsc" % (filename,))
- if (misc.get_file_sum(self, hash, filename) != expected_hashsum):
- raise ChangeFileException("%ssum for %s does not match that specified in .dsc" % (hash, filename,))
+ raise ChangeFileException("File size for %s does not match that specified in .dsc" % filename)
+ if misc.get_file_sum(self, hash, filename) != expected_hashsum:
+ raise ChangeFileException("%ssum for %s does not match that specified in .dsc" % (hash, filename))
self._logger.debug('Verified %ssum %s and size %s for %s' % (hash, expected_hashsum, expected_size, filename))
# vim:ts=4:sw=4:et: