diff options
Diffstat (limited to 'lib/python/qmk/makefile.py')
-rw-r--r-- | lib/python/qmk/makefile.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/python/qmk/makefile.py b/lib/python/qmk/makefile.py new file mode 100644 index 000000000..ce64431f9 --- /dev/null +++ b/lib/python/qmk/makefile.py | |||
@@ -0,0 +1,77 @@ | |||
1 | """ Functions for working with Makefiles | ||
2 | """ | ||
3 | import os | ||
4 | import glob | ||
5 | import re | ||
6 | |||
7 | import qmk.path | ||
8 | from qmk.errors import NoSuchKeyboardError | ||
9 | |||
10 | def parse_rules_mk(file_path): | ||
11 | """ Parse a rules.mk file | ||
12 | |||
13 | Args: | ||
14 | file_path: path to the rules.mk file | ||
15 | |||
16 | Returns: | ||
17 | a dictionary with the file's content | ||
18 | """ | ||
19 | # regex to match lines with comment at the end | ||
20 | # group(1) = option's name | ||
21 | # group(2) = operator (eg.: '=', '+=') | ||
22 | # group(3) = value(s) | ||
23 | commented_regex = re.compile(r"^\s*(\w+)\s*([\:\+\-]?=)\s*(.*?)(?=\s*\#)") | ||
24 | # regex to match lines without comment at the end | ||
25 | # group(1) = option's name | ||
26 | # group(2) = operator (eg.: '=', '+=') | ||
27 | # group(3) = value(s) | ||
28 | uncommented_regex = re.compile(r"^\s*(\w+)\s*([\:\+\-]?=)\s*(.*?)(?=\s*$)") | ||
29 | mk_content = qmk.path.unicode_lines(file_path) | ||
30 | parsed_file = dict() | ||
31 | for line in mk_content: | ||
32 | found = commented_regex.search(line) if "#" in line else uncommented_regex.search(line) | ||
33 | if found: | ||
34 | parsed_file[found.group(1)] = dict(operator = found.group(2), value = found.group(3)) | ||
35 | return parsed_file | ||
36 | |||
37 | def merge_rules_mk_files(base, revision): | ||
38 | """ Merge a keyboard revision's rules.mk file with | ||
39 | the 'base' rules.mk file | ||
40 | |||
41 | Args: | ||
42 | base: the base rules.mk file's content as dictionary | ||
43 | revision: the revision's rules.mk file's content as dictionary | ||
44 | |||
45 | Returns: | ||
46 | a dictionary with the merged content | ||
47 | """ | ||
48 | return {**base, **revision} | ||
49 | |||
50 | def get_rules_mk(keyboard, revision = ""): | ||
51 | """ Get a rules.mk for a keyboard | ||
52 | |||
53 | Args: | ||
54 | keyboard: name of the keyboard | ||
55 | revision: revision of the keyboard | ||
56 | |||
57 | Returns: | ||
58 | a dictionary with the content of the rules.mk file | ||
59 | """ | ||
60 | base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep | ||
61 | rules_mk = dict() | ||
62 | if os.path.exists(base_path + os.path.sep + revision): | ||
63 | rules_mk_path_wildcard = os.path.join(base_path, "**", "rules.mk") | ||
64 | rules_mk_regex = re.compile(r"^" + base_path + "(?:" + revision + os.path.sep + ")?rules.mk") | ||
65 | paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive = True) if rules_mk_regex.search(path)] | ||
66 | for file_path in paths: | ||
67 | rules_mk[revision if revision in file_path else "base"] = parse_rules_mk(file_path) | ||
68 | else: | ||
69 | raise NoSuchKeyboardError("The requested keyboard and/or revision does not exist.") | ||
70 | |||
71 | # if the base or the revision directory does not contain a rules.mk | ||
72 | if len(rules_mk) == 1: | ||
73 | rules_mk = rules_mk[revision] | ||
74 | # if both directories contain rules.mk files | ||
75 | elif len(rules_mk) == 2: | ||
76 | rules_mk = merge_rules_mk_files(rules_mk["base"], rules_mk[revision]) | ||
77 | return rules_mk | ||