Debian Codegraph @

PC/layout/support/constants.py

raw
1 """
2 Constants for generating the layout.
3 """
4
5 __author__ = "Steve Dower <[email protected]>"
6 __version__ = "3.8"
7
8 import os
9 import pathlib
10 import re
11 import struct
12 import sys
13
14
15 def _unpack_hexversion():
16 try:
17 hexversion = int(os.getenv("PYTHON_HEXVERSION"), 16)
18 return struct.pack(">i", hexversion)
19 except (TypeError, ValueError):
20 pass
21 if os.getenv("PYTHONINCLUDE"):
22 try:
23 return _read_patchlevel_version(pathlib.Path(os.getenv("PYTHONINCLUDE")))
24 except OSError:
25 pass
26 return struct.pack(">i", sys.hexversion)
27
28
29 def _get_suffix(field4):
30 name = {0xA0: "a", 0xB0: "b", 0xC0: "rc"}.get(field4 & 0xF0, "")
31 if name:
32 serial = field4 & 0x0F
33 return f"{name}{serial}"
34 return ""
35
36
37 def _read_patchlevel_version(sources):
38 if not sources.match("Include"):
39 sources /= "Include"
40 values = {}
41 with open(sources / "patchlevel.h", "r", encoding="utf-8") as f:
42 for line in f:
43 m = re.match(r'#\s*define\s+(PY_\S+?)\s+(\S+)', line.strip(), re.I)
44 if m and m.group(2):
45 v = m.group(2)
46 if v.startswith('"'):
47 v = v[1:-1]
48 else:
49 v = values.get(v, v)
50 if isinstance(v, str):
51 try:
52 v = int(v, 16 if v.startswith("0x") else 10)
53 except ValueError:
54 pass
55 values[m.group(1)] = v
56 return (
57 values["PY_MAJOR_VERSION"],
58 values["PY_MINOR_VERSION"],
59 values["PY_MICRO_VERSION"],
60 values["PY_RELEASE_LEVEL"] << 4 | values["PY_RELEASE_SERIAL"],
61 )
62
63
64 def check_patchlevel_version(sources):
65 got = _read_patchlevel_version(sources)
66 if got != (VER_MAJOR, VER_MINOR, VER_MICRO, VER_FIELD4):
67 return f"{got[0]}.{got[1]}.{got[2]}{_get_suffix(got[3])}"
68
69
70 VER_MAJOR, VER_MINOR, VER_MICRO, VER_FIELD4 = _unpack_hexversion()
71 VER_SUFFIX = _get_suffix(VER_FIELD4)
72 VER_FIELD3 = VER_MICRO << 8 | VER_FIELD4
73 VER_DOT = "{}.{}".format(VER_MAJOR, VER_MINOR)
74
75 PYTHON_DLL_NAME = "python{}{}.dll".format(VER_MAJOR, VER_MINOR)
76 PYTHON_STABLE_DLL_NAME = "python{}.dll".format(VER_MAJOR)
77 PYTHON_ZIP_NAME = "python{}{}.zip".format(VER_MAJOR, VER_MINOR)
78 PYTHON_PTH_NAME = "python{}{}._pth".format(VER_MAJOR, VER_MINOR)
79
80 PYTHON_CHM_NAME = "python{}{}{}{}.chm".format(
81 VER_MAJOR, VER_MINOR, VER_MICRO, VER_SUFFIX
82 )
83
84 FREETHREADED_PYTHON_DLL_NAME = "python{}{}t.dll".format(VER_MAJOR, VER_MINOR)
85 FREETHREADED_PYTHON_STABLE_DLL_NAME = "python{}t.dll".format(VER_MAJOR)