有没有Python的Cake等效工具?

7
我浏览了许多Python项目,但找不到像cake文件一样简单的项目。我正在寻找一个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()

我已经搜索了很多次,但没有找到类似的内容。如果不存在,我会自己制作,并可能用它来回答这个问题。

感谢您的帮助!


请查看http://farmdev.com/thoughts/46/the-python-make-tool/;有很多选项。 - Martijn Pieters
@MartijnPieters:是的,有很多构建工具选项,但它们要么已经死亡,要么过于复杂。我真的不需要比我提供的示例更多的东西,而且我甚至还没有找到一个Python的构建工具,即使有这么简单的选项。 - Hubro
不要看我,我每天都使用zc.buildout来完成所有的部署任务。虽然可能不是你所寻找的用例。 - Martijn Pieters
相关:paver - jfs
4个回答

4

自行构建简单的解决方案并不难:

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]]()

然后是一个小样例:

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()

使用时的效果如下:

> .\test.py
可用任务:
  print_hello_world:打印“Hello World”
  print_foo:打印“foo”
  print_both:同时打印两者
> .\test.py print_hello_world
正在执行任务print_hello_world。
Hello World!

我指的是 argparse 的子命令部分。我所想的是使用装饰器定义子命令的简便方式。 - Jonas Schäfer
+1 - 很好的回答,我已经为我的当前项目制作了类似的解决方案 :-) 我在想是否已经存在一个类似的解决方案,可能具有更多的功能,在我把我的解决方案转化为可重用的包之前,这是一个麻烦。使用函数名称和文档字符串是个不错的点子! - Hubro
我宣布这是正确的答案。这是因为fabric库实际上非常庞大,不仅是我正在寻找的一个小型构建工具。 - Hubro

3

您是否考虑过使用fabric

如果要使用它来实现您的示例,您只需要将以下内容添加到名为fabfile.py的文件中:

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

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

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

def build():
    ''' Build everything buildable '''
    build_cscript()
    build_stylus()

接下来你只需要运行fab build来构建。你可以运行fab -l来查看可用的命令以及它们的描述。

值得一提的是,Fabric还提供了其他一些功能,你可能会(或者不会)发现它们很有用。除了其他功能外,它还具有一些函数,可以帮助将文件部署到远程服务器,并且还可以通过ssh运行远程命令。由于你似乎正在开发一个基于Web的项目,所以你可能会发现这对于创建部署脚本或类似操作非常有用。


2
有趣的是,有一个名为Cake的Python构建工具使用的语法与您的示例几乎相同。在这里可以看到它:here

哇,那真的...让人非常困惑 :S - Hubro

0
我会创建一个标准的 Makefile,而不是寻找某些特定语言的东西。在我的一些项目中,make dbmake test 等等映射到用 Python 编写的脚本,但也可以很容易地使用任何可从命令行执行的语言编写。

我投票将此答案标记为“无用”,因为它没有解决我问题中的最重要问题:将所有构建代码保存在一个文件中。 - Hubro
好的。我很好奇你想要一个单一文件的动机是什么,但如果这是一个优先事项,那就是一个优先事项。Fabric确实是这样做的一个不错的选择。 - JoshMock

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接