Gaudi Framework, version v23r3

Home   Generated: Thu Jun 28 2012
Functions

env Namespace Reference

Functions

def set_env
def main

Function Documentation

def env::main (  )

Definition at line 49 of file env.py.

00050           :
00051     from optparse import OptionParser
00052     parser = OptionParser(prog = "env",
00053                           usage = "Usage: %prog [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]",
00054                           description = "Set each NAME to VALUE in the environment and run COMMAND.",
00055                           epilog = "The operations are performed in the order: unset, set, append, "
00056                                    "prepend. If no COMMAND, print the resulting environment." )
00057     parser.add_option("-i", "--ignore-environment",
00058                       action = "store_true",
00059                       help = "start with an empty environment")
00060     parser.add_option("-u", "--unset",
00061                       metavar = "NAME",
00062                       action = "append",
00063                       help = "remove variable from the environment")
00064     parser.add_option("-s", "--set",
00065                       metavar = "NAME=VALUE",
00066                       action = "append",
00067                       help = "set the variable NAME to VALUE")
00068     parser.add_option("-a", "--append",
00069                       metavar = "NAME=VALUE",
00070                       action = "append",
00071                       help = "append VALUE to the variable NAME (with a '%s' as separator)" % os.pathsep)
00072     parser.add_option("-p", "--prepend",
00073                       metavar = "NAME=VALUE",
00074                       action = "append",
00075                       help = "prepend VALUE to the variable NAME (with a '%s' as separator)" % os.pathsep)
00076     parser.add_option("-x", "--xml",
00077                       action = "append",
00078                       help = "XML file describing the changes to the environment")
00079     parser.disable_interspersed_args()
00080     parser.set_defaults(unset = [],
00081                         set = [],
00082                         append = [],
00083                         prepend = [],
00084                         xml = [],
00085                         ignore_environment = False)
00086 
00087     opts, args = parser.parse_args()
00088 
00089     # find the 'set' arguments in the list of arguments
00090     i = 0
00091     argc = len(args)
00092     while (i < argc) and ("=" in args[i]):
00093         i += 1
00094     opts.set.extend(args[:i])
00095     cmd = args[i:]
00096 
00097     # prepare initial dictionary
00098     if opts.ignore_environment:
00099         env = {}
00100     else:
00101         env = dict(os.environ)
00102 
00103     if opts.xml:
00104         from EnvConfig import Control
00105         control = Control.Environment()
00106         # declare scalar and list variables ("*PATH*" and "*DIRS*" are lists)
00107         for v in env:
00108             control.declare(v, ("PATH" in v or "DIRS" in v) and "list" or "scalar", False)
00109         for k in env:
00110             control.set(k, env[k])
00111 
00112         for f in opts.xml:
00113             control.loadXML(f)
00114         env = control.vars()
00115 
00116     env = set_env(env,
00117                   set = opts.set, unset = opts.unset,
00118                   append = opts.append, prepend = opts.prepend)
00119 
00120     if not cmd:
00121         for nv in env.items():
00122             print "%s=%s" % nv
00123         return 0
00124     else:
00125         from subprocess import Popen
00126         return Popen(cmd, env = env).wait()

def env::set_env (   env,
  set = [],
  unset = [],
  append = [],
  prepend = [] 
)
Manipulate the dictionary-like object 'env' according to the prescriptions in
the lists 'unset', 'set', 'append' and 'prepend' (in this order).
The lists must contain strings of the format 'NAME=VALUE' except for 'unset'
which requires only 'NAME'.

Definition at line 7 of file env.py.

00008                                                                  :
00009     """
00010     Manipulate the dictionary-like object 'env' according to the prescriptions in
00011     the lists 'unset', 'set', 'append' and 'prepend' (in this order).
00012     The lists must contain strings of the format 'NAME=VALUE' except for 'unset'
00013     which requires only 'NAME'.
00014     """
00015     def parse(x):
00016         """split the "NAME=VALUE" string into the tuple ("NAME", "VALUE")
00017         replacing '[:]' with os.pathsep in VALUE"""
00018         n, v = x.split('=', 1)
00019         return n, v.replace('[:]', os.pathsep)
00020     def dictlist(l):
00021         """create a dictionary from the list of pairs by appending to lists"""
00022         d = {}
00023         for n, v in l:
00024             if n in d:
00025                 d[n].append(v)
00026             else:
00027                 d[n] = [v]
00028         return d
00029     # remove the 'unset' variables
00030     for n in unset:
00031         if n in env:
00032             log.debug("unset %s", n)
00033             del env[n]
00034     # set the requested variables
00035     env.update(map(parse, set))
00036     # append
00037     for n, v in dictlist(map(parse, append)).items():
00038         if n in env:
00039             v.insert(0, env[n]) # add old value at the beginning
00040         env[n] = os.pathsep.join(v)
00041     # prepend
00042     for n, v in dictlist(map(parse, reversed(prepend))).items():
00043         if n in env:
00044             v.append(env[n]) # add old value at end
00045         env[n] = os.pathsep.join(v)
00046 
00047     return env
00048 

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

Generated at Thu Jun 28 2012 12:30:18 for Gaudi Framework, version v23r3 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004