15 inputHeaderExtension=
''
16 extensionsToCheck=[
'.h',
'.c',
'.hh',
'.cc',
'.cpp']
17 inputFileProvided =
False
18 outputFileProvided=
False
20 currentDirectory = os.getcwd()
21 inputDirectory = currentDirectory
22 outputDirectory= currentDirectory
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>'
30 for option, arg
in options:
33 if not any(extension
in arg
for extension
in extensionsToCheck):
34 print "No .h, .hh, .c, .cc, .cpp file provided."
37 inputFileProvided=
True
38 elif option ==
'-h' or option ==
'--help':
39 print 'usage: ', sys.argv[0],
' -i <source path to .h or .c> -r '
41 print 'Optional Arguments:'
42 print '-d <path to input directory>'
43 print '-o <output/destination directory>'
44 print '-r <rename class>'
54 outputFileProvided =
True
57 print 'Not enough inputs provided. Please type "-h" for help'
62 if not inputFileProvided :
63 print 'Input file not provided!'
64 print 'Please type -h for help.'
67 inputClassName = os.path.splitext(inputFile)[0]
68 inputClassName = inputClassName.split(
"_interface",1)[0]
70 if not outputFileProvided :
71 print 'No new filename provided... using ', inputFile,
'.'
72 outputClassName = inputClassName
73 outputFile = inputFile
75 outputClassName = os.path.splitext(outputFile)[0]
77 if os.path.exists(outputDirectory +
"/" + outputFile):
78 print 'File with name: ', outputFile,
' already exists!'
79 print 'Cannot create new file!'
81 print 'input and output class names'
87 if not os.path.exists(inputDirectory +
"/" + inputFile):
88 print currentDirectory +
'/' + inputFile +
' does not exist!'
89 print 'Please type -h for help.'
93 inputHeaderExtension=
''
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'
102 print 'Could not find header with name ', inputClassName,
'!'
103 print 'Tried these extensions: .h, .hh, .hpp'
108 with open(inputDirectory +
'/' + inputFile,
"rt")
as fin:
109 with open(outputDirectory +
'/' + outputClassName +
'_interface.cpp',
"wt")
as fout:
111 fout.write(line.replace(inputClassName, outputClassName))
114 with open(inputDirectory +
'/' + inputClassName + inputHeaderExtension,
"rt")
as fin:
115 with open(outputDirectory +
'/' + outputClassName + inputHeaderExtension,
"wt")
as fout:
117 fout.write(line.replace(inputClassName, outputClassName))
120 cMakeListsCopyBuffer=
''
121 foundCMakeListsEntry=
False
122 if os.path.exists(inputDirectory +
"/" +
"CMakeLists.txt"):
123 print 'Found a CMakeLists in this directory!'
126 firstLine =
"simple_plugin(" + inputClassName +
" \"interface\""
128 finishedWithReading =
True
129 with open(inputDirectory +
"/" +
"CMakeLists.txt",
"rt")
as 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
146 print 'No CMakeLists found in this directory: ', inputDirectory
148 if not foundCMakeListsEntry:
150 cMakeListsCopyBuffer =
" simple_plugin(" + inputClassName +
" \"interface\""
151 cMakeListsCopyBuffer +=
" )"
153 print 'No previous entries in the CMake List were found'
155 with open(inputDirectory +
"/" +
"CMakeLists.txt",
"a")
as cMakeLists:
156 cMakeLists.write(cMakeListsCopyBuffer)
162 if __name__ ==
"__main__":