這看起來像一個非常基本的問題,但我很難理解它,並希望得到任何幫助。如何從python中的另一個函數內調用一個函數
我希望能夠從 verify_commit_text
呼叫CheckForJiraIssueRecord
。
這裏是代碼:當我運行它,我得到錯誤:
jira ticket regex matched! printing m.group(1) QA-65 my_args ... QA-65 transaction abort! rollback completed abort: pretxncommit.jira hook exited with status 1
含義,即CheckForJiraIssueRecord(my_args)只是沒有得到所謂的
#!/usr/bin/env python import re, os, sys, jira, subprocess def verify_commit_text(tags): for line in tags: if re.match('^NO-TIK',line): return True elif re.match('^NO-REVIEW', line): return True elif re.match(r'[a-zA-Z]+-\d+', line): # Validate the JIRA ID print 'jira ticket regex matched!' m = re.search("([a-zA-Z]+-\d+)",line) print 'printing m.group(1)' print m.group(1) my_args = m.group(1) print 'my_args ...' print my_args result = CheckForJiraIssueRecord(my_args) print 'printing result....' print result if result == False: #util.warn("%s does not exist"%my_args) print 'result = False.......' else: print 'if result == False return True' return True return True else: return False def CheckForJiraIssueRecord(object): sys.stdout = os.devnull sys.stderr = os.devnull try: com = jira.Commands() logger = jira.setupLogging() jira_env = {'home':os.environ['HOME']} command_cat= "cat" command_logout= "logout" #my_args = ["QA-656"] server = "http://jira.myserver.com:8080/rpc/soap/jirasoapservice-v2?wsdl" except Exception, e: sys.exit('config error') if __name__ == '__main__': commit_text_verified = verify_commit_text(os.popen('hg tip --template "{desc}"')) #commit_text_verified = verify_commit_text(os.popen('hg log -r $1 --template "{desc}"')) if (commit_text_verified): sys.exit(0) else: print >> sys.stderr, ('[obey the rules!]') sys.exit(1); class Options: pass options = Options() options.user = 'username' options.password = 'password' try: jira.soap = jira.Client(server) jira.start_login(options, jira_env, command_cat, com, logger) issue = com.run(command_cat, logger, jira_env, my_args) except Exception, e: print sys.exit('data error')
「sys.stdout = os.devnull」意味着任何未來的打印語句都會消失無蹤。嘗試對這些行進行註釋並在CheckFor ...函數中添加幾條打印語句,並查看會發生什麼... –
順便說一句,您的密碼在Python代碼中...(options.password = ...) –
函數CheckForJiraIssueRecord不顯式返回任何內容。因此'None'將被返回,'None'!= False。順便說一句:請不要在你的代碼中發佈行號。 – GreenMatt