python自动化测试脚本部署到linux_用Python编写脚本进行软件自动安装配置

测试机器:10.10.33.119;os:redhat linux4

测试路径:/home/sonky

【脚本1】:使用exec*()系列函数,在python里面,os.exec*()系列命令会完全代替python解释器,如果执行os.exec*()函数,则python解释器和脚本将在os.exec*()执行之后终止。如果如果想继续执行,必须

使用os.fork()函数来启动一个新子进程。【建立.py文件后,用chmod

a+u *.py命令修改文件属性后,./*.py可以运行脚本】

#! /usr/bin/env

python

import

os,glob

cmd = [

'/bin/tar',

'-x',

'-z',

'-f',

]

tar_file_list =

glob.glob('*.tar.gz')

for tar_file in

tar_file_list:

if os.fork():

os.wait()

else:

os.execv(cmd[0], cmd + [tar_file,])

print "install

MPICH2..."

configure =

['./configure','--prefix=/home/sonky/MPICH2']

make =

['/usr/bin/make']

install =

['/usr/bin/make','install']

os.chdir('/home/sonky/mpich2-1.0.6')

i=os.fork()

if i!=0:

os.wait()

n=os.fork()

if

n!=0:

os.wait()

os.execv(install[0],install)

else:

os.execv(make[0],make)

else:

os.execv(configure[0],configure)

【脚本2-mpich.py】:在linux(unix),可以使用os.spawn*()系列函数,该函数功能类似于os.exec*(),

但它们可以通过参数(P_WAIT,P_NOWAIT等)来实现是否继续执行还是终止脚本,其中sleep

3只是为了调试的时候暂停一下可以看到那一步已经完成。

#!/usr/bin/python

import os,glob

tar = [

'/bin/tar','-xzf',]

print “======Unzip source

file======”

tar_file_list =

glob.glob('*.tar.gz')

for tar_file in

tar_file_list:

os.spawnv(os.P_WAIT,tar[0], tar + [tar_file,])

print "======Install

MPICH======"

sleep =

['/bin/sleep','3']

os.spawnv(os.P_WAIT,sleep[0],sleep)

configure =

['./configure','--prefix=/home/sonky/MPICH2']

make =

['/usr/bin/make']

install =

['/usr/bin/make','install']

os.chdir('/home/sonky/mpich2-1.0.6')

os.spawnv(os.P_WAIT,configure[0],configure)

print "Step1:configure

done!"

os.spawnv(os.P_WAIT,sleep[0],sleep)

os.spawnv(os.P_WAIT,make[0],make)

print "Step2:make

done!"

os.spawnv(os.P_WAIT,sleep[0],sleep)

os.spawnv(os.P_WAIT,install[0],install)

print "Step3:install

done!"

【脚本3】更加完善的脚本,加入配置文件功能

#! /usr/bin/python

from os.path import walk, join, normpath

from os import chdir

import os,time

def install(sourceFile, dirname,installDir):

print "Installing" ,sourceFile

untarCmd = [ '/bin/tar', '-x', '-z', '-f' ]

workDir = dirname + '/' + sourceFile[:-7]

configureCmd = ['./configure', '--prefix=' + installDir]

makeCmd = ['/usr/bin/make']

installCmd = ['/usr/bin/make', 'install']

os.chdir(path)

print "======Unzip source file======"

os.spawnv(os.P_WAIT, untarCmd[0], untarCmd + [sourceFile])

print "Unzip done!"

time.sleep(2)

print "======Install starting======"

os.chdir(workDir)

os.spawnv(os.P_WAIT,configureCmd[0],configureCmd)

print "Step1:Configure done!"

time.sleep(2)

os.spawnv(os.P_WAIT,makeCmd[0],makeCmd)

print "Step2:Make done!"

time.sleep(2)

os.spawnv(os.P_WAIT,installCmd[0],installCmd)

print "Step3:Install done!"

def scan(arg,dirname, names):

for sourceFile in names:

if (sourceFile[:n]==installFile) and (sourceFile[-7:] == ".tar.gz"

or sourceFile[-8:] == ".tar.bz2"):

print sourceFile,"will be installed"

install(sourceFile, dirname,installDir)

print sourceFile,"has been installed"

def configPATH(cfgFile, installDir):

f = open(cfgFile, 'r+')

f.seek(0, 2)

f.write('\n' + 'export PATH = ' + installDir + '/bin:$PATH')

f.seek(0)

f.close()

print 'Step4: PATH edit done'

def configNodes(nodeFile):

f = open(nodeFile, 'r+')

f.seek(0, 2)

for i in range(1,5):

f.write('\n' +

'oscarnode'+str(i)+'.stu') #............hostname

f.seek(0)

f.close

print 'Step5:MPI Nodes Configuration Done'

if __name__== "__main__":

path='/home/sonky'

installFile = 'mpich'

n = 5

installDir = '/home/sonky/'+installFile

walk(path, scan, 0)

cfgFile = '/home/dell/.bashrc'

configPATH(cfgFile, installDir)

nodeFile = installDir+'/share/machines.LINUX'

configNodes(nodeFile)

!!!问题:为什么python中要有这么多功能类似的函数?有什么区别?继续学习。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值