用Python编写脚本进行软件自动安装配置
2012-08-22 16:13
253 查看
测试机器:10.10.33.119;os:redhatlinux4
测试路径:/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')
fortar_fileintar_file_list:
ifos.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()
ifi!=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
importos,glob
tar=['/bin/tar','-xzf',]
print“======Unzipsourcefile======”
tar_file_list=glob.glob('*.tar.gz')
fortar_fileintar_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
fromos.pathimportwalk,join,normpath
fromosimportchdir
importos,time
definstall(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"======Unzipsourcefile======"
os.spawnv(os.P_WAIT,untarCmd[0],untarCmd+[sourceFile])
print"Unzipdone!"
time.sleep(2)
print"======Installstarting======"
os.chdir(workDir)
os.spawnv(os.P_WAIT,configureCmd[0],configureCmd)
print"Step1:Configuredone!"
time.sleep(2)
os.spawnv(os.P_WAIT,makeCmd[0],makeCmd)
print"Step2:Makedone!"
time.sleep(2)
os.spawnv(os.P_WAIT,installCmd[0],installCmd)
print"Step3:Installdone!"
defscan(arg,dirname,names):
forsourceFileinnames:
if(sourceFile[:n]==installFile)and(sourceFile[-7:]==".tar.gz"orsourceFile[-8:]==".tar.bz2"):
printsourceFile,"willbeinstalled"
install(sourceFile,dirname,installDir)
printsourceFile,"hasbeeninstalled"
defconfigPATH(cfgFile,installDir):
f=open(cfgFile,'r+')
f.seek(0,2)
f.write('\n'+'exportPATH='+installDir+'/bin:$PATH')
f.seek(0)
f.close()
print'Step4:PATHeditdone'
defconfigNodes(nodeFile):
f=open(nodeFile,'r+')
f.seek(0,2)
foriinrange(1,5):
f.write('\n'+'oscarnode'+str(i)+'.stu')#............hostname
f.seek(0)
f.close
print'Step5:MPINodesConfigurationDone'
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中要有这么多功能类似的函数?有什么区别?继续学习。
测试路径:/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')
fortar_fileintar_file_list:
ifos.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()
ifi!=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
importos,glob
tar=['/bin/tar','-xzf',]
print“======Unzipsourcefile======”
tar_file_list=glob.glob('*.tar.gz')
fortar_fileintar_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
fromos.pathimportwalk,join,normpath
fromosimportchdir
importos,time
definstall(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"======Unzipsourcefile======"
os.spawnv(os.P_WAIT,untarCmd[0],untarCmd+[sourceFile])
print"Unzipdone!"
time.sleep(2)
print"======Installstarting======"
os.chdir(workDir)
os.spawnv(os.P_WAIT,configureCmd[0],configureCmd)
print"Step1:Configuredone!"
time.sleep(2)
os.spawnv(os.P_WAIT,makeCmd[0],makeCmd)
print"Step2:Makedone!"
time.sleep(2)
os.spawnv(os.P_WAIT,installCmd[0],installCmd)
print"Step3:Installdone!"
defscan(arg,dirname,names):
forsourceFileinnames:
if(sourceFile[:n]==installFile)and(sourceFile[-7:]==".tar.gz"orsourceFile[-8:]==".tar.bz2"):
printsourceFile,"willbeinstalled"
install(sourceFile,dirname,installDir)
printsourceFile,"hasbeeninstalled"
defconfigPATH(cfgFile,installDir):
f=open(cfgFile,'r+')
f.seek(0,2)
f.write('\n'+'exportPATH='+installDir+'/bin:$PATH')
f.seek(0)
f.close()
print'Step4:PATHeditdone'
defconfigNodes(nodeFile):
f=open(nodeFile,'r+')
f.seek(0,2)
foriinrange(1,5):
f.write('\n'+'oscarnode'+str(i)+'.stu')#............hostname
f.seek(0)
f.close
print'Step5:MPINodesConfigurationDone'
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
- vim编写python脚本自动补全插件
- Linux安装配置maven以及搭建nexus私服(编写启动脚本)
- shell脚本实现DNS的自动安装与配置
- abinit自动安装脚本编写方法
- PUPPET-配置服务器之安装部署《附自动部署脚本》
- 一个系统中安装多个mysql数据库的方法,以及mysql数据库自动备份脚本的编写
- Ubuntu 配置VIM支持Python 完成YCM的安装实现自动补齐的功能
- 用python编写ios工程自动编译、打包ipa等脚本
- 用InstallShield 进行 ASP 软件的打包和自动安装
- 编写安装配置DNS服务脚本
- 编写安装配置ftp-samba服务脚本
- [置顶] 编写shell脚本实现自动化搭建安装LNMP平台全过程配置详解
- 自动安装zabbix_agent脚本 -- python2
- apacheAnt的构建脚本编写以及集成junit进行自动测试
- 用IS6 进行 ASP 软件的打包和自动安装 的方法
- GIS软件AutoIt自动安装脚本集
- 在ubuntu上进行android开发真机调试,并且配置自动开启adb服务脚本