用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中要有这么多功能类似的函数?有什么区别?继续学习。
相关文章推荐
- 用Python编写脚本进行软件自动安装配置
- shell脚本自动 进行配置免密登录,安装java环境,安装tomcat
- 使用python在windows 下 编写 自动备份脚本
- python stackeless 自动安装脚本
- Python笔记–IAR使用Python脚本实现自动对烧录文件进行处理
- 转载:用Python编写自动下载网络小说的脚本
- 编写安装配置httpd服务基于域名的虚拟主机脚本
- vim + ctags + taglist配置和使用 【及】 一键安装 ctags和cscope的方法 【及】vim语法高亮,自动缩进,python 自动缩进设置
- Centos6.5+Python2.7 +ffmpeg+opencv2自动安装脚本
- 为 IBM DB2 for Linux、UNIX and Windows 以及 IBM Content Manager 编写自动安装脚本
- 可以进行自动补全的python的eclipse配置方法
- linux 下 vim编辑器插件安装 方便python脚本编写
- 用InstallShield 进行ASP 软件的打包和自动安装
- python stackeless 自动安装脚本
- 使用shell脚本一键安装zabbix并配置客户端的自动发现选项
- Sublime Text 3 安装,配置自动补全Python
- linux软件管理之------编译安装nginx服务器并手动编写自动化运行脚本
- Python2.7 无readline模块前提下,tab自动补全脚本配置
- Appium环境的安装与配置,Python测试脚本测试
- 全自动安装JAVA SE以及自动配置环境变量脚本