首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVN: Python预提交脚本总是失败

SVN: Python预提交脚本总是失败
EN

Stack Overflow用户
提问于 2011-05-25 01:56:15
回答 1查看 2.1K关注 0票数 2

我找到了一个示例python脚本,并对它进行了修改,以检查预提交中的注释。我的问题是,python解析的注释文本总是为空白。

14:54:47

  • Python
  • Env: Windows XP
  • SVN版本: svn,版本1.5.6 (r36142),2009年3月6日编译, 2.7

pre-commit.bat

代码语言:javascript
复制
C:\Python27\python %1\hooks\pre-commit.py %1 %2

pre-commit.py

代码语言:javascript
复制
import sys, os, string, re

SVNLOOK='C:\\SVN\\bin\\svnlook.exe'


# return true or false if this passed string is a valid comment
def check(comment):
    #define regular expression
    print comment
    p = re.match("[bB][uU][gG]\s([0-9]+|NONE)+", comment)
    print p
    return (p != None) #returns false if doesn't match

def result(r, txn, repos, log_msg, log_cmd):
    if r == 1:
        sys.stderr.write ("File: " + repos + " Comment: " + txn + "\n" +\
                          "Log Msg: " + log_msg + "\n" +\
                          "Log Cmd: " + log_cmd + "\n" +\
                            "Comments must have the format of \n'Bug X' \n" +\
                          "'Comment text' where X is the issue number.\n" +\
                          "Use comma to separatemultiple bug id's\n" +\
                          "Example:\nBug 1234, 1235\nComment: Fixed things\n" +\
                          "Use 'NONE' if there is no bug ID")
        sys.exit(r)

def main(repos, txn):
    log_cmd = '%s log %s -t %s' % (SVNLOOK, txn, repos)
    log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n')

    if check(log_msg):
        result(0, txn, repos, log_msg, log_cmd)
    else:
        result(1, txn, repos, log_msg, log_cmd)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        sys.stderr.write("Usage: %s REPOS TXN\n" % (sys.argv[0]))
    else:
        main(sys.argv[1], sys.argv[2])

为了调试目的,我在错误消息中添加了vars的打印输出。

最重要的是,如果我使用bat文件命令,事情似乎会很好:

that,它只检查空提交消息:

代码语言:javascript
复制
@echo off  
:: Stops commits that have empty log messages.        
@echo off  

setlocal  

rem Subversion sends through the path to the repository and transaction id  
set REPOS=%1  
set TXN=%2           


rem line below ensures at least one character ".", 5 characters require change to "....."
C:\SVN\bin\svnlook.exe log %REPOS% -t %TXN% | findstr . > nul  
if %errorlevel% gtr 0 (goto err) else exit 0  

:err  
echo. 1>&2  
echo Your commit has been blocked because you didn't enter a comment. 1>&2  
echo Write a log message describing the changes made and try again. 1>&2
echo Thanks 1>&2
exit 1

我做错什么了?

EN

回答 1

Stack Overflow用户

发布于 2011-05-25 03:08:12

当您通过Python运行它时,Python设置的错误代码将在批处理文件中被忽略;您需要这样做:

代码语言:javascript
复制
C:\Python27\python %1\hooks\pre-commit.py %1 %2
exit %ERRORLEVEL%

引用你的金库也是个好主意,以确保它们完好无损地通过Python:

代码语言:javascript
复制
C:\Python27\python "%1\hooks\pre-commit.py" "%1" "%2"
exit %ERRORLEVEL%
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6118739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档