基本命令
东拼西凑查了好久, 才总算把这个命令补完全. 不废话, 直接上代码.
def exec_command(cmdlist,working_dir,view_result=False):
if isinstance(cmdlist, str):
cmdlist = [cmdlist]
cmd_seperator = ";"
import platform
if platform.system() == "Windows":
cmd_seperator = "&&"
runCmd=""
for cmd in cmdlist:
if runCmd=="":
runCmd = runCmd + cmd
else:
runCmd = runCmd + cmd_seperator + cmd
p=subprocess.Popen(runCmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE,cwd=working_dir)
out,err = p.communicate()
result = {'out':out,'err':err,'returncode':str(p.returncode),'pid':str(p.pid)}
if view_result:
print "===== Printed result: "
print "--> Run cmd: " + runCmd
print "--> returncode: " + result["returncode"]
print "--> out: " + result["out"]
print "--> err: " + result["err"]
return result
exec_command(["ls -l","uname -a"],".",True)
扩展功能1: CVS 操作
这个功能无甚神秘. 首先要安装有cvs, 其次要设置cvsroot. 最后, 你要时不时查查cvs的命令.
我承认拼凑这个程序还是费了不少时间, 所以有记下来的必要.
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Apr 28, 2011
@author: yunshichen@gmail.com
'''
import string
import subprocess
import platform
def exec_command(cmdlist,working_dir,view_result=False):
if isinstance(cmdlist, str):
cmdlist = [cmdlist]
cmd_seperator = ";"
if platform.system() == "Windows":
cmd_seperator = "&&"
runCmd=""
for cmd in cmdlist:
if runCmd=="":
runCmd = runCmd + cmd
else:
runCmd = runCmd + cmd_seperator + cmd
p=subprocess.Popen(runCmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE,cwd=working_dir)
out,err = p.communicate()
result = {'out':out,'err':err,'returncode':str(p.returncode),'pid':str(p.pid)}
if view_result:
print "===== Printed result: "
print "--> Run cmd: " + runCmd
print "--> returncode: " + result["returncode"]
print "--> out: " + result["out"]
print "--> err: " + result["err"]
return result
class DPY_CVS():
def __init__(self,cvs_cmd,cvs_root,is_debug=True):
self.cvs_cmd = cvs_cmd
self.cvs_root = cvs_root
def checkout(self,working_dir,proj,opts=None):
'''
opts holds optional for checkout command. For example:
{'r': diego_test_tag,'d':my_local_path}
'''
cmd_set_cvs_root = "set CVSROOT=" + self.cvs_root
cmd_checkout = self.cvs_cmd + ' -q checkout ${optional_str} -P '+proj
str=""
if(opts <> None):
for key in opts:
str = str + " -"+key + " " + opts[key]
cmd_checkout = string.Template(cmd_checkout).substitute({'optional_str':str})
self.result = exec_command([cmd_set_cvs_root,cmd_checkout],working_dir)
def tagFiles(self,working_dir,tagName,pathlist):
cmd_set_cvs_root = "set CVSROOT=" + self.cvs_root
cmd_tag_a_file = self.cvs_cmd + ' tag '+tagName+' ${cvs_file}'
if isinstance(pathlist, str):
pathlist = [pathlist]
cmdlist = [cmd_set_cvs_root]
for fpath in pathlist:
if fpath.startswith("/"):
fpath = fpath[1:]
cmd_tag = string.Template(cmd_tag_a_file).substitute({'cvs_file':fpath})
cmdlist.append(cmd_tag)
self.result = exec_command(cmdlist,working_dir)
def viewResult(self):
r = self.result
print "\n\n\n------------------ CVS run result: -----------"
print "--->returncode: " + r["returncode"]
print "out:\n" + r["out"]
print "err:\n" + r["err"]