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