Skip to content

Commit fe7a67d

Browse files
committed
[tools] Optimize the file opening method.
1 parent 4aed0e9 commit fe7a67d

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

tools/targets/vsc.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import json
3333
import utils
3434
import rtconfig
35-
from SCons.Script import *
35+
from SCons.Script import GetLaunchDir
3636

3737
from utils import _make_path_relative
3838
def find_first_node_with_two_children(tree):
@@ -82,7 +82,6 @@ def print_tree(tree, indent=''):
8282
print(indent + key)
8383
print_tree(subtree, indent + ' ')
8484

85-
8685
def extract_source_dirs(compile_commands):
8786
source_dirs = set()
8887

@@ -207,8 +206,7 @@ def GenerateCFiles(env):
207206
if not os.path.exists('.vscode'):
208207
os.mkdir('.vscode')
209208

210-
vsc_file = open('.vscode/c_cpp_properties.json', 'w')
211-
if vsc_file:
209+
with open('.vscode/c_cpp_properties.json', 'w') as vsc_file:
212210
info = utils.ProjectInfo(env)
213211

214212
cc = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
@@ -242,7 +240,6 @@ def GenerateCFiles(env):
242240
json_obj['configurations'] = [config_obj]
243241

244242
vsc_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
245-
vsc_file.close()
246243

247244
"""
248245
Generate vscode.code-workspace files by build/compile_commands.json
@@ -254,8 +251,7 @@ def GenerateCFiles(env):
254251
"""
255252
Generate vscode.code-workspace files
256253
"""
257-
vsc_space_file = open('vscode.code-workspace', 'w')
258-
if vsc_space_file:
254+
with open('vscode.code-workspace', 'w') as vsc_space_file:
259255
info = utils.ProjectInfo(env)
260256
path_list = []
261257
for i in info['CPPPATH']:
@@ -274,7 +270,6 @@ def GenerateCFiles(env):
274270
json_obj = {}
275271
path_list = delete_repeatelist(path_list)
276272
path_list = sorted(path_list, key=lambda x: x["path"])
277-
target_path_list = []
278273
for path in path_list:
279274
if path['path'] != '.':
280275
normalized_path = path['path'].replace('\\', os.path.sep)
@@ -289,7 +284,7 @@ def GenerateCFiles(env):
289284
]
290285
}
291286
vsc_space_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
292-
vsc_space_file.close()
287+
293288
return
294289

295290
def GenerateProjectFiles(env):
@@ -300,8 +295,7 @@ def GenerateProjectFiles(env):
300295
os.mkdir('.vscode')
301296

302297
project = env['project']
303-
vsc_file = open('.vscode/project.json', 'w')
304-
if vsc_file:
298+
with open('.vscode/project.json', 'w') as vsc_file:
305299
groups = []
306300
for group in project:
307301
if len(group['src']) > 0:
@@ -325,7 +319,6 @@ def GenerateProjectFiles(env):
325319

326320
# write groups to project.json
327321
vsc_file.write(json.dumps(json_dict, ensure_ascii=False, indent=4))
328-
vsc_file.close()
329322

330323
return
331324

@@ -416,8 +409,7 @@ def GenerateVSCodeWorkspace(env):
416409
if not os.path.exists(os.path.join(cwd, '.vscode')):
417410
os.mkdir(os.path.join(cwd, '.vscode'))
418411

419-
vsc_file = open(os.path.join(cwd, '.vscode/c_cpp_properties.json'), 'w')
420-
if vsc_file:
412+
with open(os.path.join(cwd, '.vscode/c_cpp_properties.json'), 'w') as vsc_file:
421413
info = utils.ProjectInfo(env)
422414

423415
cc = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
@@ -452,7 +444,6 @@ def GenerateVSCodeWorkspace(env):
452444
json_obj['configurations'] = [config_obj]
453445

454446
vsc_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
455-
vsc_file.close()
456447

457448
# generate .vscode/settings.json
458449
vsc_settings = {}
@@ -462,8 +453,7 @@ def GenerateVSCodeWorkspace(env):
462453
# read the existing settings file and load to vsc_settings
463454
vsc_settings = json.load(f)
464455

465-
vsc_file = open(settings_path, 'w')
466-
if vsc_file:
456+
with open(settings_path, 'w') as vsc_file:
467457
vsc_settings['files.exclude'] = {
468458
"**/__pycache__": True,
469459
"tools/kconfig-frontends": True,
@@ -482,7 +472,6 @@ def GenerateVSCodeWorkspace(env):
482472
vsc_settings['search.exclude'] = vsc_settings['files.exclude']
483473
# write the settings to the file
484474
vsc_file.write(json.dumps(vsc_settings, ensure_ascii=False, indent=4))
485-
vsc_file.close()
486475

487476
print('Done!')
488477

0 commit comments

Comments
 (0)