6
6
from contextlib import contextmanager
7
7
from distutils .core import Command
8
8
from pathlib import Path
9
- from tempfile import NamedTemporaryFile
9
+ from tempfile import mkstemp
10
10
from typing import Any , Dict
11
11
12
12
# app
@@ -25,31 +25,46 @@ def cd(path: Path):
25
25
os .chdir (old_path )
26
26
27
27
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
+
28
38
class CommandReader (BaseReader ):
29
39
@cached_property
30
40
def content (self ) -> Dict [str , Any ]:
31
41
# 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
+
53
68
return self ._clean (content )
54
69
55
70
0 commit comments