7 import xml.etree.ElementTree as ET
8 from multiprocessing import Pool
13 DEFAULT_RULES_XML = '@XKB_CONFIG_ROOT@/rules/evdev.xml'
15 # Meson needs to fill this in so we can call the tool in the buildir.
16 EXTRA_PATH = '@MESON_BUILD_ROOT@'
17 os.environ['PATH'] = ':'.join([EXTRA_PATH, os.getenv('PATH')])
20 def noop_progress_bar(x, total):
24 # The function generating the progress bar (if any).
25 progress_bar = noop_progress_bar
26 if os.isatty(sys.stdout.fileno()):
36 def xkbcommontool(rmlvo):
38 r = rmlvo.get('r', 'evdev')
39 m = rmlvo.get('m', 'pc105')
40 l = rmlvo.get('l', 'us')
41 v = rmlvo.get('v', None)
42 o = rmlvo.get('o', None)
44 'xkbcli-compile-keymap', # this is run in the builddir
50 args += ['--variant', v]
52 args += ['--options', o]
57 print(':: {}'.format(' '.join(args)), file=out)
60 output = subprocess.check_output(args, stderr=subprocess.STDOUT,
61 universal_newlines=True)
63 print(output, file=out)
64 except subprocess.CalledProcessError as err:
65 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
66 print(err.output, file=out)
69 return success, out.getvalue()
70 except KeyboardInterrupt:
76 r = rmlvo.get('r', 'evdev')
77 m = rmlvo.get('m', 'pc105')
78 l = rmlvo.get('l', 'us')
79 v = rmlvo.get('v', None)
80 o = rmlvo.get('o', None)
81 args = ['setxkbmap', '-print']
84 args.append('{}'.format(r))
87 args.append('{}'.format(m))
89 args.append('-layout')
90 args.append('{}'.format(l))
92 args.append('-variant')
93 args.append('{}'.format(v))
95 args.append('-option')
96 args.append('{}'.format(o))
101 print(':: {}'.format(' '.join(args)), file=out)
104 xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
106 setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE)
107 xkbcomp = subprocess.Popen(xkbcomp_args, stdin=setxkbmap.stdout,
108 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
109 universal_newlines=True)
110 setxkbmap.stdout.close()
111 stdout, stderr = xkbcomp.communicate()
112 if xkbcomp.returncode != 0:
113 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
115 if xkbcomp.returncode != 0 or verbose:
116 print(stdout, file=out)
117 print(stderr, file=out)
119 # This catches setxkbmap errors.
120 except subprocess.CalledProcessError as err:
121 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
122 print(err.output, file=out)
125 return success, out.getvalue()
126 except KeyboardInterrupt:
131 root = ET.fromstring(open(path).read())
132 layouts = root.findall('layoutList/layout')
136 for e in root.findall('optionList/group/option/configItem/name')
141 layout = l.find('configItem/name').text
142 combos.append({'l': layout})
144 variants = l.findall('variantList/variant')
146 variant = v.find('configItem/name').text
148 combos.append({'l': layout, 'v': variant})
149 for option in options:
150 combos.append({'l': layout, 'v': variant, 'o': option})
155 def run(combos, tool, njobs):
157 with Pool(njobs) as p:
158 results = p.imap_unordered(tool, combos)
159 for success, output in progress_bar(results, total=len(combos)):
163 print(output, file=sys.stdout if success else sys.stderr)
169 'libxkbcommon': xkbcommontool,
173 parser = argparse.ArgumentParser(
174 description='Tool to test all layout/variant/option combinations.'
176 parser.add_argument('path', metavar='/path/to/evdev.xml',
178 default=DEFAULT_RULES_XML,
179 help='Path to xkeyboard-config\'s evdev.xml')
180 parser.add_argument('--tool', choices=tools.keys(),
181 type=str, default='libxkbcommon',
182 help='parsing tool to use')
183 parser.add_argument('--jobs', '-j', type=int,
184 default=os.cpu_count() * 4,
185 help='number of processes to use')
186 args = parser.parse_args()
188 tool = tools[args.tool]
190 combos = parse(args.path)
191 failed = run(combos, tool, args.jobs)
195 if __name__ == '__main__':
198 except KeyboardInterrupt:
199 print('Exiting after Ctrl+C')