/src/etc/get-snapshot.py
Python | 81 lines | 78 code | 2 blank | 1 comment | 4 complexity | a60edf7ef8d764ce98972cf80d84d59b MD5 | raw file
1#!/usr/bin/env python 2 3import os, tarfile, re, shutil, sys 4from snapshot import * 5 6def unpack_snapshot(triple, dl_path): 7 print("opening snapshot " + dl_path) 8 tar = tarfile.open(dl_path) 9 kernel = get_kernel(triple) 10 for p in tar.getnames(): 11 name = p.replace("rust-stage0/", "", 1); 12 stagep = os.path.join(triple, "stage0") 13 fp = os.path.join(stagep, name) 14 print("extracting " + p) 15 tar.extract(p, download_unpack_base) 16 tp = os.path.join(download_unpack_base, p) 17 shutil.move(tp, fp) 18 tar.close() 19 shutil.rmtree(download_unpack_base) 20 21def determine_curr_snapshot(triple): 22 i = 0 23 platform = get_platform(triple) 24 25 found_file = False 26 found_snap = False 27 hsh = None 28 date = None 29 rev = None 30 31 f = open(snapshotfile) 32 for line in f.readlines(): 33 i += 1 34 parsed = parse_line(i, line) 35 if (not parsed): continue 36 37 if found_snap and parsed["type"] == "file": 38 if parsed["platform"] == platform: 39 hsh = parsed["hash"] 40 found_file = True 41 break; 42 elif parsed["type"] == "snapshot": 43 date = parsed["date"] 44 rev = parsed["rev"] 45 found_snap = True 46 47 if not found_snap: 48 raise Exception("no snapshot entries in file") 49 50 if not found_file: 51 raise Exception("no snapshot file found for platform %s, rev %s" % 52 (platform, rev)) 53 54 return full_snapshot_name(date, rev, platform, hsh) 55 56# Main 57 58# this gets called with one or two arguments: 59# The first is the O/S triple. 60# The second is an optional path to the snapshot to use. 61 62triple = sys.argv[1] 63if len(sys.argv) == 3: 64 dl_path = sys.argv[2] 65else: 66 snap = determine_curr_snapshot(triple) 67 dl = os.path.join(download_dir_base, snap) 68 url = download_url_base + "/" + snap 69 print("determined most recent snapshot: " + snap) 70 71 if (not os.path.exists(dl)): 72 get_url_to_file(url, dl) 73 74 if (snap_filename_hash_part(snap) == hash_file(dl)): 75 print("got download with ok hash") 76 else: 77 raise Exception("bad hash on download") 78 79 dl_path = os.path.join(download_dir_base, snap) 80 81unpack_snapshot(triple, dl_path)