自动从SVN checkout代码到本地编译,然后,将其结果放到目标板上运行,并记录运行结果。所涉及的技术有,Telnet、ftp、thread、socket。
-
#!/usr/bin/python
-
# import getpass
-
import os
-
import sys
-
import telnetlib
-
from ftplib import FTP
-
import time
-
from threading import Thread
-
import socket
-
import struct
-
-
svnServer = '' # ip of svn server to pull project
-
compileDir = '' # direntory of local host to compile project
-
execHost = '192.168.1.' # ip of pc to execute project
-
execPath = ''
-
execUser = 'root' # username
-
configPort = 3038 # port number
-
configFile = 'ss7lm.txt'
-
-
def checkout():
-
try:
-
os.mkdir(compileDir)
-
except OSError:
-
print '%s has already exist!'%compileDir
-
-
os.system('svn checkout '+svnServer+' '+compileDir)
-
os.chdir(compileDir)
-
os.system('make clean; make -f ppcmakefile')
-
os.chdir('..')
-
-
def execOnTarget(cmdlist, interactFlg=0):
-
tn = telnetlib.Telnet()
-
try:
-
tn.open(execHost)
-
except :
-
print 'Cannot telnet to %s'%execHost
-
sys.exit()
-
-
tn.read_until("(none) login")
-
tn.write(execUser+"\n")
-
for cmd in cmdlist:
-
try:
-
tn.read_until("# ")
-
tn.write('%s\n'%cmd)
-
except Exception, e:
-
print 'Error when execute command %s'%cmdlist
-
tn.close()
-
return
-
if interactFlg == 1:
-
try:
-
tn.interact()
-
except KeyboardInterrupt:
-
tn.close()
-
cmd_list2 = []
-
cmd_list2.append('killall -9 ss7gw')
-
execOnTarget(execHost, cmd_list2)
-
print 'Cancelled'
-
else:
-
tn.read_until('# ')
-
tn.close()
-
-
def transfer():
-
try:
-
ftp = FTP(execHost)
-
except Exception, e:
-
print 'cannot establish FTP to %s '%execHost
-
sys.exit()
-
-
ftp.login(execUser)
-
ftp.getwelcome()
-
ftp.cwd(execPath)
-
try:
-
fd1 = open('%s/ss7gw '%compileDir, 'rb')
-
except Exception:
-
print 'cannot open the source file'
-
ftp.close()
-
return
-
-
try:
-
ftp.storbinary('STOR ss7gw', fd1)
-
except:
-
print 'perhaps another ss7gw instance is running on %s!'%execHost
-
finally:
-
fd1.close()
-
ftp.close()
-
-
def execute():
-
ti = time.strftime("%m%d%H%M%S")
-
cmd_list = []
-
cmdlist.append('cd %s; chmod 777 ./ss7gw; ./ss7gw|tee /btswitch/'%execPath + ti + '.log')
-
execOnTarget(cmd_list, 1)
-
print ti+'.log'
-
-
def config():
-
time.sleep(3)
-
try:
-
f = open('%s'%configFile)
-
except :
-
print 'cannot open %s for configuration'%configFile
-
return
-
-
so = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP)
-
data = ''
-
for line in f:
-
if not line[0] is '#':
-
array = line.split()
-
for num in array:
-
data = data + struct.pack('B', int(num, 16))
-
so.sendto(data, 0, ('%s'%execHost, configPort))
-
data = ''
-
-
f.close()
-
so.close()
-
-
def start():
-
global execHost
-
global configFile
-
-
cs = raw_input('Do you wanna checkout SVN and compile it? :')
-
if cs == '':
-
checkout()
-
print ''
-
-
ip = raw_input('please finish the execHost IP: %s'%execHost)
-
try:
-
if int(ip)<1 or int(ip)>255:
-
print 'IP you input is wrong!'
-
return
-
except:
-
return
-
else:
-
execHost = '%s%s'%(execHost, ip)
-
configFile = '%s%s'%(ip, configFile)
-
-
cm = raw_input('please select a opt(1: refresh, 2:refresh and run, 3:just run, others:None) executed on %s:'%execHost)
-
try:
-
cm = int(cm)
-
except :
-
return
-
-
if cm == 1 or cm == 2:
-
transfer()
-
-
if cm == 2 or cm == 3:
-
th = Thread(None, config)
-
th.start()
-
execute()
-
print ''
-
-
if __name__ == '__main__':
-
start()
阅读(2599) | 评论(0) | 转发(0) |