Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVN pre-commit hook to reject Python files with inconsistent tab usage

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.

like image 839
Catalin Iacob Avatar asked Feb 28 '23 12:02

Catalin Iacob


1 Answers

You can do this using the py_compile module:

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

The doraise=True will raise an exception and return with a nonzero exit code that you can easily test in your pre-commit hook.

like image 192
Greg Hewgill Avatar answered Apr 08 '23 12:04

Greg Hewgill