@@ -99,14 +99,13 @@ def wrapper(*args, **kwargs):
99
99
return wrapper
100
100
return decorator
101
101
102
-
103
- def system (cmd , msg = "" ):
104
- print ("+" , cmd )
105
- status = os .system (cmd )
106
- if status != 0 :
107
- xit (msg , status = status )
108
- return status
109
-
102
+ def run_cmd (args , msg = "" , failOnError = True , cwd = None , env = None ):
103
+ cwd_log = "cd " + cwd if cwd else ""
104
+ print ("+" , cwd_log , ' ' .join (args ))
105
+ result = subprocess .run (args , cwd = cwd , env = env )
106
+ if failOnError and result .returncode != 0 :
107
+ xit (msg , status = result .returncode )
108
+ return result .returncode
110
109
111
110
def known_packages ():
112
111
@pip_package ()
@@ -377,32 +376,30 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
377
376
elif url .startswith ("https://" ) and "HTTPS_PROXY" in os_env :
378
377
curl_opts += ["--proxy" , os_env ["HTTPS_PROXY" ]]
379
378
380
- # honor env var 'CFLAGS' and 'CPPFLAGS'
381
- cppflags = os_env .get ("CPPFLAGS" , "" )
379
+ # honor env var 'CFLAGS' and the explicitly passed env
380
+ setup_env = os_env .copy ()
381
+ setup_env .update (env )
382
382
cflags = os_env .get ("CFLAGS" , "" ) + ((" " + add_cflags ) if add_cflags else "" )
383
+ setup_env ['CFLAGS' ] = cflags if cflags else ""
383
384
384
- env_str = ('CFLAGS="%s" ' % cflags if cflags else "" ) + ('CPPFLAGS="%s" ' % cppflags if cppflags else "" )
385
- for key in env .keys ():
386
- env_str = env_str + ('%s="%s" ' % (key , env [key ]))
387
-
388
- if os .system ("curl -L -o %s/%s %s" % (tempdir , name , url )) != 0 :
385
+ if run_cmd (["curl" , "-L" , "-o" , os .path .join (tempdir , name ), url ], failOnError = False ) != 0 :
389
386
# honor env var 'HTTP_PROXY' and 'HTTPS_PROXY'
390
387
env = os .environ
391
388
curl_opts = []
392
389
if url .startswith ("http://" ) and "HTTP_PROXY" in env :
393
390
curl_opts += ["--proxy" , env ["HTTP_PROXY" ]]
394
391
elif url .startswith ("https://" ) and "HTTPS_PROXY" in env :
395
392
curl_opts += ["--proxy" , env ["HTTPS_PROXY" ]]
396
- system ( "curl -L %s -o %s/%s %s" % ( " " . join (curl_opts ), tempdir , name , url ) , msg = "Download error" )
393
+ run_cmd ([ "curl" , "-L" ] + curl_opts + [ "-o" , os . path . join (tempdir , name ) , url ] , msg = "Download error" )
397
394
398
395
if name .endswith (".tar.gz" ):
399
- system ( "tar xzf %s/%s -C %s" % (tempdir , name , tempdir ) , msg = "Error extracting tar.gz" )
396
+ run_cmd ([ "tar" , " xzf" , os . path . join (tempdir , name ), "-C" , tempdir ] , msg = "Error extracting tar.gz" )
400
397
bare_name = name [:- len (".tar.gz" )]
401
398
elif name .endswith (".tar.bz2" ):
402
- system ( "tar xjf %s/%s -C %s" % (tempdir , name , tempdir ) , msg = "Error extracting tar.bz2" )
399
+ run_cmd ([ "tar" , " xjf" , os . path . join (tempdir , name ), "-C" , tempdir ] , msg = "Error extracting tar.bz2" )
403
400
bare_name = name [:- len (".tar.bz2" )]
404
401
elif name .endswith (".zip" ):
405
- system ( "unzip -u %s/%s -d %s" % (tempdir , name , tempdir ) , msg = "Error extracting zip" )
402
+ run_cmd ([ "unzip" , "-u" , os . path . join (tempdir , name ), "-d" , tempdir ] , msg = "Error extracting zip" )
406
403
bare_name = name [:- len (".zip" )]
407
404
else :
408
405
xit ("Unknown file type: %s" % name )
@@ -415,22 +412,22 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
415
412
416
413
patch_file_path = first_existing (package , versions , os .path .join (patches_dir , "sdist" ), ".patch" )
417
414
if patch_file_path :
418
- system ("patch -d %s/%s/ -p1 < %s" %
419
- (tempdir , bare_name , patch_file_path ))
415
+ run_cmd (["patch" , "-d" , os .path .join (tempdir , bare_name , "" ), "-p1" , "-i" , patch_file_path ])
420
416
421
417
whl_patches_dir = os .path .join (patches_dir , "whl" )
422
418
patch_file_path = first_existing (package , versions , whl_patches_dir , ".patch" )
423
419
subdir = read_first_existing (package , versions , whl_patches_dir , ".dir" )
424
- subdir = "" if subdir is None else "/" + subdir
420
+ subdir = "" if subdir is None else subdir
425
421
if patch_file_path :
426
- system ( "patch -d %s/%s%s -p1 < %s" %
427
- (tempdir , bare_name , subdir , patch_file_path ) )
422
+ os . path . join ( tempdir , bare_name , subdir )
423
+ run_cmd ([ "patch" , "-d" , os . path . join (tempdir , bare_name , subdir ), "-p1" , "-i" , patch_file_path ] )
428
424
429
425
if "--prefix" not in extra_opts and site .ENABLE_USER_SITE :
430
- user_arg = "--user"
426
+ user_arg = [ "--user" ]
431
427
else :
432
- user_arg = ""
433
- status = system ("cd %s/%s; %s %s setup.py install %s %s" % (tempdir , bare_name , env_str , sys .executable , user_arg , " " .join (extra_opts )))
428
+ user_arg = []
429
+ status = run_cmd ([sys .executable , "setup.py" , "install" ] + user_arg + extra_opts , env = setup_env ,
430
+ cwd = os .path .join (tempdir , bare_name ))
434
431
if status != 0 and not ignore_errors :
435
432
xit ("An error occurred trying to run `setup.py install %s %s'" % (user_arg , " " .join (extra_opts )))
436
433
0 commit comments