From 15c43f34073f6315bc006de4c992ab19a6cbaa28 Mon Sep 17 00:00:00 2001 From: Yanray Wang Date: Mon, 17 Jul 2023 11:17:12 +0800 Subject: [PATCH] code_size_compare: add a base class as CodeSizeGenerator Signed-off-by: Yanray Wang --- scripts/code_size_compare.py | 64 +++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/scripts/code_size_compare.py b/scripts/code_size_compare.py index 3c0f83d9a4..a5625c32a1 100755 --- a/scripts/code_size_compare.py +++ b/scripts/code_size_compare.py @@ -126,7 +126,44 @@ class CodeSizeInfo: # pylint: disable=too-few-public-methods sys.exit(1) -class CodeSizeGeneratorWithSize: +class CodeSizeGenerator: + """ A generator based on size measurement tool for library objects. + + This is an abstract class. To use it, derive a class that implements + size_generator_write_record and size_generator_write_comparison methods, + then call both of them with proper arguments. + """ + def size_generator_write_record( + self, + revision: str, + code_size_text: typing.Dict, + output_file: str + ) -> None: + """Write size record into a file. + + revision: Git revision.(E.g: commit) + code_size_text: text output (utf-8) from code size measurement tool. + output_file: file which the code size record is written to. + """ + raise NotImplementedError + + def size_generator_write_comparison( + self, + old_rev: str, + new_rev: str, + output_stream + ) -> None: + """Write a comparision result into a stream between two revisions. + + old_rev: old git revision to compared with. + new_rev: new git revision to compared with. + output_stream: stream which the code size record is written to. + (E.g: file / sys.stdout) + """ + raise NotImplementedError + + +class CodeSizeGeneratorWithSize(CodeSizeGenerator): """Code Size Base Class for size record saving and writing.""" class SizeEntry: # pylint: disable=too-few-public-methods @@ -249,6 +286,31 @@ class CodeSizeGeneratorWithSize: else: output.write("{} {}\n".format(fname, new_size)) + def size_generator_write_record( + self, + revision: str, + code_size_text: typing.Dict, + output_file: str + ) -> None: + """Write size record into a specified file based on Git revision and + output from `size` tool.""" + for mod, size_text in code_size_text.items(): + self.set_size_record(revision, mod, size_text) + + print("Generating code size csv for", revision) + output = open(output_file, "w") + self.write_size_record(revision, output) + + def size_generator_write_comparison( + self, + old_rev: str, + new_rev: str, + output_stream + ) -> None: + """Write a comparision result into a stream between two revisions.""" + output = open(output_stream, "w") + self.write_comparison(old_rev, new_rev, output) + class CodeSizeComparison: """Compare code size between two Git revisions."""