~james-w/udd/storm-sqlite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/python
import os
import signal
import subprocess
import sys

from debian_bundle import changelog

from launchpadlib.credentials import Credentials
from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT

from bzrlib import branch, errors, testament
from bzrlib.plugin import load_plugins

load_plugins()

from bzrlib.plugins.builddeb import import_dsc


base_dir = "/srv/package-import.canonical.com/new/"
if not os.path.exists(base_dir):
    base_dir = "."
revids_dir = os.path.join(base_dir, "revids")
lists_dir = os.path.join(base_dir, "lists")
lp_creds_file = os.path.join(base_dir, "lp_creds.txt")
lp_cache_dir = os.path.join(base_dir, "lp_cache")


lp_distro_releases = {"ubuntu": ["karmic", "warty", "hoary", "breezy", "dapper", "edgy", "feisty", "gutsy", "hardy", "intrepid", "jaunty"],
    "debian": ["squeeze", "lenny", "sid", "experimental"],
}

distro_releases = {"ubuntu": ["warty", "hoary", "breezy", "dapper", "edgy", "feisty", "gutsy", "hardy", "intrepid", "jaunty", "karmic"],
    "debian": ["woody", "sarge", "etch", "lenny", "squeeze", "sid", "experimental"],
}

distro_pockets = {"ubuntu": ["release", "security", "proposed", "updates", "backports"],
    "debian": ["release",],
}


f = open(lp_creds_file, "rb")
try:
    creds = Credentials("package-import")
    creds.load(f)
finally:
    f.close()
lp = Launchpad(creds, service_root=EDGE_SERVICE_ROOT, cache=lp_cache_dir)
ubuntu = lp.distributions['ubuntu']
u_archive = ubuntu.main_archive
debian = lp.distributions['debian']
d_archive = debian.main_archive


def subprocess_setup():
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)


def translate_lp_suite(distro, suite):
    if distro == "debian":
        if suite == "oldstable":
            suite = "etch"
        elif suite == "stable":
            suite = "lenny"
        elif suite == "testing":
            suite = "squeeze"
        elif suite == "unstable":
            suite = "sid"
    return distro, suite


def branch_location(distro, package, release, suite):
    distro, suite = translate_lp_suite(distro, suite)
    return "bzr+ssh://bazaar.launchpad.net/~ubuntu-branches/%s/%s/%s/%s" % (distro, release, package, suite)


def revid_filename(package, suite):
    revid_dir = os.path.join(revids_dir, package)
    if not os.path.exists(revid_dir):
        os.makedirs(revid_dir)
    revid_file = os.path.join(revid_dir, suite)
    return revid_file


def load_revids(package, suite):
    revid_file = revid_filename(package, suite)
    revid_map = {}
    if os.path.exists(revid_file):
        f = open(revid_file)
        try:
            for line in f:
                v, r, s = line.split()
                revid_map[v] = (r, s)
        finally:
            f.close()
    return revid_map


def make_suite(release, pocket):
    if pocket == "release":
        suite = release
    elif pocket in ("security", "proposed", "updates", "backports"):
        suite = "%s-%s" % (release, pocket)
    else:
        assert False, "Unkown pocket: %s" % pocket
    return suite


def check_revid(package, version, revid, suite, branch):
    revid_map = load_revids(package, suite)
    branch.repository.lock_read()
    try:
        tment = testament.StrictTestament3.from_revision(branch.repository, revid)
        sha = tment.as_sha1()
        if not str(version) in revid_map:
            print("%s %s %s is not marked" % (package, str(version), suite))
        if (revid, sha) != revid_map[str(version)]:
            print("%s != %s for %s %s %s, something has changed"
                 % (str((revid, sha)), str(revid_map[str(version)]), package,
                              suite, str(version)))
    finally:
        branch.repository.unlock()


def is_marked(package, version, suite):
    revid_map = load_revids(package, suite)
    return str(version) in revid_map


def check_marks(all_versions, name):
    have_imported_old_debian = False
    for (version, distro, release, component, pocket, on_lp) in reversed(all_versions):
        suite = make_suite(release, pocket)
        target_branch_path = branch_location(distro, name, release, suite)
        try:
            target_branch = branch.Branch.open(target_branch_path)
            has_version = True
        except errors.NotBranchError:
            has_version = False
        if has_version:
            db = import_dsc.DistributionBranch(target_branch, target_branch)
            has_version = db.has_version(version)
        if not has_version:
            if is_marked(name, version, suite):
                print("%s is marked in %s but not imported" % (version, revid_filename(name, suite)))
            # We'll never find old debian releases, so if there is a new one already
            # imported then don't try and re-import the old ones.
            if distro == "debian" and release not in lp_distro_releases[distro]:
                if have_imported_old_debian:
                    continue
            print("%s %s %s %s %s is not imported" % (version, distro, release, component, pocket))
        else:
            revid = db.revid_of_version(version)
            check_revid(name, version, revid, suite, db.branch)
            if distro == "debian":
                have_imported_old_debian = True


def get_debian_versions(package):
    versions = []
    publications = d_archive.getPublishedSources(source_name=package, exact_match=True)
    for publication in publications:
        assert publication.source_package_name == package
        release = publication.distro_series.name.lower()
        pocket = publication.pocket.lower()
        version = changelog.Version(publication.source_package_version)
        if (version, "debian", release, "", pocket, True) not in versions:
            versions.append((version, "debian", release, "", pocket, True))
    proc = subprocess.Popen(["/usr/bin/madison-lite", "--mirror", lists_dir,
            package], stdout=subprocess.PIPE, preexec_fn=subprocess_setup)
    ret = proc.wait()
    assert ret == 0, "Error running madison-lite"
    for line in proc.stdout:
        # package | version | release/component | arch
        ret_package = line[:line.index("|")].strip()
        assert ret_package == package
        line = line[line.index("|")+1:].strip()
        version = changelog.Version(line[:line.index("|")].strip())
        line = line[line.index("|")+1:].strip()
        release_component = line[:line.index("|")].strip()
        if "/" in release_component:
            release = release_component[:release_component.index("/")]
            component = release_component[release_component.index("/")+1:]
        else:
            release = release_component
            component = "main"
        if (version, "debian", release, "", "release", True) not in versions:
            versions.append((changelog.Version(version), "debian", release, component, "release", False))
    return versions


def get_versions(package):
    versions = []
    versions = get_debian_versions(package)
    publications = u_archive.getPublishedSources(source_name=package, exact_match=True)
    for publication in publications:
        assert publication.source_package_name == package
        release = publication.distro_series.name.lower()
        pocket = publication.pocket.lower()
        version = changelog.Version(publication.source_package_version)
        if (version, "ubuntu", release, "", pocket, True) not in versions:
            versions.append((version, "ubuntu", release, "", pocket, True))
    def compare_versions(a, b):
        if a[1] == "ubuntu" and a[1] == b[1]:
            if a[4] == "backports" and b[4] != "backports":
                return -1
            if a[4] != "backports" and b[4] == "backports":
                return 1
        if a[0] < b[0]:
            return 1
        if a[0] > b[0]:
            return -1
        if a[1] != b[1]:
            if a[1] == "debian":
                return 1
            return -1
        if a[2] != b[2]:
            a_release = distro_releases[a[1]].index(a[2])
            b_release = distro_releases[b[1]].index(b[2])
            if a_release < b_release:
                return 1
            return -1
        if a[4] != b[4]:
            a_pocket = distro_pockets[a[1]].index(a[4])
            b_pocket = distro_pockets[b[1]].index(b[4])
            if a_pocket < b_pocket:
                return 1
            return -1
        return 0
    versions.sort(cmp=compare_versions)
    return versions


for package in sys.argv[1:]:
    versions = get_versions(package)
    assert len(versions) > 0
    versions.reverse()
    check_marks(versions, package)