SVN pre-commit hook to reject Python files with inconsistent tabs

The Python interpreter can be run with -ttto raise an exception TabErrorif the interpreted file has inconsistent tabs.

I am trying to write a pre-commit hook for SVN that rejects the files that raise this exception. I can transfer the file attached to python -tt, but my problem is that the file also executes, in addition to being checked. Is there a way to tell Python "just parse the file, don't run it"? Or maybe some other approach would be better for doing what I want.

+3
source share
2 answers

You can do this using the module py_compile:

$ python -tt -c "import py_compile; py_compile.compile('test.py', doraise=True)"

doraise=True will throw an exception and return with a non-zero exit code, which you can easily test in your prefix.

+5
source

Python's preferred use of tabs doesn't use tabs at all (use four spaces for indentation). If this is your coding style, the problem can be reduced to check for any tabs in the code. And this can easily be done with a simple regular expression, even with "grep", so there is no need to run the interpreter.

The py_compile method has other advantages: it also checks the syntax of Python code, which may be desirable (although it does cost a little computing power from the SVN server).

+2

Source: https://habr.com/ru/post/1734534/


All Articles