From patchwork Wed Jan 19 21:20:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ross Burton X-Patchwork-Id: 2686 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0F82C4332F for ; Wed, 19 Jan 2022 21:20:54 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web11.2782.1642627254186026610 for ; Wed, 19 Jan 2022 13:20:54 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: ross.burton@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CCA3D1063 for ; Wed, 19 Jan 2022 13:20:53 -0800 (PST) Received: from oss-tx204.lab.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 786BB3F774 for ; Wed, 19 Jan 2022 13:20:53 -0800 (PST) From: Ross Burton To: meta-arm@lists.yoctoproject.org Subject: [PATCH 3/6] scripts/machine-summary: refactor output to classes Date: Wed, 19 Jan 2022 21:20:47 +0000 Message-Id: <20220119212050.1886613-3-ross.burton@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220119212050.1886613-1-ross.burton@arm.com> References: <20220119212050.1886613-1-ross.burton@arm.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Wed, 19 Jan 2022 21:20:54 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/meta-arm/message/2871 To prepare for future expansion, refactor the output code to be delegated to Format subclasses. Signed-off-by: Ross Burton --- scripts/machine-summary.py | 75 +++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/scripts/machine-summary.py b/scripts/machine-summary.py index e0c7870d..e5161440 100755 --- a/scripts/machine-summary.py +++ b/scripts/machine-summary.py @@ -3,28 +3,12 @@ import argparse import datetime import os +import pathlib import re import sys import jinja2 -def get_template(name): - template_dir = os.path.dirname(os.path.abspath(__file__)) - env = jinja2.Environment( - loader=jinja2.FileSystemLoader(template_dir), - autoescape=jinja2.select_autoescape(), - trim_blocks=True, - lstrip_blocks=True - ) - def is_old(version, upstream): - if "+git" in version: - # strip +git and see if this is a post-release snapshot - version = version.replace("+git", "") - return version != upstream - env.tests["old"] = is_old - - return env.get_template(f"machine-summary-{name}.jinja") - def trim_pv(pv): """ Strip anything after +git from the PV @@ -126,19 +110,66 @@ recipes = ("virtual/kernel", "gcc-aarch64-none-elf-native", "gcc-arm-none-eabi-native") + +class Format: + """ + The name of this format + """ + name = None + """ + Registry of names to classes + """ + registry = {} + + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + assert cls.name + cls.registry[cls.name] = cls + + @classmethod + def get_format(cls, name): + return cls.registry[name]() + + def render(self, context, output: pathlib.Path): + # Default implementation for convenience + with open(output, "wt") as f: + f.write(self.get_template(f"machine-summary-{self.name}.jinja").render(context)) + + def get_template(self, name): + template_dir = os.path.dirname(os.path.abspath(__file__)) + env = jinja2.Environment( + loader=jinja2.FileSystemLoader(template_dir), + autoescape=jinja2.select_autoescape(), + trim_blocks=True, + lstrip_blocks=True + ) + def is_old(version, upstream): + if "+git" in version: + # strip +git and see if this is a post-release snapshot + version = version.replace("+git", "") + return version != upstream + env.tests["old"] = is_old + + return env.get_template(name) + +class TextOverview(Format): + name = "overview.txt" + +class HtmlUpdates(Format): + name = "updates.html" + if __name__ == "__main__": parser = argparse.ArgumentParser(description="machine-summary") parser.add_argument("machines", nargs="+", help="machine names", metavar="MACHINE") - parser.add_argument("-t", "--template", required=True) - parser.add_argument("-o", "--output", required=True, type=argparse.FileType('w', encoding='UTF-8')) + parser.add_argument("-t", "--type", required=True, choices=Format.registry.keys()) + parser.add_argument("-o", "--output", type=pathlib.Path, required=True) args = parser.parse_args() - template = get_template(args.template) - context = {} # TODO: include git describe for meta-arm context["timestamp"] = str(datetime.datetime.now().strftime("%c")) context["recipes"] = sorted(recipes) context["releases"], context["data"] = harvest_data(args.machines, recipes) - args.output.write(template.render(context)) + formatter = Format.get_format(args.type) + formatter.render(context, args.output)