Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pytorch
GitHub Repository: pytorch/tutorials
Path: blob/main/tools/linter/adapters/run_from_link.py
1698 views
1
import argparse
2
import subprocess
3
import urllib.request
4
from pathlib import Path
5
6
7
REPO_ROOT = Path(__file__).absolute().parents[3]
8
9
10
def parse_args() -> argparse.Namespace:
11
parser = argparse.ArgumentParser(
12
description="Use a formatter in a different repository.",
13
)
14
parser.add_argument(
15
"--run-init",
16
action="store_true",
17
help="Run the initialization script specified by --init-name.",
18
)
19
parser.add_argument(
20
"--run-lint",
21
action="store_true",
22
help="Run the linting script specified by --lint-name.",
23
)
24
parser.add_argument(
25
"--init-name",
26
help="Name of the initialization script. This also serves as the filename.",
27
)
28
parser.add_argument(
29
"--init-link",
30
help="URL to download the initialization script from.",
31
)
32
parser.add_argument(
33
"--lint-name",
34
help="Name of the linting script. This also serves as the filename.",
35
)
36
parser.add_argument(
37
"--lint-link",
38
help="URL to download the linting script from.",
39
)
40
41
parser.add_argument("args_for_file", nargs=argparse.REMAINDER)
42
args = parser.parse_args()
43
# Skip the first -- if present
44
if args.args_for_file and args.args_for_file[0] == "--":
45
args.args_for_file = args.args_for_file[1:]
46
return args
47
48
49
def download_file(url: str, location: Path) -> bytes:
50
response = urllib.request.urlopen(url)
51
content = response.read()
52
location.write_bytes(content)
53
return content
54
55
56
def main() -> None:
57
args = parse_args()
58
59
location = REPO_ROOT / ".lintbin" / "from_link" / "adapters"
60
location.mkdir(parents=True, exist_ok=True)
61
62
if args.lint_link:
63
download_file(args.lint_link, location / args.lint_name)
64
65
if args.init_link:
66
download_file(args.init_link, location / args.init_name)
67
68
if args.run_init:
69
# Save the content to a file named after the name argument
70
subprocess_args = ["python3", location / args.init_name] + args.args_for_file
71
subprocess.run(subprocess_args, check=True)
72
if args.run_lint:
73
subprocess_args = ["python3", location / args.lint_name] + args.args_for_file
74
subprocess.run(
75
subprocess_args,
76
check=True,
77
)
78
79
80
if __name__ == "__main__":
81
main()
82
83