用Python編寫指令碼進行軟體自動安裝配置

來源:互聯網
上載者:User
用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中要有這麼多功能類似的函數?有什麼區別?繼續學習。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.