2012-12-29 32 views
2

我在將check_call語句轉換爲subprocess.Popen時遇到了下面的錯誤,我搞亂了「& &」我想,任何人都可以幫助解決這個問題嗎?將check_call語句轉換爲子進程.Popen

check_call("git fetch ssh://[email protected]:29418/platform/vendor/company-proprietary/radio %s && git cherry-pick FETCH_HEAD" % change_ref , shell=True) 
proc = subprocess.Popen(['git', 'fetch', 'ssh://[email protected]:29418/platform/vendor/company-proprietary/radio', change_ref , '&& git' , 'cherry-pick', 'FETCH_HEAD'], stderr=subprocess.PIPE) 

Error:- 

fatal: Invalid refspec '&& git 

回答

1
rc = Popen("cmd1 && cmd2", shell=True).wait() 
if rc != 0: 
    raise Error(rc) 

或者

rc = Popen(["git", "fetch", "ssh://...", change_ref]).wait() 
if rc != 0: 
    raise Error("git fetch failed: %d" % rc) 
rc = Popen("git cherry-pick FETCH_HEAD".split()).wait() 
if rc != 0: 
    raise Error("git cherry-pick failed: %d" % rc) 

要捕獲stderr

​​
+0

@塞巴斯蒂安 - 太棒了,你是對的..馬丁代碼沒有工作......你的作品馬上......感謝很多 – user1934146

+0

@sebastian - 其中一個問題實際上..當fetch或cherrypick失敗時,我不想打印stderr在控制檯上,或者相反引發一個錯誤..我想捕獲和解析錯誤,並根據錯誤信息做一些操作..如何在不改變現有功能的情況下更改上述內容? – user1934146

+1

@ user1934146:我添加了'stderr'捕獲 – jfs

1

&&是一個shell功能。單獨運行命令:

proc = subprocess.Popen(['git', 'fetch', 'ssh://[email protected]:29418/platform/vendor/company-proprietary/radio', change_ref], stderr=subprocess.PIPE) 
out, err = proc.communicate() # Wait for completion, capture stderr 

# Optional: test if there were no errors 
if not proc.returncode: 
    proc = subprocess.Popen(['git' , 'cherry-pick', 'FETCH_HEAD'], stderr=subprocess.PIPE) 
    out, err = proc.communicate() 
+0

任何機會,我們可以會在一起? – user1934146

+0

這是錯誤的。 'Popen()'是異步的,不會像原始命令那樣等待'git fetch'完成。 – jfs

+0

@ J.F.Sebastian:我沒有機會完成任何事情;被叫走了。 'proc.communicate()'在這裏是缺少的,因此中間有'#...'。 –