def checkTasksFile(self): ''' Check if 'tasks.json' file exists. If it does, check if it is a valid JSON file. If it doesn't exist, create new according to template. ''' if utils.pathExists(utils.tasksPath): # file exists, check if it loads OK try: with open(utils.tasksPath, 'r') as tasksFile: json.load(tasksFile) print("Existing 'tasks.json' file found.") return except Exception as err: errorMsg = "Invalid 'tasks.json' file. Creating backup and new one.\n" errorMsg += "Possible cause: invalid json format or comments (not supported by this scripts). Error:\n" errorMsg += str(err) print(errorMsg) utils.copyAndRename(utils.tasksPath, utils.tasksBackupPath) self.createTasksFile() else: # 'tasks.json' file does not exist jet, create it according to template string self.createTasksFile()
def restoreOriginalMakefile(self): ''' Check wether current 'Makefile' has print capabilities. If it has, this means it was already altered by this script. If it was, replace it with backup copy: 'Makefile.backup'. If it does not have prin capabilities, is is assumed 'Makefile' was regenerated with CubeMX tool - print function is added and backup file is overwritten with this new 'Makefile'. At the end, add 'print-variable' capabilities ''' if utils.pathExists(utils.makefileBackupPath): # Makefile.backup exists, check if it is original (no print capabilities) if self.hasPrintCapabilities( pathToMakefile=utils.makefileBackupPath): errorMsg = "Makefile.backup exist, but looks like it was already modified!\n" errorMsg += "Did you manually delete, replace or modify any of Makefiles? " errorMsg += "Delete all Makefiles and regenerate with CubeMX." utils.printAndQuit(errorMsg) else: # OK - seems like original Makefile, replace Makefile with Makefile.backup, add print capabilities utils.copyAndRename(utils.makefileBackupPath, utils.makefilePath) else: # Makefile.backup does not exist, check if current Makefile has print capabilities. if self.hasPrintCapabilities(pathToMakefile=utils.makefilePath): errorMsg = "Looks like Makefile was already modified! Makefile.backup does not exist.\n" errorMsg += "Did you manually delete, replace or modify any of Makefiles? " errorMsg += "Delete all Makefiles and regenerate with CubeMX." utils.printAndQuit(errorMsg) else: # Makefile looks like an original one. Create a backup copy and add print capabilities utils.copyAndRename(utils.makefilePath, utils.makefileBackupPath) self.addMakefileCustomFunctions(pathToMakefile=utils.makefilePath)
def checkCPropertiesFile(self): ''' Check if 'c_cpp_properties.json' file exists. If it does, check if it is a valid JSON file. If it doesn't exist, create new according to template. ''' if utils.pathExists(utils.cPropertiesPath): # file exists, check if it loads OK try: with open(utils.cPropertiesPath, 'r') as cPropertiesFile: currentData = json.load(cPropertiesFile) # this is a valid json file print("Existing valid 'c_cpp_properties.json' file found.") # merge current 'c_cpp_properties.json' with its template templateData = json.loads(tmpStr.c_cpp_template) dataToWrite = utils.mergeCurrentDataWithTemplate(currentData, templateData) dataToWrite = json.dumps(dataToWrite, indent=4, sort_keys=False) with open(utils.cPropertiesPath, 'w') as cPropertiesFile: cPropertiesFile.write(dataToWrite) print("\tKeys updated according to the template.") return except Exception as err: errorMsg = "Invalid 'c_cpp_properties.json' file. Creating backup and new one.\n" errorMsg += "Possible cause: invalid json format or comments (not supported by this scripts). Error:\n" errorMsg += str(err) print(errorMsg) utils.copyAndRename(utils.cPropertiesPath, utils.cPropertiesBackupPath) self.createCPropertiesFile() else: # 'c_cpp_properties.json' file does not exist jet, create it according to template string self.createCPropertiesFile()
def restoreOriginalMakefile(self): ''' Check wether current 'Makefile' has print capabilities. If it has, this means it was already altered by this script. If it was, replace it with backup copy: 'Makefile.backup'. If it does not have print capabilities, it is assumed 'Makefile' was regenerated with CubeMX tool - print function is added and backup file is overwritten with this new 'Makefile'. At the end, fresh 'Makefile' with print function should be available. ''' if utils.pathExists(utils.makefilePath): # Makefile exists, check if it is original (no print capabilities) if self.hasPrintCapabilities(utils.makefilePath): # Makefile exists, already modified if utils.pathExists(utils.makefileBackupPath): # can original file be restored from backup file? if self.hasPrintCapabilities(utils.makefileBackupPath): errorMsg = "Both, 'Makefile' and 'Makefile.backup' exists, but they are both modified!\n" errorMsg += "Did you manually delete, replace or modify any of Makefiles?\n" errorMsg += "-> Delete all Makefiles and regenerate with CubeMX." utils.printAndQuit(errorMsg) else: # original will be restored from backup file print( "Existing 'Makefile' file will be restored from 'Makefile.backup'." ) utils.copyAndRename(utils.makefileBackupPath, utils.makefilePath) else: errorMsg = "'Makefile.backup' does not exist, while 'Makefile' was already modified!\n" errorMsg += "Did you manually delete, replace or modify any of Makefiles?\n" errorMsg += "-> Delete all Makefiles and regenerate with CubeMX." utils.printAndQuit(errorMsg) else: print("Existing 'Makefile' file found (original).") utils.copyAndRename(utils.makefilePath, utils.makefileBackupPath) elif utils.pathExists(utils.makefileBackupPath): # Makefile does not exist, but Makefile.backup does if self.hasPrintCapabilities(utils.makefileBackupPath): errorMsg = "'Makefile.backup' exists, but is already modified!\n" errorMsg += "Did you manually delete, replace or modify any of Makefiles?\n" errorMsg += "-> Delete all Makefiles and regenerate with CubeMX." utils.printAndQuit(errorMsg) else: # original will be restored from backup file print( "'Makefile' file will be restored from 'Makefile.backup'.") utils.copyAndRename(utils.makefileBackupPath, utils.makefilePath) else: errorMsg = "No Makefiles available, unable to proceed!\n" errorMsg += "-> Regenerate with CubeMX." utils.printAndQuit(errorMsg) self.addMakefileCustomFunctions(pathToMakefile=utils.makefilePath)