Skip to content

Commit d68446c

Browse files
Merge pull request #47 from gabor-mezei-arm/9140_stderr_for_c_build
Validated by #9292 (dev) and #9709 (3.6) CI.
2 parents 3eafac1 + d1d2e3c commit d68446c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

scripts/mbedtls_framework/c_build_helper.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
import sys
1212
import tempfile
1313

14+
class CompileError(Exception):
15+
"""Exception to represent an error during the compilation."""
16+
17+
def __init__(self, message):
18+
"""Save the error massage"""
19+
20+
super().__init__()
21+
self.message = message
22+
1423
def remove_file_if_exists(filename):
1524
"""Remove the specified file, ignoring errors."""
1625
if not filename:
@@ -107,7 +116,13 @@ def compile_c_file(c_filename, exe_filename, include_dirs):
107116
else:
108117
cmd += ['-o' + exe_filename]
109118

110-
subprocess.check_call(cmd + [c_filename])
119+
try:
120+
subprocess.check_output(cmd + [c_filename],
121+
stderr=subprocess.PIPE,
122+
universal_newlines=True)
123+
124+
except subprocess.CalledProcessError as e:
125+
raise CompileError(e.stderr) from e
111126

112127
def get_c_expression_values(
113128
cast_to, printf_format,

scripts/mbedtls_framework/config_common.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import argparse
99
import os
1010
import re
11+
import shutil
1112
import sys
1213

1314
from abc import ABCMeta
@@ -204,6 +205,18 @@ def filename(self, name=None):
204205

205206
return self._get_configfile(name).filename
206207

208+
def backup(self, suffix='.bak'):
209+
"""Back up the configuration file."""
210+
211+
for configfile in self.configfiles:
212+
configfile.backup(suffix)
213+
214+
def restore(self):
215+
"""Restore the configuration file."""
216+
217+
for configfile in self.configfiles:
218+
configfile.restore()
219+
207220

208221
class ConfigFile(metaclass=ABCMeta):
209222
"""Representation of a configuration file."""
@@ -224,6 +237,8 @@ def __init__(self, default_path, name, filename=None):
224237
self.current_section = None
225238
self.inclusion_guard = None
226239
self.modified = False
240+
self._backupname = None
241+
self._own_backup = False
227242

228243
_define_line_regexp = (r'(?P<indentation>\s*)' +
229244
r'(?P<commented_out>(//\s*)?)' +
@@ -334,6 +349,37 @@ def write(self, settings, filename=None):
334349
with open(filename, 'w', encoding='utf-8') as output:
335350
self.write_to_stream(settings, output)
336351

352+
def backup(self, suffix='.bak'):
353+
"""Back up the configuration file.
354+
355+
If the backup file already exists, it is presumed to be the desired backup,
356+
so don't make another backup.
357+
"""
358+
if self._backupname:
359+
return
360+
361+
self._backupname = self.filename + suffix
362+
if os.path.exists(self._backupname):
363+
self._own_backup = False
364+
else:
365+
self._own_backup = True
366+
shutil.copy(self.filename, self._backupname)
367+
368+
def restore(self):
369+
"""Restore the configuration file.
370+
371+
Only delete the backup file if it was created earlier.
372+
"""
373+
if not self._backupname:
374+
return
375+
376+
if self._own_backup:
377+
shutil.move(self._backupname, self.filename)
378+
else:
379+
shutil.copy(self._backupname, self.filename)
380+
381+
self._backupname = None
382+
337383

338384
class ConfigTool(metaclass=ABCMeta):
339385
"""Command line config manipulation tool.

0 commit comments

Comments
 (0)