您的位置:首页 > 编程语言 > Python开发

用Python编写脚本进行软件自动安装配置

2013-02-05 21:46 1006 查看

用Python编写脚本进行软件自动安装配置

更新: 2010-04-11
来源: 互联网 字体:【大 中 小】

测试机器:10.10.33.119;os:redhat linux4
测试路径:/home/sonky

【脚本1】:使用exec*()系列函数,在python里面,os.exec*()系列命令会完全代替python解释器,如果执行os.exec*()函数,则python解释器和脚本将在os.exec*()执行之后终止。如果如果想继续执行,必须
使用os.fork()函数来启动一个新子进程。【建立.py文件后,用chmoda+u *.py命令修改文件属性后,./*.py可以运行脚本】

#! /usr/bin/envpython
importos,glob
cmd = ['/bin/tar',
'-x',
'-z',
'-f',
]
tar_file_list =glob.glob('*.tar.gz')
for tar_file intar_file_list:
if os.fork():
os.wait()
else:
os.execv(cmd[0], cmd + [tar_file,])
print "installMPICH2..."
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()
ifn!=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等)来实现是否继续执行还是终止脚本,其中sleep3只是为了调试的时候暂停一下可以看到那一步已经完成。
#!/usr/bin/python

import os,glob

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

print “======Unzip sourcefile======”

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

for tar_file intar_file_list:

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

print "======InstallMPICH======"

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:configuredone!"

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

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

print "Step2:makedone!"

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

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

print "Step3:installdone!"

【脚本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中要有这么多功能类似的函数?有什么区别?继续学习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: