Debian Codegraph @

src/shared/ethtool-link-mode.py

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import re
5 import shlex
6 import subprocess
7 import sys
8
9 OVERRIDES = {
10 'autoneg': 'autonegotiation',
11 }
12
13 mode = sys.argv[1]
14 cpp = sys.argv[2]
15 include_dirs = sys.argv[3:]
16 xml = mode == '--xml'
17
18 command = [*shlex.split(cpp), '-include', 'linux/ethtool.h'] + include_dirs + ['-']
19 out = subprocess.check_output(command, stdin=subprocess.DEVNULL, universal_newlines=True)
20
21 lines = iter(out.splitlines())
22 for line in lines:
23 if line.startswith('enum ethtool_link_mode_bit_indices {'):
24 break
25
26 entries = []
27 for line in lines:
28 if line.startswith('}'):
29 break
30 # ETHTOOL_LINK_MODE_10baseT_Half_BIT = 0,
31 m = re.match(r'^\s*(ETHTOOL_LINK_MODE_((\d*).*)_BIT)\s*=\s*(\d+),', line)
32 if not m:
33 continue
34 enum, name, speed, value = m.groups()
35
36 name = name.lower().replace('_', '-')
37 name = OVERRIDES.get(name, name)
38
39 duplex = name.split('-')[-1].lower()
40 if duplex not in {'half', 'full'}:
41 duplex = ''
42
43 entries += [(enum, name, speed, value, duplex)]
44
45 if xml:
46 print(' <tbody>')
47
48 entries.sort(key=lambda entry: (int(entry[2]) if entry[2] else 1e20, entry[4], entry[1], entry[3]))
49
50 for enum, name, speed, value, duplex in entries:
51 if xml:
52 print(f'''\
53 <row><entry><option>{name}</option></entry>
54 <entry>{speed}</entry><entry>{duplex}</entry></row>
55 ''')
56 else:
57 enum = f'[{enum}]'
58 print(f' {enum:50} = "{name}",')
59
60 if xml:
61 print(' </tbody>')
62
63 assert len(entries) >= 99