diff --git a/.gitignore b/.gitignore index b6e4761..6b47938 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,7 @@ dmypy.json # Pyre type checker .pyre/ + +# Emacs backup files +*~ +#* diff --git a/bin/planutils b/bin/planutils deleted file mode 100755 index cca8a7a..0000000 --- a/bin/planutils +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env python - -import planutils -planutils.main() diff --git a/planutils/__init__.py b/planutils/__init__.py index ba3e2d5..89c5448 100644 --- a/planutils/__init__.py +++ b/planutils/__init__.py @@ -26,49 +26,10 @@ def setup(): return minimal_setup() - os.mkdir(os.path.join(os.path.expanduser('~'), '.planutils', 'bin')) - - print("Adding bin folder to path (assuming ~/.bashrc exists)...") - with open(os.path.join(os.path.expanduser('~'), '.bashrc'), "a+") as f: - f.write("export PLANUTILS_PREFIX=\"~/.planutils\"\n") - f.write("export PATH=\"$PLANUTILS_PREFIX/bin:$PATH\"\n") - - print("Installing package scripts...") - for p in PACKAGES: - if PACKAGES[p]['runnable']: - script = "#!/bin/bash\n" - script += "if $(planutils check-installed %s)\n" % p - script += "then\n" - script += " ~/.planutils/packages/%s/run $@\n" % p - script += "else\n" - script += " echo\n" - script += " echo 'Package not installed!'\n" - script += " read -r -p \" Download & install? [Y/n] \" varchoice\n" - script += " varchoice=${varchoice,,}\n" # tolower - script += " if ! [[ \"$varchoice\" =~ ^(no|n)$ ]]\n" - script += " then\n" - script += " if planutils install %s;\n" % p - script += " then\n" - script += " echo 'Successfully installed %s!'\n" % p - script += " echo\n" - script += " echo \"Original command: %s $@\"\n" % p - script += " read -r -p \" Re-run command? [Y/n] \" varchoice\n" - script += " varchoice=${varchoice,,}\n" # tolower - script += " if ! [[ \"$varchoice\" =~ ^(no|n)$ ]]\n" - script += " then\n" - script += " ~/.planutils/packages/%s/run $@\n" % p - script += " fi\n" - script += " fi\n" - script += " fi\n" - script += " echo\n" - script += "fi\n" - with open(os.path.join(os.path.expanduser('~'), '.planutils', 'bin', p), 'w') as f: - f.write(script) - os.chmod(os.path.join(os.path.expanduser('~'), '.planutils', 'bin', p), 0o0755) - print("\nAll set! Be sure to start a new bash session or update your PATH variable to include ~/.planutils/bin\n") + def setup_done(): return os.path.exists(os.path.join(os.path.expanduser('~'), '.planutils')) @@ -78,7 +39,8 @@ def main(): subparsers = parser.add_subparsers(help='sub-command help', dest='command') parser_install = subparsers.add_parser('install', help='install package(s) such as a planner') - parser_install.add_argument('-f', '--force', help='force installation', action='store_true') + parser_install.add_argument('-f', '--force', help='force reinstallation if the package is already installed', action='store_true') + parser_install.add_argument('-y', '--yes', help='Answer yes to all user queries automatically', action='store_true') parser_install.add_argument('package', help='package name', nargs='+') parser_uninstall = subparsers.add_parser('uninstall', help='uninstall package(s)') @@ -109,7 +71,7 @@ def main(): elif 'install' == args.command: from planutils.package_installation import install - exit({True:0, False:1}[install(args.package, args.force)]) + exit({True:0, False:1}[install(args.package, args.force, args.yes)]) elif 'uninstall' == args.command: from planutils.package_installation import uninstall diff --git a/planutils/entrypoints.py b/planutils/entrypoints.py new file mode 100644 index 0000000..3be7db6 --- /dev/null +++ b/planutils/entrypoints.py @@ -0,0 +1,27 @@ + +import sys +from planutils.package_installation import install, run + +def entrypoint(name): + install(name) + run(name, sys.argv[1:]) + +import functools +import glob +import os.path + +class Entrypoints(): + pass + +entrypoints = Entrypoints() + +for d in glob.glob(os.path.join(os.path.dirname(__file__),"packages","*")): + if not os.path.isdir(d): + continue + + name = os.path.basename(d) + if name == "TEMPLATE": + continue + + name2 = name.replace("-","_") + setattr(entrypoints, name2, functools.partial(entrypoint, name)) diff --git a/planutils/package_installation.py b/planutils/package_installation.py index 342c7d2..d18a451 100644 --- a/planutils/package_installation.py +++ b/planutils/package_installation.py @@ -113,7 +113,7 @@ def upgrade(): subprocess.call('./uninstall', cwd=os.path.join(CUR_DIR, 'packages', package)) subprocess.call('./install', cwd=os.path.join(CUR_DIR, 'packages', package)) -def install(targets, forced=False): +def install(targets, forced=False, always_yes=False): for target in targets: if target not in PACKAGES: print("Error: Package not found -- %s" % target) @@ -123,9 +123,14 @@ def install(targets, forced=False): to_check = [] for target in targets: if check_installed(target): - print("%s is already installed." % target) + if forced: + to_check.append(target) + print("%s is present, will be re-installed (forced installation)." % target) + else: + print("%s is present, not installed." % target) else: to_check.append(target) + print("%s will be installed." % target) done = set() to_install = [] @@ -143,7 +148,7 @@ def install(targets, forced=False): to_install_desc = ["%s (%s)" % (pkg, PACKAGES[pkg]['install-size']) for pkg in to_install] print("\nAbout to install the following packages: %s" % ', '.join(to_install_desc)) - if forced: + if always_yes: user_response = True else: user_response = input(" Proceed? [Y/n] ").lower() in ['', 'y', 'yes'] @@ -184,4 +189,4 @@ def run(target, options): sys.exit(f"Package {target} is not installed") if not PACKAGES[target]["runnable"]: sys.exit(f"Package {target} is not executable") - subprocess.run([Path(settings.PLANUTILS_PREFIX) / "packages" / target / "run"] + options) + subprocess.run([Path(os.path.dirname(__file__)) / "packages" / target / "run"] + options) diff --git a/setup.py b/setup.py index 90c10e8..6606622 100644 --- a/setup.py +++ b/setup.py @@ -7,22 +7,40 @@ with open("README.md", "r") as fh: long_description = fh.read() + +console_scripts = ["planutils = planutils:main"] + +# create entrypoints dynamically +import glob +import os.path +for d in glob.glob(os.path.join(os.path.dirname(__file__),"planutils/packages/*")): + if not os.path.isdir(d): + continue + + name = os.path.basename(d) + if name == "TEMPLATE": + continue + + name2 = name.replace("-","_") + console_scripts.append(f"{name}=planutils.entrypoints:entrypoints.{name2}") + + setuptools.setup(name='planutils', - version='0.2.12', - description='General library for setting up linux-based environments for developing, running, and evaluating planners.', - long_description=long_description, - long_description_content_type="text/markdown", - url='https://github.com/AI-Planning/planutils', - author='', - author_email='', - license='MIT', - packages=['planutils'], - scripts=['bin/planutils'], - classifiers=[ - "Programming Language :: Python :: 3", - "License :: OSI Approved :: MIT License", - "Operating System :: POSIX :: Linux", - ], - python_requires='>=3.6', - include_package_data=True, - zip_safe=False) + version='0.2.12', + description='General library for setting up linux-based environments for developing, running, and evaluating planners.', + long_description=long_description, + long_description_content_type="text/markdown", + url='https://github.com/AI-Planning/planutils', + author='', + author_email='', + license='MIT', + packages=['planutils'], + entry_points={ 'console_scripts': console_scripts, }, + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: POSIX :: Linux", + ], + python_requires='>=3.6', + include_package_data=True, + zip_safe=False)