是否有Python的Cake等价物?

Hub*_*bro 8 python build-automation makefile

我已经完成了许多"make for Python"项目,但我找不到任何简单的蛋糕文件.我正在寻找的是一个Python等价物,它将让我:

  1. 将构建命令保存在项目根目录中的单个文件中
  2. 将每个任务定义为一个简单的函数,其中的描述将在没有参数的情况下运行"make"文件时自动显示
  3. 导入我的Python模块

我想象的是这样的:

from pymake import task, main

@task('reset_tables', 'Drop and recreate all MySQL tables')
def reset_tables():
    # ...

@task('build_stylus', 'Build the stylus files to public/css/*')
def build_stylus():
    from myproject import stylus_builder
    # ...

@task('build_cscript', 'Build the coffee-script files to public/js/*')
def build_cscript():
    # ...

@task('build', 'Build everything buildable')
def build():
    build_cscript()
    build_stylus()

# etc...

# Function that parses command line args etc...
main()
Run Code Online (Sandbox Code Playgroud)

我搜索过并搜索过但却没有找到它.如果它不存在,我会自己做,并可能用它回答这个问题.

谢谢你的帮助!

pok*_*oke 5

自己构建一个简单的解决方案并不难:

import sys

tasks = {}
def task (f):
    tasks[f.__name__] = f
    return f

def showHelp ():
    print('Available tasks:')
    for name, task in tasks.items():
        print('  {0}: {1}'.format(name, task.__doc__))

def main ():
    if len(sys.argv) < 2 or sys.argv[1] not in tasks:
        showHelp()
        return

    print('Executing task {0}.'.format(sys.argv[1]))
    tasks[sys.argv[1]]()
Run Code Online (Sandbox Code Playgroud)

然后是一个小样本:

from pymake import task, main

@task
def print_foo():
    '''Prints foo'''
    print('foo')

@task
def print_hello_world():
    '''Prints hello world'''
    print('Hello World!')

@task
def print_both():
    '''Prints both'''
    print_foo()
    print_hello_world()

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

以及使用时的样子:

> .\test.py
Available tasks:
  print_hello_world: Prints hello world
  print_foo: Prints foo
  print_both: Prints both
> .\test.py print_hello_world
Executing task print_hello_world.
Hello World!