Skip to content

Commit 5a206a3

Browse files
committed
add some more help
1 parent 9dcb4ba commit 5a206a3

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ To try it, you can use the bundled releases from
1414
some examples of what you can do with it, check out the
1515
[reference](https://www.graalvm.org/docs/reference-manual/languages/python/).
1616

17+
### Installing packages
18+
19+
At the moment not enough of the standard library is implemented to run the
20+
standard package installers for many packages. As a convenience, we provide a
21+
simple module to install packages that we know to be working (including
22+
potential patches required for those packages). Try the following to find out
23+
more:
24+
```python
25+
graalpython -m ginstall --help
26+
```
27+
1728
### Licensing
1829

1930
This Graal/Truffle-based implementation of Python is copyright (c) 2017, 2018
@@ -23,3 +34,4 @@ Universal Permissive License v 1.0 as shown at
2334
implementation is in part derived from and contains additional code from 3rd
2435
parties, the copyrights and licensing of which is detailed in the
2536
[LICENSE](LICENSE) and [3rd_party_licenses.txt](3rd_party_licenses.txt) files.
37+

graalpython/lib-graalpython/modules/ginstall.py

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
import argparse
4141
import json
4242
import os
43+
import shutil
44+
import site
4345
import subprocess
4446
import sys
4547
import tempfile
46-
import zipfile
4748

4849

4950
def system(cmd, msg=""):
@@ -57,8 +58,11 @@ def setuptools():
5758
install_from_pypi("setuptools")
5859

5960
def numpy():
60-
# needs setuptools
61-
setuptools()
61+
try:
62+
import setuptools
63+
except ImportError:
64+
print("Installing required dependency: setuptools")
65+
setuptools()
6266

6367
url = "https://files.pythonhosted.org/packages/b0/2b/497c2bb7c660b2606d4a96e2035e92554429e139c6c71cdff67af66b58d2/numpy-1.14.3.zip"
6468
tempdir = tempfile.mkdtemp()
@@ -309,7 +313,7 @@ def get_lapack_lite_sources(ext, build_dir):
309313
KNOWN_PACKAGES = known_packages()
310314

311315

312-
def xit(str, status=-1):
316+
def xit(msg, status=-1):
313317
print(msg)
314318
exit(-1)
315319

@@ -335,23 +339,16 @@ def install_from_pypi(package):
335339
if url:
336340
tempdir = tempfile.mkdtemp()
337341
filename = url.rpartition("/")[2]
338-
status = os.system("curl -L -o %s/%s %s" % (tempdir, filename, url))
339-
if status != 0:
340-
xit("Download error", status=status)
342+
system("curl -L -o %s/%s %s" % (tempdir, filename, url), msg="Download error")
341343
dirname = None
342344
if filename.endswith(".zip"):
343-
with zipfile.ZipFile("%s/%s" % (tempdir, filename), 'r') as zf:
344-
zf.extractall(tempdir)
345+
system("unzip -u %s/%s -d %s" % (tempdir, filename, tempdir))
345346
dirname = filename[:-4]
346347
elif filename.endswith(".tar.gz"):
347-
status = os.system("tar -C %s -xzf %s/%s" % (tempdir, tempdir, filename))
348-
if status != 0:
349-
xit("Error during extraction", status=status)
348+
system("tar -C %s -xzf %s/%s" % (tempdir, tempdir, filename), msg="Error during extraction")
350349
dirname = filename[:-7]
351350
elif filename.endswith(".tar.bz2"):
352-
status = os.system("tar -C %s -xjf %s/%s" % (tempdir, tempdir, filename))
353-
if status != 0:
354-
xit("Error during extraction", status=status)
351+
system("tar -C %s -xjf %s/%s" % (tempdir, tempdir, filename), msg="Error during extraction")
355352
dirname = filename[:-7]
356353
else:
357354
xit("Unknown file type: %s" % filename)
@@ -364,35 +361,70 @@ def install_from_pypi(package):
364361

365362

366363
def main(argv):
367-
parser = argparse.ArgumentParser()
364+
parser = argparse.ArgumentParser(description="The simple Python package installer for GraalVM")
368365

369-
subparsers = parser.add_subparsers(help="Commands", dest="command")
366+
subparsers = parser.add_subparsers(title="Commands", dest="command", metavar="Use COMMAND --help for further help.")
370367

371368
subparsers.add_parser(
372-
"list", help="list known packages with potential workarounds available for installation"
369+
"list",
370+
help="list known packages with potential workarounds available for installation"
373371
)
374372

375373
subparsers.add_parser(
376-
"install", help="install a known package"
374+
"install",
375+
help="install a known package",
376+
description="Install a known package. Known packages are " + ", ".join(KNOWN_PACKAGES.keys())
377377
).add_argument(
378-
"package", help="comma-separated list"
378+
"package",
379+
help="comma-separated list"
379380
)
380381

381382
subparsers.add_parser(
382-
"pypi", help="attempt to install a package from PyPI (untested, likely won't work, and it won't install dependencies for you)"
383+
"uninstall",
384+
help="remove installation folder of a local package",
383385
).add_argument(
384-
"package", help="comma-separated list, can use `==` at the end of a package name to specify an exact version"
386+
"package",
387+
help="comma-separated list"
388+
)
389+
390+
subparsers.add_parser(
391+
"pypi",
392+
help="attempt to install a package from PyPI (untested, likely won't work, and it won't install dependencies for you)",
393+
description="Attempt to install a package from PyPI"
394+
).add_argument(
395+
"package",
396+
help="comma-separated list, can use `==` at the end of a package name to specify an exact version"
385397
)
386398

387399
args = parser.parse_args(argv)
388400

389401
if args.command == "list":
390-
print("Known packages:")
391-
print("\n".join(" " + x for x in KNOWN_PACKAGES.keys()))
402+
user_site = site.getusersitepackages()
403+
print("Installed packages:")
404+
for p in sys.path:
405+
if p.startswith(user_site):
406+
print(p[len(user_site) + 1:])
407+
elif args.command == "uninstall":
408+
print("WARNING: I will only delete the package folder, proper uninstallation is not supported at this time.")
409+
user_site = site.getusersitepackages()
410+
for pkg in args.package.split(","):
411+
deleted = False
412+
for p in sys.path:
413+
if p.startswith(user_site) and p.endswith(pkg):
414+
if os.path.isdir(p):
415+
shutil.rmtree(p)
416+
else:
417+
os.unlink(p)
418+
deleted = True
419+
break
420+
if deleted:
421+
print("Deleted %s" % p)
422+
else:
423+
xit("Unknown package: '%s'" % pkg)
392424
elif args.command == "install":
393425
for pkg in args.package.split(","):
394426
if pkg not in KNOWN_PACKAGES:
395-
xit("Unknown package: '%s'" % args.install)
427+
xit("Unknown package: '%s'" % pkg)
396428
else:
397429
KNOWN_PACKAGES[args.install]()
398430
elif args.command == "pypi":

0 commit comments

Comments
 (0)