测试机器: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中要有这么多功能类似的函数?有什么区别?继续学习。

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐