Skip to content

Commit 7077bc6

Browse files
committed
turn ecodes.sh into a python script: genecodes.py
1 parent 47b117f commit 7077bc6

4 files changed

Lines changed: 95 additions & 5 deletions

File tree

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# make github and sdist happy
22
include README.rst
3-
include evdev/ecodes.sh
3+
include evdev/genecodes.py
44
include LICENSE

evdev/ecodes.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Generate a Python extension module that exports macros from
44
# /usr/include/linux/input.h
55

6+
# This script is obsolete. Please use `python -m evdev.genecodes`.
67

78
header=${1:-/usr/include/linux/input.h}
89
[[ ! -e $header ]] && echo "no such file: $header" && exit 1

evdev/genecodes.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8; -*-
3+
4+
'''
5+
Generate a Python extension module that exports macros from
6+
/usr/include/linux/input.h
7+
'''
8+
9+
import os, sys, re
10+
11+
12+
template = r'''
13+
#include <Python.h>
14+
#include <linux/input.h>
15+
16+
/* Automatically generated by evdev.genecodes */
17+
/* Generated on %s */
18+
19+
#define MODULE_NAME "_ecodes"
20+
#define MODULE_HELP "linux/input.h macros"
21+
22+
static PyMethodDef MethodTable[] = {
23+
{ NULL, NULL, 0, NULL}
24+
};
25+
26+
#if PY_MAJOR_VERSION >= 3
27+
static struct PyModuleDef moduledef = {
28+
PyModuleDef_HEAD_INIT,
29+
MODULE_NAME,
30+
MODULE_HELP,
31+
-1, /* m_size */
32+
MethodTable, /* m_methods */
33+
NULL, /* m_reload */
34+
NULL, /* m_traverse */
35+
NULL, /* m_clear */
36+
NULL, /* m_free */
37+
};
38+
#endif
39+
40+
static PyObject *
41+
moduleinit(void)
42+
{
43+
44+
#if PY_MAJOR_VERSION >= 3
45+
PyObject* m = PyModule_Create(&moduledef);
46+
#else
47+
PyObject* m = Py_InitModule3(MODULE_NAME, MethodTable, MODULE_HELP);
48+
#endif
49+
50+
if (m == NULL) return NULL;
51+
52+
%s
53+
54+
return m;
55+
}
56+
57+
#if PY_MAJOR_VERSION >= 3
58+
PyMODINIT_FUNC
59+
PyInit__ecodes(void)
60+
{
61+
return moduleinit();
62+
}
63+
#else
64+
PyMODINIT_FUNC
65+
init_ecodes(void)
66+
{
67+
moduleinit();
68+
}
69+
#endif
70+
'''
71+
72+
header = '/usr/include/linux/input.h' if len(sys.argv) == 1 else sys.argv[1]
73+
regex = r'#define +((?:KEY|ABS|REL|SW|MSC|LED|BTN|REP|SND|ID|EV|BUS|SYN|FF)_\w+)'
74+
regex = re.compile(regex)
75+
76+
if not os.path.exists(header):
77+
print('no such file: %s' % header)
78+
sys.exit(1)
79+
80+
def getmacros():
81+
for line in open(header):
82+
macro = regex.search(line)
83+
if macro:
84+
yield ' PyModule_AddIntMacro(m, %s);' % macro.group(1)
85+
86+
uname = list(os.uname()); del uname[1]
87+
uname = ' '.join(uname)
88+
89+
macros = os.linesep.join(getmacros())
90+
print(template % (uname, macros))

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ def create_ecodes():
7575

7676
from subprocess import check_call
7777

78-
print('writing ecodes.c (using {})'.format(header))
79-
check_call('bash ./ecodes.sh {} > ecodes.c'.format(header),
80-
cwd='{}/evdev'.format(here),
81-
shell=True)
78+
print('writing ecodes.c (using %s)' % header)
79+
cmd = '%s genecodes.py %s > ecodes.c' % (sys.executable, header)
80+
check_call(cmd, cwd="%s/evdev" % here, shell=True)
8281

8382

8483
# :todo: figure out a smarter way to do this

0 commit comments

Comments
 (0)