|
1 | 1 | """Generate the upstream stubs.""" |
| 2 | +import contextlib |
2 | 3 | import re |
3 | 4 | import shutil |
4 | 5 | import subprocess |
|
7 | 8 | import zipfile |
8 | 9 | from pathlib import Path |
9 | 10 | from tempfile import mkdtemp |
10 | | -from typing import List, Set, Tuple |
| 11 | +from typing import List, Set, Tuple, Generator, ContextManager |
11 | 12 |
|
| 13 | +import click |
12 | 14 | from libcst import MetadataWrapper, parse_module |
13 | 15 | from mypy.stubgen import Options, generate_stubs |
14 | 16 |
|
@@ -97,23 +99,37 @@ def add_uic_stubs(temp_folder: Path) -> None: |
97 | 99 | shutil.copytree(Path(gen_stub_temp_folder) / "PyQt6" / "uic", uic_path) |
98 | 100 |
|
99 | 101 |
|
100 | | -def generate_stubs_from_upstream(): |
101 | | - files = [] |
102 | | - for arg in sys.argv[1:]: |
103 | | - print(f"Adding file to process list: {arg}") |
104 | | - files.append(arg) |
| 102 | +def _yield_temp_dir(ctx, param, value) -> ContextManager[Path]: |
| 103 | + if value: |
| 104 | + return contextlib.nullcontext(Path(value)) |
| 105 | + else: |
| 106 | + return tempfile.TemporaryDirectory() |
| 107 | + |
| 108 | + |
| 109 | +@click.command() |
| 110 | +@click.option( |
| 111 | + "--tmpdir", |
| 112 | + "-t", |
| 113 | + "tmpdir_context", |
| 114 | + callback=_yield_temp_dir, |
| 115 | + help="Use as temporary directory, for debugging", |
| 116 | +) |
| 117 | +@click.argument('file', nargs=-1) |
| 118 | +def generate_stubs_from_upstream(tmpdir_context: ContextManager[Path], file: tuple[str, ...]): |
| 119 | + files = list(file) |
| 120 | + for file in files: |
| 121 | + print(f"Adding file to process list: {file}") |
105 | 122 |
|
106 | 123 | # Create PyQt6-stubs folder if necessary |
107 | 124 | SRC_DIR.mkdir(exist_ok=True) |
108 | 125 |
|
109 | 126 | # Update pip just in case |
110 | | - subprocess.check_call( |
111 | | - [sys.executable, "-m", "pip", "install", "--upgrade", "pip"] |
112 | | - ) |
| 127 | + subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"]) |
113 | 128 |
|
114 | 129 | # Download required packages |
115 | | - with tempfile.TemporaryDirectory() as temp_dwld_folder: |
116 | | - download_stubs(Path(temp_dwld_folder), files) |
| 130 | + with tmpdir_context as temp_download_folder: |
| 131 | + print(f"Using {temp_download_folder} as base temporary folder") |
| 132 | + download_stubs(Path(temp_download_folder), files) |
117 | 133 |
|
118 | 134 | # Now apply the fixes: |
119 | 135 | for file in SRC_DIR.glob("*.pyi"): |
@@ -149,13 +165,9 @@ def generate_stubs_from_upstream(): |
149 | 165 |
|
150 | 166 | # Lint the files with iSort and Black |
151 | 167 | print("Fixing files with iSort") |
152 | | - subprocess.check_call( |
153 | | - ["isort", "--profile", "black", "-l 10000", str(SRC_DIR)] |
154 | | - ) |
| 168 | + subprocess.check_call(["isort", "--profile", "black", "-l 10000", str(SRC_DIR)]) |
155 | 169 | print("Fixing files with Black") |
156 | | - subprocess.check_call( |
157 | | - ["black", "--safe", "--quiet", "-l 10000", str(SRC_DIR)] |
158 | | - ) |
| 170 | + subprocess.check_call(["black", "--safe", "--quiet", "-l 10000", str(SRC_DIR)]) |
159 | 171 |
|
160 | 172 |
|
161 | 173 | if __name__ == "__main__": |
|
0 commit comments