SVN 预提交挂钩拒绝选项卡使用不一致的 Python 文件
如果解释文件的制表符使用不一致,则可以使用 -tt
启动 Python 解释器以引发 TabError
异常。
我正在尝试为 SVN 编写一个预提交挂钩,以拒绝引发此异常的文件。我可以传递提交给 python -tt 的文件,但我的问题是,除了检查之外,该文件也被执行。有没有办法告诉Python“只分析文件,不要运行它”?或者也许其他方法可以更好地实现我想要的目标。
The Python interpreter can be started with -tt
to raise a TabError
exception if the interpreted file has inconsistent tab usage.
I'm trying to write a pre-commit hook for SVN that rejects files that raise this exception. I can pass the file being committed to python -tt
but my problem is that the file is also executed, besides being checked. Is there a way to tell Python "just analyze the file, don't run it"? Or maybe some other approach would be better for accomplishing what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
py_compile
模块来执行此操作:doraise=True
将引发异常并返回一个非零退出代码,您可以在预提交挂钩中轻松测试该代码。You can do this using the
py_compile
module:The
doraise=True
will raise an exception and return with a nonzero exit code that you can easily test in your pre-commit hook.Python 中首选的制表符用法是根本不使用制表符(使用四个空格进行缩进)。如果这是您的编码风格,那么问题可能会减少到检查代码中是否有任何选项卡。这可以通过简单的正则表达式轻松完成,甚至可以使用“grep”,因此甚至不需要运行解释器。
不过,“py_compile”方式还有其他优点:它还检查 Python 代码语法,这可能是可取的(尽管会消耗 SVN 服务器的一些计算能力)。
The preferred tab usage in Python is no tab usage at all (use four spaces for indentation). If that is your coding style then the problem may be reduced to checking if there are any tabs in the code. And this can be easily done with simple regexp, even with 'grep', so there is no even need to run the interpreter.
The 'py_compile' way have other advantage, though: it also checks Python code syntax, which may be desirable (though costs a bit of computation power of the SVN server).