Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Last active March 15, 2020 08:26
Show Gist options
  • Save eliasdorneles/5c82cef5d2b16877996a10f12ccc4e7a to your computer and use it in GitHub Desktop.
Save eliasdorneles/5c82cef5d2b16877996a10f12ccc4e7a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Cookiecutter PyPackage development tasks
"""
# To add a task, create a function with name starting with 'do_' that
# receives one argument (the parsed cmdline arguments object).
# Use a short docstring to serve as help.
#
# Example:
#
# def do_clean(args):
# run('rm -rf build/')
from __future__ import print_function
import argparse
import inspect
import shlex
import subprocess
import sys
def run(cmd):
return subprocess.check_output(shlex.split(cmd))
def do_help(args):
"print this help"
for name, func in sorted(TASKS.items()):
print('%-20s %s' % (name, func.__doc__))
def do_bake(args):
"generate project using defaults"
run('cookiecutter --no-input . --overwrite-if-exists')
def find_tasks(module):
"""Find all tasks in given module.
Tasks are just functions whose names start with 'do_'"""
return {
name[3:]: func
for name, func in inspect.getmembers(module)
if name.startswith('do_')
}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('task', nargs='?',
default='help',
choices=tuple(TASKS),
help='Which task to run')
args = parser.parse_args()
# run task
TASKS[args.task](args)
TASKS = find_tasks(sys.modules[__name__])
if '__main__' == __name__:
main()
@eliasdorneles
Copy link
Author

Sample runs:

$ python manage.py 
bake                 generate project using defaults
help                 print this help
$ python manage.py help
bake                 generate project using defaults
help                 print this help
$ python manage.py bake
$ ls python_boilerplate/
AUTHORS.rst       docs/         LICENSE*  MANIFEST.in          README.rst            setup.cfg  tests/   travis_pypi_setup.py
CONTRIBUTING.rst  HISTORY.rst*  Makefile  python_boilerplate/  requirements_dev.txt  setup.py   tox.ini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment