Skip to content

Commit 97d334d

Browse files
committed
---
yaml --- r: 2316 b: refs/heads/master c: cb53065 h: refs/heads/master v: v3
1 parent db9f77c commit 97d334d

File tree

5 files changed

+213
-1
lines changed

5 files changed

+213
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 2fc58fc6a070b44bc6a7d2629d7d6eb1d074b9fb
2+
refs/heads/master: cb53065a2116067a036993a6762259a66c4abfac

trunk/src/etc/get-snapshot.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
3+
import os, tarfile, hashlib, re, shutil
4+
from snapshot import *
5+
6+
def snap_filename_hash_part(snap):
7+
match = re.match(r".*([a-fA-F\d]{40}).tar.bz2$", snap)
8+
if not match:
9+
raise Exception("unable to find hash in filename: " + snap)
10+
return match.group(1)
11+
12+
def get_snapshot_and_check_hash(snap):
13+
14+
hsh = snap_filename_hash_part(snap)
15+
16+
h = hashlib.sha1()
17+
url = download_url_base + "/" + snap
18+
print "downloading " + url
19+
u = urllib2.urlopen(url)
20+
print "checking hash on download"
21+
data = u.read()
22+
h.update(data)
23+
if h.hexdigest() != hsh:
24+
raise Exception("hash check failed on " + snap)
25+
26+
print "hash ok"
27+
with open(os.path.join(download_dir_base, snap), "w+b") as f:
28+
f.write(data)
29+
return True
30+
31+
def unpack_snapshot(snap):
32+
dl_path = os.path.join(download_dir_base, snap)
33+
print "opening snapshot " + dl_path
34+
tar = tarfile.open(dl_path)
35+
kernel = get_kernel()
36+
for name in snapshot_files[kernel]:
37+
p = os.path.join("rust-stage0", name)
38+
fp = os.path.join("stage0", name)
39+
print "extracting " + fp
40+
tar.extract(p, download_unpack_base)
41+
tp = os.path.join(download_unpack_base, p)
42+
shutil.move(tp, fp)
43+
tar.close()
44+
shutil.rmtree(download_unpack_base)
45+
46+
def determine_last_snapshot_for_platform():
47+
lines = open(snapshotfile).readlines();
48+
49+
platform = get_platform()
50+
51+
found = False
52+
hsh = None
53+
date = None
54+
rev = None
55+
56+
for ln in range(len(lines) - 1, -1, -1):
57+
parsed = parse_line(ln, lines[ln])
58+
if (not parsed): continue
59+
60+
if parsed["type"] == "file":
61+
if parsed["platform"] == platform:
62+
hsh = parsed["hash"]
63+
elif parsed["type"] == "snapshot":
64+
date = parsed["date"]
65+
rev = parsed["rev"]
66+
found = True
67+
break
68+
elif parsed["type"] == "transition" and not foundSnapshot:
69+
raise Exception("working on a transition, not updating stage0")
70+
71+
if not found:
72+
raise Exception("no snapshot entries in file")
73+
74+
if not hsh:
75+
raise Exception("no snapshot file found for platform %s, rev %s" %
76+
(platform, rev))
77+
78+
return full_snapshot_name(date, rev, get_kernel(), get_cpu(), hsh)
79+
80+
# Main
81+
82+
snap = determine_last_snapshot_for_platform()
83+
print "determined most recent snapshot: " + snap
84+
dl = os.path.join(download_dir_base, snap)
85+
if (os.path.exists(dl)):
86+
if (snap_filename_hash_part(snap) == hash_file(dl)):
87+
print "found existing download with ok hash"
88+
else:
89+
print "bad hash on existing download, re-fetching"
90+
get_snapshot_and_check_hash(snap)
91+
else:
92+
print "no cached download, fetching"
93+
get_snapshot_and_check_hash(snap)
94+
95+
unpack_snapshot(snap)

trunk/src/etc/make-snapshot.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
3+
import shutil, tarfile
4+
from snapshot import *
5+
6+
kernel = get_kernel()
7+
cpu = get_cpu()
8+
rev = local_rev_short_sha()
9+
date = local_rev_committer_date().split()[0]
10+
11+
file0 = partial_snapshot_name(date, rev, kernel, cpu)
12+
13+
tar = tarfile.open(file0, "w:bz2")
14+
for name in snapshot_files[kernel]:
15+
tar.add(os.path.join("stage2", name),
16+
os.path.join("rust-stage0", name))
17+
tar.close()
18+
19+
h = hash_file(file0)
20+
file1 = full_snapshot_name(date, rev, kernel, cpu, h)
21+
22+
shutil.move(file0, file1)
23+
24+
print file1

trunk/src/etc/snapshot.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import re, os, sys, hashlib, tarfile, shutil, subprocess, urllib2, tempfile
2+
3+
snapshotfile = "snapshots.txt"
4+
download_url_base = "http://dl.rust-lang.org/stage0-snapshots"
5+
download_dir_base = "dl"
6+
download_unpack_base = os.path.join(download_dir_base, "unpack")
7+
8+
snapshot_files = {
9+
"linux": ["rustc", "glue.o", "libstd.so" ],
10+
"macos": ["rustc", "glue.o", "libstd.dylib" ],
11+
"winnt": ["rustc.exe", "glue.o", "std.dll" ]
12+
}
13+
14+
def parse_line(n, line):
15+
global snapshotfile
16+
17+
if re.match(r"\s*$", line): return None
18+
19+
match = re.match(r"\s+([\w_-]+) ([a-fA-F\d]{40})\s*$", line)
20+
if match:
21+
return { "type": "file",
22+
"platform": match.group(1),
23+
"hash": match.group(2).lower() }
24+
25+
match = re.match(r"([ST]) (\d{4}-\d{2}-\d{2}) ([a-fA-F\d]+)\s*$", line);
26+
if (not match):
27+
raise Exception("%s:%d:E syntax error" % (snapshotfile, n))
28+
ttype = "snapshot"
29+
if (match.group(1) == "T"):
30+
ttype = "transition"
31+
return {"type": ttype,
32+
"date": match.group(2),
33+
"rev": match.group(3)}
34+
35+
36+
def partial_snapshot_name(date, rev, kernel, cpu):
37+
return ("rust-stage0-%s-%s-%s-%s.tar.bz2"
38+
% (date, rev, kernel, cpu))
39+
40+
def full_snapshot_name(date, rev, kernel, cpu, hsh):
41+
return ("rust-stage0-%s-%s-%s-%s-%s.tar.bz2"
42+
% (date, rev, kernel, cpu, hsh))
43+
44+
45+
def get_kernel():
46+
if os.name == "nt":
47+
return "winnt"
48+
kernel = os.uname()[0].lower()
49+
if kernel == "darwin":
50+
kernel = "macos"
51+
return kernel
52+
53+
54+
def get_cpu():
55+
# return os.uname()[-1].lower()
56+
return "i386"
57+
58+
59+
def get_platform():
60+
return "%s-%s" % (get_kernel(), get_cpu())
61+
62+
63+
def cmd_out(cmdline):
64+
p = subprocess.Popen(cmdline,
65+
stdout=subprocess.PIPE)
66+
return p.communicate()[0].strip()
67+
68+
69+
def local_rev_info(field):
70+
return cmd_out(["git", "log", "-n", "1",
71+
"--format=%%%s" % field, "HEAD"])
72+
73+
74+
def local_rev_full_sha():
75+
return local_rev_info("H").split()[0]
76+
77+
78+
def local_rev_short_sha():
79+
return local_rev_info("h").split()[0]
80+
81+
82+
def local_rev_committer_date():
83+
return local_rev_info("ci")
84+
85+
86+
def hash_file(x):
87+
h = hashlib.sha1()
88+
h.update(open(x).read())
89+
return h.hexdigest()

trunk/src/snapshots.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
S 2011-04-29 7b95b5c
2+
linux-i386 f0e166816ce34adc9f7202bd3cfbd80623505f28
3+
macos-i386 abf2ee279da63676ca17c9dc9e54d04d8f752b00
4+
winnt-i386 7d27adcc5e0c111e3221751962a7df0bcb9a9288

0 commit comments

Comments
 (0)