Skip to content

Commit 1ac6bb9

Browse files
committed
Add click and use for arguments
1 parent 1f4ecaa commit 1ac6bb9

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

generate_upstream.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Generate the upstream stubs."""
2+
import contextlib
23
import re
34
import shutil
45
import subprocess
@@ -7,8 +8,9 @@
78
import zipfile
89
from pathlib import Path
910
from tempfile import mkdtemp
10-
from typing import List, Set, Tuple
11+
from typing import List, Set, Tuple, Generator, ContextManager
1112

13+
import click
1214
from libcst import MetadataWrapper, parse_module
1315
from mypy.stubgen import Options, generate_stubs
1416

@@ -97,23 +99,37 @@ def add_uic_stubs(temp_folder: Path) -> None:
9799
shutil.copytree(Path(gen_stub_temp_folder) / "PyQt6" / "uic", uic_path)
98100

99101

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}")
105122

106123
# Create PyQt6-stubs folder if necessary
107124
SRC_DIR.mkdir(exist_ok=True)
108125

109126
# 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"])
113128

114129
# 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)
117133

118134
# Now apply the fixes:
119135
for file in SRC_DIR.glob("*.pyi"):
@@ -149,13 +165,9 @@ def generate_stubs_from_upstream():
149165

150166
# Lint the files with iSort and Black
151167
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)])
155169
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)])
159171

160172

161173
if __name__ == "__main__":

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pytest==7.1.1
77
setuptools==60.5.0
88
black==22.12.0
99
wheel==0.38.4
10+
click==8.1.3

0 commit comments

Comments
 (0)