Path: blob/main/tools/linter/adapters/run_from_link.py
1698 views
import argparse1import subprocess2import urllib.request3from pathlib import Path456REPO_ROOT = Path(__file__).absolute().parents[3]789def parse_args() -> argparse.Namespace:10parser = argparse.ArgumentParser(11description="Use a formatter in a different repository.",12)13parser.add_argument(14"--run-init",15action="store_true",16help="Run the initialization script specified by --init-name.",17)18parser.add_argument(19"--run-lint",20action="store_true",21help="Run the linting script specified by --lint-name.",22)23parser.add_argument(24"--init-name",25help="Name of the initialization script. This also serves as the filename.",26)27parser.add_argument(28"--init-link",29help="URL to download the initialization script from.",30)31parser.add_argument(32"--lint-name",33help="Name of the linting script. This also serves as the filename.",34)35parser.add_argument(36"--lint-link",37help="URL to download the linting script from.",38)3940parser.add_argument("args_for_file", nargs=argparse.REMAINDER)41args = parser.parse_args()42# Skip the first -- if present43if args.args_for_file and args.args_for_file[0] == "--":44args.args_for_file = args.args_for_file[1:]45return args464748def download_file(url: str, location: Path) -> bytes:49response = urllib.request.urlopen(url)50content = response.read()51location.write_bytes(content)52return content535455def main() -> None:56args = parse_args()5758location = REPO_ROOT / ".lintbin" / "from_link" / "adapters"59location.mkdir(parents=True, exist_ok=True)6061if args.lint_link:62download_file(args.lint_link, location / args.lint_name)6364if args.init_link:65download_file(args.init_link, location / args.init_name)6667if args.run_init:68# Save the content to a file named after the name argument69subprocess_args = ["python3", location / args.init_name] + args.args_for_file70subprocess.run(subprocess_args, check=True)71if args.run_lint:72subprocess_args = ["python3", location / args.lint_name] + args.args_for_file73subprocess.run(74subprocess_args,75check=True,76)777879if __name__ == "__main__":80main()818283