Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:09

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