Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

update-gyp.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import shutil
  5. import subprocess
  6. import tarfile
  7. import tempfile
  8. import urllib.request
  9. BASE_URL = "https://github.com/nodejs/gyp-next/archive/"
  10. CHECKOUT_PATH = os.path.dirname(os.path.realpath(__file__))
  11. CHECKOUT_GYP_PATH = os.path.join(CHECKOUT_PATH, "gyp")
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument("tag", help="gyp tag to update to")
  14. args = parser.parse_args()
  15. tar_url = BASE_URL + args.tag + ".tar.gz"
  16. changed_files = subprocess.check_output(["git", "diff", "--name-only"]).strip()
  17. if changed_files:
  18. raise Exception("Can't update gyp while you have uncommitted changes in node-gyp")
  19. with tempfile.TemporaryDirectory() as tmp_dir:
  20. tar_file = os.path.join(tmp_dir, "gyp.tar.gz")
  21. unzip_target = os.path.join(tmp_dir, "gyp")
  22. with open(tar_file, "wb") as f:
  23. print("Downloading gyp-next@" + args.tag + " into temporary directory...")
  24. print("From: " + tar_url)
  25. with urllib.request.urlopen(tar_url) as in_file:
  26. f.write(in_file.read())
  27. print("Unzipping...")
  28. with tarfile.open(tar_file, "r:gz") as tar_ref:
  29. def is_within_directory(directory, target):
  30. abs_directory = os.path.abspath(directory)
  31. abs_target = os.path.abspath(target)
  32. prefix = os.path.commonprefix([abs_directory, abs_target])
  33. return prefix == abs_directory
  34. def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
  35. for member in tar.getmembers():
  36. member_path = os.path.join(path, member.name)
  37. if not is_within_directory(path, member_path):
  38. raise Exception("Attempted Path Traversal in Tar File")
  39. tar.extractall(path, members, numeric_owner=numeric_owner)
  40. safe_extract(tar_ref, unzip_target)
  41. print("Moving to current checkout (" + CHECKOUT_PATH + ")...")
  42. if os.path.exists(CHECKOUT_GYP_PATH):
  43. shutil.rmtree(CHECKOUT_GYP_PATH)
  44. shutil.move(
  45. os.path.join(unzip_target, os.listdir(unzip_target)[0]), CHECKOUT_GYP_PATH
  46. )
  47. subprocess.check_output(["git", "add", "gyp"], cwd=CHECKOUT_PATH)
  48. subprocess.check_output(["git", "commit", "-m", "feat(gyp): update gyp to " + args.tag])