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 # The function generating the progress bar (if any).
21 progress_bar = lambda x, desc: x
22 if os.isatty(sys.stdout.fileno()):
32 def xkbcommontool(rmlvo):
33 r = rmlvo.get('r', 'evdev')
34 m = rmlvo.get('m', 'pc105')
35 l = rmlvo.get('l', 'us')
36 v = rmlvo.get('v', None)
37 o = rmlvo.get('o', None)
45 args += ['--variant', v]
47 args += ['--options', o]
52 print(':: {}'.format(' '.join(args)), file=out)
55 output = subprocess.check_output(args, stderr=subprocess.STDOUT)
57 print(output.decode('utf-8'), file=out)
58 except subprocess.CalledProcessError as err:
59 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
60 print(err.output.decode('utf-8'), file=out)
63 return success, out.getvalue()
67 r = rmlvo.get('r', 'evdev')
68 m = rmlvo.get('m', 'pc105')
69 l = rmlvo.get('l', 'us')
70 v = rmlvo.get('v', None)
71 o = rmlvo.get('o', None)
72 args = ['setxkbmap', '-print']
75 args.append('{}'.format(r))
78 args.append('{}'.format(m))
80 args.append('-layout')
81 args.append('{}'.format(l))
83 args.append('-option')
84 args.append('{}'.format(o))
89 print(':: {}'.format(' '.join(args)), file=out)
92 xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
94 setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE)
95 xkbcomp = subprocess.Popen(xkbcomp_args, stdin=setxkbmap.stdout,
96 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
97 setxkbmap.stdout.close()
98 stdout, stderr = xkbcomp.communicate()
99 if xkbcomp.returncode != 0:
100 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
102 if xkbcomp.returncode != 0 or verbose:
103 print(stdout.decode('utf-8'), file=out)
104 print(stderr.decode('utf-8'), file=out)
106 # This catches setxkbmap errors.
107 except subprocess.CalledProcessError as err:
108 print('ERROR: Failed to compile: {}'.format(' '.join(args)), file=out)
109 print(err.output.decode('utf-8'), file=out)
112 return success, out.getvalue()
116 root = ET.fromstring(open(path).read())
117 layouts = root.findall('layoutList/layout')
121 for e in root.findall('optionList/group/option/configItem/name')
126 layout = l.find('configItem/name').text
127 combos.append({'l': layout})
129 variants = l.findall('variantList/variant')
131 variant = v.find('configItem/name').text
133 combos.append({'l': layout, 'v': variant})
134 for option in options:
135 combos.append({'l': layout, 'v': variant, 'o': option})
140 def run(combos, tool, njobs):
142 with Pool(njobs) as p:
143 results = p.imap_unordered(tool, combos)
144 for r in progress_bar(results, 'testing'):
149 print(output, file=sys.stdout if success else sys.stderr)
156 'libxkbcommon': xkbcommontool,
160 parser = argparse.ArgumentParser(
161 description='Tool to test all layout/variant/option combinations.'
163 parser.add_argument('path', metavar='/path/to/evdev.xml',
165 default=DEFAULT_RULES_XML,
166 help='Path to xkeyboard-config\'s evdev.xml')
167 parser.add_argument('--tool', choices=tools.keys(),
168 type=str, default='libxkbcommon',
169 help='parsing tool to use')
170 parser.add_argument('--jobs', '-j', type=int,
171 default=os.cpu_count() * 4,
172 help='number of processes to use')
173 args = parser.parse_args()
175 tool = tools[args.tool]
177 combos = parse(args.path)
178 failed = run(combos, tool, args.jobs)
182 if __name__ == '__main__':