#!/usr/bin/env python2 """Utility program to install CMT""" import os import os.path import sys import re import shutil # Figure where we are. curdir = os.getcwd() # CMT version name cmtver = "v1r20p20081118" if (len(sys.argv) > 1): if (sys.argv[1] == 'v1r20' ): print "Using CMT version v1r20p20081118" if (sys.argv[1] == 'v1r24' ): print "Using CMT version v1r24" cmtver = "v1r24" if (sys.argv[1] == 'v1r25' ): print "Using CMT version v1r25" cmtver = "v1r25" else: print "Unknown CMT version requested, using default v1r20p20081118" print "Options are v1r20 and v1r24" OldInstall="" OldBin="" if (len(sys.argv) > 3): OldInstall=sys.argv[2] OldBin=sys.argv[3] # Name of the CMT tarball. cmttar = "CMT"+cmtver+".tar.gz" # Figure out which make command. makecmd = "gmake" match = re.compile(r'Command not found') screen = os.popen("which "+makecmd) screenchar = screen.read() if match.match(screenchar) or len(screenchar)==0: print "Could not find gmake on this system, using make instead.\n" makecmd = "make" # Does the tar ball exist? if not os.path.exists(cmttar): print "Cannot find CMT tarball "+cmttar+".\nWas it checked out from the repository?\n" sys.exit(-1) # Has install CMT already been run on this version? if os.path.exists(cmtver): # Yes it has, remove the old version. if os.path.isdir(cmtver): # It's a directory, clean it up. shutil.rmtree(cmtver) else: # For some reason it's a file, so deal with that. os.unlink(cmtver) # Untar CMT ret = os.system("tar -zxf "+cmttar) if ret != 0: print "Faild to unpack" exit(1) # Tidy up the directory structure a bit. The CMT tarball untars into a CMT # directory, but we're already in one. ret = os.system("mv CMT/"+cmtver+" ./") if ret != 0: print "Faild to move CMT/"+cmtver exit(1) os.rmdir("CMT") # Apply patch ret = os.system("patch "+cmtver+"/src/setup.sh < fix_zsh_test.patch") if ret != 0: print "Faild to patch setup.sh" exit(1) # CMTROOT now exists. cmtroot = curdir+"/"+cmtver # Move to the mgr directory os.chdir(cmtroot+"/mgr") ManagedToCopy=False if len(OldInstall) >0: PathToOldBin=os.path.join(OldInstall,cmtver,OldBin) PathToNewBin=os.path.join(cmtroot,OldBin) try: shutil.copytree(PathToOldBin, PathToNewBin) ManagedToCopy=True # Directories are the same except shutil.Error, e: ManagedToCopy=False print('Directory not copied. Error: %s' % e) # Any error saying that the directory doesn't exist except OSError, e: ManagedToCopy=False print('Directory not copied. Error: %s' % e) if not ManagedToCopy: # Make, this need a script for the environment unfortunately. install_script = "setup_cmt_t2k.sh" try: batchFile = open(install_script,"w") batchFile.write("#! /bin/sh\n") batchFile.write("./INSTALL\n") batchFile.write(". ./setup.sh\n") batchFile.write(makecmd+"\n") batchFile.close() except: print "Cannot write compilation batch file.\n" sys.exit(-1) os.chmod(install_script,0744) ret = os.system("sh -x ./"+install_script) if ret != 0: print "Failed to build cmt, please investigate....\nExiting....\n" sys.exit(-1) os.unlink(install_script) # Install try: os.system("sh -e ./INSTALL") except: print "Failed to create CMT scripts, please investigate....\nExiting....\n" sys.exit(-1) # Move back os.chdir(curdir) # Make setup scripts for bash and c shells. # Bash first try: batchFile = open("setup.sh","w") batchFile.write("#!/bin/sh\n") batchFile.write("echo Setup CMT Version "+cmtver+"\n") batchFile.write(". "+cmtroot+"/mgr/setup.sh\n") batchFile.close() except: print "Cannot write batch file setup.sh\n" sys.exit(-1) os.chmod("setup.sh",0744) # Now C Shell first try: batchFile = open("setup.csh","w") batchFile.write("#!/bin/csh\n") batchFile.write("echo Setup CMT Version "+cmtver+"\n") batchFile.write("source "+cmtroot+"/mgr/setup.csh\n") batchFile.close() except: print "Cannot write batch file setup.sh\n" sys.exit(-1) os.chmod("setup.csh",0744) # Report success print "\nCMT successfully installed on your system." print "Please add setup.(c)sh to your login script.\n"