~ubuntu-branches/ubuntu/vivid/simplestreams/vivid-updates

« back to all changes in this revision

Viewing changes to tools/toolutil.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2013-06-28 16:23:11 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130628162311-zit9qzwvhsexcarl
Tags: 0.1.0~bzr266-0ubuntu1
* New upstream snapshot.
  * include sstream-query, improved sstream-sync
  * supports python3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
 
1
#!/usr/bin/python3
2
2
 
3
3
import json
4
4
import os
5
5
import os.path
6
 
import subprocess
7
6
 
 
7
from simplestreams import util
8
8
 
9
9
REL2VER = {
10
10
    "hardy": {'version': "8.04", 'devname': "Hardy Heron"},
13
13
    "precise": {'version': "12.04", 'devname': "Precise Pangolin"},
14
14
    "quantal": {'version': "12.10", 'devname': "Quantal Quetzal"},
15
15
    "raring": {'version': "13.04", 'devname': "Raring Ringtail"},
 
16
    "saucy": {'version': "13.10", 'devname': "Saucy Salamander"},
16
17
}
17
18
 
18
19
RELEASES = [k for k in REL2VER if k != "hardy"]
20
21
 
21
22
NUM_DAILIES = 4
22
23
 
 
24
 
23
25
def is_expected(repl, fields):
24
26
    rel = fields[0]
25
27
    serial = fields[3]
27
29
        if rel in ("lucid", "oneiric"):
28
30
            # lucid, oneiric do not have -root.tar.gz
29
31
            return False
30
 
        if rel == "precise" and cmp(serial, "20120202") <= 0:
 
32
        if rel == "precise" and serial <= "20120202":
31
33
            # precise got -root.tar.gz after alpha2
32
34
            return False
33
35
 
34
36
    if repl == "-disk1.img":
35
37
        if rel == "lucid":
36
38
            return False
37
 
        if rel == "oneiric" and cmp(serial, "20110802.2") <= 0:
 
39
        if rel == "oneiric" and serial <= "20110802.2":
38
40
            # oneiric got -disk1.img after alpha3
39
41
            return False
40
42
 
41
43
    #if some data in /query is not truely available, fill up this array
42
44
    #to skip it. ex: export BROKEN="precise/20121212.1 quantal/20130128.1"
43
 
    broken = os.environ.get("BROKEN","").split(" ")
 
45
    broken = os.environ.get("BROKEN", "").split(" ")
44
46
    if "%s/%s" % (rel, serial) in broken:
45
 
        print "Known broken: %s/%s" % (rel, serial)
 
47
        print("Known broken: %s/%s" % (rel, serial))
46
48
        return False
47
49
 
48
50
    return True
49
51
 
 
52
 
50
53
def load_query_download(path, builds=None, rels=None):
51
54
    if builds is None:
52
55
        builds = BUILDS
54
57
        rels = RELEASES
55
58
 
56
59
    streams = [f[0:-len(".latest.txt")]
57
 
               for f in os.listdir(path)
58
 
                   if f.endswith("latest.txt")]
 
60
               for f in os.listdir(path) if f.endswith("latest.txt")]
59
61
 
60
62
    results = []
61
63
    for stream in streams:
64
66
        latest_f = "%s/%s.latest.txt" % (path, stream)
65
67
 
66
68
        # get the builds and releases
67
 
        with open(latest_f) as fp:
 
69
        with open(latest_f, "r") as fp:
68
70
            for line in fp.readlines():
69
71
                (rel, build, _stream, _serial) = line.split("\t")
70
72
 
71
73
                if ((len(builds) and build not in builds) or
72
 
                    (len(rels) and rel not in rels)):
 
74
                        (len(rels) and rel not in rels)):
73
75
                    continue
74
76
 
75
77
                dl_files.append("%s/%s/%s/%s-dl.txt" %
76
 
                    (path, rel, build, stream))
 
78
                                (path, rel, build, stream))
77
79
 
78
80
        field_path = 5
79
81
        field_name = 6
80
82
        # stream/build/release/arch
81
83
        for dl_file in dl_files:
82
 
            olines = open(dl_file).readlines()
 
84
            olines = open(dl_file, "r").readlines()
83
85
 
84
86
            # download files in /query only contain '.tar.gz' (uec tarball)
85
87
            # file.  So we have to make up other entries.
103
105
 
104
106
    return results
105
107
 
 
108
 
106
109
def load_query_ec2(path, builds=None, rels=None, max_dailies=NUM_DAILIES):
107
110
    if builds is None:
108
111
        builds = BUILDS
110
113
        rels = RELEASES
111
114
 
112
115
    streams = [f[0:-len(".latest.txt")]
113
 
               for f in os.listdir(path)
114
 
                   if f.endswith("latest.txt")]
 
116
               for f in os.listdir(path) if f.endswith("latest.txt")]
115
117
    results = []
116
118
 
117
119
    for stream in streams:
120
122
        latest_f = "%s/%s.latest.txt" % (path, stream)
121
123
 
122
124
        # get the builds and releases
123
 
        with open(latest_f) as fp:
 
125
        with open(latest_f, "r") as fp:
124
126
            for line in fp.readlines():
125
127
                (rel, build, _stream, _serial) = line.split("\t")
126
128
 
127
129
                if ((len(builds) and build not in builds) or
128
 
                    (len(rels) and rel not in rels)):
 
130
                        (len(rels) and rel not in rels)):
129
131
                    continue
130
132
 
131
133
                id_files.append("%s/%s/%s/%s.txt" %
132
 
                    (path, rel, build, stream))
 
134
                                (path, rel, build, stream))
133
135
 
134
136
        for id_file in id_files:
135
 
            lines = reversed(open(id_file).readlines())
 
137
            lines = reversed(open(id_file, "r").readlines())
136
138
            serials_seen = 0
137
139
            last_serial = None
138
140
            for line in lines:
153
155
    return results
154
156
 
155
157
 
156
 
def get_sign_cmd(path, output=None, clearsign=False, armor=True):
157
 
    cmd = ['gpg']
158
 
    defkey = os.environ.get('SS_GPG_DEFAULT_KEY')
159
 
    if defkey:
160
 
        cmd.extend(['--default-key', defkey])
161
 
 
162
 
    batch = os.environ.get('SS_GPG_BATCH', "1").lower()
163
 
    if batch not in ("0", "false"):
164
 
        cmd.append('--batch')
165
 
 
166
 
    if output:
167
 
        cmd.extend(['--output', output])
168
 
 
169
 
    if clearsign:
170
 
        cmd.append('--clearsign')
171
 
    else:
172
 
        if armor:
173
 
            cmd.append('--armor')
174
 
        cmd.append('--sign')
175
 
 
176
 
    cmd.extend([path])
177
 
    return cmd
178
 
 
179
 
 
180
 
def signfile(path, output=None):
181
 
    if output is None:
182
 
        output = path + ".gpg"
183
 
 
184
 
    if os.path.exists(output):
185
 
        os.unlink(output)
186
 
 
187
 
    subprocess.check_output(get_sign_cmd(path, output))
188
 
 
189
 
 
190
 
def signfile_inline(path, output=None):
191
 
    infile = path
192
 
    tmpfile = None
193
 
    if output is None:
194
 
        # sign "in place" by using a temp file.
195
 
        tmpfile = "%s.tmp" % path
196
 
        os.rename(path, tmpfile)
197
 
        output = path
198
 
        infile = tmpfile
199
 
    elif os.path.exists(output):
200
 
        os.unlink(output)
201
 
 
202
 
    subprocess.check_output(get_sign_cmd(infile, output, clearsign=True))
203
 
 
204
 
    if tmpfile:
205
 
        os.unlink(tmpfile)
206
 
 
207
 
 
208
 
def signjs_file(fname, status_cb=None):
 
158
def signjson_file(fname, status_cb=None):
 
159
    # input fname should be .json
 
160
    # creates .json.gpg and .sjson
209
161
    content = ""
210
 
    with open(fname) as fp:
 
162
    with open(fname, "r") as fp:
211
163
        content = fp.read()
212
 
    data = json.loads(content)
213
 
    fmt = data.get("format")
214
 
    sjs = fname[0:-len(".js")] + ".sjs"
215
 
 
216
 
    if status_cb is None:
217
 
        def null_cb(fname, fmt):
218
 
            pass
219
 
        status_cb = null_cb
220
 
 
221
 
    if fmt == "products:1.0":
222
 
        status_cb(fname, fmt)
223
 
        signfile(fname)
224
 
        signfile_inline(fname, sjs)
225
 
    elif fmt == "index:1.0":
226
 
        status_cb(fname, fmt)
227
 
        signfile(fname)
228
 
        for _content_id, content in data.get('index').iteritems():
229
 
            path = content.get('path')
230
 
            if path.endswith(".js"):
231
 
                content['path'] = path[0:-len(".js")] + ".sjs"
232
 
        with open(sjs, "w") as fp:
233
 
            fp.write(json.dumps(data, indent=1))
234
 
        signfile_inline(sjs)
235
 
        return
 
164
    (changed, scontent) = util.make_signed_content_paths(content)
 
165
 
 
166
    if status_cb:
 
167
        status_cb(fname)
 
168
 
 
169
    util.sign_file(fname, inline=False)
 
170
    if changed:
 
171
        util.sign_content(scontent, util.signed_fname(fname, inline=True),
 
172
                          inline=True)
236
173
    else:
237
 
        status_cb(fname, fmt)
238
 
        return
 
174
        util.sign_file(fname, inline=True)
 
175
 
 
176
    return
 
177
 
239
178
 
240
179
# vi: ts=4 expandtab syntax=python