Skip to content

Commit 10708f1

Browse files
author
Oren Cohen
committed
Overhaul logging and verbosity
* Default verbose * -q for quiet build with minimal status prints
1 parent f1e664b commit 10708f1

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

tools/psa/release.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@
1919
import subprocess
2020
import sys
2121
import shutil
22+
import logging
2223
from argparse import ArgumentParser
2324

25+
FNULL = open(os.devnull, 'w')
2426
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__),
2527
os.pardir, os.pardir))
2628
sys.path.insert(0, ROOT)
2729
from tools.targets import Target, TARGET_MAP, TARGET_NAMES
2830

2931
MAKE_PY_LOCATTION = os.path.join(ROOT, 'tools', 'make.py')
3032
TEST_PY_LOCATTION = os.path.join(ROOT, 'tools', 'test.py')
33+
34+
logging.basicConfig(level=logging.DEBUG,
35+
format='[%(name)s] %(asctime)s: %(message)s.',
36+
datefmt='%H:%M:%S')
37+
logger = logging.getLogger('PSA release tool')
38+
subprocess_output = None
39+
subprocess_err = None
40+
3141
TFM_MBED_APP = os.path.join(ROOT, 'tools', 'psa', 'tfm', 'mbed_app.json')
3242
MBED_PSA_TESTS = '*psa-spm*,*psa-crypto_access_control'
3343
TFM_TESTS = {
@@ -80,6 +90,8 @@ def get_mbed_official_psa_release(target=None):
8090
psa_targets_release_list = []
8191
psa_secure_targets = [t for t in TARGET_NAMES if
8292
Target.get_target(t).is_PSA_secure_target]
93+
logger.debug("Found the following PSA targets: {}".format(
94+
', '.join(psa_secure_targets)))
8395
if target is not None:
8496
if target not in psa_secure_targets:
8597
raise Exception("{} is not a PSA secure target".format(target))
@@ -97,6 +109,7 @@ def create_mbed_ignore(build_dir):
97109
98110
:param build_dir: Directory to create .mbedignore file.
99111
"""
112+
logger.debug('Created .mbedignore in {}'.format(build_dir))
100113
with open(os.path.join(build_dir, '.mbedignore'), 'w') as f:
101114
f.write('*\n')
102115

@@ -123,6 +136,9 @@ def build_mbed_spm_platform(target, toolchain, profile='release'):
123136
target, 'build_data.json'),
124137
'-n', MBED_PSA_TESTS
125138
])
139+
logger.info(
140+
"Building tests images({}) for {} using {} with {} profile".format(
141+
MBED_PSA_TESTS, target, toolchain, profile))
126142

127143
subprocess.check_call([
128144
sys.executable, MAKE_PY_LOCATTION,
@@ -133,6 +149,9 @@ def build_mbed_spm_platform(target, toolchain, profile='release'):
133149
'--build', os.path.join(ROOT, 'BUILD', target),
134150
'--artifact-name', 'psa_release_1.0'
135151
])
152+
logger.info(
153+
"Finished building tests images({}) for {} successfully".format(
154+
MBED_PSA_TESTS, target))
136155

137156

138157
def _tfm_test_defines(test):
@@ -154,6 +173,9 @@ def build_tfm_platform(target, toolchain, profile='release'):
154173
:param profile: build profile.
155174
"""
156175
for test in TFM_TESTS.keys():
176+
logger.info(
177+
"Building tests image({}) for {} using {} with {} profile".format(
178+
test, target, toolchain, profile))
157179
subprocess.check_call([
158180
sys.executable, TEST_PY_LOCATTION,
159181
'--greentea',
@@ -178,6 +200,8 @@ def build_tfm_platform(target, toolchain, profile='release'):
178200
'--build', os.path.join(ROOT, 'BUILD', target),
179201
'--app-config', TFM_MBED_APP
180202
])
203+
logger.info(
204+
"Finished Building tests image({}) for {}".format(test, target))
181205

182206

183207
def commit_binaries(target, delivery_dir):
@@ -193,22 +217,28 @@ def commit_binaries(target, delivery_dir):
193217
'-C', ROOT,
194218
'diff', '--exit-code', '--quiet',
195219
delivery_dir
196-
])
220+
], stdout=subprocess_output, stderr=subprocess_err)
197221

198222
if changes_made:
223+
logger.info("Change in images for {} has been detected".format(target))
199224
subprocess.check_call([
200225
'git',
201226
'-C', ROOT,
202227
'add', os.path.relpath(delivery_dir, ROOT)
203-
])
228+
], stdout=subprocess_output, stderr=subprocess_err)
204229

205230
commit_message = '-m\"Update secure binaries for {}\"'.format(target)
231+
logger.info("Committing images for {}".format(target))
232+
commit_message = '--message="Update secure binaries for {}"'.format(
233+
target)
206234
subprocess.check_call([
207235
'git',
208236
'-C', ROOT,
209237
'commit',
210238
commit_message
211-
])
239+
], stdout=subprocess_output, stderr=subprocess_err)
240+
else:
241+
logger.info("No changes detected for {}, Skipping commit".format(target))
212242

213243

214244
def build_psa_platform(target, toolchain, delivery_dir, debug=False,
@@ -227,6 +257,11 @@ def build_psa_platform(target, toolchain, delivery_dir, debug=False,
227257
build_tfm_platform(target, toolchain, profile)
228258
else:
229259
build_mbed_spm_platform(target, toolchain, profile)
260+
logger.info("Building default image for {} using {} with {} profile".format(
261+
target, toolchain, profile))
262+
263+
logger.info(
264+
"Finished building default image for {} successfully".format(target))
230265

231266
if git_commit:
232267
commit_binaries(target, delivery_dir)
@@ -244,6 +279,11 @@ def get_parser():
244279
action="store_true",
245280
default=False)
246281

282+
parser.add_argument('-q', '--quiet',
283+
action="store_true",
284+
default=False,
285+
help="No Build log will be printed")
286+
247287
parser.add_argument("--commit",
248288
help="create a git commit for each platform",
249289
action="store_true",
@@ -258,20 +298,31 @@ def prep_build_dir():
258298
"""
259299
build_dir = os.path.join(ROOT, 'BUILD')
260300
if os.path.exists(build_dir):
301+
logger.debug("BUILD directory already exists... Deleting")
261302
shutil.rmtree(build_dir)
262303

263304
os.makedirs(build_dir)
305+
logger.info("BUILD directory created in {}".format(build_dir))
264306
create_mbed_ignore(build_dir)
265307

266308

267309
def main():
268310
parser = get_parser()
269311
options = parser.parse_args()
312+
if options.quiet:
313+
logger.setLevel(logging.INFO)
314+
global subprocess_output, subprocess_err
315+
subprocess_output = FNULL
316+
subprocess_err = subprocess.STDOUT
317+
270318
prep_build_dir()
271319
psa_platforms_list = get_mbed_official_psa_release(options.mcu)
320+
logger.info("Building the following platforms: {}".format(
321+
', '.join([t[0] for t in psa_platforms_list])))
272322

273323
for target, tc, directory in psa_platforms_list:
274324
build_psa_platform(target, tc, directory, options.debug, options.commit)
325+
logger.info("Finished Updating PSA images")
275326

276327

277328
if __name__ == '__main__':

0 commit comments

Comments
 (0)