Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entrypoints #56

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ dmypy.json

# Pyre type checker
.pyre/

# Emacs backup files
*~
#*
4 changes: 0 additions & 4 deletions bin/planutils

This file was deleted.

46 changes: 4 additions & 42 deletions planutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand All @@ -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)')
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions planutils/entrypoints.py
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 9 additions & 4 deletions planutils/package_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = []
Expand All @@ -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']
Expand Down Expand Up @@ -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)
54 changes: 36 additions & 18 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)