3 import re, os, sys, glob, tarfile, shutil, subprocess, tempfile
7 sha_func = hashlib.sha1
13 if sys.version_info >= (3,) and type(b) == bytes:
14 return b.decode('ascii')
18 src_dir = scrub(os.getenv("CFG_SRC_DIR"))
20 raise Exception("missing env var CFG_SRC_DIR")
22 snapshotfile = os.path.join(src_dir, "src", "snapshots.txt")
23 download_url_base = "http://static.rust-lang.org/stage0-snapshots"
24 download_dir_base = "dl"
25 download_unpack_base = os.path.join(download_dir_base, "unpack")
28 "linux": ["bin/rustc",
34 "lib/librustllvm.so"],
35 "macos": ["bin/rustc",
37 "lib/libextra-*.dylib",
38 "lib/librustc-*.dylib",
39 "lib/libsyntax-*.dylib",
40 "lib/librustrt.dylib",
41 "lib/librustllvm.dylib"],
42 "winnt": ["bin/rustc.exe",
49 "freebsd": ["bin/rustc",
58 def parse_line(n, line):
61 if re.match(r"\s*$", line): return None
63 if re.match(r"^T\s*$", line): return None
65 match = re.match(r"\s+([\w_-]+) ([a-fA-F\d]{40})\s*$", line)
67 return { "type": "file",
68 "platform": match.group(1),
69 "hash": match.group(2).lower() }
71 match = re.match(r"([ST]) (\d{4}-\d{2}-\d{2}) ([a-fA-F\d]+)\s*$", line);
73 raise Exception("%s:%d:E syntax error: " % (snapshotfile, n))
74 return {"type": "snapshot",
75 "date": match.group(2),
76 "rev": match.group(3)}
79 def partial_snapshot_name(date, rev, platform):
80 return ("rust-stage0-%s-%s-%s.tar.bz2"
81 % (date, rev, platform))
83 def full_snapshot_name(date, rev, platform, hsh):
84 return ("rust-stage0-%s-%s-%s-%s.tar.bz2"
85 % (date, rev, platform, hsh))
88 def get_kernel(triple):
89 os_name = triple.split('-')[-1]
90 #scrub(os.getenv("CFG_ENABLE_MINGW_CROSS")):
91 if os_name == "nt" or os_name == "mingw32":
93 if os_name == "darwin":
95 if os_name == "freebsd":
100 arch = triple.split('-')[0]
105 def get_platform(triple):
106 return "%s-%s" % (get_kernel(triple), get_cpu(triple))
109 def cmd_out(cmdline):
110 p = subprocess.Popen(cmdline,
111 stdout=subprocess.PIPE)
112 return scrub(p.communicate()[0].strip())
115 def local_rev_info(field):
116 return cmd_out(["git", "--git-dir=" + os.path.join(src_dir, ".git"),
118 "--format=%%%s" % field, "HEAD"])
121 def local_rev_full_sha():
122 return local_rev_info("H").split()[0]
125 def local_rev_short_sha():
126 return local_rev_info("h").split()[0]
129 def local_rev_committer_date():
130 return local_rev_info("ci")
132 def get_url_to_file(u,f):
133 # no security issue, just to stop partial download leaving a stale file
135 returncode = subprocess.call(["curl", "-o", tmpf, u])
141 def snap_filename_hash_part(snap):
142 match = re.match(r".*([a-fA-F\d]{40}).tar.bz2$", snap)
144 raise Exception("unable to find hash in filename: " + snap)
145 return match.group(1)
149 h.update(open(x, "rb").read())
150 return scrub(h.hexdigest())
153 def make_snapshot(stage, triple, flag):
154 kernel = get_kernel(triple)
155 platform = get_platform(triple)
156 rev = local_rev_short_sha()
157 date = local_rev_committer_date().split()[0]
159 file0 = partial_snapshot_name(date, rev, platform)
162 cs = re.split(r"[\\/]", fn)
164 return os.sep.join(cs[-2:])
166 tar = tarfile.open(file0, "w:bz2")
167 for name in snapshot_files[kernel]:
169 if stage == "stage1" and re.match(r"^lib/(lib)?std.*", name):
171 fn_glob = os.path.join(triple, dir, name)
172 matches = glob.glob(fn_glob)
174 raise Exception("Not found file with name like " + fn_glob)
175 if len(matches) == 1:
176 tar.add(matches[0], "rust-stage0/" + in_tar_name(matches[0]))
178 raise Exception("Found stale files: \n %s\n\
179 Please make a clean build." % "\n ".join(matches))
183 file1 = full_snapshot_name(date, rev, platform, h)
185 shutil.move(file0, file1)
187 if flag == "install":
188 # FIXME (#2664): this is an ugly quick hack; pls make it better
190 comps = path.split("-")
191 parts = { 'year': comps[2], \
197 'sha': comps[8].split(".")[0] }
199 shutil.move(path, "dl/" + path)
200 shutil.move('src/snapshots.txt', 'src/snapshots-old.txt')
202 newf = open('src/snapshots.txt', 'w')
203 newf.write("T %(year)s-%(month)s-%(date)s %(check)s\n" % parts)
204 newf.write(" %(plat)s-%(arch)s %(sha)s\n\n" % parts)
206 oldf = open('src/snapshots-old.txt', 'r')
213 os.remove('src/snapshots-old.txt')