1212
1313import argparse
1414import asyncio
15- import glob
16- import os .path
1715import re
1816import subprocess
1917import sys
2018import urllib .parse
2119from http import HTTPStatus
2220from importlib .metadata import distribution
21+ from pathlib import Path
2322
2423import aiohttp
2524import termcolor
2625
27- PYRIGHT_CONFIG = "pyrightconfig.stricter.json"
26+ from ts_utils .paths import STDLIB_PATH , STUBS_PATH
27+
28+ PYRIGHT_CONFIG = Path ("pyrightconfig.stricter.json" )
2829
2930
3031def search_pip_freeze_output (project : str , output : str ) -> tuple [str , str ] | None :
@@ -52,22 +53,22 @@ def get_installed_package_info(project: str) -> tuple[str, str] | None:
5253 return search_pip_freeze_output (project , r .stdout )
5354
5455
55- def run_stubgen (package : str , output : str ) -> None :
56+ def run_stubgen (package : str , output : Path ) -> None :
5657 print (f"Running stubgen: stubgen -o { output } -p { package } " )
5758 subprocess .run (["stubgen" , "-o" , output , "-p" , package , "--export-less" ], check = True )
5859
5960
60- def run_stubdefaulter (stub_dir : str ) -> None :
61+ def run_stubdefaulter (stub_dir : Path ) -> None :
6162 print (f"Running stubdefaulter: stubdefaulter --packages { stub_dir } " )
6263 subprocess .run (["stubdefaulter" , "--packages" , stub_dir ], check = False )
6364
6465
65- def run_black (stub_dir : str ) -> None :
66+ def run_black (stub_dir : Path ) -> None :
6667 print (f"Running Black: black { stub_dir } " )
67- subprocess .run (["pre-commit" , "run" , "black" , "--files" , * glob . iglob ( f" { stub_dir } /**/ *.pyi" )], check = False )
68+ subprocess .run (["pre-commit" , "run" , "black" , "--files" , * stub_dir . rglob ( " *.pyi" )], check = False )
6869
6970
70- def run_ruff (stub_dir : str ) -> None :
71+ def run_ruff (stub_dir : Path ) -> None :
7172 print (f"Running Ruff: ruff check { stub_dir } --fix-only" )
7273 subprocess .run ([sys .executable , "-m" , "ruff" , "check" , stub_dir , "--fix-only" ], check = False )
7374
@@ -115,14 +116,14 @@ async def get_upstream_repo_url(project: str) -> str | None:
115116 return None
116117
117118
118- def create_metadata (project : str , stub_dir : str , version : str ) -> None :
119+ def create_metadata (project : str , stub_dir : Path , version : str ) -> None :
119120 """Create a METADATA.toml file."""
120121 match = re .match (r"[0-9]+.[0-9]+" , version )
121122 if match is None :
122123 sys .exit (f"Error: Cannot parse version number: { version } " )
123- filename = os . path . join ( stub_dir , "METADATA.toml" )
124+ filename = stub_dir / "METADATA.toml"
124125 version = match .group (0 )
125- if os . path . exists (filename ):
126+ if filename . exists ():
126127 return
127128 metadata = f'version = "{ version } .*"\n '
128129 upstream_repo_url = asyncio .run (get_upstream_repo_url (project ))
@@ -135,13 +136,12 @@ def create_metadata(project: str, stub_dir: str, version: str) -> None:
135136 else :
136137 metadata += f'upstream_repository = "{ upstream_repo_url } "\n '
137138 print (f"Writing { filename } " )
138- with open (filename , "w" , encoding = "UTF-8" ) as file :
139- file .write (metadata )
139+ filename .write_text (metadata , encoding = "UTF-8" )
140140
141141
142- def add_pyright_exclusion (stub_dir : str ) -> None :
142+ def add_pyright_exclusion (stub_dir : Path ) -> None :
143143 """Exclude stub_dir from strict pyright checks."""
144- with open (PYRIGHT_CONFIG , encoding = "UTF-8" ) as f :
144+ with PYRIGHT_CONFIG . open (encoding = "UTF-8" ) as f :
145145 lines = f .readlines ()
146146 i = 0
147147 while i < len (lines ) and not lines [i ].strip ().startswith ('"exclude": [' ):
@@ -167,7 +167,7 @@ def add_pyright_exclusion(stub_dir: str) -> None:
167167 third_party_excludes [- 1 ] = last_line + "\n "
168168
169169 # Must use forward slash in the .json file
170- line_to_add = f' "{ stub_dir } ",\n ' . replace ( " \\ " , "/" )
170+ line_to_add = f' "{ stub_dir . as_posix () } ",\n '
171171
172172 if line_to_add in third_party_excludes :
173173 print (f"{ PYRIGHT_CONFIG } already up-to-date" )
@@ -177,7 +177,7 @@ def add_pyright_exclusion(stub_dir: str) -> None:
177177 third_party_excludes .sort (key = str .lower )
178178
179179 print (f"Updating { PYRIGHT_CONFIG } " )
180- with open (PYRIGHT_CONFIG , "w" , encoding = "UTF-8" ) as f :
180+ with PYRIGHT_CONFIG . open ("w" , encoding = "UTF-8" ) as f :
181181 f .writelines (before_third_party_excludes )
182182 f .writelines (third_party_excludes )
183183 f .writelines (after_third_party_excludes )
@@ -194,7 +194,7 @@ def main() -> None:
194194 parser .add_argument ("--package" , help = "generate stubs for this Python package (default is autodetected)" )
195195 args = parser .parse_args ()
196196 project = args .project
197- package = args .package
197+ package : str = args .package
198198
199199 if not re .match (r"[a-zA-Z0-9-_.]+$" , project ):
200200 sys .exit (f"Invalid character in project name: { project !r} " )
@@ -214,7 +214,7 @@ def main() -> None:
214214 print (f'Using detected package "{ package } " for project "{ project } "' , file = sys .stderr )
215215 print ("Suggestion: Try again with --package argument if that's not what you wanted" , file = sys .stderr )
216216
217- if not os . path . isdir ( "stubs" ) or not os . path . isdir ( "stdlib" ):
217+ if not STUBS_PATH . is_dir ( ) or not STDLIB_PATH . is_dir ( ):
218218 sys .exit ("Error: Current working directory must be the root of typeshed repository" )
219219
220220 # Get normalized project name and version of installed package.
@@ -226,9 +226,9 @@ def main() -> None:
226226 sys .exit (1 )
227227 project , version = info
228228
229- stub_dir = os . path . join ( "stubs" , project )
230- package_dir = os . path . join ( stub_dir , package )
231- if os . path . exists (package_dir ):
229+ stub_dir = STUBS_PATH / project
230+ package_dir = stub_dir / package
231+ if package_dir . exists ():
232232 sys .exit (f"Error: { package_dir } already exists (delete it first)" )
233233
234234 run_stubgen (package , stub_dir )
0 commit comments