rewrite_version_html.py 3.31 KB
Newer Older
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
#!/usr/bin/env python3

import re


def get_pi_version():
    """ return a dict with raspi version info """

    names = {
        "0002": "(Model B Rev 1, 256MB)",
        "0003": "(Model B Rev 1, ECN0001 (no fuses, D14 removed), 256MB)",
        "0004": "(Model B Rev 2, 256MB)",
        "0005": "(Model B Rev 2, 256MB)",
        "0006": "(Model B Rev 2, 256MB)",
        "0007": "(Model A, 256MB)",
        "0008": "(Model A, 256MB)",
        "0009": "(Model A, 256MB)",
        "000d": "(Model B Rev 2, 512MB)",
        "000e": "(Model B Rev 2, 512MB)",
        "000f": "(Model B Rev 2, 512MB)",
        "0010": "(Model B+, 512MB)",
        "0013": "(Model B+, 512MB)",
        "900032": "(Model B+, 512MB)",
        "0011": "(Compute Module, 512MB)",
        "0014": "(Compute Module, (Embest, China), 512MB)",
        "0012": "(Model A+, 256MB)",
rgaudin's avatar
rgaudin committed
27
28
29
        "0015": "(Model A+, (Embest, China), 256MB/512MB)",
        "a01041": "(Pi 2 Model B v1.1, (Sony, UK), 1GB)",
        "a21041": "(Pi 2 Model B v1.1, (Embest, China), 1GB)",
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
103
104
105
        "a22042": "(Pi 2 Model B v1.2, (Sony, UK), 1GB)",
        "900092": "(Pi Zero v1.2, 512MB)",
        "900093": "(Pi Zero v1.3, 512MB)",
        "9000C1": "(Pi Zero W, 512MB)",
        "a02082": "(Pi 3 Model B, (Sony, UK), 1GB)",
        "a22082": "(Pi 3 Model B, (Embest, China), 1GB)",
        "a020d3": "(Pi 3 Model B+, (Sony, UK), 1GB)",
    }

    def _get_revision():
        try:
            with open("/proc/cpuinfo", "r") as fp:
                return (
                    [l for l in fp.readlines() if l.startswith("Revision")][-1]
                    .split(":")[-1]
                    .strip()
                )
        except Exception:
            return None

    def _get_model():
        try:
            with open("/sys/firmware/devicetree/base/model", "r") as fp:
                return fp.read().strip().rstrip("\x00")
        except Exception:
            return None

    revision = _get_revision()

    return {"revision": revision, "name": names.get(revision), "model": _get_model()}


def get_string(revision, name, model, as_html=False):
    """ a single line version string for humans from the details """

    if revision is None and model is None:
        return "Unknown (not Pi?)"

    if revision is None and model:
        return model

    if as_html:
        revision_string = "<code>{revision}</code> / {name}".format(
            revision=revision, name=name
        )
    else:
        revision_string = "{revision} / {name}".format(revision=revision, name=name)

    if model is None and revision:
        return revision_string

    return "{model} / {revision_string}".format(
        model=model, revision_string=revision_string
    )


def update_version_file(version_string, fpath="/var/www/version.html"):
    with open(fpath, "r") as fp:
        lines = fp.readlines()
        for index, line in enumerate(lines):
            if 'id="device"' in line:
                lines[index] = re.sub(
                    r'<h3 id="device">(.*)</h3>',
                    '<h3 id="device">{}</h3>'.format(version_string),
                    line,
                )
                break

    with open(fpath, "w") as fp:
        fp.write("".join(lines))


if __name__ == "__main__":
    version_string = get_string(**get_pi_version(), as_html=True)
    update_version_file(version_string)
    print(version_string)