From e7082e21222bab2bf9f497b8af0bb76dc6922017 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 10 Jan 2025 11:45:27 +0100 Subject: [PATCH] [Tizen] Forward stderr from Tizen Studio CLI tool (#36976) * [Tizen] Forward stderr from Tizen Studio CLI tool * Fix process management * Use shlex for joining command arguments --- .../bouffalolab/generate_factory_data.py | 7 +++-- scripts/tools/zap_regen_all.py | 3 +- third_party/tizen/tizen_dev_certificate.py | 30 +++++++++++++------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/scripts/tools/bouffalolab/generate_factory_data.py b/scripts/tools/bouffalolab/generate_factory_data.py index 28f362ba2d3b21..f6475ed07650d2 100755 --- a/scripts/tools/bouffalolab/generate_factory_data.py +++ b/scripts/tools/bouffalolab/generate_factory_data.py @@ -22,6 +22,7 @@ import os import random import secrets +import shlex import subprocess import sys from datetime import datetime, timedelta @@ -185,7 +186,7 @@ def verify_certificates(chip_cert, paa_cert, pai_cert, dac_cert): "--pai", pai_cert, "--paa", paa_cert, ] - log.info("Verify Certificate Chain: {}".format(" ".join(cmd))) + log.info("Verify Certificate Chain: {}".format(shlex.join(cmd))) subprocess.run(cmd) def gen_dac_certificate(chip_cert, device_name, vendor_id, product_id, pai_cert, pai_key, dac_cert, dac_key, pai_issue_date, pai_expire_date): @@ -214,7 +215,7 @@ def gen_valid_times(issue_date, expire_date): "--valid-from", valid_from, "--lifetime", str(lifetime), ] - log.info("Generate DAC: {}".format(" ".join(cmd))) + log.info("Generate DAC: {}".format(shlex.join(cmd))) subprocess.run(cmd) def convert_pem_to_der(chip_cert, action, pem): @@ -254,7 +255,7 @@ def gen_cd(chip_cert, dac_vendor_id, dac_product_id, vendor_id, product_id, cd_c "--dac-origin-product-id", hex(dac_product_id), ] - log.info("Generate CD: {}".format(" ".join(cmd))) + log.info("Generate CD: {}".format(shlex.join(cmd))) subprocess.run(cmd) pai_vendor_id, pai_product_id, pai_issue_date, pai_expire_date = parse_cert_file(pai_cert) diff --git a/scripts/tools/zap_regen_all.py b/scripts/tools/zap_regen_all.py index 0284e9eff9dd93..65fbab040ec58b 100755 --- a/scripts/tools/zap_regen_all.py +++ b/scripts/tools/zap_regen_all.py @@ -20,6 +20,7 @@ import multiprocessing import os import os.path +import shlex import shutil import subprocess import sys @@ -209,7 +210,7 @@ def generate(self) -> TargetRunStats: """Runs a ZAP generate command on the configured zap/template/outputs. """ cmd = self.build_cmd() - logging.info("Generating target: %s" % " ".join(cmd)) + logging.info("Generating target: %s" % shlex.join(cmd)) generate_start = time.time() subprocess.check_call(cmd) diff --git a/third_party/tizen/tizen_dev_certificate.py b/third_party/tizen/tizen_dev_certificate.py index 9e43991fa51296..8f898949a70324 100755 --- a/third_party/tizen/tizen_dev_certificate.py +++ b/third_party/tizen/tizen_dev_certificate.py @@ -17,6 +17,7 @@ import argparse import logging import os +import shlex import subprocess import sys @@ -28,6 +29,17 @@ logging.basicConfig(level=logging.DEBUG) +def run(cmd): + logging.debug("Run: %s", shlex.join(cmd)) + proc = subprocess.Popen(cmd, errors='replace', + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + proc.wait() + for line in proc.stderr.readlines(): + logging.error("%s", line.rstrip()) + return proc + + def create_author_certificate(alias: str, password: str, name: str = "", email: str = ""): cmd = [tizen_cli, "certificate", "--alias", alias, "--password", password] @@ -35,13 +47,15 @@ def create_author_certificate(alias: str, password: str, cmd.extend(["--name", name]) if email: cmd.extend(["--email", email]) - logging.debug("Execute: %s", " ".join(cmd)) - with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: + wd = None + with run(cmd) as proc: for line in proc.stdout.readlines(): - line = line.decode().rstrip() + line = line.rstrip() if line.startswith("Working path:"): wd = line[len("Working path:"):].strip() print(line) + if not wd: + return None return os.path.join(wd, "author.p12") @@ -69,10 +83,9 @@ def check_security_profile(profile): f.write('') cmd = [tizen_cli, "security-profiles", "list", "--name", profile] - logging.debug("Execute: %s", " ".join(cmd)) - with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: + with run(cmd) as proc: for line in proc.stdout.readlines(): - line = line.decode().rstrip() + line = line.rstrip() print(line) return proc.wait() == 0 @@ -80,10 +93,9 @@ def check_security_profile(profile): def add_security_profile(profile: str, certificate: str, password: str): cmd = [tizen_cli, "security-profiles", "add", "--active", "--name", profile, "--author", certificate, "--password", password] - logging.debug("Execute: %s", " ".join(cmd)) - with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: + with run(cmd) as proc: for line in proc.stdout.readlines(): - line = line.decode().rstrip() + line = line.rstrip() print(line) return proc.wait() == 0