2 import sys, os, re, gzip, bz2, hashlib
6 'Packages.gz' : gzip.GzipFile,
7 'Packages.bz2' : bz2.BZ2File,
9 'Sources.gz' : gzip.GzipFile,
10 'Sources.bz2' : bz2.BZ2File,
13 def parse_packages_file(path):
15 open_func = package_file_map[os.path.basename(path)]
16 file = open_func(path)
18 print "WARNING: failed to open %s: %s" % (path, e)
25 line = file.readline()
27 print "WARNING: failed to read %s: %s" % (path, e)
28 print "WARNING: %s" % e
31 # check if we are done with current value
32 if (line == '' or line[0] == '\n' or line[0] != ' ') and key != None:
35 if line == '' or line == '\n': # done current block
37 ret_list.append(cur_dict)
41 elif line[0] == ' ': # multi-line value
42 value += '\n' + line[1:-1]
44 if line[-1] == '\n': line = line[:-1]
47 if key == '': key = None
51 def find_packages_files(path):
53 for file in os.listdir(path):
54 file_path = "%s/%s" % (path, file)
55 if os.path.islink(file_path):
57 elif os.path.isdir(file_path):
58 files += find_packages_files(file_path)
59 elif file in package_file_map:
60 files.append(file_path)
63 if len(sys.argv) != 2:
64 print "Usage: debian-check-md5sum.py base-dir"
66 base_dir = sys.argv[1]
69 files_regex = re.compile('(\S+)\s+(\S+)\s+(\S+)')
70 for file in find_packages_files(base_dir):
71 file_type = os.path.basename(file).split('.')[0]
72 a = parse_packages_file(file)
73 for package in parse_packages_file(file):
74 if file_type == 'Packages':
75 if 'Filename' in package:
76 all[package['Filename']] = package
77 elif file_type == 'Sources':
78 files = package['Files'].split('\n')
80 if file == '': continue
81 match = files_regex.match(file)
82 file_path = '%s/%s' % (package['Directory'], match.group(3))
83 all[file_path] = { 'MD5sum' : match.group(1) }
84 print "NOTICE: need to check %d files" % len(all)
88 for (file, package) in all.iteritems():
89 path = '%s/%s' % (base_dir, file)
91 file = open(path, 'rb')
93 print "WARNING: missing %s" % path
95 if 'SHA256' in package:
97 hash = package['SHA256']
98 elif 'SHA1' in package:
100 hash = package['SHA1']
101 elif 'MD5sum' in package:
103 hash = package['MD5sum']
105 print "WARNING: no hash found for %s" % path
109 data = file.read(block_size)
112 hash_calc = md.hexdigest()
113 if hash == hash_calc:
114 print "NOTICE: hash ok for %s [hash = %s]" % (path, hash)
116 print "ERROR: hash mismatch for %s [hash = %s, hash_calc = %s]" % \
117 (path, hash, hash_calc)