|
| 1 | +import argparse |
| 2 | +from pathlib import Path |
| 3 | +import subprocess |
| 4 | +import urllib.request |
| 5 | + |
| 6 | +REPO_ROOT = Path(__file__).absolute().parents[3] |
| 7 | + |
| 8 | +def parse_args() -> argparse.Namespace: |
| 9 | + parser = argparse.ArgumentParser( |
| 10 | + description="Use a formatter in a different repository.", |
| 11 | + ) |
| 12 | + parser.add_argument( |
| 13 | + "--run-init", |
| 14 | + action="store_true", |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "--init-name", |
| 18 | + ) |
| 19 | + parser.add_argument( |
| 20 | + "--init-link", |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + "--lint-name", |
| 24 | + required=True, |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "--lint-link", |
| 28 | + required=True, |
| 29 | + ) |
| 30 | + parser.add_argument('args_for_file', nargs=argparse.REMAINDER) |
| 31 | + args = parser.parse_args() |
| 32 | + # Skip the first -- if present |
| 33 | + if args.args_for_file and args.args_for_file[0] == '--': |
| 34 | + args.args_for_file = args.args_for_file[1:] |
| 35 | + return args |
| 36 | + |
| 37 | + |
| 38 | +def download_file(url: str, location: Path) -> bytes: |
| 39 | + response = urllib.request.urlopen(url) |
| 40 | + content = response.read() |
| 41 | + location.write_bytes(content) |
| 42 | + return content |
| 43 | + |
| 44 | + |
| 45 | +def main() -> None: |
| 46 | + args = parse_args() |
| 47 | + |
| 48 | + location = REPO_ROOT / ".lintbin" / "from_link" / "adapters" |
| 49 | + |
| 50 | + if args.run_init: |
| 51 | + location.mkdir(parents=True, exist_ok=True) |
| 52 | + # Save the content to a file named after the name argument |
| 53 | + download_file(args.lint_link, location / args.lint_name) |
| 54 | + download_file(args.init_link, location / args.init_name) |
| 55 | + subprocess.run(["python3", location / args.init_name] + args.args_for_file, check=True) |
| 56 | + else: |
| 57 | + subprocess.run( |
| 58 | + ["python3", location / args.lint_name] + args.args_for_file, |
| 59 | + check=True, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments