Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32485
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 117
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-21 17:50
个人简介

程序员中的一员。 另一博客地址 http://tech-note.github.io/

文章分类

全部博文(3)

文章存档

2013年(3)

我的朋友

分类: Python/Ruby

2013-10-24 12:55:50

自动从SVN checkout代码到本地编译,然后,将其结果放到目标板上运行,并记录运行结果。所涉及的技术有,Telnet、ftp、thread、socket。

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # import getpass
  3. import os
  4. import sys
  5. import telnetlib
  6. from ftplib import FTP
  7. import time
  8. from threading import Thread
  9. import socket
  10. import struct

  11. svnServer = '' # ip of svn server to pull project
  12. compileDir = '' # direntory of local host to compile project
  13. execHost = '192.168.1.' # ip of pc to execute project
  14. execPath = ''
  15. execUser = 'root' # username
  16. configPort = 3038 # port number
  17. configFile = 'ss7lm.txt'

  18. def checkout():
  19.     try:
  20.         os.mkdir(compileDir)
  21.     except OSError:
  22.         print '%s has already exist!'%compileDir
  23.     
  24.     os.system('svn checkout '+svnServer+' '+compileDir)
  25.     os.chdir(compileDir)
  26.     os.system('make clean; make -f ppcmakefile')
  27.     os.chdir('..')

  28. def execOnTarget(cmdlist, interactFlg=0):
  29.     tn = telnetlib.Telnet()
  30.     try:
  31.         tn.open(execHost)
  32.     except :
  33.         print 'Cannot telnet to %s'%execHost
  34.         sys.exit()

  35.     tn.read_until("(none) login")
  36.     tn.write(execUser+"\n")
  37.     for cmd in cmdlist:
  38.         try:
  39.             tn.read_until("# ")
  40.             tn.write('%s\n'%cmd)
  41.         except Exception, e:
  42.             print 'Error when execute command %s'%cmdlist
  43.             tn.close()
  44.             return
  45.         if interactFlg == 1:
  46.             try:
  47.                 tn.interact()
  48.             except KeyboardInterrupt:
  49.                 tn.close()
  50.                 cmd_list2 = []
  51.                 cmd_list2.append('killall -9 ss7gw')
  52.                 execOnTarget(execHost, cmd_list2)
  53.                 print 'Cancelled'
  54.         else:
  55.             tn.read_until('# ')
  56.             tn.close()

  57. def transfer():
  58.     try:
  59.         ftp = FTP(execHost)
  60.     except Exception, e:
  61.         print 'cannot establish FTP to %s '%execHost
  62.         sys.exit()

  63.     ftp.login(execUser)
  64.     ftp.getwelcome()
  65.     ftp.cwd(execPath)
  66.     try:
  67.         fd1 = open('%s/ss7gw '%compileDir, 'rb')
  68.     except Exception:
  69.         print 'cannot open the source file'
  70.         ftp.close()
  71.         return

  72.     try:
  73.         ftp.storbinary('STOR ss7gw', fd1)
  74.     except:
  75.         print 'perhaps another ss7gw instance is running on %s!'%execHost
  76.     finally:
  77.         fd1.close()
  78.         ftp.close()

  79. def execute():
  80.     ti = time.strftime("%m%d%H%M%S")
  81.     cmd_list = []
  82.     cmdlist.append('cd %s; chmod 777 ./ss7gw; ./ss7gw|tee /btswitch/'%execPath + ti + '.log')
  83.     execOnTarget(cmd_list, 1)
  84.     print ti+'.log'

  85. def config():
  86.     time.sleep(3)
  87.     try:
  88.         f = open('%s'%configFile)
  89.     except :
  90.         print 'cannot open %s for configuration'%configFile
  91.         return

  92.     so = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP)
  93.     data = ''
  94.     for line in f:
  95.         if not line[0] is '#':
  96.             array = line.split()
  97.             for num in array:
  98.                 data = data + struct.pack('B', int(num, 16))
  99.             so.sendto(data, 0, ('%s'%execHost, configPort))
  100.             data = ''

  101.     f.close()
  102.     so.close()

  103. def start():
  104.     global execHost
  105.     global configFile

  106.     cs = raw_input('Do you wanna checkout SVN and compile it? :')
  107.     if cs == '':
  108.         checkout()
  109.     print ''

  110.     ip = raw_input('please finish the execHost IP: %s'%execHost)
  111.     try:
  112.         if int(ip)<1 or int(ip)>255:
  113.             print 'IP you input is wrong!'
  114.             return
  115.     except:
  116.         return
  117.     else:
  118.         execHost = '%s%s'%(execHost, ip)
  119.         configFile = '%s%s'%(ip, configFile)

  120.     cm = raw_input('please select a opt(1: refresh, 2:refresh and run, 3:just run, others:None) executed on %s:'%execHost)
  121.     try:
  122.         cm = int(cm)
  123.     except :
  124.         return

  125.     if cm == 1 or cm == 2:
  126.         transfer()

  127.     if cm == 2 or cm == 3:
  128.         th = Thread(None, config)
  129.         th.start()
  130.         execute()
  131.     print ''

  132. if __name__ == '__main__':
  133.     start()

阅读(2599) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~