Skip to content

Commit 0c5b105

Browse files
committed
Add flag --quiet to ginstall
1 parent 59e612c commit 0c5b105

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

graalpython/lib-graalpython/modules/ginstall.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ def wrapper(*args, **kwargs):
9999
return wrapper
100100
return decorator
101101

102-
def run_cmd(args, msg="", failOnError=True, cwd=None, env=None):
103-
cwd_log = "cd " + cwd if cwd else ""
102+
def run_cmd(args, msg="", failOnError=True, cwd=None, env=None, quiet=False, **kwargs):
103+
cwd_log = "cd " + cwd + " ;" if cwd else ""
104104
print("+", cwd_log, ' '.join(args))
105-
result = subprocess.run(args, cwd=cwd, env=env)
105+
result = subprocess.run(args, cwd=cwd, env=env, capture_output=quiet, **kwargs)
106106
if failOnError and result.returncode != 0:
107-
xit(msg, status=result.returncode)
107+
xit(os.linesep.join((msg, str(result.stdout))), status=result.returncode)
108108
return result.returncode
109109

110110
def known_packages():
@@ -398,13 +398,13 @@ def xit(msg, status=-1):
398398
exit(-1)
399399

400400

401-
def _download_with_curl_and_extract(dest_dir, url):
401+
def _download_with_curl_and_extract(dest_dir, url, quiet=False):
402402
name = url[url.rfind("/")+1:]
403403

404404
downloaded_path = os.path.join(dest_dir, name)
405405

406406
# first try direct connection
407-
if run_cmd(["curl", "-L", "-o", downloaded_path, url], failOnError=False) != 0:
407+
if run_cmd(["curl", "-L", "-o", downloaded_path, url], failOnError=False, quiet=quiet) != 0:
408408
# honor env var 'HTTP_PROXY', 'HTTPS_PROXY', and 'NO_PROXY'
409409
env = os.environ
410410
curl_opts = []
@@ -417,16 +417,16 @@ def _download_with_curl_and_extract(dest_dir, url):
417417
using_proxy = True
418418
if using_proxy and "NO_PROXY" in env:
419419
curl_opts += ["--noproxy", env["NO_PROXY"]]
420-
run_cmd(["curl", "-L"] + curl_opts + ["-o", downloaded_path, url], msg="Download error")
420+
run_cmd(["curl", "-L"] + curl_opts + ["-o", downloaded_path, url], msg="Download error", quiet=quiet)
421421

422422
if name.endswith(".tar.gz"):
423-
run_cmd(["tar", "xzf", downloaded_path, "-C", dest_dir], msg="Error extracting tar.gz")
423+
run_cmd(["tar", "xzf", downloaded_path, "-C", dest_dir], msg="Error extracting tar.gz", quiet=quiet)
424424
bare_name = name[:-len(".tar.gz")]
425425
elif name.endswith(".tar.bz2"):
426-
run_cmd(["tar", "xjf", downloaded_path, "-C", dest_dir], msg="Error extracting tar.bz2")
426+
run_cmd(["tar", "xjf", downloaded_path, "-C", dest_dir], msg="Error extracting tar.bz2", quiet=quiet)
427427
bare_name = name[:-len(".tar.bz2")]
428428
elif name.endswith(".zip"):
429-
run_cmd(["unzip", "-u", downloaded_path, "-d", dest_dir], msg="Error extracting zip")
429+
run_cmd(["unzip", "-u", downloaded_path, "-d", dest_dir], msg="Error extracting zip", quiet=quiet)
430430
bare_name = name[:-len(".zip")]
431431
else:
432432
xit("Unknown file type: %s" % name)
@@ -437,6 +437,8 @@ def _download_with_curl_and_extract(dest_dir, url):
437437
def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=False, env={}, version=None, pre_install_hook=None, build_cmd=[]):
438438
tempdir = tempfile.mkdtemp()
439439

440+
quiet = "-q" in extra_opts
441+
440442
os_env = os.environ
441443

442444
# honor env var 'CFLAGS' and the explicitly passed env
@@ -445,7 +447,7 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
445447
cflags = os_env.get("CFLAGS", "") + ((" " + add_cflags) if add_cflags else "")
446448
setup_env['CFLAGS'] = cflags if cflags else ""
447449

448-
bare_name = _download_with_curl_and_extract(tempdir, url)
450+
bare_name = _download_with_curl_and_extract(tempdir, url, quiet=quiet)
449451

450452
file_realpath = os.path.dirname(os.path.realpath(__file__))
451453
patches_dir = os.path.join(Path(file_realpath).parent, 'patches', package)
@@ -455,15 +457,15 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
455457

456458
patch_file_path = first_existing(package, versions, os.path.join(patches_dir, "sdist"), ".patch")
457459
if patch_file_path:
458-
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, ""), "-p1", "-i", patch_file_path])
460+
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, ""), "-p1", "-i", patch_file_path], quiet=quiet)
459461

460462
whl_patches_dir = os.path.join(patches_dir, "whl")
461463
patch_file_path = first_existing(package, versions, whl_patches_dir, ".patch")
462464
subdir = read_first_existing(package, versions, whl_patches_dir, ".dir")
463465
subdir = "" if subdir is None else subdir
464466
if patch_file_path:
465467
os.path.join(tempdir, bare_name, subdir)
466-
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, subdir), "-p1", "-i", patch_file_path])
468+
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, subdir), "-p1", "-i", patch_file_path], quiet=quiet)
467469

468470
if pre_install_hook:
469471
pre_install_hook(os.path.join(tempdir, bare_name))
@@ -473,9 +475,11 @@ def _install_from_url(url, package, extra_opts=[], add_cflags="", ignore_errors=
473475
else:
474476
user_arg = []
475477
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))
478+
cwd=os.path.join(tempdir, bare_name), quiet=quiet)
477479
if status != 0 and not ignore_errors:
478480
xit("An error occurred trying to run `setup.py install %s %s'" % (user_arg, " ".join(extra_opts)))
481+
elif quiet:
482+
info("{} successfully installed", package)
479483

480484
# NOTE: Following 3 functions are duplicated in pip_hook.py:
481485
# creates a search list of a versioned file:
@@ -522,7 +526,7 @@ def install_from_pypi(package, extra_opts=[], add_cflags="", ignore_errors=True,
522526
# this is already the url to the actual package
523527
pass
524528
else:
525-
r = subprocess.check_output("curl -L %s" % url, shell=True).decode("utf8")
529+
r = subprocess.check_output("curl -L %s" % url, stderr=subprocess.DEVNULL, shell=True).decode("utf8")
526530
url = None
527531
try:
528532
urls = json.loads(r)["urls"]
@@ -568,6 +572,7 @@ def get_site_packages_path():
568572

569573
def main(argv):
570574
parser = argparse.ArgumentParser(description="The simple Python package installer for GraalVM")
575+
parser.add_argument("--quiet", "-q", action="store_true", help="Do not show build output")
571576

572577
subparsers = parser.add_subparsers(title="Commands", dest="command", metavar="Use COMMAND --help for further help.")
573578

@@ -614,6 +619,8 @@ def main(argv):
614619
)
615620

616621
args = parser.parse_args(argv)
622+
623+
quiet_flag = ["-q"] if args.quiet else []
617624

618625
if args.command == "list":
619626
user_site = get_site_packages_path()
@@ -646,15 +653,15 @@ def main(argv):
646653
if pkg not in KNOWN_PACKAGES:
647654
xit("Unknown package: '%s'" % pkg)
648655
else:
649-
extra_opts = []
656+
extra_opts = [] + quiet_flag
650657
if args.prefix:
651658
extra_opts += ["--prefix", args.prefix]
652659
if args.user:
653660
extra_opts += ["--user"]
654661
KNOWN_PACKAGES[pkg](extra_opts=extra_opts)
655662
elif args.command == "pypi":
656663
for pkg in args.package.split(","):
657-
install_from_pypi(pkg, ignore_errors=False)
664+
install_from_pypi(pkg, extra_opts=quiet_flag, ignore_errors=False)
658665

659666

660667
if __name__ == "__main__":

0 commit comments

Comments
 (0)