PathStripper.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from sys import argv, stdout
4 from os import pathsep, listdir, environ, fdopen
5 from os.path import exists, isdir, realpath
6 from optparse import OptionParser, OptionValueError
7 from tempfile import mkstemp
8 from zipfile import is_zipfile
9 
10 def StripPath(path):
11  collected = []
12  for p in path.split(pathsep):
13  rp = realpath(p)
14  # We keep the entry if it is a directory not empty or a zipfile
15  try :
16  if exists(rp) and ((isdir(rp) and listdir(rp))
17  or is_zipfile(rp)) and p not in collected:
18  collected.append(p)
19  except OSError :
20  pass
21  return pathsep.join(collected)
22 
23 def CleanVariable(varname, shell, out):
24  if environ.has_key(varname):
25  pth = StripPath(environ[varname])
26  if shell == "csh" or shell.find("csh") != -1 :
27  out.write("setenv %s %s\n" % (varname, pth))
28  elif shell == "sh" or shell.find("sh") != -1 :
29  out.write("export %s=%s\n" % (varname, pth))
30  elif shell == "bat" :
31  out.write("set %s=%s\n" % (varname, pth))
32 
33 
34 def _check_output_options_cb(option, opt_str, value, parser):
35  if opt_str == "--mktemp" :
36  if parser.values.output != stdout :
37  raise OptionValueError("--mktemp cannot be used at the same time as --output")
38  else :
39  parser.values.mktemp = True
40  fd, outname = mkstemp()
41  parser.values.output = fdopen(fd, "w")
42  print outname
43  elif opt_str == "--output" or opt_str == "-o" :
44  if parser.values.mktemp:
45  raise OptionValueError("--mktemp cannot be used at the same time as --output")
46  else :
47  parser.values.output = open(value, "w")
48 
49 
50 if __name__ == '__main__':
51 
52  parser = OptionParser()
53 
54  parser.add_option("-e", "--env",
55  action="append",
56  dest="envlist",
57  metavar="PATHVAR",
58  help="add environment variable to be processed")
59  parser.add_option("--shell", action="store", dest="shell", type="choice", metavar="SHELL",
60  choices = ['csh','sh','bat'],
61  help="select the type of shell to use")
62 
63  parser.set_defaults(output=stdout)
64  parser.add_option("-o", "--output", action="callback", metavar="FILE",
65  type = "string", callback = _check_output_options_cb,
66  help="(internal) output the command to set up the environment ot the given file instead of stdout")
67  parser.add_option("--mktemp", action="callback",
68  dest="mktemp",
69  callback = _check_output_options_cb,
70  help="(internal) send the output to a temporary file and print on stdout the file name (like mktemp)")
71 
72  options, args = parser.parse_args()
73 
74  if not options.shell and environ.has_key("SHELL"):
75  options.shell = environ["SHELL"]
76 
77 
78  if options.envlist:
79  for v in options.envlist :
80  CleanVariable(v, options.shell, options.output)
81 
82  for a in args:
83  print StripPath(a)
84 
85 
def StripPath(path)
Definition: PathStripper.py:10
def _check_output_options_cb(option, opt_str, value, parser)
Definition: PathStripper.py:34
def CleanVariable(varname, shell, out)
Definition: PathStripper.py:23