mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Add hook for checking python modules
This commit is contained in:
parent
76c604c9fb
commit
1722bd0ce2
5 changed files with 71 additions and 0 deletions
50
pre_commit_hooks/check_python_modules.py
Normal file
50
pre_commit_hooks/check_python_modules.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
|
||||
import os.path
|
||||
from argparse import ArgumentParser
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
from typing import Set
|
||||
|
||||
OK = 0
|
||||
ERR = 1
|
||||
|
||||
|
||||
class ModuleInitChecker:
|
||||
def __init__(self):
|
||||
# type: () -> None
|
||||
self.seen_dirnames = set() # type: Set[str]
|
||||
|
||||
def check(self, filename):
|
||||
# type: (str) -> int
|
||||
dirname = os.path.dirname(filename)
|
||||
if dirname in self.seen_dirnames:
|
||||
return OK
|
||||
|
||||
init_file = os.path.join(dirname, '__init__.py')
|
||||
if dirname and not os.path.exists(init_file):
|
||||
self.seen_dirnames.add(dirname)
|
||||
with open(init_file, 'w'):
|
||||
print(f'Created {init_file}')
|
||||
return ERR
|
||||
|
||||
return OK
|
||||
|
||||
|
||||
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
status = OK
|
||||
checker = ModuleInitChecker()
|
||||
for filename in args.filenames:
|
||||
status |= checker.check(filename)
|
||||
|
||||
return status
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue