File: generate.py

package info (click to toggle)
xr-hardware 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 220 kB
  • sloc: python: 244; makefile: 40
file content (59 lines) | stat: -rw-r--r-- 1,801 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
#!/usr/bin/env python3
# Copyright 2019-2024, Collabora, Ltd.
# SPDX-License-Identifier: BSL-1.0
#
# Original Author: Rylie Pavlik <rylie.pavlik@collabora.com>
"""XR Hardware generation helper code."""

# pylint: disable=E501
_SHARED_PREFIX = "\n".join(
    (
        "# Do not edit this file - generated by {script}",
        "#",
        "# Copyright"
        + " 2019-2024, Collabora, Ltd. and the "
        + "xr-hardware contributors",
        "#",
        "# SPDX-License-" + "Identifier: BSL-1.0",
        "",
    )
)


_RULES_TEMPLATE_PREFIX = """
# Must be located at a number smaller than 73 for uaccess to work right!

# Skip if a remove
ACTION=="remove", GOTO="xrhardware_end"

"""

# Must be before 73 to have uaccess do what it should!
_RULES_TEMPLATE_SUFFIX = """

# Exit if we didn't find one
ENV{ID_xrhardware}!="1", GOTO="xrhardware_end"

# XR devices with serial ports aren't modems, modem-manager
ENV{ID_xrhardware_USBSERIAL_NAME}!="", SUBSYSTEM=="usb", ENV{ID_MM_DEVICE_IGNORE}="1"

# Make friendly symlinks for XR USB-Serial devices.
ENV{ID_xrhardware_USBSERIAL_NAME}!="", SUBSYSTEM=="tty", SYMLINK+="ttyUSB.$env{ID_xrhardware_USBSERIAL_NAME}"

LABEL="xrhardware_end"
"""  # noqa: E501 # it's too long because that's what the template is.


def generate_file_contents(script, body):
    """Return generated file contents for the given script file and body."""
    from pathlib import Path

    script = str(Path(script).name)
    shared_prefix = _SHARED_PREFIX.format(script=script)
    return "\n".join((shared_prefix, body))


def generate_rules_file_contents(script, fn, body):
    """Return the body surrounded by the rules template."""
    parts = ["# " + fn, _RULES_TEMPLATE_PREFIX, body, _RULES_TEMPLATE_SUFFIX]
    return generate_file_contents(script, "\n".join(parts))