From 9d91566927bd5a9131ecd811cf7f04828f81c93b Mon Sep 17 00:00:00 2001 From: Brian Daniels Date: Thu, 3 Nov 2016 13:13:50 -0500 Subject: [PATCH] Using Popen for uvision and unifying the structure of the build function across exporters --- tools/export/iar/__init__.py | 42 ++++++++++++++++++++----------- tools/export/makefile/__init__.py | 39 +++++++++++++++++----------- tools/export/uvision/__init__.py | 33 +++++++++++++++--------- 3 files changed, 73 insertions(+), 41 deletions(-) diff --git a/tools/export/iar/__init__.py b/tools/export/iar/__init__.py index d9b92db4f3e..475f86615e5 100644 --- a/tools/export/iar/__init__.py +++ b/tools/export/iar/__init__.py @@ -122,12 +122,11 @@ def generate(self): self.gen_file(self.get_ewp_template(), ctx, self.project_name + ".ewp") @staticmethod - def build(project_name, cleanup=True): + def build(project_name, log_name="build_log.txt", cleanup=True): """ Build IAR project """ # > IarBuild [project_path] -build [project_name] - proj_file = project_name + ".ewp" - cmd = ["IarBuild.exe", proj_file, '-build', project_name] + cmd = ["IarBuild", proj_file, '-build', project_name] # IAR does not support a '0' option to automatically use all # available CPUs, so we use Python's multiprocessing library @@ -139,23 +138,38 @@ def build(project_name, cleanup=True): if jobs: cmd += ['-parallel', str(jobs)] + # Build the project p = Popen(cmd, stdout=PIPE, stderr=PIPE) - num_errors = 0 - #Parse the output for printing and errors - for line in p.stdout.readlines(): - sys.stdout.write(line) - error_re = '\s*Total number of errors:\s*(\d+)\s*' - m = re.match(error_re, line) - if m is not None: - num_errors = int(m.group(1)) + out, err = p.communicate() + ret_code = p.returncode + + out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n" + out_string += out + out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n" + out_string += err + + if ret_code == 0: + out_string += "SUCCESS" + else: + out_string += "FAILURE" + + print out_string + + if log_name: + # Write the output to the log file + with open(log_name, 'w+') as f: + f.write(out_string) + # Cleanup the exported and built files if cleanup: os.remove(project_name + ".ewp") os.remove(project_name + ".ewd") os.remove(project_name + ".eww") - shutil.rmtree('.build') + if exists('.build'): + shutil.rmtree('.build') - if num_errors !=0: + if ret_code !=0: # Seems like something went wrong. return -1 - return 0 + else: + return 0 diff --git a/tools/export/makefile/__init__.py b/tools/export/makefile/__init__.py index 816ec8785dc..05200bfb8d2 100644 --- a/tools/export/makefile/__init__.py +++ b/tools/export/makefile/__init__.py @@ -110,32 +110,41 @@ def build(project_name, log_name="build_log.txt", cleanup=True): """ Build Make project """ # > Make -j cmd = ["make", "-j"] + + # Build the project p = Popen(cmd, stdout=PIPE, stderr=PIPE) - ret = p.communicate() - out, err = ret[0], ret[1] + out, err = p.communicate() ret_code = p.returncode - with open(log_name, 'w+') as f: - f.write("=" * 10 + "OUT" + "=" * 10 + "\n") - f.write(out) - f.write("=" * 10 + "ERR" + "=" * 10 + "\n") - f.write(err) - if ret_code == 0: - f.write("SUCCESS") - else: - f.write("FAILURE") - with open(log_name, 'r') as f: - print "\n".join(f.readlines()) - sys.stdout.flush() + out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n" + out_string += out + out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n" + out_string += err + + if ret_code == 0: + out_string += "SUCCESS" + else: + out_string += "FAILURE" + + print out_string + + if log_name: + # Write the output to the log file + with open(log_name, 'w+') as f: + f.write(out_string) + + # Cleanup the exported and built files if cleanup: remove("Makefile") remove(log_name) if exists('.build'): shutil.rmtree('.build') + if ret_code != 0: # Seems like something went wrong. return -1 - return 0 + else: + return 0 class GccArm(Makefile): diff --git a/tools/export/uvision/__init__.py b/tools/export/uvision/__init__.py index 14eedbc637b..e815a00d53a 100644 --- a/tools/export/uvision/__init__.py +++ b/tools/export/uvision/__init__.py @@ -4,7 +4,7 @@ import copy from collections import namedtuple import shutil -import subprocess +from subprocess import Popen, PIPE import re from tools.arm_pack_manager import Cache @@ -208,21 +208,30 @@ def generate(self): @staticmethod def build(project_name, log_name='build_log.txt', cleanup=True): """ Build Uvision project """ - # > UV4.exe -r -j0 -o [log_name] [project_name].uvprojx - success = 0 - warn = 1 - cmd = ["UV4.exe", '-r', '-j0', '-o', log_name, project_name+".uvprojx"] - ret_code = subprocess.call(cmd) - with open(log_name, 'r') as build_log: - print build_log.read() + # > UV4 -r -j0 -o [log_name] [project_name].uvprojx + proj_file = project_name + ".uvprojx" + cmd = ['UV4', '-r', '-j0', '-o', log_name, proj_file] + + # Build the project + p = Popen(cmd, stdout=PIPE, stderr=PIPE) + out, err = p.communicate() + ret_code = p.returncode + + # Print the log file to stdout + with open(log_name, 'r') as f: + print f.read() + + # Cleanup the exported and built files if cleanup: os.remove(log_name) os.remove(project_name+".uvprojx") os.remove(project_name+".uvoptx") - shutil.rmtree(".build") - + if exists('.build'): + shutil.rmtree(".build") - if ret_code != success and ret_code != warn: + # Returns 0 upon success, 1 upon a warning, and neither upon an error + if ret_code != 0 and ret_code != 1: # Seems like something went wrong. return -1 - return 0 + else: + return 0