命令行调用SHELL
# -*- coding: utf-8 -*-
#!/usr/local/bin/python
import os, sys,time,datetime,time,subprocess,pdb,fnmatch
from threading import Thread
def commandline_invoke(command_list):
#stdin_value ="./" + command + " ".join(parameter_list)
stdin_value = command_list
child1 = subprocess.Popen(stdin_value, stdout=subprocess.PIPE)
#print "ExcuteCommand: " + " ".join(stdin_value)
#print "ExcuteResult: "
while True :
line = child1.stdout.readline()
if not line : break
print line
#这个方法在处理大量的返回的数据的时候,会出现异常。还是得用readline方式来操作。所以现在不建议用这种方式
def commandline_invoke_pipline_sunset(command_list):
#stdin_value ="./" + command + " ".join(parameter_list)
stdin_value = command_list
res = subprocess.check_output(stdin_value, shell=True)
#print "ExcuteCommand: " + " ".join(stdin_value)
#print "ExcuteResult: "
line = res.decode()
return line
def commandline_invoke_pipline(command_str):
stdin_value = command_str
child1 = subprocess.Popen(stdin_value, stdout=subprocess.PIPE,shell=True)
while True :
line = child1.stdout.readline()
if not line : break
print line
def get_dst_files(root,patterns='*' , single_level=False ,yieild_folders= False):
#将模式从字符串中取出放入列表中
patterns = patterns.split(';')
for path ,subdirs ,files in os.walk(root):
if yieild_folders :
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name,pattern):
yield os.path.join(path,name)
break
if single_level:
break
def unzipfile(fileName):
#copy
print "copy fileName " + fileName
commandline_invoke(['cp','/Users/wenlezhou/Desktop/mk/source/' + fileName, '/Users/wenlezhou/Desktop/mk/dst/'])
#unzip
print "gunzip fileName " + fileName
commandline_invoke(['gunzip' , "./dst/" + fileName])
#word count status all
#pipline command line
str1 = './dst/' + fileName[:-3] + '|grep '
str2 = "'plugin=102'|grep "
str3 = "'V+8.0.0.1016.r403165' "
str4 = '''|wc'''
print 'cat '+ str1+ str2+str3+str4
wc = commandline_invoke_pipline('cat '+ str1+ str2+str3+str4)
word_count = fileName[:-3] +'\t' + wc
#write the result to file
fp = open("test_result_all","aw")
fp.writelines(word_count)
fp.flush()
fp.close()
#del file
delFileName = './dst/' + fileName[:-3]
print delFileName
commandline_invoke(['rm',delFileName])
def get_all_file_2():
fileList = []
search_path = './source/'
for path in get_dst_files(search_path,'*.gz',True):
fileList.append(path)
return fileList
def test_demo_cat_file_name_and_word_count():
fileNameList = get_all_file_2()
for fileName in fileNameList :
unzipfile(fileName[9:])
def test_pipline_command():
#test command line :find ./dst/* | xargs grep "V+8.0.0.1016.r403165"
commandLineDemo = 'find ./dst/* | xargs grep "V+8.0.0.1016.r403165"'
commandline_invoke_pipline(commandLineDemo)
def test_normal_command():
#test normal commandline : find ./dst/* -exec grep "V+8.0.0.1016.r403165" {} \;
commandLineDemo = 'find ./dst/* -exec grep "V+8.0.0.1016.r403165" {} \;'
commandline_invoke_pipline(commandLineDemo)
if __name__ == '__main__':
#test_pipline_command()
test_normal_command()