Python是否有一个蛋糕?

Python是否有一个蛋糕?,python,makefile,build-automation,Python,Makefile,Build Automation,我已经浏览了很多“makeforpython”项目,但是我找不到一个简单的。我要找的是一个Python等价物,它可以让我: 将生成命令保存在项目根目录中的单个文件中 将每个任务定义为一个简单的函数,其描述将在运行“make”文件时自动显示 导入我的Python模块 我在想象这样的事情: from pymake import task, main @task('reset_tables', 'Drop and recreate all MySQL tables') def reset_table

我已经浏览了很多“makeforpython”项目,但是我找不到一个简单的。我要找的是一个Python等价物,它可以让我:

  • 将生成命令保存在项目根目录中的单个文件中
  • 将每个任务定义为一个简单的函数,其描述将在运行“make”文件时自动显示
  • 导入我的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()
    
    我找了又找,但找不到类似的东西。如果它不存在,我会自己做,也许会用它来回答这个问题


    谢谢你的帮助

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

    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]]()
    
    以及使用时的外观:

    > .\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! >test.py 可用任务: 打印你好世界:打印你好世界 打印foo:打印foo 打印两个:打印两个 >.\test.py打印\u你好\u世界 正在执行任务打印\u hello\u world。 你好,世界!
    你考虑过使用它吗

    要使用它实现示例,只需将其添加到名为
    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的项目,所以您可能会发现这对于创建部署脚本或类似内容很有用。

    我只创建一个标准的Makefile,而不是查找特定于语言的内容。在我的一些项目中,
    make db
    make test
    ,等等。映射到用Python编写的脚本,但也可以很容易地映射到任何语言,这些语言的脚本可以从命令行执行。

    有趣的是,有一个名为Cake的Python构建工具,它使用的语法与您的示例几乎相同。看吧。

    看;有很多选择。@MartijnPieters:是的,有很多制作工具的选择,但它们要么都是死的,要么过于复杂和复杂。我真的不需要比我提供的例子更多的东西了,而且我还没有找到一个Python的构建工具,它甚至可以选择如此简单,不要看我;我使用zc.buildout(每日)完成所有部署任务。可能与您正在寻找的用例不同。相关:我指的是argparse的子命令部分。有一个很好的使用装饰器来定义子命令的方法是我的想法。+1-回答很好,我已经为我当前的项目做了一个类似的解决方案:-)我想知道,在我将我的解决方案变成一个可重用的包之前,是否已经存在类似的解决方案,可能还有更多的功能。不过,使用函数名和文档字符串的感觉很好!我宣布这是正确的答案。这是因为fabric库实际上相当庞大,而不仅仅是我正在寻找的一个小型构建工具。我认为这个答案没有用,因为它没有解决我问题中的#1问题:将所有构建代码保持在一个文件中足够公平。对你想要一个文件的动机很好奇,但如果这是一个优先事项,那就是一个优先事项。织物的确是一个很好的选择。哇,这是。。。令人难以置信的困惑:S
    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()