test/rmlvo-to-keymap.c: fix compilation on Darwin (#101)
[platform/upstream/libxkbcommon.git] / test / xkeyboard-config-test.py.in
1 #!/usr/bin/env python
2 import sys
3 import subprocess
4 import os
5 import xml.etree.ElementTree as ET
6
7
8 verbose = True
9
10 DEFAULT_RULES_XML = '@XKB_CONFIG_ROOT@/rules/evdev.xml'
11
12 # Meson needs to fill this in so we can call the tool in the buildir.
13 EXTRA_PATH='@MESON_BUILD_ROOT@'
14 os.environ['PATH'] = ':'.join([EXTRA_PATH, os.getenv('PATH')])
15
16
17 # The function generating the progress bar (if any).
18 progress_bar = lambda x, desc: x
19 if os.isatty(sys.stdout.fileno()):
20     try:
21         from tqdm import tqdm
22         progress_bar = tqdm
23
24         verbose = False
25     except ImportError:
26         pass
27
28
29 def xkbcommontool(r='evdev', m='pc105', l='us', v=None, o=None):
30     args = [
31         'rmlvo-to-keymap',
32         '--rules', r,
33         '--model', m,
34         '--layout', l,
35     ]
36     if v is not None:
37         args += ['--variant', v]
38     if o is not None:
39         args += ['--options', o]
40
41     if verbose:
42         print(':: {}'.format(' '.join(args)))
43
44     try:
45         output = subprocess.check_output(args, stderr=subprocess.STDOUT)
46         if verbose:
47             print(output.decode('utf-8'))
48     except subprocess.CalledProcessError as err:
49         print('ERROR: Failed to compile: {}'.format(' '.join(args)))
50         print(err.output.decode('utf-8'))
51         sys.exit(1)
52
53
54 def xkbcomp(r='evdev', m='pc105', l='us', v='', o=''):
55     args = ['setxkbmap', '-print']
56     if r is not None:
57         args.append('-rules')
58         args.append('{}'.format(r))
59     if m is not None:
60         args.append('-model')
61         args.append('{}'.format(m))
62     if l is not None:
63         args.append('-layout')
64         args.append('{}'.format(l))
65     if o is not None:
66         args.append('-option')
67         args.append('{}'.format(o))
68
69     if verbose:
70         print(':: {}'.format(' '.join(args)))
71
72     try:
73         xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
74
75         setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE)
76         xkbcomp = subprocess.Popen(xkbcomp_args, stdin=setxkbmap.stdout,
77                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
78         setxkbmap.stdout.close()
79         stdout, stderr = xkbcomp.communicate()
80         if xkbcomp.returncode != 0:
81             print('ERROR: Failed to compile: {}'.format(' '.join(args)))
82         if xkbcomp.returncode != 0 or verbose:
83             print(stdout.decode('utf-8'))
84             print(stderr.decode('utf-8'))
85
86     # This catches setxkbmap errors.
87     except subprocess.CalledProcessError as err:
88         print('ERROR: Failed to compile: {}'.format(' '.join(args)))
89         print(err.output.decode('utf-8'))
90
91
92 def parse(root):
93     layouts = root.findall('layoutList/layout')
94
95     options = [
96         e.text
97         for e in root.findall('optionList/group/option/configItem/name')
98     ]
99
100     # Switch this to xkbcomp if needed.
101     tool = xkbcommontool
102     # tool = xkbcomp
103
104     for l in progress_bar(layouts, 'layout '):
105         layout = l.find('configItem/name').text
106         tool(l=layout)
107
108         variants = l.findall('variantList/variant')
109         for v in progress_bar(variants, 'variant'):
110             variant = v.find('configItem/name').text
111             tool(l=layout, v=variant)
112
113             for option in progress_bar(options, 'option '):
114                 tool(l=layout, v=variant, o=option)
115
116
117 def main(args):
118     try:
119         path = args[1]
120     except IndexError:
121         path = DEFAULT_RULES_XML
122
123     with open(path) as f:
124         root = ET.fromstring(f.read())
125         parse(root)
126
127
128 if __name__ == '__main__':
129     main(sys.argv)