Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:44

0001 from __future__ import print_function
0002 import os,sys,imp
0003 import subprocess
0004 import logging
0005 import fnmatch
0006 import signal
0007 
0008 log = logging.getLogger(__name__)
0009 
0010 def replaceTemplate(template,**opts):
0011     result = open(template).read()
0012     for item in opts:
0013          old = '@@%s@@'%item
0014          new = str(opts[item])
0015          print("Replacing",old,"to",new)
0016          result = result.replace(old,new)
0017 
0018     return result
0019 
0020 def getDatasetStr(datasetpath):
0021     datasetstr = datasetpath
0022     datasetstr.strip()
0023     if datasetstr[0] == '/': datasetstr = datasetstr[1:]
0024     datasetstr = datasetstr.replace('/','_')
0025 
0026     return datasetstr
0027 
0028 def listFilesLocal(paths, extension = '.root'):
0029     file_paths = []
0030     for path in paths:
0031         if not os.path.exists( path ):
0032             log.error( "Specified input path '%s' does not exist!" % path )
0033             continue
0034         if path.endswith( extension ):
0035             file_paths.append( path )
0036         for root, dirnames, filenames in os.walk( path ):
0037             for filename in fnmatch.filter( filenames, '*' + extension ):
0038                 file_paths.append( os.path.join( root, filename ) )
0039     return file_paths
0040 
0041 def haddLocal(localdir,result_file,extension = 'root'):
0042     if not os.path.exists( localdir ):
0043         raise ValueError("localdir for hadd operation does not exist" )
0044 
0045     files = listFilesLocal([localdir],extension)
0046     process = subprocess.Popen( ['hadd','-f', result_file] + files,
0047                                 stdout=subprocess.PIPE,
0048                                 stderr=subprocess.STDOUT)
0049     stdout = process.communicate()[0]
0050     return process.returncode
0051 
0052 def loadCmsProcessFile(psetName):
0053     pset = imp.load_source("psetmodule",psetName)
0054     return pset.process
0055 
0056 def loadCmsProcess(psetPath):
0057     module = __import__(psetPath)
0058     process = sys.modules[psetPath].process
0059 
0060     import copy
0061     #FIXME: clone process
0062     #processNew = copy.deepcopy(process)
0063     processNew = copy.copy(process)
0064     return processNew
0065 
0066 def prependPaths(process,seqname):
0067     for path in process.paths:
0068         getattr(process,path)._seq = getattr(process,seqname)*getattr(process,path)._seq
0069 
0070 def stdinWait(text, default, time, timeoutDisplay = None, **kwargs):
0071     # taken and adjusted from http://stackoverflow.com/a/25860968
0072     signal.signal(signal.SIGALRM, interrupt)
0073     signal.alarm(time) # sets timeout
0074     global timeout
0075     try:
0076         inp = raw_input(text)
0077         signal.alarm(0)
0078         timeout = False
0079     except (KeyboardInterrupt):
0080         printInterrupt = kwargs.get("printInterrupt", True)
0081         if printInterrupt:
0082             print("Keyboard interrupt")
0083         timeout = True # Do this so you don't mistakenly get input when there is none
0084         inp = default
0085     except:
0086         timeout = True
0087         if not timeoutDisplay is None:
0088             print(timeoutDisplay)
0089         signal.alarm(0)
0090         inp = default
0091     return inp
0092 
0093 def interrupt(signum, frame):
0094     raise Exception("")
0095 
0096 def getTerminalSize():
0097     #taken from http://stackoverflow.com/a/566752
0098     # returns width, size of terminal
0099     env = os.environ
0100     def ioctl_GWINSZ(fd):
0101         try:
0102             import fcntl, termios, struct, os
0103             cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
0104         '1234'))
0105         except:
0106             return
0107         return cr
0108     cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
0109     if not cr:
0110         try:
0111             fd = os.open(os.ctermid(), os.O_RDONLY)
0112             cr = ioctl_GWINSZ(fd)
0113             os.close(fd)
0114         except:
0115             pass
0116     if not cr:
0117         cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
0118 
0119         ### Use get(key[, default]) instead of a try/catch
0120         #try:
0121         #    cr = (env['LINES'], env['COLUMNS'])
0122         #except:
0123         #    cr = (25, 80)
0124     return int(cr[1]), int(cr[0])