keyboard stuff
1""" Functions for working with Makefiles
2"""
3from pathlib import Path
4
5
6def parse_rules_mk_file(file, rules_mk=None):
7 """Turn a rules.mk file into a dictionary.
8
9 Args:
10 file: path to the rules.mk file
11 rules_mk: already parsed rules.mk the new file should be merged with
12
13 Returns:
14 a dictionary with the file's content
15 """
16 if not rules_mk:
17 rules_mk = {}
18
19 file = Path(file)
20 if file.exists():
21 rules_mk_lines = file.read_text(encoding='utf-8').split("\n")
22
23 for line in rules_mk_lines:
24 # Filter out comments
25 if line.strip().startswith("#"):
26 continue
27
28 # Strip in-line comments
29 if '#' in line:
30 line = line[:line.index('#')].strip()
31
32 if '=' in line:
33 # Append
34 if '+=' in line:
35 key, value = line.split('+=', 1)
36 if key.strip() not in rules_mk:
37 rules_mk[key.strip()] = value.strip()
38 else:
39 rules_mk[key.strip()] += ' ' + value.strip()
40 # Set if absent
41 elif "?=" in line:
42 key, value = line.split('?=', 1)
43 if key.strip() not in rules_mk:
44 rules_mk[key.strip()] = value.strip()
45 else:
46 if ":=" in line:
47 line.replace(":", "")
48 key, value = line.split('=', 1)
49 rules_mk[key.strip()] = value.strip()
50
51 return rules_mk