~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
3024.3.38 by James Troup
Add a script to check md5sum+size on disk from Packages/Sources.
5
6
# Check the integrity of an archive via it's indices files
7
8
################################################################################
9
10
import commands
11
import os
12
import stat
13
import sys
14
15
import apt_pkg
16
17
import dak_utils
18
19
################################################################################
20
21
Filelist = None
22
ArchiveRoot = "/srv/launchpad.net/ubuntu-archive/ubuntu/"
23
Count = 0
24
25
################################################################################
26
27
def error(msg):
28
    sys.stderr.write("E: %s\n" % (msg))
29
30
################################################################################
31
32
def check_file(filename, md5sum_expected, size_expected):
33
    global Count
34
35
    # Check existence/readability
36
    if os.access(filename, os.R_OK) == 0:
37
        if os.path.exists(filename):
38
            error("%s could not be read (permission denied)" \
39
                  % (filename))
40
        else:
41
            error("%s is missing" % (filename))
42
        return
43
44
    # Check md5sum
45
    filehandle = open(filename)
46
    md5sum_found = apt_pkg.md5sum(filehandle)
47
    if md5sum_found != md5sum_expected:
48
        error("%s failed md5sum check ('%s' vs '%s')" \
49
              % (filename, md5sum_expected, md5sum_found))
50
    filehandle.close()
51
    # Check size
52
    size_found = os.stat(filename)[stat.ST_SIZE]
53
    size_expected = int(size_expected)
54
    if size_found != size_expected:
55
        error("%s failed size check (expected: %d, got: %d)" \
56
              % (filename, size_expected, size_found))
57
58
    Count += 1
59
    if Count % 10 == 0:
60
        sys.stdout.write(".")
61
        sys.stdout.flush()
62
63
################################################################################
64
                
65
def validate_sources(sources_filename, suite, component):
66
    if suite == "dapper":
67
        return
68
    sys.stdout.write("Checking %s/%s/source: " % (suite, component))
69
    sys.stdout.flush()
70
    # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
71
    temp_filename = dak_utils.temp_filename()
72
    (result, output) = commands.getstatusoutput("gunzip -c %s > %s" \
73
                                                % (sources_filename,
74
                                                   temp_filename))
75
    if (result != 0):
76
        sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
77
        sys.exit(result)
78
    sources = open(temp_filename)
79
    Sources = apt_pkg.ParseTagFile(sources)
80
    while Sources.Step():
81
        directory = Sources.Section.Find('Directory')
82
        files = Sources.Section.Find('Files')
83
        for i in files.split('\n'):
84
            (md5sum_expected, size_expected, name) = i.split()
85
            filename = os.path.join(ArchiveRoot, directory, name)
86
            check_file(filename, md5sum_expected, size_expected)
87
    sys.stdout.write("done.\n")
88
    sys.stdout.flush()
89
    sources.close()
90
    os.unlink(temp_filename)
91
92
################################################################################
93
94
def validate_packages(packages_filename, suite, component, architecture):
95
    if suite == "dapper":
96
        return
97
98
    sys.stdout.write("Checking %s/%s/%s: " % (suite, component, architecture))
99
    sys.stdout.flush()
100
    # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
101
    temp_filename = dak_utils.temp_filename()
102
    (result, output) = commands.getstatusoutput("gunzip -c %s > %s"
103
                                                % (packages_filename,
104
                                                   temp_filename))
105
    if (result != 0):
106
        sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
107
        sys.exit(result)
108
    packages = open(temp_filename)
109
    Packages = apt_pkg.ParseTagFile(packages)
110
    while Packages.Step():
111
        md5sum_expected = Packages.Section.Find('MD5sum')
112
        size_expected = Packages.Section.Find('Size')
113
        filename = Packages.Section.Find('Filename')
114
        filename = os.path.join(ArchiveRoot, filename)
115
        check_file(filename, md5sum_expected, size_expected)
116
117
    sys.stdout.write("done.\n")
118
    sys.stdout.flush()
119
    packages.close()
120
    os.unlink(temp_filename)
121
122
################################################################################
123
124
def _process_dir(_, dirname, filenames):
125
    global Filelist
126
127
    for filename in filenames:
128
        if filename == "Packages.gz" or filename == "Sources.gz":
129
            split = dirname.split('/')
130
            if split[-2] == "debian-installer":
131
                (suite, component, _, architecture) = split[-4:]
132
            else:
133
                (suite, component, architecture) = split[-3:]
134
            architecture = architecture.replace("binary-", "")
135
            full_filename = os.path.join(dirname, filename)
136
            if architecture == "source":
137
                validate_sources(full_filename, suite, component)
138
            else:
139
                validate_packages(full_filename, suite, component, architecture)
140
141
################################################################################
142
143
def main():
144
    global Filelist
145
146
    Filelist = {}
147
    apt_pkg.init()
148
149
    os.path.walk(os.path.join(ArchiveRoot, "dists"),
150
                 _process_dir, None)
151
152
    return 0
153
154
################################################################################
155
156
if __name__ == '__main__':
157
    sys.exit(main())