00001
00002
00003
00004
00005
00006 import sys, getopt, os
00007
00008 def main(argv):
00009 inputFile= ''
00010 outputFile=''
00011 inputClassName= ''
00012 outputClassName=''
00013
00014
00015 inputHeaderExtension=''
00016 extensionsToCheck=['.h', '.c', '.hh', '.cc', '.cpp']
00017 inputFileProvided =False
00018 outputFileProvided=False
00019
00020 currentDirectory = os.getcwd()
00021 inputDirectory = currentDirectory
00022 outputDirectory= currentDirectory
00023
00024
00025 try:
00026 options, args = getopt.getopt(argv, "o:d:r:i:h", ["output=", "input=", "help"])
00027 except getopt.GetoptError:
00028 print 'importer.py <source path to .h or .c>'
00029
00030 for option, arg in options:
00031 if option == '-i':
00032
00033 if not any(extension in arg for extension in extensionsToCheck):
00034 print "No .h, .hh, .c, .cc, .cpp file provided."
00035 sys.exit(2)
00036 inputFile=arg
00037 inputFileProvided=True
00038 elif option == '-h' or option == '--help':
00039 print 'usage: ', sys.argv[0], ' -i <source path to .h or .c> -r '
00040 print ''
00041 print 'Optional Arguments:'
00042 print '-d <path to input directory>'
00043 print '-o <output/destination directory>'
00044 print '-r <rename class>'
00045 print ''
00046 print ''
00047 sys.exit()
00048 elif option == '-o':
00049 outputDirectory=arg
00050 elif option == '-d':
00051 inputDirectory=arg
00052 elif option == '-r':
00053 outputFile=arg
00054 outputFileProvided = True
00055
00056 if len(sys.argv) < 3:
00057 print 'Not enough inputs provided. Please type "-h" for help'
00058 sys.exit(2)
00059
00060
00061 print inputFile
00062 if not inputFileProvided :
00063 print 'Input file not provided!'
00064 print 'Please type -h for help.'
00065 sys.exit(2)
00066
00067 inputClassName = os.path.splitext(inputFile)[0]
00068 inputClassName = inputClassName.split("_interface",1)[0]
00069
00070 if not outputFileProvided :
00071 print 'No new filename provided... using ', inputFile, '.'
00072 outputClassName = inputClassName
00073 outputFile = inputFile
00074 else :
00075 outputClassName = os.path.splitext(outputFile)[0]
00076
00077 if os.path.exists(outputDirectory + "/" + outputFile):
00078 print 'File with name: ', outputFile, ' already exists!'
00079 print 'Cannot create new file!'
00080
00081 print 'input and output class names'
00082 print inputClassName
00083 print outputClassName
00084
00085
00086
00087 if not os.path.exists(inputDirectory + "/" + inputFile):
00088 print currentDirectory + '/' + inputFile + ' does not exist!'
00089 print 'Please type -h for help.'
00090 sys.exit(2)
00091
00092
00093 inputHeaderExtension=''
00094 foundHeader=False
00095 if os.path.exists(inputDirectory + "/" + inputClassName + ".h"):
00096 inputHeaderExtension='.h'
00097 elif os.path.exists(inputDirectory + "/" + inputClassName + ".hh"):
00098 inputHeaderExtension='.hh'
00099 elif os.path.exists(inputDirectory + "/" + inputClassName + ".hpp"):
00100 inputHeaderExtension='.hpp'
00101 else :
00102 print 'Could not find header with name ', inputClassName, '!'
00103 print 'Tried these extensions: .h, .hh, .hpp'
00104 sys.exit(2)
00105
00106
00107
00108 with open(inputDirectory + '/' + inputFile, "rt") as fin:
00109 with open(outputDirectory + '/' + outputClassName + '_interface.cpp', "wt") as fout:
00110 for line in fin:
00111 fout.write(line.replace(inputClassName, outputClassName))
00112
00113
00114 with open(inputDirectory + '/' + inputClassName + inputHeaderExtension, "rt") as fin:
00115 with open(outputDirectory + '/' + outputClassName + inputHeaderExtension, "wt") as fout:
00116 for line in fin:
00117 fout.write(line.replace(inputClassName, outputClassName))
00118
00119
00120 cMakeListsCopyBuffer=''
00121 foundCMakeListsEntry=False
00122 if os.path.exists(inputDirectory + "/" + "CMakeLists.txt"):
00123 print 'Found a CMakeLists in this directory!'
00124
00125
00126 firstLine = "simple_plugin(" + inputClassName + " \"interface\""
00127 lastLine = ")"
00128 finishedWithReading = True
00129 with open(inputDirectory + "/" + "CMakeLists.txt", "rt") as fin:
00130 for line in fin:
00131 if firstLine in line:
00132 print 'Located the plugin ', inputClassName, ' in the CMakeLists!'
00133 cMakeListsCopyBuffer += line.replace(inputClassName, outputClassName)
00134 finishedWithReading = False
00135 foundCMakeListsEntry= True
00136 elif lastLine in line and not finishedWithReading:
00137 cMakeListsCopyBuffer += line
00138 finishedWithReading = True
00139 elif not finishedWithReading :
00140 cMakeListsCopyBuffer += line
00141
00142
00143
00144
00145 else:
00146 print 'No CMakeLists found in this directory: ', inputDirectory
00147
00148 if not foundCMakeListsEntry:
00149
00150 cMakeListsCopyBuffer = " simple_plugin(" + inputClassName + " \"interface\""
00151 cMakeListsCopyBuffer += " )"
00152
00153 print 'No previous entries in the CMake List were found'
00154
00155 with open(inputDirectory + "/" + "CMakeLists.txt", "a") as cMakeLists:
00156 cMakeLists.write(cMakeListsCopyBuffer)
00157
00158
00159
00160
00161
00162 if __name__ == "__main__":
00163 main(sys.argv[1:])