From a25c7025e11a9abc0fea873de73148770105fcb3 Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Tue, 3 Nov 2015 17:33:25 -0800 Subject: mec1322: killing the white whale (removing temp files left behind) This has been bothering me literally for years: once in a while there would be tons of files in /tmp directory named tmpXXXXXX where XXXXXXX is some random string. Finally, it became clear that the files are generated when 'make buildall -j' is called in the ec directory. Next step - it looks like one of the culprits is building for board named 'chell'. Indeed, this board uses its own version of cmd_obj_to_bin make function, which, among other things invokes the pack_ec script to pack the image. The script was creating temporary files and leaving them behind. This patch makes the name pattern of the temp files better recognizable, juts in case, and makes sure that the files are deleted once they are not needed. BRANCH=none BUG=none TEST=invoking 'make buildall -j' still succeeds but does not result in leaving temp files behind. Change-Id: I50c511773caa87d4e92980c4c9a36768b0c3101f Signed-off-by: Vadim Bendebury Reviewed-on: https://chromium-review.googlesource.com/310586 Reviewed-by: Aseda Aboagye --- chip/mec1322/util/pack_ec.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/chip/mec1322/util/pack_ec.py b/chip/mec1322/util/pack_ec.py index 236d364b2c..7fed5a8008 100755 --- a/chip/mec1322/util/pack_ec.py +++ b/chip/mec1322/util/pack_ec.py @@ -109,19 +109,23 @@ def BuildHeader(args, payload_len, rorofile): return header def SignByteArray(data, pem_file): - hash_file = tempfile.mkstemp()[1] - sign_file = tempfile.mkstemp()[1] - with open(hash_file, 'wb') as f: - hasher = hashlib.sha256() - hasher.update(data) - f.write(hasher.digest()) - subprocess.check_call(['openssl', 'rsautl', '-sign', '-inkey', pem_file, - '-keyform', 'PEM', '-in', hash_file, - '-out', sign_file]) - with open(sign_file, 'rb') as f: - signed = list(f.read()) - signed.reverse() - return bytearray(''.join(signed)) + hash_file = tempfile.mkstemp(prefix='pack_ec.')[1] + sign_file = tempfile.mkstemp(prefix='pack_ec.')[1] + try: + with open(hash_file, 'wb') as f: + hasher = hashlib.sha256() + hasher.update(data) + f.write(hasher.digest()) + subprocess.check_call(['openssl', 'rsautl', '-sign', '-inkey', pem_file, + '-keyform', 'PEM', '-in', hash_file, + '-out', sign_file]) + with open(sign_file, 'rb') as f: + signed = list(f.read()) + signed.reverse() + return bytearray(''.join(signed)) + finally: + os.remove(hash_file) + os.remove(sign_file) def BuildTag(args): tag = bytearray([(args.header_loc >> 8) & 0xff, -- cgit v1.2.1