为什么Python项目中没有用于自动化的makefile?

2024-05-20 20:45:37 发布

您现在位置:Python中文网/ 问答频道 /正文

作为一名长期从事Python编程的程序员,我想知道,如果Python文化的一个核心方面在很长一段时间里没有出现:我们应该做什么来代替makefile?

我见过的大多数ruby项目(不仅仅是rails)使用Rake,在node.js流行之后不久,就有了cake。在许多其他(编译和非编译)语言中,都有经典的Make文件。

但在Python中,似乎没有人需要这样的基础设施。我随机选择了GitHub上的Python项目,除了安装之外,它们没有自动化,由setup.py提供。

这背后的原因是什么?

没有什么可以自动化的吗?大多数程序员喜欢手动运行样式检查、测试等吗?

一些例子:

  • dependencies设置virtualenv并安装依赖项
  • check调用pep8pylint命令行工具。
  • test任务依赖于dependencies启用virtualenv,启动selenium服务器进行集成测试,并调用nosetest
  • 任务coffeescript将所有咖啡脚本编译为小型javascript
  • runserver任务依赖于dependenciescoffeescript
  • deploy任务依赖于checktest并部署项目。
  • 任务使用适当的参数调用sphinx

其中有些只是一两句台词,但总的来说是。由于Makefile,我不需要记住它们。

要澄清的是:我不是在为Rake寻找一个Python等价物。我很喜欢摊铺机。我在找原因。


Tags: 项目test核心virtualenvcheck编程dependencies原因
3条回答

实际上,自动化对Python开发人员也很有用!

Invoke可能是最接近您所想的工具,用于自动化常见的重复Python任务:https://github.com/pyinvoke/invoke

使用invoke,您可以创建这样的tasks.py(从invoke文档中借用)

from invoke import run, task

@task
def clean(docs=False, bytecode=False, extra=''):
    patterns = ['build']
    if docs:
        patterns.append('docs/_build')
    if bytecode:
        patterns.append('**/*.pyc')
    if extra:
        patterns.append(extra)
    for pattern in patterns:
        run("rm -rf %s" % pattern)

@task
def build(docs=False):
    run("python setup.py build")
    if docs:
        run("sphinx-build docs docs/_build")

然后可以在命令行运行任务,例如:

$ invoke clean
$ invoke build --docs

另一种选择是简单地使用Makefile。例如,Python项目的Makefile可能如下所示:

docs:
    $(MAKE) -C docs clean
    $(MAKE) -C docs html
    open docs/_build/html/index.html

release: clean
    python setup.py sdist upload

sdist: clean
    python setup.py sdist
    ls -l dist

^{}可以自动处理很多事情,对于不是内置的事情,它很容易扩展。

  • 要运行unittests,可以在向setup()调用添加test_suite参数后使用setup.py test命令。(documentation
  • 依赖项(即使在PyPI上不可用)可以通过向install_requires/extras_require/dependency_links调用添加setup()参数来处理。(documentation
  • 要创建.deb包,可以使用^{}模块。
  • 对于其他一切,你可以add custom setup.py commands

但是我同意S.Lott,大多数您希望自动化的任务(除了依赖项处理,这可能是我发现唯一真正有用的任务)都不是每天都运行的任务,因此通过自动化它们不会有任何真正的生产力改进。

Is there nothing to automate?

不是真的。除了两个例子之外,所有的例子都是单行命令。

tl;dr很少有真正有趣或复杂的事情。这些似乎很少从“自动化”中受益。

由于文档的原因,我不需要记住执行此操作的命令。

Do most programmers prefer to run stylechecks, tests, etc. manually?

是的。

generation documentation, the docs task calls sphinx with the appropiate arguments

这是一行代码。自动化没有多大帮助。 sphinx-build -b html source build/html。那是剧本。用Python编写。

我们很少这样做。一周几次。在“重大”变化之后。

running stylechecks (Pylint, Pyflakes and the pep8-cmdtool). check calls the pep8 and pylint commandlinetools

我们不会这么做的。我们使用单元测试而不是pylint。 你可以把这三步过程自动化。

但我知道SCons或make如何帮助这里的人。

tests

这里可能有“自动化”的空间。它有两行:非Django单元测试(python test/main.py)和Django测试。(manage.py test)。自动化可以应用于运行两条线路。

我们每天做几十次。我们从来不知道我们需要“自动化”。

dependecies sets up a virtualenv and installs the dependencies

很少这样做,以至于我们只需要一个简单的步骤列表。我们非常,非常仔细地跟踪我们的依赖关系,所以从来没有任何意外。

我们不会这么做的。

the test task depends on dependencies enables the virtualenv, starts selenium-server for the integration tests, and calls nosetest

start server & run nosetest作为两步“自动化”有一定的意义。这样可以避免您输入两个shell命令来运行这两个步骤。

the coffeescript task compiles all coffeescripts to minified javascript

这对我们来说是很罕见的。我想这是自动化的一个很好的例子。自动化单行脚本可能会有帮助。

我知道SCons或make如何帮助这里的人。

the runserver task depends on dependencies and coffeescript

除了。依赖关系很少改变,所以这看起来有点过头了。我想这可能是一个好主意,你没有跟踪依赖关系在一开始。

the deploy task depends on check and test and deploys the project.

它是服务器上的一个svn copython setup.py install,然后是从subversion区域到客户/www区域的一组客户特定副本。那是剧本。用Python编写。

这不是一般的做工或烤饼之类的东西。它只有一个参与者(一个系统管理员)和一个用例。我们永远不会将部署与其他开发、QA或测试任务混为一谈。

相关问题 更多 >