]> git.lizzy.rs Git - rust.git/blob - src/etc/get-snapshot.py
make get-snapshot externally usable
[rust.git] / src / etc / get-snapshot.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2011-2014 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12
13 import os, tarfile, re, shutil, sys
14 from snapshot import *
15
16 def unpack_snapshot(triple, dl_path):
17     print("opening snapshot " + dl_path)
18     tar = tarfile.open(dl_path)
19     kernel = get_kernel(triple)
20
21     stagep = os.path.join(triple, "stage0")
22
23     # Remove files from prior unpackings, since snapshot rustc may not
24     # be able to disambiguate between multiple candidate libraries.
25     # (Leave dirs in place since extracting step still needs them.)
26     for root, _, files in os.walk(stagep):
27         for f in files:
28             print("removing " + os.path.join(root, f))
29             os.unlink(os.path.join(root, f))
30
31     for p in tar.getnames():
32         name = p.replace("rust-stage0/", "", 1);
33
34         fp = os.path.join(stagep, name)
35         print("extracting " + p)
36         tar.extract(p, download_unpack_base)
37         tp = os.path.join(download_unpack_base, p)
38         if os.path.isdir(tp) and os.path.exists(fp):
39                 continue
40         shutil.move(tp, fp)
41     tar.close()
42     shutil.rmtree(download_unpack_base)
43
44
45 # Main
46
47 # this gets called with one or two arguments:
48 # The first is the O/S triple.
49 # The second is an optional path to the snapshot to use.
50
51 def main(argv):
52     triple = argv[1]
53     if len(argv) == 3:
54         dl_path = argv[2]
55     else:
56         snap = determine_curr_snapshot(triple)
57         dl = os.path.join(download_dir_base, snap)
58         url = download_url_base + "/" + snap
59         print("determined most recent snapshot: " + snap)
60
61         if (not os.path.exists(dl)):
62             get_url_to_file(url, dl)
63
64         if (snap_filename_hash_part(snap) == hash_file(dl)):
65             print("got download with ok hash")
66         else:
67             raise Exception("bad hash on download")
68
69         dl_path = os.path.join(download_dir_base, snap)
70
71     unpack_snapshot(triple, dl_path)
72
73 if __name__ == '__main__':
74     main(sys.argv)