我搜索一个简单的脚本,在提交之前检查源代码。
我想拒绝所有包含“logging.info(...)”的文件,因为我们现在想要使用这个模式:
import logging
logger=logging.getLogger(__name__)
del(logging)
#...
logger.info()
答案 0 :(得分:0)
由于我找不到解决方案,我自己写了:
#!/usr/bin/env python
# scp svn@svnserver:REPOSITORY/hooks/
# ssh svn@svnserver chmod a+rx REPOSITORY/hooks/pre-commit
# Above chmod is important!
import re, sys, subprocess
sys.stdout=sys.stderr
svnlookPath = 'svnlook'
not_allowed=[
(re.compile(r'^\s*logging\.(error|info|debug).*'), u'use logger!'),
]
def main():
changed=[]
# sys.argv ['/svn/modwork/hooks/pre-commit', '/svn/modwork', '4508-bm']
pipe = subprocess.Popen([svnlookPath, 'changed', sys.argv[1], '--transaction', sys.argv[2]], stdout=subprocess.PIPE)
for line in pipe.stdout:
change_type, fn = line.split(' ', 1)
if not change_type[0] in 'AUC':
continue # skip, not a source code change: delete or meta data update.
changed.append(fn.strip())
ret=pipe.wait()
if ret:
sys.exit(ret)
for fn in changed:
if not fn.endswith('.py'):
continue
pipe = subprocess.Popen([svnlookPath, 'cat', sys.argv[1], '--transaction', sys.argv[2], fn], stdout=subprocess.PIPE)
for line in pipe.stdout:
for regex, help in not_allowed:
if regex.match(line):
print 'Content in file %s is not allowed: %s: %r' % (fn, help, line.strip())
sys.exit(1)
#print 'OK', fn, repr(line)
ret=pipe.wait()
if ret:
sys.exit(ret)
if __name__=='__main__':
main()