123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/usr/bin/env python3
- import argparse
- import hashlib
- import io
- import json
- import os.path
- import secrets
- import subprocess
- import sys
- import tarfile
- import urllib.request
- VOLTA = "1.0.8"
- VOLTA_DIR = os.path.expanduser("~/.volta")
- VOLTA_BIN = os.path.join(VOLTA_DIR, "bin")
- URL = {
- "darwin": f"https://github.com/volta-cli/volta/releases/download/v{VOLTA}/volta-{VOLTA}-macos.tar.gz",
- "linux": f"https://github.com/volta-cli/volta/releases/download/v{VOLTA}/volta-{VOLTA}-linux-openssl-1.1.tar.gz",
- }
- SHA256 = {
- "darwin": "b07461399f934b43bb34dfb0541eaf398537ccddcd2955166567459de52159dd",
- "linux": "981b61a35e0070b0a998361cdbc9040c99b7fb64b6fee0cc2de52243a1851412",
- }
- def _volta_bin(exe: str) -> str:
- return os.path.join(VOLTA_BIN, exe)
- def _command_vars() -> int:
- with open("package.json") as f:
- package_json = json.load(f)
- node = package_json["volta"]["node"]
- yarn = package_json["volta"]["yarn"]
- cache_key = f"{sys.platform}-volta-{VOLTA}-node-{node}-yarn-{yarn}"
- print(f"::set-output name=cache-key::{cache_key}")
- print(f"::set-output name=volta-dir::{VOLTA_DIR}")
- print(f"adding {VOLTA_BIN} to path")
- with open(os.environ["GITHUB_PATH"], "a+") as f:
- f.write(f"{VOLTA_BIN}\n")
- print(f"adding VOLTA_HOME={VOLTA_DIR} to env")
- with open(os.environ["GITHUB_ENV"], "a+") as f:
- f.write(f"VOLTA_HOME={VOLTA_DIR}\n")
- return 0
- def _command_install() -> int:
- print("downloading...")
- req = urllib.request.urlopen(URL[sys.platform], timeout=30)
- bio = io.BytesIO(req.read())
- checksum = hashlib.sha256(bio.getvalue()).hexdigest()
- if not secrets.compare_digest(checksum, SHA256[sys.platform]):
- print(f"volta checksum mismatch {checksum} {SHA256[sys.platform]}")
- return 1
- print("extracting...")
- with tarfile.open(fileobj=bio) as tarf:
- tarf.extractall(VOLTA_DIR)
- print("setting up volta...")
- if subprocess.call((os.path.join(VOLTA_DIR, "volta"), "setup")):
- return 1
- print("setting up node...")
- if subprocess.call((_volta_bin("node"), "--version")):
- return 1
- print("setting up yarn...")
- if subprocess.call((_volta_bin("yarn"), "--version")):
- return 1
- return 0
- def _command_yarn_cache_dir() -> int:
- out = subprocess.check_output((_volta_bin("yarn"), "cache", "dir"))
- cache_dir = out.decode().strip()
- print(f"::set-output name=cache-dir::{cache_dir}")
- return 0
- def main() -> int:
- parser = argparse.ArgumentParser()
- parser.add_argument("command", choices=("vars", "install", "yarn-cache-dir"))
- args = parser.parse_args()
- return {
- "vars": _command_vars,
- "install": _command_install,
- "yarn-cache-dir": _command_yarn_cache_dir,
- }[args.command]()
- if __name__ == "__main__":
- raise SystemExit(main())
|