otsdaq_utilities  v2_05_02_indev
AddNewFrontEndInterface.py
1 #!/usr/bin/python
2 # Author: Daniel Parilla
3 # September 26, 2017
4 # Version: 1.00
5 
6 import sys, getopt, os
7 
8 def main(argv):
9  inputFile= ''
10  outputFile=''
11  inputClassName= ''
12  outputClassName=''
13 
14 
15  inputHeaderExtension=''
16  extensionsToCheck=['.h', '.c', '.hh', '.cc', '.cpp']
17  inputFileProvided =False
18  outputFileProvided=False
19 
20  currentDirectory = os.getcwd()
21  inputDirectory = currentDirectory
22  outputDirectory= currentDirectory #FIXME should default to OTS directory
23 
24 
25  try:
26  options, args = getopt.getopt(argv, "o:d:r:i:h", ["output=", "input=", "help"])
27  except getopt.GetoptError:
28  print 'importer.py <source path to .h or .c>'
29 
30  for option, arg in options:
31  if option == '-i':
32  #check if the input file is a valid extension
33  if not any(extension in arg for extension in extensionsToCheck):# not in arg: #check if it points to a file
34  print "No .h, .hh, .c, .cc, .cpp file provided."
35  sys.exit(2)
36  inputFile=arg
37  inputFileProvided=True
38  elif option == '-h' or option == '--help':
39  print 'usage: ', sys.argv[0], ' -i <source path to .h or .c> -r '
40  print ''
41  print 'Optional Arguments:'
42  print '-d <path to input directory>'
43  print '-o <output/destination directory>'
44  print '-r <rename class>'
45  print ''
46  print ''
47  sys.exit()
48  elif option == '-o':
49  outputDirectory=arg
50  elif option == '-d':
51  inputDirectory=arg
52  elif option == '-r':
53  outputFile=arg
54  outputFileProvided = True
55 
56  if len(sys.argv) < 3:
57  print 'Not enough inputs provided. Please type "-h" for help'
58  sys.exit(2)
59 
60  #Error Checks
61  print inputFile
62  if not inputFileProvided :
63  print 'Input file not provided!'
64  print 'Please type -h for help.'
65  sys.exit(2)
66 
67  inputClassName = os.path.splitext(inputFile)[0] #Take off the extension
68  inputClassName = inputClassName.split("_interface",1)[0] #Remove the '_interface' nomenclature
69 
70  if not outputFileProvided :
71  print 'No new filename provided... using ', inputFile, '.'
72  outputClassName = inputClassName
73  outputFile = inputFile
74  else :
75  outputClassName = os.path.splitext(outputFile)[0]
76  #Check to see if the new filename already exists
77  if os.path.exists(outputDirectory + "/" + outputFile):
78  print 'File with name: ', outputFile, ' already exists!'
79  print 'Cannot create new file!'
80 
81  print 'input and output class names'
82  print inputClassName #FIXME take off _interface if it has it
83  print outputClassName
84 
85 
86  #check if the input file is valid
87  if not os.path.exists(inputDirectory + "/" + inputFile):
88  print currentDirectory + '/' + inputFile + ' does not exist!'
89  print 'Please type -h for help.'
90  sys.exit(2)
91 
92  #need to check if the header file is .h, .hh, .hpp
93  inputHeaderExtension=''
94  foundHeader=False
95  if os.path.exists(inputDirectory + "/" + inputClassName + ".h"):
96  inputHeaderExtension='.h'
97  elif os.path.exists(inputDirectory + "/" + inputClassName + ".hh"):
98  inputHeaderExtension='.hh'
99  elif os.path.exists(inputDirectory + "/" + inputClassName + ".hpp"):
100  inputHeaderExtension='.hpp'
101  else :
102  print 'Could not find header with name ', inputClassName, '!'
103  print 'Tried these extensions: .h, .hh, .hpp'
104  sys.exit(2)
105 
106  #FIXME? Will it break if we change a .c to a .cpp
107  #replace the old class name with the new one in the source file
108  with open(inputDirectory + '/' + inputFile, "rt") as fin:
109  with open(outputDirectory + '/' + outputClassName + '_interface.cpp', "wt") as fout:
110  for line in fin:
111  fout.write(line.replace(inputClassName, outputClassName))
112 
113  #replace the old class name with the new one in the header file
114  with open(inputDirectory + '/' + inputClassName + inputHeaderExtension, "rt") as fin:
115  with open(outputDirectory + '/' + outputClassName + inputHeaderExtension, "wt") as fout:
116  for line in fin:
117  fout.write(line.replace(inputClassName, outputClassName))
118 
119  #Grab CMake and make changes
120  cMakeListsCopyBuffer=''
121  foundCMakeListsEntry=False
122  if os.path.exists(inputDirectory + "/" + "CMakeLists.txt"):
123  print 'Found a CMakeLists in this directory!'
124 
125  #looking for the line "simple_plugin(/*Interface Name*/Interface "interface"
126  firstLine = "simple_plugin(" + inputClassName + " \"interface\""
127  lastLine = ")"
128  finishedWithReading = True
129  with open(inputDirectory + "/" + "CMakeLists.txt", "rt") as fin:
130  for line in fin:
131  if firstLine in line:
132  print 'Located the plugin ', inputClassName, ' in the CMakeLists!'
133  cMakeListsCopyBuffer += line.replace(inputClassName, outputClassName)
134  finishedWithReading = False
135  foundCMakeListsEntry= True
136  elif lastLine in line and not finishedWithReading:
137  cMakeListsCopyBuffer += line
138  finishedWithReading = True
139  elif not finishedWithReading :
140  cMakeListsCopyBuffer += line
141 
142  #print cMakeListsCopyBuffer
143 
144 
145  else:
146  print 'No CMakeLists found in this directory: ', inputDirectory
147 
148  if not foundCMakeListsEntry:
149  #Add in default behavior for CMakeLists
150  cMakeListsCopyBuffer = " simple_plugin(" + inputClassName + " \"interface\""
151  cMakeListsCopyBuffer += " )"
152 
153  print 'No previous entries in the CMake List were found'
154 
155  with open(inputDirectory + "/" + "CMakeLists.txt", "a") as cMakeLists:
156  cMakeLists.write(cMakeListsCopyBuffer)
157 
158 
159 
160 
161 
162 if __name__ == "__main__":
163  main(sys.argv[1:])