| # Gaudi Framework, version v21r9 |

Home |  | Generated: 3 May 2010

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