github.com/pachyderm/pachyderm@v1.13.4/etc/fetch_release_pachctl.py (about) 1 #!/usr/bin/env python3 2 3 import os 4 import sys 5 import stat 6 import shutil 7 import tarfile 8 import zipfile 9 import tempfile 10 import urllib.request 11 12 def fetch_darwin(version, bin_path): 13 archive_name = "pachctl_{}_darwin_amd64".format(version) 14 archive_url = "https://github.com/pachyderm/pachyderm/releases/download/v{}/{}.zip".format(version, archive_name) 15 16 with tempfile.TemporaryFile() as temp_file: 17 with urllib.request.urlopen(archive_url) as response: 18 shutil.copyfileobj(response, temp_file) 19 20 with zipfile.ZipFile(file=temp_file) as zip_dir: 21 with zip_dir.open("{}/pachctl".format(archive_name), "r") as zip_file: 22 with open(bin_path, "wb") as bin_file: 23 shutil.copyfileobj(zip_file, bin_file) 24 25 def fetch_linux(version, bin_path): 26 archive_name = "pachctl_{}_linux_amd64".format(version) 27 archive_url = "https://github.com/pachyderm/pachyderm/releases/download/v{}/{}.tar.gz".format(version, archive_name) 28 29 with tempfile.TemporaryFile() as temp_file: 30 with urllib.request.urlopen(archive_url) as response: 31 shutil.copyfileobj(response, temp_file) 32 33 temp_file.seek(0) 34 35 with tarfile.open(fileobj=temp_file, mode="r:gz") as tar_file: 36 with tar_file.extractfile("pachctl_{}_linux_amd64/pachctl".format(version)) as f: 37 with open(bin_path, "wb") as bin_file: 38 shutil.copyfileobj(f, bin_file) 39 40 def main(): 41 version = sys.argv[1] 42 # TODO(msteffen) replace this with 'go env GOPATH', in case go's default 43 # changes 44 go_path = os.getenv("GOPATH", os.path.join(os.environ["HOME"], "go")) 45 go_bin = os.getenv("GOBIN", os.path.join(go_path, "bin")) 46 pachctl_path = os.path.join(go_bin, "pachctl") 47 48 if sys.platform == "darwin": 49 fetch_darwin(version, pachctl_path) 50 else: 51 fetch_linux(version, pachctl_path) 52 53 st = os.stat(pachctl_path) 54 os.chmod(pachctl_path, st.st_mode | stat.S_IEXEC) 55 56 if __name__ == "__main__": 57 main()