Gaudi Framework, version v23r7

Home   Generated: Wed Mar 20 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Functions
env Namespace Reference

Classes

class  EnvError
 

Functions

def splitNameValue
 
def parse_args
 
def prepareEnv
 
def makeEnv
 
def main
 

Function Documentation

def env.main ( )
Main function of the script.

Definition at line 120 of file env.py.

121 def main():
122  '''
123  Main function of the script.
124  '''
125 
126  opts, args = parse_args()
127 
128  cmd = []
129  # find the (implicit) 'set' arguments in the list of arguments
130  # and put the rest in the command
131  try:
132  for i, a in enumerate(args):
133  opts.actions.append(('set', splitNameValue(a)))
134  except EnvError:
135  cmd = args[i:]
136 
137  if opts.shell and cmd:
138  print >> sys.stderr, "Invalid arguments: --%s cannot be used with a command." % opts.shell
139  return 2
140 
141  env = makeEnv(opts.actions, opts.ignore_environment)
142 
143  if "LD_LIBRARY_PATH" in env:
144  # replace LD_LIBRARY_PATH with the corresponding one on other systems
145  if sys.platform.startswith("win"):
146  other = "PATH"
147  elif sys.platform.startswith("darwin"):
148  other = "DYLD_LIBRARY_PATH"
149  else:
150  other = None
151  if other:
152  if other in env:
153  env[other] = env[other] + os.pathsep + env["LD_LIBRARY_PATH"]
154  else:
155  env[other] = env["LD_LIBRARY_PATH"]
156  del env["LD_LIBRARY_PATH"]
157 
158  if not cmd:
159  if opts.shell == 'py':
160  from pprint import pprint
161  pprint(env)
162  else:
163  template = {'sh': "export %s='%s'",
164  'csh': "setenv %s '%s'"}.get(opts.shell, "%s=%s")
165  for nv in sorted(env.items()):
166  print template % nv
167  return 0
168  else:
169  from subprocess import Popen
170  return Popen(cmd, env=env).wait()
def env.makeEnv (   actions,
  ignore_system = False 
)
Return a dictionary of the environment variables after applying the actions.

@param ignore_system: if set to True, the system environment is ignored.

Definition at line 104 of file env.py.

105 def makeEnv(actions, ignore_system=False):
106  '''
107  Return a dictionary of the environment variables after applying the actions.
108 
109  @param ignore_system: if set to True, the system environment is ignored.
110  '''
111  # prepare initial control object
112  control = prepareEnv(ignore_system)
113 
114  # apply all the actions
115  for action, args in actions:
116  apply(getattr(control, action), args)
117 
118  # extract the result env dictionary
119  return control.vars()
def env.parse_args ( )
Parse the command line arguments.

Definition at line 21 of file env.py.

21 
22 def parse_args():
23  '''
24  Parse the command line arguments.
25  '''
26 
27  from optparse import OptionParser, OptionValueError
28 
29  def addOperation(option, opt, value, parser, action):
30  '''
31  Append to the list of actions the tuple (action, (<args>, ...)).
32  '''
33  if action not in ('unset', 'loadXML'):
34  try:
35  value = splitNameValue(value)
36  except EnvError:
37  raise OptionValueError("Invalid value for option %s: '%s', it requires NAME=VALUE." % (opt, value))
38  else:
39  value = (value,)
40  parser.values.actions.append((action, value))
41 
42  parser = OptionParser(prog = "env.py",
43  usage = "Usage: %prog [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]",
44  description = "Set each NAME to VALUE in the environment and run COMMAND.",
45  epilog = "The operations are performed in the order they appear on the "
46  "command line. If no COMMAND is provided, print the resulting "
47  "environment. (Note: this command is modeled after the Unix "
48  "command 'env', see \"man env\")" )
49 
50  parser.add_option("-i", "--ignore-environment",
51  action="store_true",
52  help="start with an empty environment")
53  parser.add_option("-u", "--unset",
54  metavar="NAME",
55  action="callback", callback=addOperation,
56  type="str", nargs=1, callback_args=('unset',),
57  help="remove variable from the environment")
58  parser.add_option("-s", "--set",
59  metavar="NAME=VALUE",
60  action="callback", callback=addOperation,
61  type="str", nargs=1, callback_args=('set',),
62  help="set the variable NAME to VALUE")
63  parser.add_option("-a", "--append",
64  metavar="NAME=VALUE",
65  action="callback", callback=addOperation,
66  type="str", nargs=1, callback_args=('append',),
67  help="append VALUE to the variable NAME (with a '%s' as separator)" % os.pathsep)
68  parser.add_option("-p", "--prepend",
69  metavar="NAME=VALUE",
70  action="callback", callback=addOperation,
71  type="str", nargs=1, callback_args=('prepend',),
72  help="prepend VALUE to the variable NAME (with a '%s' as separator)" % os.pathsep)
73  parser.add_option("-x", "--xml",
74  action="callback", callback=addOperation,
75  type="str", nargs=1, callback_args=('loadXML',),
76  help="XML file describing the changes to the environment")
77  parser.add_option("--sh",
78  action="store_const", const="sh", dest="shell",
79  help="Print the environment as shell commands for 'sh'-derived shells.")
80  parser.add_option("--csh",
81  action="store_const", const="csh", dest="shell",
82  help="Print the environment as shell commands for 'csh'-derived shells.")
83  parser.add_option("--py",
84  action="store_const", const="py", dest="shell",
85  help="Print the environment as Python dictionary.")
86  parser.disable_interspersed_args()
87  parser.set_defaults(actions=[], ignore_environment=False)
88 
89  return parser.parse_args()
def env.prepareEnv (   ignore_system = False)
Prepare an EnvConfig.Control instance to operate on the environment.

@param ignore_system: if set to True, the system environment is ignored.

Definition at line 90 of file env.py.

90 
91 def prepareEnv(ignore_system=False):
92  '''
93  Prepare an EnvConfig.Control instance to operate on the environment.
94 
95  @param ignore_system: if set to True, the system environment is ignored.
96  '''
97  from EnvConfig import Control
98  control = Control.Environment()
99 
100  if not ignore_system:
101  control.presetFromSystem()
102 
103  return control
def env.splitNameValue (   name_value)
split the "NAME=VALUE" string into the tuple ("NAME", "VALUE")
replacing '[:]' with os.pathsep in VALUE

Definition at line 13 of file env.py.

13 
14 def splitNameValue(name_value):
15  """split the "NAME=VALUE" string into the tuple ("NAME", "VALUE")
16  replacing '[:]' with os.pathsep in VALUE"""
17  if '=' not in name_value:
18  raise EnvError("Invalid variable argument '%s'." % name_value)
19  n, v = name_value.split('=', 1)
20  return n, v.replace('[:]', os.pathsep)

Generated at Wed Mar 20 2013 17:59:48 for Gaudi Framework, version v23r7 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004