CMS 3D CMS Logo

tools.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import os,sys,imp
3 import subprocess
4 import logging
5 import fnmatch
6 import signal
7 
8 log = logging.getLogger(__name__)
9 
10 def replaceTemplate(template,**opts):
11  result = open(template).read()
12  for item in opts:
13  old = '@@%s@@'%item
14  new = str(opts[item])
15  print("Replacing",old,"to",new)
16  result = result.replace(old,new)
17 
18  return result
19 
20 def getDatasetStr(datasetpath):
21  datasetstr = datasetpath
22  datasetstr.strip()
23  if datasetstr[0] == '/': datasetstr = datasetstr[1:]
24  datasetstr = datasetstr.replace('/','_')
25 
26  return datasetstr
27 
28 def listFilesLocal(paths, extension = '.root'):
29  file_paths = []
30  for path in paths:
31  if not os.path.exists( path ):
32  log.error( "Specified input path '%s' does not exist!" % path )
33  continue
34  if path.endswith( extension ):
35  file_paths.append( path )
36  for root, dirnames, filenames in os.walk( path ):
37  for filename in fnmatch.filter( filenames, '*' + extension ):
38  file_paths.append( os.path.join( root, filename ) )
39  return file_paths
40 
41 def haddLocal(localdir,result_file,extension = 'root'):
42  if not os.path.exists( localdir ):
43  raise ValueError("localdir for hadd operation does not exist" )
44 
45  files = listFilesLocal([localdir],extension)
46  process = subprocess.Popen( ['hadd','-f', result_file] + files,
47  stdout=subprocess.PIPE,
48  stderr=subprocess.STDOUT)
49  stdout = process.communicate()[0]
50  return process.returncode
51 
52 def loadCmsProcessFile(psetName):
53  pset = imp.load_source("psetmodule",psetName)
54  return pset.process
55 
56 def loadCmsProcess(psetPath):
57  module = __import__(psetPath)
58  process = sys.modules[psetPath].process
59 
60  import copy
61  #FIXME: clone process
62  #processNew = copy.deepcopy(process)
63  processNew = copy.copy(process)
64  return processNew
65 
66 def prependPaths(process,seqname):
67  for path in process.paths:
68  getattr(process,path)._seq = getattr(process,seqname)*getattr(process,path)._seq
69 
70 def stdinWait(text, default, time, timeoutDisplay = None, **kwargs):
71  # taken and adjusted from http://stackoverflow.com/a/25860968
72  signal.signal(signal.SIGALRM, interrupt)
73  signal.alarm(time) # sets timeout
74  global timeout
75  try:
76  inp = raw_input(text)
77  signal.alarm(0)
78  timeout = False
79  except (KeyboardInterrupt):
80  printInterrupt = kwargs.get("printInterrupt", True)
81  if printInterrupt:
82  print("Keyboard interrupt")
83  timeout = True # Do this so you don't mistakenly get input when there is none
84  inp = default
85  except:
86  timeout = True
87  if not timeoutDisplay is None:
88  print(timeoutDisplay)
89  signal.alarm(0)
90  inp = default
91  return inp
92 
93 def interrupt(signum, frame):
94  raise Exception("")
95 
97  #taken from http://stackoverflow.com/a/566752
98  # returns width, size of terminal
99  env = os.environ
100  def ioctl_GWINSZ(fd):
101  try:
102  import fcntl, termios, struct, os
103  cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
104  '1234'))
105  except:
106  return
107  return cr
108  cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
109  if not cr:
110  try:
111  fd = os.open(os.ctermid(), os.O_RDONLY)
112  cr = ioctl_GWINSZ(fd)
113  os.close(fd)
114  except:
115  pass
116  if not cr:
117  cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
118 
119  ### Use get(key[, default]) instead of a try/catch
120  #try:
121  # cr = (env['LINES'], env['COLUMNS'])
122  #except:
123  # cr = (25, 80)
124  return int(cr[1]), int(cr[0])
def getDatasetStr(datasetpath)
Definition: tools.py:20
def replaceTemplate(template, opts)
Definition: tools.py:10
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def haddLocal(localdir, result_file, extension='root')
Definition: tools.py:41
def interrupt(signum, frame)
Definition: tools.py:93
def prependPaths(process, seqname)
Definition: tools.py:66
def loadCmsProcess(psetPath)
Definition: tools.py:56
def getTerminalSize()
Definition: tools.py:96
def stdinWait(text, default, time, timeoutDisplay=None, kwargs)
Definition: tools.py:70
def loadCmsProcessFile(psetName)
Definition: tools.py:52
#define str(s)
def listFilesLocal(paths, extension='.root')
Definition: tools.py:28