update-gyp.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. import tarfile
  8. import tempfile
  9. import urllib.request
  10. BASE_URL = "https://github.com/nodejs/gyp-next/archive/"
  11. CHECKOUT_PATH = os.path.dirname(os.path.realpath(__file__))
  12. CHECKOUT_GYP_PATH = os.path.join(CHECKOUT_PATH, 'gyp')
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument("tag", help="gyp tag to update to")
  15. args = parser.parse_args()
  16. tar_url = BASE_URL + args.tag + ".tar.gz"
  17. changed_files = subprocess.check_output(["git", "diff", "--name-only"]).strip()
  18. if changed_files:
  19. raise Exception("Can't update gyp while you have uncommitted changes in node-gyp")
  20. with tempfile.TemporaryDirectory() as tmp_dir:
  21. tar_file = os.path.join(tmp_dir, 'gyp.tar.gz')
  22. unzip_target = os.path.join(tmp_dir, 'gyp')
  23. with open(tar_file, 'wb') as f:
  24. print("Downloading gyp-next@" + args.tag + " into temporary directory...")
  25. print("From: " + tar_url)
  26. with urllib.request.urlopen(tar_url) as in_file:
  27. f.write(in_file.read())
  28. print("Unzipping...")
  29. with tarfile.open(tar_file, "r:gz") as tar_ref:
  30. tar_ref.extractall(unzip_target)
  31. print("Moving to current checkout (" + CHECKOUT_PATH + ")...")
  32. if os.path.exists(CHECKOUT_GYP_PATH):
  33. shutil.rmtree(CHECKOUT_GYP_PATH)
  34. shutil.move(os.path.join(unzip_target, os.listdir(unzip_target)[0]), CHECKOUT_GYP_PATH)
  35. subprocess.check_output(["git", "add", "gyp"], cwd=CHECKOUT_PATH)
  36. subprocess.check_output(["git", "commit", "-m", "gyp: update gyp to " + args.tag])