~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/ftpmaster-tools/archive-integrity-check.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-03-17 20:41:13 UTC
  • mfrom: (3277.1.4 launchpad-foobar2)
  • Revision ID: pqm@pqm.ubuntu.com-20060317204113-9841a4470db3611b
[r=jamesh] Mainline soyuz

Show diffs side-by-side

added added

removed removed

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