-
-
Save sophacles/fc6bf504af1d4e3d4485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now you can do this from the command line: | |
$ commandname --list | |
--list | |
Available tasks: | |
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Install this as a module in your environment''' | |
import inspect | |
import os | |
import invoke.cli | |
def dispatch_me(): | |
'''This function does a little magic to allow CLI entry points. | |
When you want to create a script that has a semantic name, and | |
not meddle with invoke's --collection and --root arguments, | |
just have your module's main() (or other entrypoint) function | |
call dispatch_me(), and this function will figure out where | |
it was called, and auto populate the collection and root | |
arguments for you. | |
Keep in mind if you are using other functions from the same | |
module/package that calls "dispatch_me", you'll need to | |
do your imports in the form of: | |
from . import othermodulename | |
But this is a small price to pay :)''' | |
thisframe = inspect.currentframe() | |
caller = inspect.getouterframes(thisframe)[1] | |
caller_fname = caller[0].f_code.co_filename | |
caller_fname = os.path.realpath(caller_fname) | |
caller_root, pyfile = os.path.split(caller_fname) | |
task_module = os.path.splitext(pyfile)[0] | |
clargs = sys.argv[1:] | |
args = ['invoke', | |
'--collection={}'.format(task_module), | |
'--root={}'.format(caller_root)] | |
args.extend(clargs) | |
invoke.cli.dispatch(args) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In your setup.py for the module or package containing some_tasks, | |
have this snippet in your entry points. | |
... | |
entry_points={ | |
'console_scripts': [ | |
"commandname=some_tasks:main", | |
], | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Install some_tasks as a module, or as part of a package''' | |
from invoke import task, run | |
import dispatch | |
@task | |
def foo(): | |
run("echo here i am") | |
def main(): | |
dispatch.dispatchme() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment