使用正则表达式校验svn日志

by kevin 31. 八月 2019 20:08 >

最近想要对svn提交的日志进行管理。

考虑到使用bat命令进行正则校验感觉比较麻烦,而且灵活性也不好,所以选择python来实现。

 

首先添加svn钩子pre-submit

   1: Setlocal
   2: set sd=D:\Repositories\hooks
   3: set py="D:\Program Files\Python37\python" %sd%\pre-commit.py %1 %2
   4: for /F %%i in ('%py%') do (set r=%%i)
   5: echo %r% 1>&2
   6: if %r%==1 (
   7: exit 1
   8: ) else (
   9: exit 0
  10: )
  11: Endlocal

 

 

上面的代码调用了pre-commit.py,具体代码如下

   1: import sys, os, re
   2:  
   3: if(len(sys.argv)<3):
   4:     print(1)
   5:     exit()
   6:  
   7: repos = sys.argv[1]
   8: txn = sys.argv[2]
   9: svnserver = os.environ["VISUALSVN_SERVER"]
  10: svnlook = "\"" + svnserver + "bin\\svnlook.exe" + "\""
  11: logcmd = ("\"{0}bin\\svnlook.exe\" log \"{1}\" -t \"{2}\"").format(svnserver,  repos , txn)
  12: loghandler = os.popen(logcmd)
  13: log = loghandler.read()
  14: loghandler.close()    
  15: if(not log):
  16:     print(1)
  17:     exit()
  18:  
  19: mr = re.match("^\\d+\\s.*", log)
  20: if(mr == None):
  21:     print(1)
  22: else:
  23:     print(0)
  24: exit()
分享到: 更多