|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from typing import Any, Dict |
| 5 | + |
| 6 | +VERSIONS_LUT: Dict[str, Dict[str, Any]] = { |
| 7 | + "1.4.0": dict(torchvision="0.5.0", torchtext="0.5"), |
| 8 | + "1.5.0": dict(torchvision="0.6.0", torchtext="0.6"), |
| 9 | + "1.5.1": dict(torchvision="0.6.1", torchtext="0.6"), |
| 10 | + "1.6.0": dict(torchvision="0.7.0", torchtext="0.7"), |
| 11 | + "1.7.0": dict(torchvision="0.8.1", torchtext="0.8"), |
| 12 | + "1.7.1": dict(torchvision="0.8.2", torchtext="0.8.1"), |
| 13 | + "1.8.0": dict(torchvision="0.9.0", torchtext="0.9"), |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +def find_latest(ver: str, versions_all: list) -> str: |
| 18 | + # drop all except semantic version |
| 19 | + ver = re.search(r'([\.\d]+)', ver).groups()[0] |
| 20 | + # find candidates, by starting version pattern |
| 21 | + options = [v for v in versions_all if v.startswith(ver)] |
| 22 | + assert options, f"missing {ver} among {versions_all}" |
| 23 | + # take the last one... |
| 24 | + return sorted(options)[-1] |
| 25 | + |
| 26 | + |
| 27 | +def main(path_req: str, torch_version: str = None) -> None: |
| 28 | + with open(path_req, "r") as fp: |
| 29 | + req = fp.read() |
| 30 | + |
| 31 | + if not torch_version: |
| 32 | + import torch |
| 33 | + torch_version = torch.__version__ |
| 34 | + assert torch_version, f"invalid/missing Torch: {torch_version}" |
| 35 | + |
| 36 | + torch_version = find_latest(torch_version, list(VERSIONS_LUT.keys())) |
| 37 | + dep_versions = VERSIONS_LUT[torch_version] |
| 38 | + dep_versions["torch"] = torch_version |
| 39 | + for lib in dep_versions: |
| 40 | + version = dep_versions[lib] |
| 41 | + replace = f"{lib}=={version}\n" |
| 42 | + req = re.sub(rf"{lib}[>=]*[\d\.]*{os.linesep}", replace, req) |
| 43 | + |
| 44 | + with open(path_req, "w") as fp: |
| 45 | + fp.write(req) |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + main(*sys.argv[1:]) |
0 commit comments