File: stamp_metadata_parser_version.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (92 lines) | stat: -rwxr-xr-x 3,277 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Build rules to generate metadata schema versions."""

import argparse
import shutil
import sys
import os

PLACEHOLDER_STRING = "{LATEST_METADATA_PARSER_VERSION}"


def extract_version_from_file_comment(metadata_schema_file: str):
    """Extracts the schema semantic version from a comment in the schema file.

    Args:
        metadata_schema_file: The file path of the metadata schema file which contains
        the schema semantic version.
    Returns:
        A string of the schema semantic version.

    Raises:
        ValueError: An error occurred accessing the schema semantic version.
    """
    with open(metadata_schema_file, 'r') as file:
        for line in list(file):
            if "Schema Semantic version:" in line:
                return line.split(":", 1)[1].strip()
    raise ValueError("Schema semantic version not found.")


def copy_template(template: str, directory: str):
    """Copies header template into target directory if template exists and
    contains placeholder string.

    Args:
        template: The file path of the header template.
        directory: The desired directory of the new metadata parser.
    Raises:
        ValueError: An error occurred finding the placeholder string in the template.
    """
    with open(template, 'r') as file:
        if PLACEHOLDER_STRING not in file.read():
            raise ValueError("Placeholder string: " + PLACEHOLDER_STRING +
                             " not found in template")
    os.makedirs(os.path.dirname(directory), exist_ok=True)
    shutil.copyfile(template, directory)


def replace_version_in_template(template_copy: str, version: str):
    """Replaces the parser version placeholder string with a version number.

    Args:
        template_copy: The file path of the copied header template.
        version: The metadata parser version.
    Raises:
        ValueError: An error occurred finding the placeholder string in the template copy.
    """
    with open(template_copy, 'r') as file:
        filedata = file.read()
        if PLACEHOLDER_STRING not in filedata:
            raise ValueError("Placeholder string: " + PLACEHOLDER_STRING +
                             " not found in template copy")
        filedata = filedata.replace(PLACEHOLDER_STRING, version)
    with open(template_copy, 'w') as file:
        file.write(filedata)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--schema',
                        required = True,
                        help = 'File path of metadata schema file.')
    parser.add_argument('--template',
                        required = True,
                        help = 'File path of header template file.')
    parser.add_argument('--output',
                        required = True,
                        help = 'Desired output directory of metadata parser header.')
    args = parser.parse_args()

    version = extract_version_from_file_comment(args.schema)
    copy_template(args.template, args.output)
    replace_version_in_template(args.output, version)

    return 0


if __name__ == '__main__':
    sys.exit(main())