1515import sys
1616import tempfile
1717import textwrap
18+ from typing import Optional
1819
1920
2021def check_state () -> None :
@@ -26,14 +27,27 @@ def check_state() -> None:
2627 sys .exit ('error: Output of "git status -s mypy/typeshed" must be empty' )
2728
2829
29- def update_typeshed (typeshed_dir : str ) -> None :
30+ def update_typeshed (typeshed_dir : str , commit : Optional [str ]) -> str :
31+ """Update contents of local typeshed copy.
32+
33+ Return the normalized typeshed commit hash.
34+ """
3035 assert os .path .isdir (os .path .join (typeshed_dir , 'stdlib' ))
3136 assert os .path .isdir (os .path .join (typeshed_dir , 'stubs' ))
37+ if commit :
38+ subprocess .run (['git' , 'checkout' , commit ], check = True , cwd = typeshed_dir )
39+ commit = git_head_commit (typeshed_dir )
3240 stub_dir = os .path .join ('mypy' , 'typeshed' , 'stdlib' )
3341 # Remove existing stubs.
3442 shutil .rmtree (stub_dir )
3543 # Copy new stdlib stubs.
3644 shutil .copytree (os .path .join (typeshed_dir , 'stdlib' ), stub_dir )
45+ return commit
46+
47+
48+ def git_head_commit (repo : str ) -> str :
49+ commit = subprocess .check_output (['git' , 'rev-parse' , 'HEAD' ], cwd = repo ).decode ('ascii' )
50+ return commit .strip ()
3751
3852
3953def main () -> None :
@@ -53,12 +67,17 @@ def main() -> None:
5367 if answer .lower () != 'y' :
5468 sys .exit ('Aborting' )
5569
56- # TODO: Clone typeshed repo if no directory given
57- assert args .typeshed_dir
58- update_typeshed (args .typeshed_dir )
70+ if not args .typeshed_dir :
71+ # Clone typeshed repo if no directory given.
72+ with tempfile .TemporaryDirectory () as tempdir :
73+ print ('Cloning typeshed in {}...' .format (tempdir ))
74+ subprocess .run (['git' , 'clone' , 'https://github.com/python/typeshed.git' ],
75+ check = True , cwd = tempdir )
76+ repo = os .path .join (tempdir , 'typeshed' )
77+ commit = update_typeshed (repo , args .commit )
78+ else :
79+ commit = update_typeshed (args .typeshed_dir , args .commit )
5980
60- commit = args .commit
61- # TODO: If cloning typeshd repo, use master commit
6281 assert commit
6382
6483 # Create a commit
0 commit comments