Debian Codegraph @

src/evdev/genecodes_c.py

raw
1 """
2 Generate a Python extension module with the constants defined in linux/input.h.
3 """
4
5 import getopt
6 import os
7 import re
8 import sys
9
10 # -----------------------------------------------------------------------------
11 # The default header file locations to try.
12 headers = [
13 "/usr/include/linux/input.h",
14 "/usr/include/linux/input-event-codes.h",
15 "/usr/include/linux/uinput.h",
16 ]
17
18 opts, args = getopt.getopt(sys.argv[1:], "", ["ecodes", "stubs", "reproducible"])
19 if not opts:
20 print("usage: genecodes.py [--ecodes|--stubs] [--reproducible] <headers>")
21 exit(2)
22
23 if args:
24 headers = args
25
26 reproducible = ("--reproducible", "") in opts
27
28
29 # -----------------------------------------------------------------------------
30 macro_regex = r"#define\s+((?:KEY|ABS|REL|SW|MSC|LED|BTN|REP|SND|ID|EV|BUS|SYN|FF|UI_FF|INPUT_PROP)_\w+)"
31 macro_regex = re.compile(macro_regex)
32
33 if reproducible:
34 uname = "hidden for reproducibility"
35 else:
36 # Uname without hostname.
37 uname = list(os.uname())
38 uname = " ".join((uname[0], *uname[2:]))
39
40
41 # -----------------------------------------------------------------------------
42 template_ecodes = r"""
43 #include <Python.h>
44 #ifdef __FreeBSD__
45 #include <dev/evdev/input.h>
46 #include <dev/evdev/uinput.h>
47 #else
48 #include <linux/input.h>
49 #include <linux/uinput.h>
50 #endif
51
52 /* Automatically generated by evdev.genecodes */
53 /* Generated on %s */
54 /* Generated from %s */
55
56 #define MODULE_NAME "_ecodes"
57 #define MODULE_HELP "linux/input.h macros"
58
59 static PyMethodDef MethodTable[] = {
60 { NULL, NULL, 0, NULL}
61 };
62
63 static struct PyModuleDef moduledef = {
64 PyModuleDef_HEAD_INIT,
65 MODULE_NAME,
66 MODULE_HELP,
67 -1, /* m_size */
68 MethodTable, /* m_methods */
69 NULL, /* m_reload */
70 NULL, /* m_traverse */
71 NULL, /* m_clear */
72 NULL, /* m_free */
73 };
74
75 PyMODINIT_FUNC
76 PyInit__ecodes(void)
77 {
78 PyObject* m = PyModule_Create(&moduledef);
79 if (m == NULL) return NULL;
80 #ifdef Py_GIL_DISABLED
81 PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
82 #endif
83
84 %s
85
86 return m;
87 }
88 """
89
90
91 template_stubs = r"""
92 # Automatically generated by evdev.genecodes
93 # Generated on %s
94 # Generated from %s
95
96 # pylint: skip-file
97
98 ecodes: dict[str, int]
99 keys: dict[int, str|list[str]]
100 bytype: dict[int, dict[int, str|list[str]]]
101
102 KEY: dict[int, str|list[str]]
103 ABS: dict[int, str|list[str]]
104 REL: dict[int, str|list[str]]
105 SW: dict[int, str|list[str]]
106 MSC: dict[int, str|list[str]]
107 LED: dict[int, str|list[str]]
108 BTN: dict[int, str|list[str]]
109 REP: dict[int, str|list[str]]
110 SND: dict[int, str|list[str]]
111 ID: dict[int, str|list[str]]
112 EV: dict[int, str|list[str]]
113 BUS: dict[int, str|list[str]]
114 SYN: dict[int, str|list[str]]
115 FF_STATUS: dict[int, str|list[str]]
116 FF_INPUT_PROP: dict[int, str|list[str]]
117
118 %s
119 """
120
121
122 def parse_headers(headers=headers):
123 for header in headers:
124 try:
125 fh = open(header)
126 except (IOError, OSError):
127 continue
128
129 for line in fh:
130 macro = macro_regex.search(line)
131 if macro:
132 yield macro.group(1)
133
134
135 all_macros = list(parse_headers())
136 if not all_macros:
137 print("no input macros found in: %s" % " ".join(headers), file=sys.stderr)
138 sys.exit(1)
139
140 # pylint: disable=possibly-used-before-assignment, used-before-assignment
141 if ("--ecodes", "") in opts:
142 body = (" PyModule_AddIntMacro(m, %s);" % macro for macro in all_macros)
143 template = template_ecodes
144 elif ("--stubs", "") in opts:
145 body = ("%s: int" % macro for macro in all_macros)
146 template = template_stubs
147
148 body = os.linesep.join(body)
149 text = template % (uname, headers if not reproducible else ["hidden for reproducibility"], body)
150 print(text.strip())