CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
helperFunctions Namespace Reference

Functions

def addIndex
 
def castorDirExists
 
def getCommandOutput2
 
def parsecolor
 
def parsestyle
 
def replaceByMap
 — Helpers —############################ More...
 
def replacelast
 

Variables

list fileExtensions = ["_cfg.py", ".sh", ".root"]
 

Function Documentation

def helperFunctions.addIndex (   filename,
  njobs,
  index = None 
)

Definition at line 87 of file helperFunctions.py.

References replacelast().

Referenced by genericValidation.GenericValidation.createFiles(), genericValidation.GenericValidationData.createScript(), and genericValidation.GenericValidationData.getRepMap().

87 
88 def addIndex(filename, njobs, index = None):
89  if index is None:
90  return [addIndex(filename, njobs, i) for i in range(njobs)]
91  if njobs == 1:
92  return filename
93 
94  fileExtension = None
95  for extension in fileExtensions:
96  if filename.endswith(extension):
97  fileExtension = extension
98  if fileExtension is None:
99  raise AllInOneError(fileName + " does not end with any of the extensions "
100  + str(fileExtensions))
101  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
def helperFunctions.castorDirExists (   path)
This function checks if the directory given by `path` exists.

Arguments:
- `path`: Path to castor directory

Definition at line 60 of file helperFunctions.py.

References getCommandOutput2().

60 
61 def castorDirExists(path):
62  """This function checks if the directory given by `path` exists.
63 
64  Arguments:
65  - `path`: Path to castor directory
66  """
67 
68  if path[-1] == "/":
69  path = path[:-1]
70  containingPath = os.path.join( *path.split("/")[:-1] )
71  dirInQuestion = path.split("/")[-1]
72  try:
73  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
74  except RuntimeError:
75  return False
76  for line in rawLines:
77  if line.split()[0][0] == "d":
78  if line.split()[8] == dirInQuestion:
79  return True
80  return False
def helperFunctions.getCommandOutput2 (   command)
This function executes `command` and returns it output.

Arguments:
- `command`: Shell command to be invoked by this function.

Definition at line 45 of file helperFunctions.py.

Referenced by castorDirExists(), geometryComparison.GeometryComparison.createScript(), validateAlignments.main(), and validateAlignments.ValidationJob.runJob().

45 
46 def getCommandOutput2(command):
47  """This function executes `command` and returns it output.
48 
49  Arguments:
50  - `command`: Shell command to be invoked by this function.
51  """
52 
53  child = os.popen(command)
54  data = child.read()
55  err = child.close()
56  if err:
57  raise RuntimeError('%s failed w/ exit code %d' % (command, err))
58  return data
59 
def helperFunctions.parsecolor (   color)

Definition at line 102 of file helperFunctions.py.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

103 def parsecolor(color):
104  try: #simplest case: it's an int
105  return int(color)
106  except ValueError:
107  pass
108 
109  try: #kRed, kBlue, ...
110  color = str(getattr(ROOT, color))
111  return int(color)
112  except (AttributeError, ValueError):
113  pass
114 
115  if color.count("+") + color.count("-") == 1: #kRed+5, kGreen-2
116  if "+" in color: #don't want to deal with nonassociativity of -
117  split = color.split("+")
118  color1 = parsecolor(split[0])
119  color2 = parsecolor(split[1])
120  return color1 + color2
121 
122  if "-" in color:
123  split = color.split("-")
124  color1 = parsecolor(split[0])
125  color2 = parsecolor(split[1])
126  return color1 - color2
127 
128  raise AllInOneError("color has to be an integer, a ROOT constant (kRed, kBlue, ...), or a two-term sum or difference (kGreen-5)!")
def helperFunctions.parsestyle (   style)

Definition at line 129 of file helperFunctions.py.

Referenced by preexistingValidation.PreexistingValidation.getRepMap().

130 def parsestyle(style):
131  try:
132  int(style)
133  return style
134  except ValueError:
135  raise AllInOneError("style has to be an integer!")
def helperFunctions.replaceByMap (   target,
  the_map 
)

— Helpers —############################

This function replaces `.oO[key]Oo.` by `the_map[key]` in target.

Arguments:
- `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
- `the_map`: Dictionary which has to contain the `key`s in `target` as keys

Definition at line 6 of file helperFunctions.py.

Referenced by zMuMuValidation.ZMuMuValidation.appendToExtendedValidation(), zMuMuValidation.ZMuMuValidation.createConfiguration(), geometryComparison.GeometryComparison.createConfiguration(), validateAlignments.createExtendedValidationScript(), genericValidation.GenericValidation.createFiles(), validateAlignments.createMergeScript(), validateAlignments.createMergeZmumuPlotsScript(), validateAlignments.createOfflineParJobsMergeScript(), validateAlignments.createPrimaryVertexPlotScript(), geometryComparison.GeometryComparison.createScript(), validateAlignments.createTrackSplitPlotScript(), alignment.Alignment.getConditions(), genericValidation.GenericValidationData.getRepMap(), and plottingOptions.PlottingOptionsTrackSplitting.validsubdets().

6 
7 def replaceByMap(target, the_map):
8  """This function replaces `.oO[key]Oo.` by `the_map[key]` in target.
9 
10  Arguments:
11  - `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
12  - `the_map`: Dictionary which has to contain the `key`s in `target` as keys
13  """
14 
15  result = target
16  for key in the_map:
17  lifeSaver = 10e3
18  iteration = 0
19  while ".oO[" in result and "]Oo." in result:
20  for key in the_map:
21  try:
22  result = result.replace(".oO["+key+"]Oo.",the_map[key])
23  except TypeError: #try a dict
24  try:
25  for keykey, value in the_map[key].iteritems():
26  result = result.replace(".oO[" + key + "['" + keykey + "']]Oo.", value)
27  result = result.replace(".oO[" + key + '["' + keykey + '"]]Oo.', value)
28  except AttributeError: #try a list
29  try:
30  for index, value in enumerate(the_map[key]):
31  result = result.replace(".oO[" + key + "[" + str(index) + "]]Oo.", value)
32  except TypeError:
33  raise TypeError("Something is wrong in replaceByMap! Need a string, dict, or list, but the_map(%s)=%s!"%(repr(key), repr(the_map[key])))
34  iteration += 1
35  if iteration > lifeSaver:
36  problematicLines = ""
37  for line in result.splitlines():
38  if ".oO[" in result and "]Oo." in line:
39  problematicLines += "%s\n"%line
40  msg = ("Oh Dear, there seems to be an endless loop in "
41  "replaceByMap!!\n%s\n%s"%(problematicLines, the_map))
42  raise AllInOneError(msg)
43  return result
44 
def replaceByMap
— Helpers —############################
def helperFunctions.replacelast (   string,
  old,
  new,
  count = 1 
)
Replace the last occurances of a string

Definition at line 81 of file helperFunctions.py.

Referenced by addIndex().

81 
82 def replacelast(string, old, new, count = 1):
83  """Replace the last occurances of a string"""
84  return new.join(string.rsplit(old,count))

Variable Documentation

list helperFunctions.fileExtensions = ["_cfg.py", ".sh", ".root"]

Definition at line 85 of file helperFunctions.py.