setup-volta 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. import argparse
  3. import hashlib
  4. import io
  5. import json
  6. import os.path
  7. import secrets
  8. import subprocess
  9. import sys
  10. import tarfile
  11. import urllib.request
  12. VOLTA = "1.0.8"
  13. VOLTA_DIR = os.path.expanduser("~/.volta")
  14. VOLTA_BIN = os.path.join(VOLTA_DIR, "bin")
  15. URL = {
  16. "darwin": f"https://github.com/volta-cli/volta/releases/download/v{VOLTA}/volta-{VOLTA}-macos.tar.gz",
  17. "linux": f"https://github.com/volta-cli/volta/releases/download/v{VOLTA}/volta-{VOLTA}-linux-openssl-1.1.tar.gz",
  18. }
  19. SHA256 = {
  20. "darwin": "b07461399f934b43bb34dfb0541eaf398537ccddcd2955166567459de52159dd",
  21. "linux": "981b61a35e0070b0a998361cdbc9040c99b7fb64b6fee0cc2de52243a1851412",
  22. }
  23. def _volta_bin(exe: str) -> str:
  24. return os.path.join(VOLTA_BIN, exe)
  25. def _command_vars() -> int:
  26. with open("package.json") as f:
  27. package_json = json.load(f)
  28. node = package_json["volta"]["node"]
  29. yarn = package_json["volta"]["yarn"]
  30. cache_key = f"{sys.platform}-volta-{VOLTA}-node-{node}-yarn-{yarn}"
  31. print(f"::set-output name=cache-key::{cache_key}")
  32. print(f"::set-output name=volta-dir::{VOLTA_DIR}")
  33. print(f"adding {VOLTA_BIN} to path")
  34. with open(os.environ["GITHUB_PATH"], "a+") as f:
  35. f.write(f"{VOLTA_BIN}\n")
  36. print(f"adding VOLTA_HOME={VOLTA_DIR} to env")
  37. with open(os.environ["GITHUB_ENV"], "a+") as f:
  38. f.write(f"VOLTA_HOME={VOLTA_DIR}\n")
  39. return 0
  40. def _command_install() -> int:
  41. print("downloading...")
  42. req = urllib.request.urlopen(URL[sys.platform], timeout=30)
  43. bio = io.BytesIO(req.read())
  44. checksum = hashlib.sha256(bio.getvalue()).hexdigest()
  45. if not secrets.compare_digest(checksum, SHA256[sys.platform]):
  46. print(f"volta checksum mismatch {checksum} {SHA256[sys.platform]}")
  47. return 1
  48. print("extracting...")
  49. with tarfile.open(fileobj=bio) as tarf:
  50. tarf.extractall(VOLTA_DIR)
  51. print("setting up volta...")
  52. if subprocess.call((os.path.join(VOLTA_DIR, "volta"), "setup")):
  53. return 1
  54. print("setting up node...")
  55. if subprocess.call((_volta_bin("node"), "--version")):
  56. return 1
  57. print("setting up yarn...")
  58. if subprocess.call((_volta_bin("yarn"), "--version")):
  59. return 1
  60. return 0
  61. def _command_yarn_cache_dir() -> int:
  62. out = subprocess.check_output((_volta_bin("yarn"), "cache", "dir"))
  63. cache_dir = out.decode().strip()
  64. print(f"::set-output name=cache-dir::{cache_dir}")
  65. return 0
  66. def main() -> int:
  67. parser = argparse.ArgumentParser()
  68. parser.add_argument("command", choices=("vars", "install", "yarn-cache-dir"))
  69. args = parser.parse_args()
  70. return {
  71. "vars": _command_vars,
  72. "install": _command_install,
  73. "yarn-cache-dir": _command_yarn_cache_dir,
  74. }[args.command]()
  75. if __name__ == "__main__":
  76. raise SystemExit(main())