diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2020-03-30 08:28:32 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2020-03-30 09:05:53 +0200 |
commit | cd4e46ee65dab6baa6a143bf3b3f64244be36712 (patch) | |
tree | 689e53265691e782ae35a961445c975219e1155d /core/core_builders.py | |
parent | 0168709978154a89f137b44f33647e5d28a46250 (diff) | |
download | redot-engine-cd4e46ee65dab6baa6a143bf3b3f64244be36712.tar.gz |
SCons: Format buildsystem files with psf/black
Configured for a max line length of 120 characters.
psf/black is very opinionated and purposely doesn't leave much room for
configuration. The output is mostly OK so that should be fine for us,
but some things worth noting:
- Manually wrapped strings will be reflowed, so by using a line length
of 120 for the sake of preserving readability for our long command
calls, it also means that some manually wrapped strings are back on
the same line and should be manually merged again.
- Code generators using string concatenation extensively look awful,
since black puts each operand on a single line. We need to refactor
these generators to use more pythonic string formatting, for which
many options are available (`%`, `format` or f-strings).
- CI checks and a pre-commit hook will be added to ensure that future
buildsystem changes are well-formatted.
Diffstat (limited to 'core/core_builders.py')
-rw-r--r-- | core/core_builders.py | 139 |
1 files changed, 92 insertions, 47 deletions
diff --git a/core/core_builders.py b/core/core_builders.py index a06b61cb9b..d03874608e 100644 --- a/core/core_builders.py +++ b/core/core_builders.py @@ -11,15 +11,15 @@ def escape_string(s): rev_result = [] while c >= 256: c, low = (c // 256, c % 256) - rev_result.append('\\%03o' % low) - rev_result.append('\\%03o' % c) - return ''.join(reversed(rev_result)) + rev_result.append("\\%03o" % low) + rev_result.append("\\%03o" % c) + return "".join(reversed(rev_result)) - result = '' + result = "" if isinstance(s, str): - s = s.encode('utf-8') + s = s.encode("utf-8") for c in s: - if not(32 <= c < 127) or c in (ord('\\'), ord('"')): + if not (32 <= c < 127) or c in (ord("\\"), ord('"')): result += charcode_to_c_escapes(c) else: result += chr(c) @@ -34,6 +34,7 @@ def make_certs_header(target, source, env): buf = f.read() decomp_size = len(buf) import zlib + buf = zlib.compress(buf) g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") @@ -41,9 +42,9 @@ def make_certs_header(target, source, env): g.write("#define CERTS_COMPRESSED_GEN_H\n") # System certs path. Editor will use them if defined. (for package maintainers) - path = env['system_certs_path'] - g.write("#define _SYSTEM_CERTS_PATH \"%s\"\n" % str(path)) - if env['builtin_certs']: + path = env["system_certs_path"] + g.write('#define _SYSTEM_CERTS_PATH "%s"\n' % str(path)) + if env["builtin_certs"]: # Defined here and not in env so changing it does not trigger a full rebuild. g.write("#define BUILTIN_CERTS_ENABLED\n") g.write("static const int _certs_compressed_size = " + str(len(buf)) + ";\n") @@ -59,8 +60,18 @@ def make_certs_header(target, source, env): def make_authors_header(target, source, env): - sections = ["Project Founders", "Lead Developer", "Project Manager", "Developers"] - sections_id = ["AUTHORS_FOUNDERS", "AUTHORS_LEAD_DEVELOPERS", "AUTHORS_PROJECT_MANAGERS", "AUTHORS_DEVELOPERS"] + sections = [ + "Project Founders", + "Lead Developer", + "Project Manager", + "Developers", + ] + sections_id = [ + "AUTHORS_FOUNDERS", + "AUTHORS_LEAD_DEVELOPERS", + "AUTHORS_PROJECT_MANAGERS", + "AUTHORS_DEVELOPERS", + ] src = source[0] dst = target[0] @@ -80,7 +91,7 @@ def make_authors_header(target, source, env): for line in f: if reading: if line.startswith(" "): - g.write("\t\"" + escape_string(line.strip()) + "\",\n") + g.write('\t"' + escape_string(line.strip()) + '",\n') continue if line.startswith("## "): if reading: @@ -103,10 +114,22 @@ def make_authors_header(target, source, env): def make_donors_header(target, source, env): - sections = ["Platinum sponsors", "Gold sponsors", "Mini sponsors", - "Gold donors", "Silver donors", "Bronze donors"] - sections_id = ["DONORS_SPONSOR_PLAT", "DONORS_SPONSOR_GOLD", "DONORS_SPONSOR_MINI", - "DONORS_GOLD", "DONORS_SILVER", "DONORS_BRONZE"] + sections = [ + "Platinum sponsors", + "Gold sponsors", + "Mini sponsors", + "Gold donors", + "Silver donors", + "Bronze donors", + ] + sections_id = [ + "DONORS_SPONSOR_PLAT", + "DONORS_SPONSOR_GOLD", + "DONORS_SPONSOR_MINI", + "DONORS_GOLD", + "DONORS_SILVER", + "DONORS_BRONZE", + ] src = source[0] dst = target[0] @@ -126,7 +149,7 @@ def make_donors_header(target, source, env): for line in f: if reading >= 0: if line.startswith(" "): - g.write("\t\"" + escape_string(line.strip()) + "\",\n") + g.write('\t"' + escape_string(line.strip()) + '",\n') continue if line.startswith("## "): if reading: @@ -169,8 +192,8 @@ def make_license_header(target, source, env): return line def next_tag(self): - if not ':' in self.current: - return ('', []) + if not ":" in self.current: + return ("", []) tag, line = self.current.split(":", 1) lines = [line.strip()] while self.next_line() and self.current.startswith(" "): @@ -178,6 +201,7 @@ def make_license_header(target, source, env): return (tag, lines) from collections import OrderedDict + projects = OrderedDict() license_list = [] @@ -218,26 +242,30 @@ def make_license_header(target, source, env): with open(src_license, "r", encoding="utf-8") as license_file: for line in license_file: escaped_string = escape_string(line.strip()) - f.write("\n\t\t\"" + escaped_string + "\\n\"") + f.write('\n\t\t"' + escaped_string + '\\n"') f.write(";\n\n") - f.write("struct ComponentCopyrightPart {\n" - "\tconst char *license;\n" - "\tconst char *const *files;\n" - "\tconst char *const *copyright_statements;\n" - "\tint file_count;\n" - "\tint copyright_count;\n" - "};\n\n") - - f.write("struct ComponentCopyright {\n" - "\tconst char *name;\n" - "\tconst ComponentCopyrightPart *parts;\n" - "\tint part_count;\n" - "};\n\n") + f.write( + "struct ComponentCopyrightPart {\n" + "\tconst char *license;\n" + "\tconst char *const *files;\n" + "\tconst char *const *copyright_statements;\n" + "\tint file_count;\n" + "\tint copyright_count;\n" + "};\n\n" + ) + + f.write( + "struct ComponentCopyright {\n" + "\tconst char *name;\n" + "\tconst ComponentCopyrightPart *parts;\n" + "\tint part_count;\n" + "};\n\n" + ) f.write("const char *const COPYRIGHT_INFO_DATA[] = {\n") for line in data_list: - f.write("\t\"" + escape_string(line) + "\",\n") + f.write('\t"' + escape_string(line) + '",\n') f.write("};\n\n") f.write("const ComponentCopyrightPart COPYRIGHT_PROJECT_PARTS[] = {\n") @@ -246,11 +274,21 @@ def make_license_header(target, source, env): for project_name, project in iter(projects.items()): part_indexes[project_name] = part_index for part in project: - f.write("\t{ \"" + escape_string(part["License"][0]) + "\", " - + "©RIGHT_INFO_DATA[" + str(part["file_index"]) + "], " - + "©RIGHT_INFO_DATA[" + str(part["copyright_index"]) + "], " - + str(len(part["Files"])) + ", " - + str(len(part["Copyright"])) + " },\n") + f.write( + '\t{ "' + + escape_string(part["License"][0]) + + '", ' + + "©RIGHT_INFO_DATA[" + + str(part["file_index"]) + + "], " + + "©RIGHT_INFO_DATA[" + + str(part["copyright_index"]) + + "], " + + str(len(part["Files"])) + + ", " + + str(len(part["Copyright"])) + + " },\n" + ) part_index += 1 f.write("};\n\n") @@ -258,30 +296,37 @@ def make_license_header(target, source, env): f.write("const ComponentCopyright COPYRIGHT_INFO[] = {\n") for project_name, project in iter(projects.items()): - f.write("\t{ \"" + escape_string(project_name) + "\", " - + "©RIGHT_PROJECT_PARTS[" + str(part_indexes[project_name]) + "], " - + str(len(project)) + " },\n") + f.write( + '\t{ "' + + escape_string(project_name) + + '", ' + + "©RIGHT_PROJECT_PARTS[" + + str(part_indexes[project_name]) + + "], " + + str(len(project)) + + " },\n" + ) f.write("};\n\n") f.write("const int LICENSE_COUNT = " + str(len(license_list)) + ";\n") f.write("const char *const LICENSE_NAMES[] = {\n") for l in license_list: - f.write("\t\"" + escape_string(l[0]) + "\",\n") + f.write('\t"' + escape_string(l[0]) + '",\n') f.write("};\n\n") f.write("const char *const LICENSE_BODIES[] = {\n\n") for l in license_list: for line in l[1:]: if line == ".": - f.write("\t\"\\n\"\n") + f.write('\t"\\n"\n') else: - f.write("\t\"" + escape_string(line) + "\\n\"\n") - f.write("\t\"\",\n\n") + f.write('\t"' + escape_string(line) + '\\n"\n') + f.write('\t"",\n\n') f.write("};\n\n") f.write("#endif // LICENSE_GEN_H\n") -if __name__ == '__main__': +if __name__ == "__main__": subprocess_main(globals()) |