File: create-plugin.py

package info (click to toggle)
fwupd 2.0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,340 kB
  • sloc: ansic: 274,440; python: 11,468; xml: 9,432; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (102 lines) | stat: -rwxr-xr-x 3,307 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
#
# Copyright 2023 Richard Hughes <richard@hughsie.com>
#
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# pylint: disable=invalid-name,missing-docstring,consider-using-f-string

import os
import datetime
import argparse
import glob
import sys
from jinja2 import Environment, FileSystemLoader, select_autoescape

subst = {}


# convert a snake-case name into CamelCase
def _to_camelcase(value: str) -> str:
    return "".join([tmp[0].upper() + tmp[1:].lower() for tmp in value.split("_")])


def _subst_replace(data: str) -> str:
    for key, value in subst.items():
        data = data.replace(key, value)
    return data


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--vendor",
        type=str,
        help="Vendor name",
        required=True,
    )
    parser.add_argument(
        "--example",
        type=str,
        help="Plugin basename",
        required=True,
    )
    parser.add_argument("--parent", type=str, default="Usb", help="Device parent GType")
    parser.add_argument(
        "--year", type=int, default=datetime.date.today().year, help="Copyright year"
    )
    parser.add_argument("--author", type=str, help="Copyright author", required=True)
    parser.add_argument("--email", type=str, help="Copyright email", required=True)
    args = parser.parse_args()

    try:
        vendor: str = args.vendor.replace("-", "_")
        example: str = args.example.replace("-", "_")

        # first in list
        subst["VendorExample"] = _to_camelcase(vendor) + _to_camelcase(example)
        subst["vendor_example"] = vendor.lower() + "_" + example.lower()
        subst["vendor_dash_example"] = vendor.lower() + "-" + example.lower()
        subst["VENDOR_EXAMPLE"] = vendor.upper() + "_" + example.upper()

        # second
        for key, value in {
            "Vendor": vendor,
            "Example": example,
            "Parent": args.parent,
            "Year": str(args.year),
            "Author": args.author,
            "Email": args.email,
        }.items():
            subst[key] = _to_camelcase(value)
            subst[key.lower()] = value.lower()
            subst[key.upper()] = value.upper()

        # overwrite author and email so they appear as entered
        subst["Author"] = args.author
        subst["Email"] = args.email

    except NotImplementedError as e:
        print(e)
        sys.exit(1)

    template_src: str = "vendor-example"
    os.makedirs(os.path.join("plugins", _subst_replace(template_src)), exist_ok=True)

    srcdir: str = sys.argv[0].rsplit("/", maxsplit=2)[0]
    env = Environment(
        loader=FileSystemLoader(srcdir),
        autoescape=select_autoescape(),
        keep_trailing_newline=True,
    )
    for fn in glob.iglob(f"{srcdir}/plugins/{template_src}/**", recursive=True):
        if os.path.isdir(fn):
            fn_rel: str = os.path.relpath(fn, srcdir)
            os.makedirs(_subst_replace(fn_rel), exist_ok=True)
            continue
        fn_rel: str = os.path.relpath(fn, srcdir)
        template = env.get_template(fn_rel)
        filename: str = _subst_replace(fn_rel.replace(".in", ""))
        with open(filename, "wb") as f_dst:
            f_dst.write(template.render(subst).encode())
        print(f"wrote {filename}")