46
46
import subprocess
47
47
import tempfile
48
48
import importlib
49
+ import time
49
50
50
51
import sys
51
52
@@ -99,12 +100,12 @@ def wrapper(*args, **kwargs):
99
100
return wrapper
100
101
return decorator
101
102
102
- def run_cmd (args , msg = "" , failOnError = True , cwd = None , env = None ):
103
- cwd_log = "cd " + cwd if cwd else ""
103
+ def run_cmd (args , msg = "" , failOnError = True , cwd = None , env = None , quiet = False , ** kwargs ):
104
+ cwd_log = "cd " + cwd + " ;" if cwd else ""
104
105
print ("+" , cwd_log , ' ' .join (args ))
105
- result = subprocess .run (args , cwd = cwd , env = env )
106
+ result = subprocess .run (args , cwd = cwd , env = env , capture_output = quiet , ** kwargs )
106
107
if failOnError and result .returncode != 0 :
107
- xit (msg , status = result .returncode )
108
+ xit (os . linesep . join (( msg , str ( result . stdout ))) , status = result .returncode )
108
109
return result .returncode
109
110
110
111
def known_packages ():
@@ -398,13 +399,13 @@ def xit(msg, status=-1):
398
399
exit (- 1 )
399
400
400
401
401
- def _download_with_curl_and_extract (dest_dir , url ):
402
+ def _download_with_curl_and_extract (dest_dir , url , quiet = False ):
402
403
name = url [url .rfind ("/" )+ 1 :]
403
404
404
405
downloaded_path = os .path .join (dest_dir , name )
405
406
406
407
# first try direct connection
407
- if run_cmd (["curl" , "-L" , "-o" , downloaded_path , url ], failOnError = False ) != 0 :
408
+ if run_cmd (["curl" , "-L" , "-o" , downloaded_path , url ], failOnError = False , quiet = quiet ) != 0 :
408
409
# honor env var 'HTTP_PROXY', 'HTTPS_PROXY', and 'NO_PROXY'
409
410
env = os .environ
410
411
curl_opts = []
@@ -417,16 +418,16 @@ def _download_with_curl_and_extract(dest_dir, url):
417
418
using_proxy = True
418
419
if using_proxy and "NO_PROXY" in env :
419
420
curl_opts += ["--noproxy" , env ["NO_PROXY" ]]
420
- run_cmd (["curl" , "-L" ] + curl_opts + ["-o" , downloaded_path , url ], msg = "Download error" )
421
+ run_cmd (["curl" , "-L" ] + curl_opts + ["-o" , downloaded_path , url ], msg = "Download error" , quiet = quiet )
421
422
422
423
if name .endswith (".tar.gz" ):
423
- run_cmd (["tar" , "xzf" , downloaded_path , "-C" , dest_dir ], msg = "Error extracting tar.gz" )
424
+ run_cmd (["tar" , "xzf" , downloaded_path , "-C" , dest_dir ], msg = "Error extracting tar.gz" , quiet = quiet )
424
425
bare_name = name [:- len (".tar.gz" )]
425
426
elif name .endswith (".tar.bz2" ):
426
- run_cmd (["tar" , "xjf" , downloaded_path , "-C" , dest_dir ], msg = "Error extracting tar.bz2" )
427
+ run_cmd (["tar" , "xjf" , downloaded_path , "-C" , dest_dir ], msg = "Error extracting tar.bz2" , quiet = quiet )
427
428
bare_name = name [:- len (".tar.bz2" )]
428
429
elif name .endswith (".zip" ):
429
- run_cmd (["unzip" , "-u" , downloaded_path , "-d" , dest_dir ], msg = "Error extracting zip" )
430
+ run_cmd (["unzip" , "-u" , downloaded_path , "-d" , dest_dir ], msg = "Error extracting zip" , quiet = quiet )
430
431
bare_name = name [:- len (".zip" )]
431
432
else :
432
433
xit ("Unknown file type: %s" % name )
@@ -437,6 +438,8 @@ def _download_with_curl_and_extract(dest_dir, url):
437
438
def _install_from_url (url , package , extra_opts = [], add_cflags = "" , ignore_errors = False , env = {}, version = None , pre_install_hook = None , build_cmd = []):
438
439
tempdir = tempfile .mkdtemp ()
439
440
441
+ quiet = "-q" in extra_opts
442
+
440
443
os_env = os .environ
441
444
442
445
# honor env var 'CFLAGS' and the explicitly passed env
@@ -445,7 +448,7 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
445
448
cflags = os_env .get ("CFLAGS" , "" ) + ((" " + add_cflags ) if add_cflags else "" )
446
449
setup_env ['CFLAGS' ] = cflags if cflags else ""
447
450
448
- bare_name = _download_with_curl_and_extract (tempdir , url )
451
+ bare_name = _download_with_curl_and_extract (tempdir , url , quiet = quiet )
449
452
450
453
file_realpath = os .path .dirname (os .path .realpath (__file__ ))
451
454
patches_dir = os .path .join (Path (file_realpath ).parent , 'patches' , package )
@@ -455,15 +458,15 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
455
458
456
459
patch_file_path = first_existing (package , versions , os .path .join (patches_dir , "sdist" ), ".patch" )
457
460
if patch_file_path :
458
- run_cmd (["patch" , "-d" , os .path .join (tempdir , bare_name , "" ), "-p1" , "-i" , patch_file_path ])
461
+ run_cmd (["patch" , "-d" , os .path .join (tempdir , bare_name , "" ), "-p1" , "-i" , patch_file_path ], quiet = quiet )
459
462
460
463
whl_patches_dir = os .path .join (patches_dir , "whl" )
461
464
patch_file_path = first_existing (package , versions , whl_patches_dir , ".patch" )
462
465
subdir = read_first_existing (package , versions , whl_patches_dir , ".dir" )
463
466
subdir = "" if subdir is None else subdir
464
467
if patch_file_path :
465
468
os .path .join (tempdir , bare_name , subdir )
466
- run_cmd (["patch" , "-d" , os .path .join (tempdir , bare_name , subdir ), "-p1" , "-i" , patch_file_path ])
469
+ run_cmd (["patch" , "-d" , os .path .join (tempdir , bare_name , subdir ), "-p1" , "-i" , patch_file_path ], quiet = quiet )
467
470
468
471
if pre_install_hook :
469
472
pre_install_hook (os .path .join (tempdir , bare_name ))
@@ -472,10 +475,14 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
472
475
user_arg = ["--user" ]
473
476
else :
474
477
user_arg = []
478
+ start = time .time ()
475
479
status = run_cmd ([sys .executable , "setup.py" ] + build_cmd + ["install" ] + user_arg + extra_opts , env = setup_env ,
476
- cwd = os .path .join (tempdir , bare_name ))
480
+ cwd = os .path .join (tempdir , bare_name ), quiet = quiet )
481
+ end = time .time ()
477
482
if status != 0 and not ignore_errors :
478
483
xit ("An error occurred trying to run `setup.py install %s %s'" % (user_arg , " " .join (extra_opts )))
484
+ elif quiet :
485
+ info ("{} successfully installed (took {:.2f} s)" , package , (end - start ))
479
486
480
487
# NOTE: Following 3 functions are duplicated in pip_hook.py:
481
488
# creates a search list of a versioned file:
@@ -522,7 +529,7 @@ def install_from_pypi(package, extra_opts=[], add_cflags="", ignore_errors=True,
522
529
# this is already the url to the actual package
523
530
pass
524
531
else :
525
- r = subprocess .check_output ("curl -L %s" % url , shell = True ).decode ("utf8" )
532
+ r = subprocess .check_output ("curl -L %s" % url , stderr = subprocess . DEVNULL , shell = True ).decode ("utf8" )
526
533
url = None
527
534
try :
528
535
urls = json .loads (r )["urls" ]
@@ -568,6 +575,7 @@ def get_site_packages_path():
568
575
569
576
def main (argv ):
570
577
parser = argparse .ArgumentParser (description = "The simple Python package installer for GraalVM" )
578
+ parser .add_argument ("--quiet" , "-q" , action = "store_true" , help = "Do not show build output" )
571
579
572
580
subparsers = parser .add_subparsers (title = "Commands" , dest = "command" , metavar = "Use COMMAND --help for further help." )
573
581
@@ -614,6 +622,8 @@ def main(argv):
614
622
)
615
623
616
624
args = parser .parse_args (argv )
625
+
626
+ quiet_flag = ["-q" ] if args .quiet else []
617
627
618
628
if args .command == "list" :
619
629
user_site = get_site_packages_path ()
@@ -646,15 +656,15 @@ def main(argv):
646
656
if pkg not in KNOWN_PACKAGES :
647
657
xit ("Unknown package: '%s'" % pkg )
648
658
else :
649
- extra_opts = []
659
+ extra_opts = [] + quiet_flag
650
660
if args .prefix :
651
661
extra_opts += ["--prefix" , args .prefix ]
652
662
if args .user :
653
663
extra_opts += ["--user" ]
654
664
KNOWN_PACKAGES [pkg ](extra_opts = extra_opts )
655
665
elif args .command == "pypi" :
656
666
for pkg in args .package .split ("," ):
657
- install_from_pypi (pkg , ignore_errors = False )
667
+ install_from_pypi (pkg , extra_opts = quiet_flag , ignore_errors = False )
658
668
659
669
660
670
if __name__ == "__main__" :
0 commit comments