|
| 1 | +''' |
| 2 | +Update existing workspace file with debug paths in "settings": |
| 3 | + - "cortex-debug.armToolchainPath" |
| 4 | + - "cortex-debug.openocdPath" |
| 5 | +''' |
| 6 | +import os |
| 7 | +import json |
| 8 | +import datetime |
| 9 | + |
| 10 | +import utilities as utils |
| 11 | +import updatePaths as pth |
| 12 | +import updateBuildData as build |
| 13 | + |
| 14 | +__version__ = utils.__version__ |
| 15 | + |
| 16 | + |
| 17 | +class UpdateWorkspaceFile(): |
| 18 | + def __init__(self): |
| 19 | + self.bStr = build.BuildDataStrings() |
| 20 | + |
| 21 | + def checkWorkspaceFile(self): |
| 22 | + ''' |
| 23 | + Check if workspace '*.code-workspace' file exists. If it does, check if it is a valid JSON file. |
| 24 | + If it doesn't exist report error and quit. |
| 25 | + ''' |
| 26 | + workspaceFileName = utils.getWorkspaceName() |
| 27 | + if utils.fileFolderExists(utils.workspaceFilePath): |
| 28 | + # file exists, check if it loads OK |
| 29 | + try: |
| 30 | + with open(utils.workspaceFilePath, 'r') as workspaceFile: |
| 31 | + workspaceFileData = json.load(workspaceFile) |
| 32 | + |
| 33 | + print("Existing " + workspaceFileName + " file found.") |
| 34 | + |
| 35 | + except Exception as err: |
| 36 | + errorMsg = "Invalid " + workspaceFileName + " file.\n" |
| 37 | + errorMsg += "Possible cause: invalid json format or comments (not supported by this scripts). Error:\n" |
| 38 | + errorMsg += str(err) |
| 39 | + print(errorMsg) |
| 40 | + |
| 41 | + # else: verified in 'utils.verifyFolderStructure()' |
| 42 | + |
| 43 | + def getWorkspaceFileData(self): |
| 44 | + ''' |
| 45 | + Get data from current '*.code-workspace' file. |
| 46 | + File existance is previoulsy checked in 'checkWorkspaceFile()'. |
| 47 | + ''' |
| 48 | + with open(utils.workspaceFilePath, 'r') as workspaceFile: |
| 49 | + data = json.load(workspaceFile) |
| 50 | + |
| 51 | + return data |
| 52 | + |
| 53 | + def addBuildDataToWorkspaceFile(self, workspaceData, buildData): |
| 54 | + ''' |
| 55 | + This function ads "cortex-debug.*" items to workspace file, if they don't exist yet. |
| 56 | + Returns new data. |
| 57 | + ''' |
| 58 | + armToolchainPath = os.path.dirname(buildData[self.bStr.gccExePath]) |
| 59 | + armToolchainPath = utils.pathWithForwardSlashes(armToolchainPath) |
| 60 | + |
| 61 | + workspaceData["settings"]["cortex-debug.armToolchainPath"] = armToolchainPath |
| 62 | + workspaceData["settings"]["cortex-debug.openocdPath"] = buildData[self.bStr.openOCDPath] |
| 63 | + |
| 64 | + return workspaceData |
| 65 | + |
| 66 | + def overwriteWorkspaceFile(self, data): |
| 67 | + ''' |
| 68 | + Overwrite existing '*.code-workspace' file with new data. |
| 69 | + ''' |
| 70 | + try: |
| 71 | + with open(utils.workspaceFilePath, 'r+') as workspaceFile: |
| 72 | + workspaceFile.seek(0) |
| 73 | + workspaceFile.truncate() |
| 74 | + dataToWrite = json.dumps(data, indent=4, sort_keys=False) |
| 75 | + workspaceFile.write(dataToWrite) |
| 76 | + |
| 77 | + print("'*.code-workspace' file updated!") |
| 78 | + |
| 79 | + except Exception as err: |
| 80 | + errorMsg = "Exception error overwriting '*.code-workspace' file:\n" |
| 81 | + errorMsg += str(err) |
| 82 | + utils.printAndQuit(errorMsg) |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +######################################################################################################################## |
| 87 | +if __name__ == "__main__": |
| 88 | + utils.verifyFolderStructure() |
| 89 | + |
| 90 | + paths = pth.UpdatePaths() |
| 91 | + bData = build.BuildData() |
| 92 | + wksFile = UpdateWorkspaceFile() |
| 93 | + |
| 94 | + # build data (update tools paths if neccessary) |
| 95 | + bData.checkBuildDataFile() |
| 96 | + buildData = bData.getBuildData() |
| 97 | + if not paths.verifyExistingPaths(buildData): |
| 98 | + buildData = paths.forceUpdatePaths(buildData) |
| 99 | + bData.overwriteBuildDataFile(buildData) |
| 100 | + |
| 101 | + wksFile.checkWorkspaceFile() |
| 102 | + wksData = wksFile.getWorkspaceFileData() |
| 103 | + wksData = wksFile.addBuildDataToWorkspaceFile(wksData, buildData) |
| 104 | + |
| 105 | + wksFile.overwriteWorkspaceFile(wksData) |
0 commit comments