66from contextlib import contextmanager
77from distutils .core import Command
88from pathlib import Path
9- from tempfile import NamedTemporaryFile
9+ from tempfile import mkstemp
1010from typing import Any , Dict
1111
1212# app
@@ -25,31 +25,46 @@ def cd(path: Path):
2525 os .chdir (old_path )
2626
2727
28+ @contextmanager
29+ def tmpfile ():
30+ fd , path = mkstemp ()
31+ os .close (fd )
32+ try :
33+ yield Path (path )
34+ finally :
35+ os .unlink (path )
36+
37+
2838class CommandReader (BaseReader ):
2939 @cached_property
3040 def content (self ) -> Dict [str , Any ]:
3141 # generate a temporary json file which contains the metadata
32- output_json = NamedTemporaryFile ()
33- cmd = [
34- sys .executable ,
35- self .path .name ,
36- '-q' ,
37- '--command-packages' , 'dephell_setuptools' ,
38- 'distutils_cmd' ,
39- '-o' , output_json .name ,
40- ]
41- with cd (self .path .parent ):
42- result = subprocess .run (
43- cmd ,
44- stderr = subprocess .PIPE ,
45- stdout = subprocess .PIPE ,
46- env = {'PYTHONPATH' : str (Path (__file__ ).parent .parent )},
47- )
48- if result .returncode != 0 :
49- raise RuntimeError (result .stderr .decode ().strip ().split ('\n ' )[- 1 ])
50-
51- with open (output_json .name ) as stream :
52- content = json .load (stream )
42+ with tmpfile () as output_json :
43+ cmd = [
44+ sys .executable ,
45+ self .path .name ,
46+ '-q' ,
47+ '--command-packages' , 'dephell_setuptools' ,
48+ 'distutils_cmd' ,
49+ '-o' , str (output_json ),
50+ ]
51+ with cd (self .path .parent ):
52+ env = {'PYTHONPATH' : str (Path (__file__ ).parent .parent )}
53+ if sys .platform == 'win32' :
54+ env ['SystemRoot' ] = os .environ ['SystemRoot' ]
55+
56+ result = subprocess .run (
57+ cmd ,
58+ stderr = subprocess .PIPE ,
59+ stdout = subprocess .PIPE ,
60+ env = env ,
61+ )
62+ if result .returncode != 0 :
63+ raise RuntimeError (result .stderr .decode ().strip ().split ('\n ' )[- 1 ])
64+
65+ with output_json .open () as stream :
66+ content = json .load (stream )
67+
5368 return self ._clean (content )
5469
5570
0 commit comments