Fully automatic shell detection. Automatic switch to Debug.

This commit is contained in:
Jean-Paul Chaput 2017-04-14 12:09:48 +02:00
parent 4126a53406
commit 2396ff7120
1 changed files with 60 additions and 38 deletions

View File

@ -9,26 +9,35 @@ import subprocess
import optparse import optparse
coriolisPattern = re.compile ( r".*coriolis.*" ) reCoriolisPattern = re.compile( r".*coriolis.*" )
reReleaseSharedPattern = re.compile( r".*Release\.Shared.*" )
reReleaseStaticPattern = re.compile( r".*Release\.Static.*" )
reDebugSharedPattern = re.compile( r".*Debug\.Shared.*" )
reDebugStaticPattern = re.compile( r".*Debug\.Static.*" )
def stripPath ( pathName ): def scrubPath ( pathName ):
pathEnv = os.getenv ( pathName ) pathEnv = os.getenv( pathName )
if not pathEnv: return "" if not pathEnv: return ""
pathList = string.split ( pathEnv, ':' ) pathList = string.split( pathEnv, ':' )
strippedList = [] scrubbedList = []
for pathElement in pathList: for pathElement in pathList:
if not coriolisPattern.match(pathElement): if reCoriolisPattern .match(pathElement) \
strippedList += [ pathElement ] or reReleaseSharedPattern.match(pathElement) \
or reReleaseStaticPattern.match(pathElement) \
or reDebugSharedPattern .match(pathElement) \
or reDebugStaticPattern .match(pathElement):
continue
scrubbedList += [ pathElement ]
if len(strippedList) == 0: return "" if len(scrubbedList) == 0: return ""
strippedEnv = strippedList[0] scrubbedEnv = scrubbedList[0]
for pathElement in strippedList[1:]: for pathElement in scrubbedList[1:]:
strippedEnv += ":" + pathElement scrubbedEnv += ":" + pathElement
return strippedEnv return scrubbedEnv
def guessOs (): def guessOs ():
@ -131,25 +140,31 @@ def guessOs ():
return (osType,libDir,useDevtoolset2) return (osType,libDir,useDevtoolset2)
def guessCsh ():
cshBins = [ '/usr/bin/tcsh'
, '/bin/tcsh'
, '/usr/pkg/bin/tcsh'
, '/usr/bin/csh'
, '/bin/csh'
, '/usr/pkg/bin/csh'
]
for cshBin in cshBins:
if os.path.isfile(cshBin): return cshBin
return None
def guessShell (): def guessShell ():
if os.environ.has_key('SHELL'): return os.environ['SHELL'] # This environement variable cannot be trusted as it is set once when
# the user logs in. If aftewards it changes it that variable is *not*
# affected :-(.
#if os.environ.has_key('SHELL'): return os.environ['SHELL']
# If SHELL is not set, it is likely we are under C-Shell variant. psCommand = subprocess.Popen ( ['ps', '-p', str(os.getppid()) ], stdout=subprocess.PIPE )
# Look for standard places where the binaries are expecteds. shell = psCommand.stdout.readlines()[1][:-1].split()[-1]
return guessCsh() whichCommand = subprocess.Popen ( ['which', shell ], stdout=subprocess.PIPE )
shellPath = whichCommand.stdout.readlines()[0][:-1]
isBourneShell = True
cshBins = [ '/usr/bin/tcsh'
, '/bin/tcsh'
, '/usr/pkg/bin/tcsh'
, '/usr/local/bin/tcsh'
, '/usr/bin/csh'
, '/bin/csh'
, '/usr/pkg/bin/csh'
, '/usr/local/bin/csh'
]
if shellPath in cshBins: isBourneShell = False
#print 'GUESSED SHELL: "%s"' % shellPath
return shellPath, isBourneShell
@ -159,13 +174,12 @@ if __name__ == "__main__":
buildType = "Release" buildType = "Release"
linkType = "Shared" linkType = "Shared"
rootDir = None rootDir = None
shellBin = guessShell() shellBin, isBourneShell = guessShell()
parser = optparse.OptionParser () parser = optparse.OptionParser ()
# Build relateds. # Build relateds.
parser.add_option ( "--query-inst-root", action="store_true" , dest="queryInstRoot" ) parser.add_option ( "--query-inst-root", action="store_true" , dest="queryInstRoot" )
parser.add_option ( "--query-isys-root", action="store_true" , dest="queryISysRoot" ) parser.add_option ( "--query-isys-root", action="store_true" , dest="queryISysRoot" )
parser.add_option ( "--csh" , action="store_true" , dest="csh" )
parser.add_option ( "--release" , action="store_true" , dest="release" ) parser.add_option ( "--release" , action="store_true" , dest="release" )
parser.add_option ( "--debug" , action="store_true" , dest="debug" ) parser.add_option ( "--debug" , action="store_true" , dest="debug" )
parser.add_option ( "--devel" , action="store_true" , dest="devel" ) parser.add_option ( "--devel" , action="store_true" , dest="devel" )
@ -176,7 +190,6 @@ if __name__ == "__main__":
parser.add_option ( "--root" , action="store" , type="string", dest="rootDir" ) parser.add_option ( "--root" , action="store" , type="string", dest="rootDir" )
( options, args ) = parser.parse_args () ( options, args ) = parser.parse_args ()
if options.csh: shellBin = guessCsh()
if options.release: buildType = "Release" if options.release: buildType = "Release"
if options.debug: buildType = "Debug" if options.debug: buildType = "Debug"
if options.devel: buildType = "Debug" if options.devel: buildType = "Debug"
@ -184,13 +197,16 @@ if __name__ == "__main__":
if options.shared: linkType = "Shared" if options.shared: linkType = "Shared"
if options.rootDir: rootDir = options.rootDir if options.rootDir: rootDir = options.rootDir
scriptPath = os.path.abspath(os.path.dirname(sys.argv[0]))
if 'Debug.' in scriptPath: buildType = 'Debug'
if not shellBin: if not shellBin:
print 'echo "[ERROR] coriolisEnv.py was not able to guess/find the current shell interpeter."' print 'echo "[ERROR] coriolisEnv.py was not able to guess/find the current shell interpeter."'
sys.exit( 1 ) sys.exit( 1 )
strippedPath = stripPath ( "PATH" ) strippedPath = scrubPath( "PATH" )
strippedLibraryPath = stripPath ( "LD_LIBRARY_PATH" ) strippedLibraryPath = scrubPath( "LD_LIBRARY_PATH" )
strippedPythonPath = stripPath ( "PYTHONPATH" ) strippedPythonPath = scrubPath( "PYTHONPATH" )
shellScriptSh = \ shellScriptSh = \
'echo "%(MESSAGE)s";' \ 'echo "%(MESSAGE)s";' \
@ -246,6 +262,10 @@ if __name__ == "__main__":
strippedPath = "%s/bin:%s" % ( coriolisTop, strippedPath ) strippedPath = "%s/bin:%s" % ( coriolisTop, strippedPath )
strippedLibraryPath = "%s:%s" % ( absLibDir , strippedLibraryPath ) strippedLibraryPath = "%s:%s" % ( absLibDir , strippedLibraryPath )
if not os.path.exists(coriolisTop):
print 'echo "[ERROR] coriolisEnv.py, top directory <%s> do not exists."' % coriolisTop
sys.exit( 1 )
if not options.nopython: if not options.nopython:
pyVersion = sys.version_info pyVersion = sys.version_info
version = "%d.%d" % (pyVersion[0],pyVersion[1]) version = "%d.%d" % (pyVersion[0],pyVersion[1])
@ -281,11 +301,13 @@ if __name__ == "__main__":
shellScriptSh += "hash -r;" shellScriptSh += "hash -r;"
shellScriptCsh += "rehash;" shellScriptCsh += "rehash;"
if options.csh: shellScript = shellScriptCsh if isBourneShell: shellScript = shellScriptSh
else: shellScript = shellScriptSh else: shellScript = shellScriptCsh
if useDevtoolset2: if useDevtoolset2:
shellScript += ' scl enable devtoolset-2 %(SHELL)s' shellScript += \
' echo "Launching a devtoolset-2 subshell though scl (CTRL+D to exit).";' \
' scl enable devtoolset-2 %(SHELL)s'
evalScript = shellScript % { "PATH" : strippedPath evalScript = shellScript % { "PATH" : strippedPath
, "LD_LIBRARY_PATH" : strippedLibraryPath , "LD_LIBRARY_PATH" : strippedLibraryPath