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, desc):
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)
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('-option')
93 args.append('{}'.format(o))
98 print(':: {}'.format(' '.join(args)), file=out)
101 xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
103 setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE)
104 xkbcomp = subprocess.Popen(xkbcomp_args, stdin=setxkbmap.stdout,
105 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
106 universal_newlines=True)
107 setxkbmap.stdout.close()
108 stdout, stderr = xkbcomp.communicate()
109 if xkbcomp.returncode != 0:
110 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
112 if xkbcomp.returncode != 0 or verbose:
113 print(stdout, file=out)
114 print(stderr, file=out)
116 # This catches setxkbmap errors.
117 except subprocess.CalledProcessError as err:
118 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
119 print(err.output, file=out)
122 return success, out.getvalue()
123 except KeyboardInterrupt:
128 root = ET.fromstring(open(path).read())
129 layouts = root.findall('layoutList/layout')
133 for e in root.findall('optionList/group/option/configItem/name')
138 layout = l.find('configItem/name').text
139 combos.append({'l': layout})
141 variants = l.findall('variantList/variant')
143 variant = v.find('configItem/name').text
145 combos.append({'l': layout, 'v': variant})
146 for option in options:
147 combos.append({'l': layout, 'v': variant, 'o': option})
152 def run(combos, tool, njobs):
154 with Pool(njobs) as p:
155 results = p.imap_unordered(tool, combos)
156 for r in progress_bar(results, 'testing'):
161 print(output, file=sys.stdout if success else sys.stderr)
168 'libxkbcommon': xkbcommontool,
172 parser = argparse.ArgumentParser(
173 description='Tool to test all layout/variant/option combinations.'
175 parser.add_argument('path', metavar='/path/to/evdev.xml',
177 default=DEFAULT_RULES_XML,
178 help='Path to xkeyboard-config\'s evdev.xml')
179 parser.add_argument('--tool', choices=tools.keys(),
180 type=str, default='libxkbcommon',
181 help='parsing tool to use')
182 parser.add_argument('--jobs', '-j', type=int,
183 default=os.cpu_count() * 4,
184 help='number of processes to use')
185 args = parser.parse_args()
187 tool = tools[args.tool]
189 combos = parse(args.path)
190 failed = run(combos, tool, args.jobs)
194 if __name__ == '__main__':
197 except KeyboardInterrupt:
198 print('Exiting after Ctrl+C')