X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Futils.py;h=52b902f9ffe84e298fce9fb4c127b935b5376f6a;hb=df1bf169c5b89fa9764a326bbd7a6883a8789f6b;hp=02278e9130f78579a8e04dea6aef8cd998a55ca8;hpb=b5d21dfae245e479a1dfd261b7f1a9d9bf2e9b99;p=dak.git diff --git a/daklib/utils.py b/daklib/utils.py index 02278e91..52b902f9 100755 --- a/daklib/utils.py +++ b/daklib/utils.py @@ -26,6 +26,7 @@ import codecs, commands, email.Header, os, pwd, re, select, socket, shutil, \ sys, tempfile, traceback, stat import apt_pkg import database +import time from dak_exceptions import * ################################################################################ @@ -50,6 +51,9 @@ re_verwithext = re.compile(r"^(\d+)(?:\.(\d+))(?:\s+\((\S+)\))?$") re_srchasver = re.compile(r"^(\S+)\s+\((\S+)\)$") +html_escaping = {'"':'"', '&':'&', '<':'<', '>':'>'} +re_html_escaping = re.compile('|'.join(map(re.escape, html_escaping.keys()))) + default_config = "/etc/dak/dak.conf" default_apt_config = "/etc/dak/apt.conf" @@ -62,6 +66,11 @@ known_hashes = [("sha1", apt_pkg.sha1sum, (1, 8)), ################################################################################ +def html_escape(s): + return re_html_escaping.sub(lambda x: html_escaping.get(x.group(0)), s) + +################################################################################ + def open_file(filename, mode='r'): try: f = open(filename, mode) @@ -250,24 +259,28 @@ def check_hash(where, files, hashname, hashfunc): rejmsg = [] for f in files.keys(): + file_handle = None try: - file_handle = open_file(f) - - # Check for the hash entry, to not trigger a KeyError. - if not files[f].has_key(hash_key(hashname)): - rejmsg.append("%s: misses %s checksum in %s" % (f, hashname, - where)) + try: + file_handle = open_file(f) + + # Check for the hash entry, to not trigger a KeyError. + if not files[f].has_key(hash_key(hashname)): + rejmsg.append("%s: misses %s checksum in %s" % (f, hashname, + where)) + continue + + # Actually check the hash for correctness. + if hashfunc(file_handle) != files[f][hash_key(hashname)]: + rejmsg.append("%s: %s check failed in %s" % (f, hashname, + where)) + except CantOpenError: + # TODO: This happens when the file is in the pool. + # warn("Cannot open file %s" % f) continue - - # Actually check the hash for correctness. - if hashfunc(file_handle) != files[f][hash_key(hashname)]: - rejmsg.append("%s: %s check failed in %s" % (f, hashname, - where)) - except CantOpenError: - # XXX: IS THIS THE BLOODY CASE WHEN THE FILE'S IN THE POOL!? - continue finally: - file_handle.close() + if file_handle: + file_handle.close() return rejmsg ################################################################################ @@ -278,7 +291,15 @@ def check_size(where, files): rejmsg = [] for f in files.keys(): - actual_size = os.stat(f)[stat.ST_SIZE] + try: + entry = os.stat(f) + except OSError, exc: + if exc.errno == 2: + # TODO: This happens when the file is in the pool. + continue + raise + + actual_size = entry[stat.ST_SIZE] size = int(files[f]["size"]) if size != actual_size: rejmsg.append("%s: actual file size (%s) does not match size (%s) in %s" @@ -383,11 +404,15 @@ def parse_checksums(where, files, manifest, hashname): break hash, size, file = line.strip().split(' ') if not files.has_key(file): - rejmsg.append("%s: not present in files but in checksums-%s in %s" % - (file, hashname, where)) + # TODO: check for the file's entry in the original files dict, not + # the one modified by (auto)byhand and other weird stuff + # rejmsg.append("%s: not present in files but in checksums-%s in %s" % + # (file, hashname, where)) + continue if not files[file]["size"] == size: rejmsg.append("%s: size differs for files and checksums-%s entry "\ "in %s" % (file, hashname, where)) + continue files[file][hash_key(hashname)] = hash for f in files.keys(): if not files[f].has_key(hash_key(hashname)): @@ -420,7 +445,10 @@ def build_file_list(changes, is_a_dsc=0, field="files", hashname="md5sum"): format = format[:2] if is_a_dsc: - if format != (1,0): + # format = (1,0) are the only formats we currently accept, + # format = (0,0) are missing format headers of which we still + # have some in the archive. + if format != (1,0) and format != (0,0): raise UnknownFormatError, "%s" % (changes.get("format","0.0")) else: if (format < (1,5) or format > (1,8)): @@ -1204,11 +1232,22 @@ used.""" if keywords.has_key("NODATA"): reject("no signature found in %s." % (sig_filename)) bad = 1 + if keywords.has_key("EXPKEYSIG"): + args = keywords["EXPKEYSIG"] + if len(args) >= 1: + key = args[0] + reject("Signature made by expired key 0x%s" % (key)) + bad = 1 if keywords.has_key("KEYEXPIRED") and not keywords.has_key("GOODSIG"): args = keywords["KEYEXPIRED"] + expiredate="" if len(args) >= 1: - key = args[0] - reject("The key (0x%s) used to sign %s has expired." % (key, sig_filename)) + timestamp = args[0] + if timestamp.count("T") == 0: + expiredate = time.strftime("%Y-%m-%d", time.gmtime(timestamp)) + else: + expiredate = timestamp + reject("The key used to sign %s has expired on %s" % (sig_filename, expiredate)) bad = 1 if bad: @@ -1368,3 +1407,52 @@ if which_conf_file() != default_config: apt_pkg.ReadConfigFileISC(Cnf,which_conf_file()) ################################################################################ + +def generate_contents_information(filename): + """ + Generate a list of flies contained in a .deb + + @type filename: string + @param filename: the path to a .deb + + @rtype: list + @return: a list of files in the data.tar.* portion of the .deb + """ + cmd = "ar t %s" % (filename) + (result, output) = commands.getstatusoutput(cmd) + if result != 0: + reject("%s: 'ar t' invocation failed." % (filename)) + reject(utils.prefix_multi_line_string(output, " [ar output:] "), "") + + # Ugh ... this is ugly ... Code ripped from process_unchecked.py + chunks = output.split('\n') + + contents = [] + try: + cmd = "ar x %s %s" % (filename, chunks[2]) + (result, output) = commands.getstatusoutput(cmd) + if result != 0: + reject("%s: '%s' invocation failed." % (filename, cmd)) + reject(utils.prefix_multi_line_string(output, " [ar output:] "), "") + + # Got deb tarballs, now lets go through and determine what bits + # and pieces the deb had ... + if chunks[2] == "data.tar.gz": + data = tarfile.open("data.tar.gz", "r:gz") + elif data_tar == "data.tar.bz2": + data = tarfile.open("data.tar.bz2", "r:bz2") + else: + os.remove(chunks[2]) + reject("couldn't find data.tar.*") + + for tarinfo in data: + if not tarinfo.isdir(): + contents.append(tarinfo.name[2:]) + + finally: + if os.path.exists( chunks[2] ): + os.remove( chunks[2] ) + + return contents + +###############################################################################