| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# PathStripper.py

Go to the documentation of this file.

    00001 #!/usr/bin/env python
    00002
    00003 from sys import argv, stdout
    00004 from os import pathsep, listdir, environ, fdopen
    00005 from os.path import exists, isdir, realpath
    00006 from optparse import OptionParser, OptionValueError
    00007 from tempfile import mkstemp
    00008
    00009 def StripPath(path):
    00010     collected = []
    00011     for p in path.split(pathsep):
    00012         rp = realpath(p)
    00013         if exists(rp) and isdir(rp):
    00014             if len(listdir(rp)) != 0:
    00015                 collected.append(p)
    00016     return pathsep.join(collected)
    00017
    00018 def CleanVariable(varname, shell, out):
    00019     if environ.has_key(varname):
    00020         pth = StripPath(environ[varname])
    00021         if shell == "csh" or shell.find("csh") != -1 :
    00022             out.write("setenv %s %s\n" % (varname, pth))
    00023         elif shell == "sh" or shell.find("sh") != -1 :
    00024             out.write("export %s=%s\n" % (varname, pth))
    00025         elif shell == "bat" :
    00026             out.write("set %s=%s\n" % (varname, pth))
    00027
    00028
    00029 def _check_output_options_cb(option, opt_str, value, parser):
    00030     if opt_str == "--mktemp" :
    00031         if parser.values.output != stdout :
    00032             raise OptionValueError("--mktemp cannot be used at the same time as --output")
    00033         else :
    00034             parser.values.mktemp = True
    00035             fd, outname = mkstemp()
    00036             parser.values.output = fdopen(fd, "w")
    00037             print outname
    00038     elif opt_str == "--output" or opt_str == "-o" :
    00039         if parser.values.mktemp:
    00040             raise OptionValueError("--mktemp cannot be used at the same time as --output")
    00041         else :
    00042             parser.values.output = open(value, "w")
    00043
    00044
    00045 if __name__ == '__main__':
    00046
    00047     parser = OptionParser()
    00048
    00049     parser.add_option("-e", "--env",
    00050                       action="append",
    00051                       dest="envlist",
    00052                       metavar="PATHVAR",
    00053                       help="add environment variable to be processed")
    00054     parser.add_option("--shell", action="store", dest="shell", type="choice", metavar="SHELL",
    00055                       choices = ['csh','sh','bat'],
    00056                       help="select the type of shell to use")
    00057
    00058     parser.set_defaults(output=stdout)
    00059     parser.add_option("-o", "--output", action="callback", metavar="FILE",
    00060                       type = "string", callback = _check_output_options_cb,
    00061                       help="(internal) output the command to set up the environment ot the given file instead of stdout")
    00062     parser.add_option("--mktemp", action="callback",
    00063                       dest="mktemp",
    00064                       callback = _check_output_options_cb,
    00065                       help="(internal) send the output to a temporary file and print on stdout the file name (like mktemp)")
    00066
    00067     options, args = parser.parse_args()
    00068
    00069     if not options.shell and environ.has_key("SHELL"):
    00070         options.shell = environ["SHELL"]
    00071
    00072
    00073     if options.envlist:
    00074         for v in options.envlist :
    00075             CleanVariable(v, options.shell, options.output)
    00076
    00077     for a in args:
    00078         print StripPath(a)
    00079
    00080