40
40
import argparse
41
41
import json
42
42
import os
43
+ import shutil
44
+ import site
43
45
import subprocess
44
46
import sys
45
47
import tempfile
46
- import zipfile
47
48
48
49
49
50
def system (cmd , msg = "" ):
@@ -57,8 +58,11 @@ def setuptools():
57
58
install_from_pypi ("setuptools" )
58
59
59
60
def numpy ():
60
- # needs setuptools
61
- setuptools ()
61
+ try :
62
+ import setuptools
63
+ except ImportError :
64
+ print ("Installing required dependency: setuptools" )
65
+ setuptools ()
62
66
63
67
url = "https://files.pythonhosted.org/packages/b0/2b/497c2bb7c660b2606d4a96e2035e92554429e139c6c71cdff67af66b58d2/numpy-1.14.3.zip"
64
68
tempdir = tempfile .mkdtemp ()
@@ -309,7 +313,7 @@ def get_lapack_lite_sources(ext, build_dir):
309
313
KNOWN_PACKAGES = known_packages ()
310
314
311
315
312
- def xit (str , status = - 1 ):
316
+ def xit (msg , status = - 1 ):
313
317
print (msg )
314
318
exit (- 1 )
315
319
@@ -335,23 +339,16 @@ def install_from_pypi(package):
335
339
if url :
336
340
tempdir = tempfile .mkdtemp ()
337
341
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" )
341
343
dirname = None
342
344
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 ))
345
346
dirname = filename [:- 4 ]
346
347
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" )
350
349
dirname = filename [:- 7 ]
351
350
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" )
355
352
dirname = filename [:- 7 ]
356
353
else :
357
354
xit ("Unknown file type: %s" % filename )
@@ -364,35 +361,70 @@ def install_from_pypi(package):
364
361
365
362
366
363
def main (argv ):
367
- parser = argparse .ArgumentParser ()
364
+ parser = argparse .ArgumentParser (description = "The simple Python package installer for GraalVM" )
368
365
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. " )
370
367
371
368
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"
373
371
)
374
372
375
373
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 ())
377
377
).add_argument (
378
- "package" , help = "comma-separated list"
378
+ "package" ,
379
+ help = "comma-separated list"
379
380
)
380
381
381
382
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" ,
383
385
).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"
385
397
)
386
398
387
399
args = parser .parse_args (argv )
388
400
389
401
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 )
392
424
elif args .command == "install" :
393
425
for pkg in args .package .split ("," ):
394
426
if pkg not in KNOWN_PACKAGES :
395
- xit ("Unknown package: '%s'" % args . install )
427
+ xit ("Unknown package: '%s'" % pkg )
396
428
else :
397
429
KNOWN_PACKAGES [args .install ]()
398
430
elif args .command == "pypi" :
0 commit comments