File: check_config_variables.py

package info (click to toggle)
ddnet 19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,960 kB
  • sloc: cpp: 195,050; ansic: 58,572; python: 5,568; asm: 946; sh: 941; java: 366; xml: 206; makefile: 31
file content (61 lines) | stat: -rwxr-xr-x 2,230 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import os
import re
import sys

def read_all_lines(filename):
	with open(filename, 'r', encoding='utf-8') as file:
		return file.readlines()

def parse_config_variables(lines):
	pattern = r'^MACRO_CONFIG_[A-Z]+\((.*?), (.*?),.*'
	matches = {}
	for line in lines:
		match = re.match(pattern, line)
		if match:
			matches[match.group(1)] = match.group(2)
	return matches

def generate_regex(variable_code):
	return fr'(g_Config\.m_{variable_code}\b|Config\(\)->m_{variable_code}\b|m_pConfig->m_{variable_code}\b)'

def find_config_variables(config_variables):
	"""Returns the config variables which were not found."""
	# Set of variables that have yet to be found.
	variables_not_found = set(config_variables)
	# Precompile regex for every config variable (~1.6x speedup).
	regex_cache = {}
	for variable_code in variables_not_found.copy():
		regex_cache[variable_code] = re.compile(generate_regex(variable_code))
	for root, _, files in os.walk('src'):
		if not variables_not_found:
			break
		for file in files:
			if not variables_not_found:
				break
			if file.endswith(('.cpp', '.h')) and not 'external' in root:
				filepath = os.path.join(root, file)
				with open(filepath, 'r', encoding='utf-8') as f:
					content = f.read()
					# Only variables not yet found are searched in the remaining files (~3.6x speedup).
					# Copy set to remove elements while iterating, which is slightly faster than collecting
					# the elements to remove in another set and removing them after the loop.
					for variable_code in variables_not_found.copy():
						if regex_cache[variable_code].search(content):
							variables_not_found.remove(variable_code)
	return variables_not_found

def main():
	lines = read_all_lines('src/engine/shared/config_variables.h')
	config_variables = parse_config_variables(lines)
	config_variables_not_found = find_config_variables(config_variables)
	for variable_code in config_variables_not_found:
		print(f'The config variable \'{config_variables[variable_code]}\' is unused.')
	if config_variables_not_found:
		print('Error: Unused config variables found.')
		return 1
	print('Success: No unused config variables found.')
	return 0

if __name__ == '__main__':
	sys.exit(main())