| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines