1212EXT_ROOT = pathlib .Path (__file__ ).parent
1313
1414
15+ def delete_dir (path : pathlib .Path , ignore_errors = None ):
16+ attempt = 0
17+ known = []
18+ while attempt < 5 :
19+ try :
20+ shutil .rmtree (os .fspath (path ), ignore_errors = ignore_errors )
21+ return
22+ except PermissionError as pe :
23+ if os .fspath (pe .filename ) in known :
24+ break
25+ print (f"Changing permissions on { pe .filename } " )
26+ os .chmod (pe .filename , 0o666 )
27+
28+ shutil .rmtree (os .fspath (path ))
29+
1530@nox .session ()
1631def install_python_libs (session : nox .Session ):
1732 requirements = [
@@ -48,6 +63,45 @@ def install_python_libs(session: nox.Session):
4863 if pathlib .Path ("./python_files/lib/temp" ).exists ():
4964 shutil .rmtree ("./python_files/lib/temp" )
5065
66+ @nox .session ()
67+ def azure_pet_checkout (session : nox .Session ):
68+ branch = os .getenv ("PYTHON_ENV_TOOLS_REF" , "main" )
69+
70+ # dest dir should be <vscode-python repo root>/python-env-tools
71+ dest_dir = (pathlib .Path (os .getenv ("PYTHON_ENV_TOOLS_DEST" )) / "python-env-tools" ).resolve ()
72+
73+ # temp dir should be <agent temp dir>
74+ temp_dir = (pathlib .Path (os .getenv ("PYTHON_ENV_TOOLS_TEMP" )) / "python-env-tools" ).resolve ()
75+ session .log (f"Cloning python-environment-tools to { temp_dir } " )
76+ temp_dir .mkdir (0o766 , parents = True , exist_ok = True )
77+
78+ try :
79+ with session .cd (temp_dir ):
80+ session .run ("git" , "init" , external = True )
81+ session .run (
82+ "git" ,
83+ "remote" ,
84+ "add" ,
85+ "origin" ,
86+ "https://github.com/microsoft/python-environment-tools" ,
87+ external = True ,
88+ )
89+ session .run ("git" , "fetch" , "origin" , branch , external = True )
90+ session .run (
91+ "git" , "checkout" , "--force" , "-B" , branch , f"origin/{ branch } " , external = True
92+ )
93+ delete_dir (temp_dir / ".git" )
94+ delete_dir (temp_dir / ".github" )
95+ delete_dir (temp_dir / ".vscode" )
96+ (temp_dir / "CODE_OF_CONDUCT.md" ).unlink ()
97+ shutil .move (os .fspath (temp_dir ), os .fspath (dest_dir ))
98+ except PermissionError as e :
99+ print (f"Permission error: { e } " )
100+ if not dest_dir .exists ():
101+ raise
102+ finally :
103+ delete_dir (temp_dir , ignore_errors = True )
104+
51105
52106@nox .session ()
53107def azure_pet_build_before (session : nox .Session ):
@@ -132,37 +186,19 @@ def native_build(session: nox.Session):
132186 vscode_ignore .write_text ("\n " .join (filtered_lines ) + "\n " , encoding = "utf-8" )
133187
134188
135- def delete_dir (path : pathlib .Path , ignore_errors = None ):
136- attempt = 0
137- known = []
138- while attempt < 5 :
139- try :
140- shutil .rmtree (os .fspath (path ), ignore_errors = ignore_errors )
141- return
142- except PermissionError as pe :
143- if os .fspath (pe .filename ) in known :
144- break
145- print (f"Changing permissions on { pe .filename } " )
146- os .chmod (pe .filename , 0o666 )
147-
148- shutil .rmtree (os .fspath (path ))
149-
150-
151189@nox .session ()
152190def checkout_native (session : nox .Session ):
153191 dest = (pathlib .Path .cwd () / "python-env-tools" ).resolve ()
154192 if dest .exists ():
155193 shutil .rmtree (os .fspath (dest ))
156194
157- tempdir = os .getenv ("TEMP" ) or os .getenv ("TMP" ) or "/tmp"
158- tempdir = pathlib .Path (tempdir ) / str (uuid .uuid4 ()) / "python-env-tools"
159- tempdir .mkdir (0o666 , parents = True )
160-
161- session .log (f"Temp dir: { tempdir } " )
195+ temp_dir = os .getenv ("TEMP" ) or os .getenv ("TMP" ) or "/tmp"
196+ temp_dir = pathlib .Path (temp_dir ) / str (uuid .uuid4 ()) / "python-env-tools"
197+ temp_dir .mkdir (0o766 , parents = True )
162198
163- session .log (f"Cloning python-environment-tools to { tempdir } " )
199+ session .log (f"Cloning python-environment-tools to { temp_dir } " )
164200 try :
165- with session .cd (tempdir ):
201+ with session .cd (temp_dir ):
166202 session .run ("git" , "init" , external = True )
167203 session .run (
168204 "git" ,
@@ -176,17 +212,17 @@ def checkout_native(session: nox.Session):
176212 session .run (
177213 "git" , "checkout" , "--force" , "-B" , "main" , "origin/main" , external = True
178214 )
179- delete_dir (tempdir / ".git" )
180- delete_dir (tempdir / ".github" )
181- delete_dir (tempdir / ".vscode" )
182- (tempdir / "CODE_OF_CONDUCT.md" ).unlink ()
183- shutil .move (os .fspath (tempdir ), os .fspath (dest ))
215+ delete_dir (temp_dir / ".git" )
216+ delete_dir (temp_dir / ".github" )
217+ delete_dir (temp_dir / ".vscode" )
218+ (temp_dir / "CODE_OF_CONDUCT.md" ).unlink ()
219+ shutil .move (os .fspath (temp_dir ), os .fspath (dest ))
184220 except PermissionError as e :
185221 print (f"Permission error: { e } " )
186222 if not dest .exists ():
187223 raise
188224 finally :
189- delete_dir (tempdir .parent , ignore_errors = True )
225+ delete_dir (temp_dir .parent , ignore_errors = True )
190226
191227
192228@nox .session ()
0 commit comments