1

我搜索了一个简单的脚本,它在提交之前检查源代码。

我想拒绝所有包含“logging.info(...)”的文件,因为我们现在想使用这个模式:

import logging
logger=logging.getLogger(__name__)
del(logging)

#...
logger.info()
4

1 回答 1

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()
于 2013-01-17T14:19:01.737 回答