Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:29

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(files,result_file,extension = 'root'):
0042     log.info("hadd command: {}".format(" ".join(['hadd','-f', result_file] + files)))
0043     process = subprocess.Popen( ['hadd','-f', result_file] + files,
0044                                 stdout=subprocess.PIPE,
0045                                 stderr=subprocess.STDOUT)
0046     stdout = process.communicate()[0]
0047     log.info(f"hadd output: {stdout}")
0048     return process.returncode
0049 
0050 def loadCmsProcessFile(psetName):
0051     pset = imp.load_source("psetmodule",psetName)
0052     return pset.process
0053 
0054 def loadCmsProcess(psetPath):
0055     module = __import__(psetPath)
0056     process = sys.modules[psetPath].process
0057 
0058     import copy
0059     #FIXME: clone process
0060     #processNew = copy.deepcopy(process)
0061     processNew = copy.copy(process)
0062     return processNew
0063 
0064 def prependPaths(process,seqname):
0065     for path in process.paths:
0066         getattr(process,path)._seq = getattr(process,seqname)*getattr(process,path)._seq
0067 
0068 def stdinWait(text, default, time, timeoutDisplay = None, **kwargs):
0069     # taken and adjusted from http://stackoverflow.com/a/25860968
0070     signal.signal(signal.SIGALRM, interrupt)
0071     signal.alarm(time) # sets timeout
0072     global timeout
0073     try:
0074         inp = raw_input(text)
0075         signal.alarm(0)
0076         timeout = False
0077     except (KeyboardInterrupt):
0078         printInterrupt = kwargs.get("printInterrupt", True)
0079         if printInterrupt:
0080             print("Keyboard interrupt")
0081         timeout = True # Do this so you don't mistakenly get input when there is none
0082         inp = default
0083     except:
0084         timeout = True
0085         if not timeoutDisplay is None:
0086             print(timeoutDisplay)
0087         signal.alarm(0)
0088         inp = default
0089     return inp
0090 
0091 def interrupt(signum, frame):
0092     raise Exception("")
0093 
0094 def getTerminalSize():
0095     #taken from http://stackoverflow.com/a/566752
0096     # returns width, size of terminal
0097     env = os.environ
0098     def ioctl_GWINSZ(fd):
0099         try:
0100             import fcntl, termios, struct, os
0101             cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
0102         '1234'))
0103         except:
0104             return
0105         return cr
0106     cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
0107     if not cr:
0108         try:
0109             fd = os.open(os.ctermid(), os.O_RDONLY)
0110             cr = ioctl_GWINSZ(fd)
0111             os.close(fd)
0112         except:
0113             pass
0114     if not cr:
0115         cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
0116 
0117         ### Use get(key[, default]) instead of a try/catch
0118         #try:
0119         #    cr = (env['LINES'], env['COLUMNS'])
0120         #except:
0121         #    cr = (25, 80)
0122     return int(cr[1]), int(cr[0])