/src/etc/get-snapshot.py

http://github.com/jruderman/rust · Python · 81 lines · 60 code · 16 blank · 5 comment · 14 complexity · a60edf7ef8d764ce98972cf80d84d59b MD5 · raw file

  1. #!/usr/bin/env python
  2. import os, tarfile, re, shutil, sys
  3. from snapshot import *
  4. def unpack_snapshot(triple, dl_path):
  5. print("opening snapshot " + dl_path)
  6. tar = tarfile.open(dl_path)
  7. kernel = get_kernel(triple)
  8. for p in tar.getnames():
  9. name = p.replace("rust-stage0/", "", 1);
  10. stagep = os.path.join(triple, "stage0")
  11. fp = os.path.join(stagep, name)
  12. print("extracting " + p)
  13. tar.extract(p, download_unpack_base)
  14. tp = os.path.join(download_unpack_base, p)
  15. shutil.move(tp, fp)
  16. tar.close()
  17. shutil.rmtree(download_unpack_base)
  18. def determine_curr_snapshot(triple):
  19. i = 0
  20. platform = get_platform(triple)
  21. found_file = False
  22. found_snap = False
  23. hsh = None
  24. date = None
  25. rev = None
  26. f = open(snapshotfile)
  27. for line in f.readlines():
  28. i += 1
  29. parsed = parse_line(i, line)
  30. if (not parsed): continue
  31. if found_snap and parsed["type"] == "file":
  32. if parsed["platform"] == platform:
  33. hsh = parsed["hash"]
  34. found_file = True
  35. break;
  36. elif parsed["type"] == "snapshot":
  37. date = parsed["date"]
  38. rev = parsed["rev"]
  39. found_snap = True
  40. if not found_snap:
  41. raise Exception("no snapshot entries in file")
  42. if not found_file:
  43. raise Exception("no snapshot file found for platform %s, rev %s" %
  44. (platform, rev))
  45. return full_snapshot_name(date, rev, platform, hsh)
  46. # Main
  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. triple = sys.argv[1]
  51. if len(sys.argv) == 3:
  52. dl_path = sys.argv[2]
  53. else:
  54. snap = determine_curr_snapshot(triple)
  55. dl = os.path.join(download_dir_base, snap)
  56. url = download_url_base + "/" + snap
  57. print("determined most recent snapshot: " + snap)
  58. if (not os.path.exists(dl)):
  59. get_url_to_file(url, dl)
  60. if (snap_filename_hash_part(snap) == hash_file(dl)):
  61. print("got download with ok hash")
  62. else:
  63. raise Exception("bad hash on download")
  64. dl_path = os.path.join(download_dir_base, snap)
  65. unpack_snapshot(triple, dl_path)