Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-29 03:17:18

0001 import os,sys,imp
0002 import subprocess
0003 import logging
0004 import fnmatch
0005 import select
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 
0068 def stdinWait(prompt_text, default, time, timeoutDisplay = None, **kwargs):
0069     print(prompt_text)
0070     i, o, e = select.select( [sys.stdin], [], [], 10 )
0071 
0072     if (i):
0073         inp = sys.stdin.readline().strip()
0074     else:
0075         inp = default
0076     return inp
0077 
0078 def interrupt(signum, frame):
0079     raise Exception("")
0080 
0081 def getTerminalSize():
0082     #taken from http://stackoverflow.com/a/566752
0083     # returns width, size of terminal
0084     env = os.environ
0085     def ioctl_GWINSZ(fd):
0086         try:
0087             import fcntl, termios, struct, os
0088             cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
0089         '1234'))
0090         except:
0091             return
0092         return cr
0093     cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
0094     if not cr:
0095         try:
0096             fd = os.open(os.ctermid(), os.O_RDONLY)
0097             cr = ioctl_GWINSZ(fd)
0098             os.close(fd)
0099         except:
0100             pass
0101     if not cr:
0102         cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
0103 
0104         ### Use get(key[, default]) instead of a try/catch
0105         #try:
0106         #    cr = (env['LINES'], env['COLUMNS'])
0107         #except:
0108         #    cr = (25, 80)
0109     return int(cr[1]), int(cr[0])