Use python to write scripts for automatic software installation and configuration updates:
Source: Internet Font: [big, medium, and small] Test Machine: 10.10.33.119; OS: RedHat linux4
Test path:/home/sonky
[Script 1]: OS .exe C * (replace pythoninterpreter) uses the exec * (serial series) command to completely replace the python interpreter. If OS .exe C * is executed, the pythoninterpreter and script are terminated after OS .exe C * () is executed. Required if you want to continue execution
Use the OS. Fork () function to start a new sub-process. [After the. py file is created, run the chmoda + u *. py command to modify the file attributes./*. py can run the script]
#! /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 .exe CV (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 ()
Interferon! = 0:
OS. Wait ()
OS .exe CV (install [0], install)
Else:
OS .exe CV (make [0], make)
Else:
OS .exe CV (configure [0], configure)
[Script 2-mpich.py]: in Linux (UNIX), you can use OS. Spawn * (), which features OS .exe C *(),
However, they can use the parameters (p_wait, p_nowait, etc.) to determine whether to continue or terminate the script. sleep3 is only used to pause the script during debugging, and you can see that the step has been completed.
#! /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! "
[Script 3] more complete script, adding the configuration file Function
#! /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 (export file, installdir ):
F = open (writable file, 'r + ')
F. Seek (0, 2)
F. Write ('\ n' + 'export Path =' + installdir + '/bin: $ path ')
F. Seek (0)
F. Close ()
Print 'step4: path edit one'
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)
Export File = '/home/Dell/. bashrc'
Configpath (export file, installdir)
Nodefile = installdir + '/share/machines. Linux'
Confignodes (nodefile)
!!! Q: Why is there such a function similar to this in Python? What is the difference? Continue learning.