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()
Exemplo n.º 2
0
    def prepareBuildData(self, request=False):
        '''
        This function is used in all 'update*.py' scripts and makes sure, that 'toolsPaths.json' and 'buildData.json' with a 
        valid tools/target cofniguration paths exist. Invalid paths are updated (requested from the user).
        Returns available, valid build data.

        Note: tools paths listed in 'BuildDataStrings.toolsPaths' are stored in system local 'toolsPaths.json' file, and are 
        copied (overwritten) to 'buildData.json' on first 'Update' task run. This makes it possible for multiple code contributors.
        '''
        paths = pth.UpdatePaths()

        self.checkBuildDataFile()
        buildData = self.getBuildData()

        if self.checkToolsPathFile():  # a valid toolsPaths.json exists
            toolsPathsData = self.getToolsPathsData()

        else:
            # no valid data from 'toolsPaths.json' file
            # try to get data from current 'buildData.json' - backward compatibility for paths that already exist in 'buildData.json'
            toolsPathsData = json.loads(tmpStr.toolsPathsTemplate)
            for path in self.bStr.toolsPaths:
                if path in buildData:
                    if utils.pathExists(buildData[path]):
                        toolsPathsData[path] = buildData[path]

        # update/overwrite tools paths file. Don't mind if paths are already valid.
        toolsPathsData = paths.verifyToolsPaths(toolsPathsData, request)
        self.createUserToolsFile(toolsPathsData)

        buildData = self.addToolsPathsToBuildData(buildData, toolsPathsData)

        templateBuildData = json.loads(tmpStr.buildDataTemplate)
        buildData = utils.mergeCurrentDataWithTemplate(buildData,
                                                       templateBuildData)

        buildData = paths.verifyTargetConfigurationPaths(buildData, request)
        buildData = paths.copyTargetConfigurationFiles(buildData)

        return buildData