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  result = result.replace(".oO["+key+"]Oo.",the_map[key])
20  iteration += 1
21  if iteration > lifeSaver:
22  problematicLines = ""
23  for line in result.splitlines():
24  if ".oO[" in result and "]Oo." in line:
25  problematicLines += "%s\n"%line
26  msg = ("Oh Dear, there seems to be an endless loop in "
27  "replaceByMap!!\n%s\nrepMap"%problematicLines)
28  raise AllInOneError(msg)
29  return result
30 
31 
32 def getCommandOutput2(command):
33  """This function executes `command` and returns it output.
34 
35  Arguments:
36  - `command`: Shell command to be invoked by this function.
37  """
38 
39  child = os.popen(command)
40  data = child.read()
41  err = child.close()
42  if err:
43  raise RuntimeError, '%s failed w/ exit code %d' % (command, err)
44  return data
45 
46 
47 def castorDirExists(path):
48  """This function checks if the directory given by `path` exists.
49 
50  Arguments:
51  - `path`: Path to castor directory
52  """
53 
54  if path[-1] == "/":
55  path = path[:-1]
56  containingPath = os.path.join( *path.split("/")[:-1] )
57  dirInQuestion = path.split("/")[-1]
58  try:
59  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
60  except RuntimeError:
61  return False
62  for line in rawLines:
63  if line.split()[0][0] == "d":
64  if line.split()[8] == dirInQuestion:
65  return True
66  return False
def replaceByMap
— Helpers —############################