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  for index, value in enumerate(the_map[key]):
28  result = result.replace(".oO[" + key + "[" + str(index) + "]]Oo.", value)
29  iteration += 1
30  if iteration > lifeSaver:
31  problematicLines = ""
32  for line in result.splitlines():
33  if ".oO[" in result and "]Oo." in line:
34  problematicLines += "%s\n"%line
35  msg = ("Oh Dear, there seems to be an endless loop in "
36  "replaceByMap!!\n%s\nrepMap"%problematicLines)
37  raise AllInOneError(msg)
38  return result
39 
40 
41 def getCommandOutput2(command):
42  """This function executes `command` and returns it output.
43 
44  Arguments:
45  - `command`: Shell command to be invoked by this function.
46  """
47 
48  child = os.popen(command)
49  data = child.read()
50  err = child.close()
51  if err:
52  raise RuntimeError('%s failed w/ exit code %d' % (command, err))
53  return data
54 
55 
56 def castorDirExists(path):
57  """This function checks if the directory given by `path` exists.
58 
59  Arguments:
60  - `path`: Path to castor directory
61  """
62 
63  if path[-1] == "/":
64  path = path[:-1]
65  containingPath = os.path.join( *path.split("/")[:-1] )
66  dirInQuestion = path.split("/")[-1]
67  try:
68  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
69  except RuntimeError:
70  return False
71  for line in rawLines:
72  if line.split()[0][0] == "d":
73  if line.split()[8] == dirInQuestion:
74  return True
75  return False
76 
77 def replacelast(string, old, new, count = 1):
78  """Replace the last occurances of a string"""
79  return new.join(string.rsplit(old,count))
80 
81 fileExtensions = ["_cfg.py", ".sh", ".root"]
82 
83 def addIndex(filename, njobs, index = None):
84  if index is None:
85  return [addIndex(filename, njobs, i) for i in range(njobs)]
86  if njobs == 1:
87  return filename
88 
89  fileExtension = None
90  for extension in fileExtensions:
91  if filename.endswith(extension):
92  fileExtension = extension
93  if fileExtension is None:
94  raise AllInOneError(fileName + " does not end with any of the extensions "
95  + str(fileExtensions))
96  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
def replaceByMap
— Helpers —############################