Last active
March 15, 2020 08:26
-
-
Save eliasdorneles/5c82cef5d2b16877996a10f12ccc4e7a 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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample runs: