CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
helperFunctions.py
Go to the documentation of this file.
1 import os
2 from TkAlExceptions import AllInOneError
3 
4 ####################--- Helpers ---############################
5 def replaceByMap(target, the_map):
6  """This function replaces `.oO[key]Oo.` by `the_map[key]` in target.
7 
8  Arguments:
9  - `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
10  - `the_map`: Dictionary which has to contain the `key`s in `target` as keys
11  """
12 
13  result = target
14  for key in the_map:
15  lifeSaver = 10e3
16  iteration = 0
17  while ".oO[" in result and "]Oo." in result:
18  for key in the_map:
19  try:
20  result = result.replace(".oO["+key+"]Oo.",the_map[key])
21  except TypeError: #try a dict
22  try:
23  for keykey, value in the_map[key].iteritems():
24  result = result.replace(".oO[" + key + "['" + keykey + "']]Oo.", value)
25  result = result.replace(".oO[" + key + '["' + keykey + '"]]Oo.', value)
26  except AttributeError: #try a list
27  try:
28  for index, value in enumerate(the_map[key]):
29  result = result.replace(".oO[" + key + "[" + str(index) + "]]Oo.", value)
30  except TypeError:
31  raise TypeError("Something is wrong in replaceByMap! Need a string, dict, or list, but the_map(%s)=%s!"%(repr(key), repr(the_map[key])))
32  iteration += 1
33  if iteration > lifeSaver:
34  problematicLines = ""
35  for line in result.splitlines():
36  if ".oO[" in result and "]Oo." in line:
37  problematicLines += "%s\n"%line
38  msg = ("Oh Dear, there seems to be an endless loop in "
39  "replaceByMap!!\n%s\nrepMap"%problematicLines)
40  raise AllInOneError(msg)
41  return result
42 
43 
44 def getCommandOutput2(command):
45  """This function executes `command` and returns it output.
46 
47  Arguments:
48  - `command`: Shell command to be invoked by this function.
49  """
50 
51  child = os.popen(command)
52  data = child.read()
53  err = child.close()
54  if err:
55  raise RuntimeError('%s failed w/ exit code %d' % (command, err))
56  return data
57 
58 
59 def castorDirExists(path):
60  """This function checks if the directory given by `path` exists.
61 
62  Arguments:
63  - `path`: Path to castor directory
64  """
65 
66  if path[-1] == "/":
67  path = path[:-1]
68  containingPath = os.path.join( *path.split("/")[:-1] )
69  dirInQuestion = path.split("/")[-1]
70  try:
71  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
72  except RuntimeError:
73  return False
74  for line in rawLines:
75  if line.split()[0][0] == "d":
76  if line.split()[8] == dirInQuestion:
77  return True
78  return False
79 
80 def replacelast(string, old, new, count = 1):
81  """Replace the last occurances of a string"""
82  return new.join(string.rsplit(old,count))
83 
84 fileExtensions = ["_cfg.py", ".sh", ".root"]
85 
86 def addIndex(filename, njobs, index = None):
87  if index is None:
88  return [addIndex(filename, njobs, i) for i in range(njobs)]
89  if njobs == 1:
90  return filename
91 
92  fileExtension = None
93  for extension in fileExtensions:
94  if filename.endswith(extension):
95  fileExtension = extension
96  if fileExtension is None:
97  raise AllInOneError(fileName + " does not end with any of the extensions "
98  + str(fileExtensions))
99  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
def replaceByMap
— Helpers —############################