|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Downloads domain pytorch and library packages from channel |
| 3 | +# And backs them up to S3 |
| 4 | +# Do not use unless you know what you are doing |
| 5 | +# Usage: python backup_conda.py --version 1.6.0 |
| 6 | + |
| 7 | +import boto3 |
| 8 | +from typing import List, Optional |
| 9 | +import conda.api |
| 10 | +import urllib |
| 11 | +import os |
| 12 | +import hashlib |
| 13 | +import argparse |
| 14 | + |
| 15 | +S3 = boto3.resource('s3') |
| 16 | +BUCKET = S3.Bucket('pytorch-backup') |
| 17 | +_known_subdirs = ["linux-64", "osx-64", "osx-arm64", "win-64"] |
| 18 | + |
| 19 | + |
| 20 | +def compute_md5(path:str) -> str: |
| 21 | + with open(path, "rb") as f: |
| 22 | + return hashlib.md5(f.read()).hexdigest() |
| 23 | + |
| 24 | + |
| 25 | +def download_conda_package(package:str, version:Optional[str] = None, |
| 26 | + depends:Optional[str] = None, channel:Optional[str] = None) -> List[str]: |
| 27 | + packages = conda.api.SubdirData.query_all(package, |
| 28 | + channels = [channel] if channel is not None else None, |
| 29 | + subdirs = _known_subdirs) |
| 30 | + rc = [] |
| 31 | + |
| 32 | + for pkg in packages: |
| 33 | + if version is not None and pkg.version != version: |
| 34 | + continue |
| 35 | + if depends is not None and depends not in pkg.depends: |
| 36 | + continue |
| 37 | + |
| 38 | + print(f"Downloading {pkg.url}...") |
| 39 | + os.makedirs(pkg.subdir, exist_ok = True) |
| 40 | + fname = f"{pkg.subdir}/{pkg.fn}" |
| 41 | + if not os.path.exists(fname): |
| 42 | + with open(fname, "wb") as f, urllib.request.urlopen(pkg.url) as url: |
| 43 | + f.write(url.read()) |
| 44 | + if compute_md5(fname) != pkg.md5: |
| 45 | + print(f"md5 of {fname} is {compute_md5(fname)} does not match {pkg.md5}") |
| 46 | + continue |
| 47 | + rc.append(fname) |
| 48 | + |
| 49 | + return rc |
| 50 | + |
| 51 | +def upload_to_s3(prefix: str, fnames: List[str]) -> None: |
| 52 | + for fname in fnames: |
| 53 | + BUCKET.upload_file(fname, f"{prefix}/{fname}") |
| 54 | + print(fname) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + parser = argparse.ArgumentParser() |
| 60 | + parser.add_argument( |
| 61 | + "--version", |
| 62 | + help="PyTorch Version to backup", |
| 63 | + type=str, |
| 64 | + required = True |
| 65 | + ) |
| 66 | + options = parser.parse_args() |
| 67 | + rc = download_conda_package("pytorch", channel = "pytorch", version = options.version) |
| 68 | + upload_to_s3(f"v{options.version}/conda", rc) |
| 69 | + |
| 70 | + for libname in ["torchvision", "torchaudio", "torchtext"]: |
| 71 | + print(f"processing {libname}") |
| 72 | + rc = download_conda_package(libname, channel = "pytorch", depends = f"pytorch {options.version}") |
| 73 | + upload_to_s3(f"v{options.version}/conda", rc) |
0 commit comments