From patchwork Tue Aug 16 08:42:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefan Herbrechtsmeier X-Patchwork-Id: 1666732 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=weidmueller.onmicrosoft.com header.i=@weidmueller.onmicrosoft.com header.a=rsa-sha256 header.s=selector1-weidmueller-onmicrosoft-com header.b=3wgggDaG; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4M6Pn94JPcz1yft for ; Tue, 16 Aug 2022 18:43:25 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 75C6284994; Tue, 16 Aug 2022 10:42:49 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=weidmueller.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=weidmueller.onmicrosoft.com header.i=@weidmueller.onmicrosoft.com header.b="3wgggDaG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5123F849A5; Tue, 16 Aug 2022 10:42:42 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70048.outbound.protection.outlook.com [40.107.7.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7AFD6848EB for ; Tue, 16 Aug 2022 10:42:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=weidmueller.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=stefan.herbrechtsmeier-oss@weidmueller.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nbdLk8GMe2MNsTg8l1+Vc98Kk5Wd55lAg8lAM8bNspkSa97A7zdXg/HDpf+xBMPGNUTjQabT2PptDLpk17beYyebtcY9jBHT/83KJe6glFKEunL6GQPVMXo7msQYN45vP/Jjb+kTeiyxpRrAjTmAxQXxdSQCm9k8qy8Q9z5pxac545ivwE0FvxtP+j1EttM98aoQzQVFKxCiVbT14pQ/kc1ULM8BH2RlQVsWBcuyIoLuiz4Tog08Qo5aaQtsr8e8ljDo7+n9NUQLlfPyI8SXT7JaNMNzW4anWScMx8pKp5fi+Wyc4jLv7g+ZPWyLcs2S3gVoPpVuZuAcqe0TKuNT4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Wz0NpmMjpU91MjguLvpOa5td0LtyyagRFKdAIYG/Cuc=; b=n6YJCQszD9PnEIogf7kzZURjxn0Ja+rXOOqYfBxymhD9fJnMQ06xqSFr/f1MUGHKYl0BJaHdLiyArWc/SV2I26hvC6fU82AjxEr8SB/nJXxfeJUZ6xx70ccfDd12Mb4tEFXLwCfxreFwZ7BFkwxWEZbkfLQdd3Z/u9mHZRq5Zuab8TKqy0n32b8nFyjPP1uEfI8+jJ5v6BtQLMUb684+kPL50iVOLC2Qka5yq+i6HJ5rvBxuduB5ufpxwjm0tmiYAWX5Kt8KeXwYcK5peHKos2C6LFMIf1VCbL+iU5sYzro6ZCMiQ0yqDUgwhu5pQSGLvlszlkZfAgbOtqFEER2MDw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=weidmueller.com; dmarc=pass action=none header.from=weidmueller.com; dkim=pass header.d=weidmueller.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=weidmueller.onmicrosoft.com; s=selector1-weidmueller-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Wz0NpmMjpU91MjguLvpOa5td0LtyyagRFKdAIYG/Cuc=; b=3wgggDaGv7fHTOPO3Dftnd42yOP0CKd9uAARWIP1C8PT7WrPHJhxZ4jvV84oPdvX6YaukG4RA61SWRf2RpOf81OYC1CPsK/VYu7C5Asu67eTxP7Ht9bw1FakRAvOgkQwLxsaFqjzBjJapKaPlhQsJZKQsAqWgMMnDTUSRFMzaV0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=weidmueller.com; Received: from PAXPR08MB6969.eurprd08.prod.outlook.com (2603:10a6:102:1d8::23) by AM6PR08MB3096.eurprd08.prod.outlook.com (2603:10a6:209:43::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5504.17; Tue, 16 Aug 2022 08:42:35 +0000 Received: from PAXPR08MB6969.eurprd08.prod.outlook.com ([fe80::f95b:2e64:9ee6:4d44]) by PAXPR08MB6969.eurprd08.prod.outlook.com ([fe80::f95b:2e64:9ee6:4d44%3]) with mapi id 15.20.5525.011; Tue, 16 Aug 2022 08:42:35 +0000 From: Stefan Herbrechtsmeier To: u-boot@lists.denx.de Cc: Stefan Herbrechtsmeier , Alper Nebi Yasak , Simon Glass Subject: [PATCH v4 05/13] binman: Simplify comp_util class Date: Tue, 16 Aug 2022 10:42:02 +0200 Message-Id: <20220816084210.14972-5-stefan.herbrechtsmeier-oss@weidmueller.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220816084210.14972-1-stefan.herbrechtsmeier-oss@weidmueller.com> References: <20220816084210.14972-1-stefan.herbrechtsmeier-oss@weidmueller.com> X-ClientProxiedBy: FR0P281CA0139.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:96::14) To PAXPR08MB6969.eurprd08.prod.outlook.com (2603:10a6:102:1d8::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0f359fae-22e7-4c9f-49df-08da7f63442d X-MS-TrafficTypeDiagnostic: AM6PR08MB3096:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: N6bnA+oRBQSvr5mjYql0bBNwrbwnPZ/V+vHrJdFoY6nbSnhmBsezQN7Pzfc1mwM1C1xRqNFiRMBRNt2/QIbuNyFz4ckVblAi/s/dSYt88kPcllzLFx9E1mmq+TaO123M+7W1em26VlsMzsObD7G7HcOrc8yBK8Vljb6bbHUHhAmO/IZjdg0/zk8vk2eFt/7q85beELzUgYh6X8BMvehhSElc4m6GK/CCmeqB6o2lp2fckjjh7QThucjdB3R1FSwZR1MJFu8QCR5dHZ/RChMqgeS1W9h9QYYoqngzkNNDyhDe3/ss+ybWUHaKyNjI6xBQn1Lg52KvS6uzJKEpW8VKmeUlUNDlyzywN+xBoZlmYf2nKLeEJ8m5mLN2atQFJ+8k6yvsuMcjqfGi4FtDUyhVppoiuy16JhMayRBls7ODIcgOg2lRWelbVdm47gHb38u8IExihj36RpXu/NZBPAvLOwa8cmVs8X8kXAngPxasg+VaNRD8vXU0ZDa8H7ey/GBYSuW0Y0vIdkLwpuI9WylNMtcfne0vuCsUJH/7+VGeMVeQ27QrZ/6p/hzrrO3Hc65wk3hPeHnqCprdcZUcgSJsOYg2xjwuYiZyDpchpcpQpG8GwMTq7l1BcQD7YVaF+lyDJHvJeTZ7QFjnqvSKvVxwLpKSQxdKtdvuVcH9AOsn8MJe+CzGAf02LFRnlA10PRnZcRiu0JtDpMnXF9vYirIu3o+VR6wKE2HqcD3IEk+47qXRRudtwSsRFm9/3Oi4ZNtAqv/xzSQQcUpvhorZYoJ7xdy3KH1OuXkm1K38MgtsR4POxvFIY0REGEAo+wN1iUs8 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAXPR08MB6969.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(6666004)(66556008)(66476007)(38100700002)(38350700002)(478600001)(8676002)(8936002)(66946007)(6486002)(5660300002)(54906003)(4326008)(6506007)(316002)(186003)(26005)(6512007)(2616005)(36756003)(1076003)(83380400001)(86362001)(6916009)(2906002)(52116002)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?2IsKAwLV+QKPTf8OXr6ludYzkVAR?= =?utf-8?q?WE2c/2/F5OKDaDxVu3U1dwVONk4CeqQ1kL/ZMSVjExRyT+KXwovhpeTzz4i1pNNTz?= =?utf-8?q?IyNuCl0jetMC9VvNHxvN4aHO76S82XjtRR72isceLphlzyCl9p04iuC7zx4KCDdXj?= =?utf-8?q?jrBIh5XXfw3OV3ZgnAC8NIPVD+pJuyjK9GotkM5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wvSWEfE5ZUaJFryRxNp8spN+?= =?utf-8?q?WUrhKOKaRH4KvQQtXvw6G9Vvpx6FkkVcSQggXJdjXHSdvH1qHoMHRTihjoQBSTqvv?= =?utf-8?q?XI1zfUNjSGvlbCGB2QCxkAdfT/0Mlfbfm7ssIYwQN8L5hWiZiPu+CsWvXCdWB3sRo?= =?utf-8?q?fLLBjva1zLeKLWJIle0kvyxj8j4VKi2Rp7O55LDjNOSWhgmXcw0se39CNts2KMwQX?= =?utf-8?q?4zk5uTAeUA5A48YEKdbHOOOZvhDlVN3ahGoTrJU1ksOQocHpwlqj7ijhfP5Fvc5/o?= =?utf-8?q?O9znTCed2eTz4OTP8z22f/+xfhpaAw9CuTAi2jaNkv4UblhMj2gbPjJTOE6/6VtZ2?= =?utf-8?q?7H9Zhiff3ZWBb3SlvF88jg8c8V9bGYtalY26PC1x9OQPYBKpL9Nw8U/WXVWbWctQT?= =?utf-8?q?Olh276tV3v6H/rMyvbTp1m4g8TkQLt6M6falszP0uhfpVBiZPYSkuA2LiTwTmuC8Y?= =?utf-8?q?N8r2sFAraT021xOykZnBCN7a0T5xMXveaw=3D=3D?= X-OriginatorOrg: weidmueller.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0f359fae-22e7-4c9f-49df-08da7f63442d X-MS-Exchange-CrossTenant-AuthSource: PAXPR08MB6969.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Aug 2022 08:42:35.4604 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: e4289438-1c5f-4c95-a51a-ee553b8b18ec X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: F6vguyWrt2YT0wSRhnEkHw2Nq2BXR6hoAel+PrNd7frHJLY45c8AROUqOvECexkOT07o/LT6ZYU5viuWl209dw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3096 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean From: Stefan Herbrechtsmeier Simplify the comp_util class by remove duplicate code as preparation for additional compress algorithms. Signed-off-by: Stefan Herbrechtsmeier Reviewed-by: Simon Glass --- (no changes since v3) Changes in v3: - Rename COMPRESSIONS to ALGORITHMS - Rework existing comments - Add _get_tool_name function comment - Add _get_tool function comment tools/binman/cbfs_util_test.py | 2 +- tools/binman/comp_util.py | 101 +++++++++++++++++++++++---------- tools/binman/ftest.py | 2 +- 3 files changed, 72 insertions(+), 33 deletions(-) diff --git a/tools/binman/cbfs_util_test.py b/tools/binman/cbfs_util_test.py index f86b295149..44ebd04278 100755 --- a/tools/binman/cbfs_util_test.py +++ b/tools/binman/cbfs_util_test.py @@ -50,7 +50,7 @@ class TestCbfs(unittest.TestCase): cls.cbfstool = bintool.Bintool.create('cbfstool') cls.have_cbfstool = cls.cbfstool.is_present() - cls.have_lz4 = comp_util.HAVE_LZ4 + cls.have_lz4 = comp_util.is_present('lz4') @classmethod def tearDownClass(cls): diff --git a/tools/binman/comp_util.py b/tools/binman/comp_util.py index 269bbf7975..00761d44cc 100644 --- a/tools/binman/comp_util.py +++ b/tools/binman/comp_util.py @@ -1,69 +1,108 @@ # SPDX-License-Identifier: GPL-2.0+ # Copyright 2022 Google LLC +# Copyright (C) 2022 Weidmüller Interface GmbH & Co. KG +# Stefan Herbrechtsmeier # -"""Utilities to compress and decompress data""" +"""Utilities to compress and decompress data + +This supports the following compression algorithm: + none + lz4 + lzma + +Note that for lzma this uses an old version of the algorithm, not that +provided by xz. + +This requires the following tools: + lz4 + lzma_alone + +It also requires an output directory to be previously set up, by calling +PrepareOutputDir(). +""" import tempfile from binman import bintool from patman import tools -LZ4 = bintool.Bintool.create('lz4') -HAVE_LZ4 = LZ4.is_present() +# Supported compression algorithms +ALGORITHMS = ['lz4', 'lzma'] -LZMA_ALONE = bintool.Bintool.create('lzma_alone') -HAVE_LZMA_ALONE = LZMA_ALONE.is_present() +bintools = {} +def _get_tool_name(algo): + """Get the tool name of a compression algorithm -def compress(indata, algo): - """Compress some data using a given algorithm + Args: + algo (str): Algorithm to use - Note that for lzma this uses an old version of the algorithm, not that - provided by xz. + Returns: + str: Tool name + """ + names = {'lzma': 'lzma_alone'} + return names.get(algo, algo) + +def _get_tool(algo): + """Get the bintool object of a compression algorithm - This requires 'lz4' and 'lzma_alone' tools. It also requires an output - directory to be previously set up, by calling PrepareOutputDir(). + The function creates new bintool object on demand per compression algorithm + and save it in a global bintools dictionary. + + Args: + algo (str): Algorithm to use + + Returns: + A bintool object for the compression algorithm + """ + global bintools + name = _get_tool_name(algo) + tool = bintools.get(name) + if not tool: + tool = bintool.Bintool.create(name) + bintools[name] = tool + return tool + +def compress(indata, algo): + """Compress some data using a given algorithm Args: indata (bytes): Input data to compress - algo (str): Algorithm to use ('none', 'lz4' or 'lzma') + algo (str): Algorithm to use Returns: bytes: Compressed data """ if algo == 'none': return indata - if algo == 'lz4': - data = LZ4.compress(indata) - # cbfstool uses a very old version of lzma - elif algo == 'lzma': - data = LZMA_ALONE.compress(indata) - else: + if algo not in ALGORITHMS: raise ValueError("Unknown algorithm '%s'" % algo) + + tool = _get_tool(algo) + data = tool.compress(indata) + return data def decompress(indata, algo): """Decompress some data using a given algorithm - Note that for lzma this uses an old version of the algorithm, not that - provided by xz. - - This requires 'lz4' and 'lzma_alone' tools. It also requires an output - directory to be previously set up, by calling PrepareOutputDir(). - Args: indata (bytes): Input data to decompress - algo (str): Algorithm to use ('none', 'lz4' or 'lzma') + algo (str): Algorithm to use Returns: - (bytes) Compressed data + bytes: Decompressed data """ if algo == 'none': return indata - if algo == 'lz4': - data = LZ4.decompress(indata) - elif algo == 'lzma': - data = LZMA_ALONE.decompress(indata) - else: + if algo not in ALGORITHMS: raise ValueError("Unknown algorithm '%s'" % algo) + + tool = _get_tool(algo) + data = tool.decompress(indata) + return data + +def is_present(algo): + tool = _get_tool(algo) + return tool.is_present() diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py index 057b4e28b7..96c15cff77 100644 --- a/tools/binman/ftest.py +++ b/tools/binman/ftest.py @@ -212,7 +212,7 @@ class TestFunctional(unittest.TestCase): TestFunctional._MakeInputFile('tee.elf', tools.read_file(cls.ElfTestFile('elf_sections'))) - cls.have_lz4 = comp_util.HAVE_LZ4 + cls.have_lz4 = comp_util.is_present('lz4') @classmethod def tearDownClass(cls):