From 4e28c7a7e404d6ccecf7009a971c2c31abdc24ad Mon Sep 17 00:00:00 2001 From: Kyle Mandli Date: Thu, 19 Jan 2012 19:51:23 -0600 Subject: [PATCH 1/4] Add shell type output option --- src/python/setenv.py | 68 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/python/setenv.py b/src/python/setenv.py index 5d30bb86..0dcd3b60 100755 --- a/src/python/setenv.py +++ b/src/python/setenv.py @@ -38,6 +38,40 @@ "visclaw":["clawutil"], "sharpclaw":["riemann","clawutil"]} +bash_path_modification_functions = """ +var_append () { + # Check to see if the variable exists + if [ -z "${1}" ]; then + export ${1}="${2}" + else + var_remove $1 $2 + export ${1}="`/usr/bin/printenv $1`:${2}" + fi +} +var_prepend () { + # Check to see if variable exists + if [ -z "${1}" ]; then + export ${1}="${2}" + else + var_remove $1 $2 + export ${1}="${2}:`/usr/bin/printenv $1`" + fi +} +var_remove () { + VAR_CONTENTS=`/usr/bin/printenv $1` + NEW_VAR=`echo -n $VAR_CONTENTS | awk -v RS=: -v ORS=: '$0 != "'$2'"' | sed 's/:$//'` + export ${1}=${NEW_VAR} +} + +path_append () { var_append PATH $1; } +path_prepend () { var_prepend PATH $1; } +path_remove () { var_remove PATH $1; } +python_append () { var_append PYTHONPATH $1;} +python_prepend () { var_prepend PYTHONPATH $1;} +python_remove () { var_remove PYTHONPATH $1;} + +""" + # ============================================================================ # Help display class Usage(Exception): @@ -62,6 +96,10 @@ def __init__(self, msg): -h, --help - Display help message -o, --output= (string) - The base name for the output bash and csh files (default == "setenv") + -s, --shel= (string) - Type of shell script to output, valid options include + 'csh', 'bash', 'sh', or 'both'. The option 'both' + will output both a "csh" and "sh" compatable file. + (default == 'both') Project path options: -c, --claw= (string) - Path to base CLAW directory. If this option is choosen @@ -77,8 +115,10 @@ def __init__(self, msg): # ============================================================================ # Helper functions def write_environment_variable(csh_handle,bash_handle,var,value): - csh_handle.write('setenv %s "%s"\n' % (var.upper(),value)) - bash_handle.write('export %s="%s"\n' % (var.upper(),value)) + if csh_handle is not None: + csh_handle.write('setenv %s "%s"\n' % (var.upper(),value)) + if bash_handle is not None: + bash_handle.write('export %s="%s"\n' % (var.upper(),value)) def check_repos_dependencies(project_name,available_projects): r"""Checks that required repositories of project_name are present""" @@ -91,8 +131,8 @@ def check_repos_dependencies(project_name,available_projects): return None # ============================================================================ -def write_env_files(claw_path,verbose=True,outfile_base="setenv",**kargs): - +def write_env_files(claw_path,verbose=True,outfile_base="setenv", + shell_type='both',**kargs): # Find projects available_projects = {} print "Found the following Clawpack projects:" @@ -129,8 +169,14 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv",**kargs): # Write out out_file_base.csh and out_file_base.sh # Open output files - csh_file = open(os.path.join(claw_path,".".join((outfile_base,"csh"))),'w') - bash_file = open(os.path.join(claw_path,".".join((outfile_base,"bash"))),'w') + if "csh" in shell_type: + csh_file = open(os.path.join(claw_path,".".join((outfile_base,"csh"))),'w') + else: + csh_file = None + if "bash" == shell_type or "sh" == shell_type: + bash_file = open(os.path.join(claw_path,".".join((outfile_base,"bash"))),'w') + else: + bash_file = None # Write out boiler plate boiler_plate = ("# Clawpack environment settings\n") @@ -215,17 +261,18 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv",**kargs): project_paths = {} try: try: - long_options = ["help","output=","verbose", + long_options = ["help","output=","verbose","shell=" "claw="] for proj_name in git_repos: long_options.append("%s=" % proj_name) - opts, args = getopt.getopt(argv[1:], "ho:vc",long_options) + opts, args = getopt.getopt(argv[1:], "ho:vs:c",long_options) except getopt.error, msg: raise Usage(msg) # Default script parameter values verbose = False out_file_base = "setenv" + shell_type = 'both' # Default claw path claw_path = os.path.abspath(os.curdir) @@ -237,6 +284,8 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv",**kargs): key_args["verbose"] = True if option in ("-o","--output"): key_args["out_file_base"] = value + if option in ("-s","--shell"): + shell_type = value if option in ("-h","--help"): raise Usage(help_message) @@ -253,5 +302,6 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv",**kargs): sys.exit(2) sys.exit(write_env_files(claw_path,verbose=verbose, - outfile_base=out_file_base,**project_paths)) + outfile_base=out_file_base,shell_type=shell_type, + **project_paths)) From 525365d15b8b824b80b307d24461a46e75a72715 Mon Sep 17 00:00:00 2001 From: Kyle Mandli Date: Thu, 19 Jan 2012 20:08:41 -0600 Subject: [PATCH 2/4] Bug fix to newly added options --- src/python/setenv.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/python/setenv.py b/src/python/setenv.py index 0dcd3b60..668fb9ec 100755 --- a/src/python/setenv.py +++ b/src/python/setenv.py @@ -38,7 +38,11 @@ "visclaw":["clawutil"], "sharpclaw":["riemann","clawutil"]} +# This was intended to be a prefered means for adding and deleting paths for +# the resulting output script but we need a csh version before this will really +# work correctly without adding a lot of complexity to this utility (KTM) bash_path_modification_functions = """ +# These are utility functions for manipulating paths var_append () { # Check to see if the variable exists if [ -z "${1}" ]; then @@ -96,7 +100,7 @@ def __init__(self, msg): -h, --help - Display help message -o, --output= (string) - The base name for the output bash and csh files (default == "setenv") - -s, --shel= (string) - Type of shell script to output, valid options include + -s, --shell= (string) - Type of shell script to output, valid options include 'csh', 'bash', 'sh', or 'both'. The option 'both' will output both a "csh" and "sh" compatable file. (default == 'both') @@ -167,22 +171,19 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", # ========================================================================= # Write out out_file_base.csh and out_file_base.sh - # Open output files + boiler_plate = ("# Clawpack environment settings\n") if "csh" in shell_type: csh_file = open(os.path.join(claw_path,".".join((outfile_base,"csh"))),'w') + csh_file.write(boiler_plate) else: csh_file = None if "bash" == shell_type or "sh" == shell_type: bash_file = open(os.path.join(claw_path,".".join((outfile_base,"bash"))),'w') + bash_file.write(boiler_plate) else: bash_file = None - # Write out boiler plate - boiler_plate = ("# Clawpack environment settings\n") - csh_file.write(boiler_plate) - bash_file.write(boiler_plate) - # Write out variables python_path = "${PYTHONPATH}" matlab_path = "${MATLABPATH}" @@ -252,8 +253,10 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", print "" # Close output files - csh_file.close() - bash_file.close() + if csh_file is not None: + csh_file.close() + if bash_file is not None: + bash_file.close() if __name__ == "__main__": # Parse input arguments @@ -261,7 +264,7 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", project_paths = {} try: try: - long_options = ["help","output=","verbose","shell=" + long_options = ["help","output=","verbose","shell=", "claw="] for proj_name in git_repos: long_options.append("%s=" % proj_name) @@ -301,6 +304,14 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", print >> sys.stderr, "\t for help use --help" sys.exit(2) + if verbose: + print "Basic options:" + if 'csh' in shell_type: + print " output = %s" % '.'.join((out_file_base,'csh')) + elif 'bash' == shell_type or 'sh' == shell_type: + print " output = %s" % '.'.join((out_file_base,'bash')) + elif 'both' == shell_type: + print " output = %s" '.'.join((out_file_base,'{csh,bash}')) sys.exit(write_env_files(claw_path,verbose=verbose, outfile_base=out_file_base,shell_type=shell_type, **project_paths)) From 7b6907ee92cdec9f531b7bd93fc896d8f4f6dedf Mon Sep 17 00:00:00 2001 From: Kyle Mandli Date: Thu, 19 Jan 2012 20:11:14 -0600 Subject: [PATCH 3/4] Final bug fix for shell type option --- src/python/setenv.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/python/setenv.py b/src/python/setenv.py index 668fb9ec..0cf29dd7 100755 --- a/src/python/setenv.py +++ b/src/python/setenv.py @@ -284,9 +284,9 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", for option, value in opts: # Script parameters if option in ("-v","--verbose"): - key_args["verbose"] = True + verbose = True if option in ("-o","--output"): - key_args["out_file_base"] = value + out_file_base = value if option in ("-s","--shell"): shell_type = value if option in ("-h","--help"): @@ -303,15 +303,7 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) print >> sys.stderr, "\t for help use --help" sys.exit(2) - - if verbose: - print "Basic options:" - if 'csh' in shell_type: - print " output = %s" % '.'.join((out_file_base,'csh')) - elif 'bash' == shell_type or 'sh' == shell_type: - print " output = %s" % '.'.join((out_file_base,'bash')) - elif 'both' == shell_type: - print " output = %s" '.'.join((out_file_base,'{csh,bash}')) + sys.exit(write_env_files(claw_path,verbose=verbose, outfile_base=out_file_base,shell_type=shell_type, **project_paths)) From 055b203420bdce5198582281b0356a99a99b7693 Mon Sep 17 00:00:00 2001 From: Kyle Mandli Date: Fri, 3 Feb 2012 20:31:07 +0300 Subject: [PATCH 4/4] Add clawapps repository support, fix missing error This commit removes the exception that the clawapps environment has not been implemented and now sets an environment variable called CLAWAPPS that points to the appropriate path. It also fixes a bug related to printing out missing project dependencies. --- src/python/setenv.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/python/setenv.py b/src/python/setenv.py index 34ff0dfc..d862e1fd 100755 --- a/src/python/setenv.py +++ b/src/python/setenv.py @@ -166,8 +166,7 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", missing_projects = check_repos_dependencies(project,available_projects.keys()) if missing_projects is not None: error_msg = "The project %s depends on the following missing projects:" % project_name - for project in missing_projects: - error_msg += "\n %s" % project + error_msg += ("\n %s" % name for name in missing_projects) # ========================================================================= # Write out out_file_base.csh and out_file_base.sh @@ -217,8 +216,9 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", write_environment_variable(csh_file,bash_file,"PYCLAW",available_projects["pyclaw"]) if "clawapps" in available_projects: - raise NotImplementedError("Environment settings not implemented for clawapps!") - + print " CLAWAPPS = %s" % available_projects["clawapps"] + write_environment_variable(csh_file,bash_file,"CLAWAPPS",available_projects["clawapps"]) + if "doc" in available_projects: pass @@ -239,8 +239,7 @@ def write_env_files(claw_path,verbose=True,outfile_base="setenv", write_environment_variable(csh_file,bash_file,"SHARPCLAW",available_projects["sharpclaw"]) if "clawpack-4.x" in available_projects: - # python_path - # = ":".join((os.path.join(available_projects["clawpack-4.x"],"python"),python_path)) + # python_path = ":".join((os.path.join(available_projects["clawpack-4.x"],"python"),python_path)) print " CLAW_4 = %s" % available_projects["clawpack-4.x"] write_environment_variable(csh_file,bash_file,"CLAW_4",available_projects["clawpack-4.x"])