|
29 | 29 | source_pattern = ['*.c', '*.cpp', '*.cxx', '*.cc', '*.s', '*.S', '*.asm','*.cmd'] |
30 | 30 |
|
31 | 31 |
|
| 32 | +def _write_text_if_changed(path, content, encoding='utf-8'): |
| 33 | + old_content = None |
| 34 | + if os.path.exists(path): |
| 35 | + with open(path, 'r', encoding=encoding, errors='ignore') as f: |
| 36 | + old_content = f.read() |
| 37 | + |
| 38 | + if old_content == content: |
| 39 | + return False |
| 40 | + |
| 41 | + with open(path, 'w', encoding=encoding, newline='\n') as f: |
| 42 | + f.write(content) |
| 43 | + |
| 44 | + return True |
| 45 | + |
| 46 | + |
| 47 | +def _serialize_xml(root, xml_decl): |
| 48 | + xml_indent(root) |
| 49 | + return xml_decl + etree.tostring(root, encoding='utf-8').decode('utf-8') |
| 50 | + |
| 51 | + |
32 | 52 | def OSPath(path): |
33 | 53 | import platform |
34 | 54 |
|
@@ -257,11 +277,11 @@ def HandleToolOption(tools, env, project, reset): |
257 | 277 | file_tail = '\n#endif /*RTCONFIG_PREINC_H__*/\n' |
258 | 278 | rtt_pre_inc_item = '"${workspace_loc:/${ProjName}/rtconfig_preinc.h}"' |
259 | 279 | # save the CPPDEFINES in to rtconfig_preinc.h |
260 | | - with open('rtconfig_preinc.h', mode = 'w+') as f: |
261 | | - f.write(file_header) |
262 | | - for cppdef in CPPDEFINES: |
263 | | - f.write("#define " + cppdef.replace('=', ' ') + '\n') |
264 | | - f.write(file_tail) |
| 280 | + preinc_content = file_header |
| 281 | + for cppdef in CPPDEFINES: |
| 282 | + preinc_content += "#define " + cppdef.replace('=', ' ') + '\n' |
| 283 | + preinc_content += file_tail |
| 284 | + _write_text_if_changed('rtconfig_preinc.h', preinc_content) |
265 | 285 | # change the c.compiler.include.files |
266 | 286 | files = option.findall('listOptionValue') |
267 | 287 | find_ok = False |
@@ -380,11 +400,8 @@ def UpdateProjectStructure(env, prj_name): |
380 | 400 | name = SubElement(root, 'name') |
381 | 401 | name.text = prj_name |
382 | 402 |
|
383 | | - out = open('.project', 'w') |
384 | | - out.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
385 | | - xml_indent(root) |
386 | | - out.write(etree.tostring(root, encoding='utf-8').decode('utf-8')) |
387 | | - out.close() |
| 403 | + project_content = _serialize_xml(root, '<?xml version="1.0" encoding="UTF-8"?>\n') |
| 404 | + _write_text_if_changed('.project', project_content) |
388 | 405 |
|
389 | 406 | return |
390 | 407 |
|
@@ -518,12 +535,11 @@ def UpdateCproject(env, project, excluding, reset, prj_name): |
518 | 535 | SubElement(configuration, 'resource', {'resourceType': "PROJECT", 'workspacePath': prj_name}) |
519 | 536 |
|
520 | 537 | # write back to .cproject |
521 | | - out = open('.cproject', 'w') |
522 | | - out.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') |
523 | | - out.write('<?fileVersion 4.0.0?>') |
524 | | - xml_indent(root) |
525 | | - out.write(etree.tostring(root, encoding='utf-8').decode('utf-8')) |
526 | | - out.close() |
| 538 | + cproject_content = _serialize_xml( |
| 539 | + root, |
| 540 | + '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<?fileVersion 4.0.0?>' |
| 541 | + ) |
| 542 | + _write_text_if_changed('.cproject', cproject_content) |
527 | 543 |
|
528 | 544 |
|
529 | 545 | def TargetEclipse(env, reset=False, prj_name=None): |
|
0 commit comments