ATLAS Offline Software
d3pdReaderSFrameMaker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # $Id: d3pdReaderSFrameMaker.py 471380 2011-11-29 15:25:42Z krasznaa $
3 #
4 # This script can be used to quickly create an SFrame package out of the
5 # source files generated in a previous step.
6 #
7 # @author Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch>
8 
9 # Base module(s):
10 import sys
11 import os
12 import re
13 
14 # Local module(s):
15 from D3PDMakerReader.Helpers import *
16 
17 
24 def main():
25 
26  # Create a logger object first of all:
27  from AthenaCommon.Logging import logging
28  logger = logging.getLogger( os.path.splitext( os.path.basename( sys.argv[ 0 ] ) )[ 0 ] )
29 
30  #
31  # Specify the command line options:
32  #
33  desc = "This script can be used to generate an SFrame package out of the D3PDReader " \
34  "sources generated by one of the D3PDReader code generators."
35  vers = "$Revision: 471380 $"
36  import optparse
37  parser = optparse.OptionParser( description = desc,
38  version = vers,
39  usage = "%prog [options] <source files>" )
40  parser.add_option( "-v", "--verbosity", dest = "verbosity",
41  action = "store", type = "int", default = 3,
42  help = "Message verbosity level" )
43  parser.add_option( "-p", "--package-name", dest = "packagename",
44  action = "store", type = "string", default = "D3PDReader",
45  help = "Name for the RootCore package" )
46  parser.add_option( "-o", "--output", dest = "output",
47  action = "store", type = "string", default = "./",
48  help = "Output directory for the RootCore package" )
49 
50  # Parse the command line options:
51  ( options, files ) = parser.parse_args()
52 
53  # Do a small sanity check:
54  if not len( files ):
55  logger.error( "You have to define at least one source file!" )
56  parser.print_help()
57  return 255
58 
59  # Set the default logging level:
60  logging.getLogger( "" ).setLevel( options.verbosity )
61 
62  # Print some welcome message:
63  logger.info( "***********************************************************" )
64  logger.info( "* D3PDReader SFrame Package Maker" )
65  logger.info( "* Version: $Revision: 471380 $" )
66  logger.info( "*" )
67  logger.info( "* Output level: " + str( options.verbosity ) )
68  logger.info( "* Package name: " + options.packagename )
69  logger.info( "* Output directory: " + options.output )
70  logger.info( "* Source files: " + ", ".join( files ) )
71  logger.info( "***********************************************************" )
72 
73  # Filter out the header and source files:
74  ( header_files, source_files ) = separateSources( files )
75  dictionary_header_files = dictionaryHeaders( files )
76 
77  # Create the package's skeleton:
78  if makeSFramePackageSkeleton( options.output, options.packagename ):
79  logger.error( "Failed to create RootCore package skeleton" )
80  return 255
81 
82  # Copy the header files:
83  import shutil
84  for header in header_files:
85  shutil.copy( header, options.output + "/" + options.packagename + "/include/" )
86  pass
87 
88  # Copy the source files:
89  for source in source_files:
90  shutil.copy( source, options.output + "/" + options.packagename + "/src/" )
91  pass
92 
93  # Transform the include paths in the source files:
94  sframe_sources = [ ( options.output + "/" + options.packagename + "/src/" + \
95  os.path.basename( source ) ) for source in source_files ]
96  for source in sframe_sources:
97  # Read in the file's contents:
98  sfile = open( source, "r" )
99  cont = sfile.read()
100  sfile.close()
101 
102  # Modify the file's contents:
103  cont = re.sub( "#include \"(.*)\"",
104  "#include \"../include/\g<1>\"", cont )
105 
106  # Overwrite the original file:
107  sfile = open( source, "w" )
108  sfile.write( cont )
109  sfile.close()
110  pass
111 
112  # Extract the class names to generate a dictionary for:
113  classnames = dictionaryClasses( dictionary_header_files )
114 
115  # Create the LinkDef file:
116  if writeLinkDefFile( options.output + "/" + options.packagename + "/include/" + \
117  options.packagename + "_LinkDef.h", classnames ):
118  logger.error( "Failed writing SFrame LinkDef.h file" )
119  return 255
120 
121  # Write the ChangeLog, just for fun:
122  import datetime
123  clog = open( options.output + "/" + options.packagename + "/ChangeLog", "w" )
124  clog.write( "%s d3pdReaderSFrameMaker.py <Attila.Krasznahorkay@cern.ch>\n" % \
125  datetime.date.today() )
126  clog.write( "\t* Created the package\n" )
127  clog.close()
128 
129  return 0
130 
131 # Execute the main function:
132 if __name__ == "__main__":
133  import sys
134  sys.exit( main() )
d3pdReaderSFrameMaker.main
def main()
C/C++ style main function.
Definition: d3pdReaderSFrameMaker.py:24
pool::DbPrintLvl::setLevel
void setLevel(MsgLevel l)
Definition: DbPrint.h:32
python.Helpers.separateSources
def separateSources(filenames)
Separate the source and header files based on their names.
Definition: PhysicsAnalysis/D3PDMaker/D3PDMakerReader/python/Helpers.py:17
python.Helpers.dictionaryClasses
def dictionaryClasses(filenames)
Function collecting the class names to create a dictionary for.
Definition: PhysicsAnalysis/D3PDMaker/D3PDMakerReader/python/Helpers.py:80
python.Helpers.writeLinkDefFile
def writeLinkDefFile(filename, classnames, headers=[])
Function writing a LinkDef file.
Definition: PhysicsAnalysis/D3PDMaker/D3PDMakerReader/python/Helpers.py:172
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
Helpers
python.Helpers.makeSFramePackageSkeleton
def makeSFramePackageSkeleton(directory, name)
Create an SFrame package skeleton.
Definition: PhysicsAnalysis/D3PDMaker/D3PDMakerReader/python/Helpers.py:345
python.Helpers.dictionaryHeaders
def dictionaryHeaders(filenames)
Find the headers needed for dictionary generation.
Definition: PhysicsAnalysis/D3PDMaker/D3PDMakerReader/python/Helpers.py:48