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 /editor | |
parent | 0168709978154a89f137b44f33647e5d28a46250 (diff) |
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 'editor')
-rw-r--r-- | editor/SCsub | 46 | ||||
-rw-r--r-- | editor/debugger/SCsub | 2 | ||||
-rw-r--r-- | editor/editor_builders.py | 9 | ||||
-rw-r--r-- | editor/fileserver/SCsub | 2 | ||||
-rw-r--r-- | editor/icons/SCsub | 14 | ||||
-rw-r--r-- | editor/icons/editor_icons_builders.py | 14 | ||||
-rw-r--r-- | editor/import/SCsub | 2 | ||||
-rw-r--r-- | editor/plugins/SCsub | 2 | ||||
-rwxr-xr-x | editor/translations/extract.py | 40 |
9 files changed, 70 insertions, 61 deletions
diff --git a/editor/SCsub b/editor/SCsub index 61562d70d3..72a168efe2 100644 --- a/editor/SCsub +++ b/editor/SCsub @@ -1,6 +1,6 @@ #!/usr/bin/env python -Import('env') +Import("env") env.editor_sources = [] @@ -16,24 +16,24 @@ def _make_doc_data_class_path(to_path): g.write("static const int _doc_data_class_path_count = " + str(len(env.doc_class_path)) + ";\n") g.write("struct _DocDataClassPath { const char* name; const char* path; };\n") - g.write("static const _DocDataClassPath _doc_data_class_paths[" + str(len(env.doc_class_path) + 1) + "] = {\n"); + g.write("static const _DocDataClassPath _doc_data_class_paths[" + str(len(env.doc_class_path) + 1) + "] = {\n") for c in sorted(env.doc_class_path): - g.write("\t{\"" + c + "\", \"" + env.doc_class_path[c] + "\"},\n") + g.write('\t{"' + c + '", "' + env.doc_class_path[c] + '"},\n') g.write("\t{NULL, NULL}\n") g.write("};\n") g.close() -if env['tools']: +if env["tools"]: # Register exporters reg_exporters_inc = '#include "register_exporters.h"\n' - reg_exporters = 'void register_exporters() {\n' + reg_exporters = "void register_exporters() {\n" for e in env.platform_exporters: env.add_source_files(env.editor_sources, "#platform/" + e + "/export/export.cpp") - reg_exporters += '\tregister_' + e + '_exporter();\n' + reg_exporters += "\tregister_" + e + "_exporter();\n" reg_exporters_inc += '#include "platform/' + e + '/export/export.h"\n' - reg_exporters += '}\n' + reg_exporters += "}\n" # NOTE: It is safe to generate this file here, since this is still executed serially with open("register_exporters.gen.cpp", "w", encoding="utf-8") as f: @@ -50,12 +50,12 @@ if env['tools']: for d in doc_dirs: try: - for f in os.listdir(os.path.join(env.Dir('#').abspath, d)): + for f in os.listdir(os.path.join(env.Dir("#").abspath, d)): docs.append("#" + os.path.join(d, f)) except OSError: pass - _make_doc_data_class_path(os.path.join(env.Dir('#').abspath, "editor")) + _make_doc_data_class_path(os.path.join(env.Dir("#").abspath, "editor")) docs = sorted(docs) env.Depends("#editor/doc_data_compressed.gen.h", docs) @@ -63,32 +63,36 @@ if env['tools']: import glob - path = env.Dir('.').abspath + path = env.Dir(".").abspath # Editor translations tlist = glob.glob(path + "/translations/*.po") - env.Depends('#editor/editor_translations.gen.h', tlist) - env.CommandNoCache('#editor/editor_translations.gen.h', tlist, run_in_subprocess(editor_builders.make_editor_translations_header)) + env.Depends("#editor/editor_translations.gen.h", tlist) + env.CommandNoCache( + "#editor/editor_translations.gen.h", tlist, run_in_subprocess(editor_builders.make_editor_translations_header) + ) # Documentation translations tlist = glob.glob(env.Dir("#doc").abspath + "/translations/*.po") - env.Depends('#editor/doc_translations.gen.h', tlist) - env.CommandNoCache('#editor/doc_translations.gen.h', tlist, run_in_subprocess(editor_builders.make_doc_translations_header)) + env.Depends("#editor/doc_translations.gen.h", tlist) + env.CommandNoCache( + "#editor/doc_translations.gen.h", tlist, run_in_subprocess(editor_builders.make_doc_translations_header) + ) # Fonts flist = glob.glob(path + "/../thirdparty/fonts/*.ttf") flist.extend(glob.glob(path + "/../thirdparty/fonts/*.otf")) flist.sort() - env.Depends('#editor/builtin_fonts.gen.h', flist) - env.CommandNoCache('#editor/builtin_fonts.gen.h', flist, run_in_subprocess(editor_builders.make_fonts_header)) + env.Depends("#editor/builtin_fonts.gen.h", flist) + env.CommandNoCache("#editor/builtin_fonts.gen.h", flist, run_in_subprocess(editor_builders.make_fonts_header)) env.add_source_files(env.editor_sources, "*.cpp") - SConscript('debugger/SCsub') - SConscript('fileserver/SCsub') - SConscript('icons/SCsub') - SConscript('import/SCsub') - SConscript('plugins/SCsub') + SConscript("debugger/SCsub") + SConscript("fileserver/SCsub") + SConscript("icons/SCsub") + SConscript("import/SCsub") + SConscript("plugins/SCsub") lib = env.add_library("editor", env.editor_sources) env.Prepend(LIBS=[lib]) diff --git a/editor/debugger/SCsub b/editor/debugger/SCsub index 2b1e889fb0..359d04e5df 100644 --- a/editor/debugger/SCsub +++ b/editor/debugger/SCsub @@ -1,5 +1,5 @@ #!/usr/bin/env python -Import('env') +Import("env") env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/editor_builders.py b/editor/editor_builders.py index 44c3e50dfc..0c9cf91b2f 100644 --- a/editor/editor_builders.py +++ b/editor/editor_builders.py @@ -25,6 +25,7 @@ def make_doc_header(target, source, env): buf = (docbegin + buf + docend).encode("utf-8") decomp_size = len(buf) import zlib + buf = zlib.compress(buf) g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") @@ -55,7 +56,7 @@ def make_fonts_header(target, source, env): # saving uncompressed, since freetype will reference from memory pointer xl_names = [] for i in range(len(source)): - with open(source[i], "rb")as f: + with open(source[i], "rb") as f: buf = f.read() name = os.path.splitext(os.path.basename(source[i]))[0] @@ -111,7 +112,9 @@ def make_translations_header(target, source, env, category): g.write("};\n\n") g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category)) for x in xl_names: - g.write("\t{{ \"{}\", {}, {}, _{}_translation_{}_compressed }},\n".format(x[0], str(x[1]), str(x[2]), category, x[0])) + g.write( + '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format(x[0], str(x[1]), str(x[2]), category, x[0]) + ) g.write("\t{NULL, 0, 0, NULL}\n") g.write("};\n") @@ -128,5 +131,5 @@ def make_doc_translations_header(target, source, env): make_translations_header(target, source, env, "doc") -if __name__ == '__main__': +if __name__ == "__main__": subprocess_main(globals()) diff --git a/editor/fileserver/SCsub b/editor/fileserver/SCsub index 2b1e889fb0..359d04e5df 100644 --- a/editor/fileserver/SCsub +++ b/editor/fileserver/SCsub @@ -1,5 +1,5 @@ #!/usr/bin/env python -Import('env') +Import("env") env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/icons/SCsub b/editor/icons/SCsub index b39c74c66a..f0d51999f0 100644 --- a/editor/icons/SCsub +++ b/editor/icons/SCsub @@ -1,21 +1,21 @@ #!/usr/bin/env python -Import('env') +Import("env") from platform_methods import run_in_subprocess import editor_icons_builders -make_editor_icons_builder = Builder(action=run_in_subprocess(editor_icons_builders.make_editor_icons_action), - suffix='.h', - src_suffix='.svg') +make_editor_icons_builder = Builder( + action=run_in_subprocess(editor_icons_builders.make_editor_icons_action), suffix=".h", src_suffix=".svg" +) -env['BUILDERS']['MakeEditorIconsBuilder'] = make_editor_icons_builder +env["BUILDERS"]["MakeEditorIconsBuilder"] = make_editor_icons_builder # Editor's own icons icon_sources = Glob("*.svg") # Module icons for module_icons in env.module_icons_paths: - icon_sources += Glob('#' + module_icons + "/*.svg") + icon_sources += Glob("#" + module_icons + "/*.svg") -env.Alias('editor_icons', [env.MakeEditorIconsBuilder('#editor/editor_icons.gen.h', icon_sources)]) +env.Alias("editor_icons", [env.MakeEditorIconsBuilder("#editor/editor_icons.gen.h", icon_sources)]) diff --git a/editor/icons/editor_icons_builders.py b/editor/icons/editor_icons_builders.py index a00f21c265..d7145abe50 100644 --- a/editor/icons/editor_icons_builders.py +++ b/editor/icons/editor_icons_builders.py @@ -22,16 +22,16 @@ def make_editor_icons_action(target, source, env): icons_string.write('\t"') - with open(fname, 'rb') as svgf: + with open(fname, "rb") as svgf: b = svgf.read(1) - while(len(b) == 1): + while len(b) == 1: icons_string.write("\\" + str(hex(ord(b)))[1:]) b = svgf.read(1) icons_string.write('"') if fname != svg_icons[-1]: icons_string.write(",") - icons_string.write('\n') + icons_string.write("\n") s = StringIO() s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n") @@ -40,7 +40,7 @@ def make_editor_icons_action(target, source, env): s.write("static const int editor_icons_count = {};\n".format(len(svg_icons))) s.write("static const char *editor_icons_sources[] = {\n") s.write(icons_string.getvalue()) - s.write('};\n\n') + s.write("};\n\n") s.write("static const char *editor_icons_names[] = {\n") # this is used to store the indices of thumbnail icons @@ -63,11 +63,11 @@ def make_editor_icons_action(target, source, env): if fname != svg_icons[-1]: s.write(",") - s.write('\n') + s.write("\n") index += 1 - s.write('};\n') + s.write("};\n") if thumb_medium_indices: s.write("\n\n") @@ -91,5 +91,5 @@ def make_editor_icons_action(target, source, env): icons_string.close() -if __name__ == '__main__': +if __name__ == "__main__": subprocess_main(globals()) diff --git a/editor/import/SCsub b/editor/import/SCsub index 2b1e889fb0..359d04e5df 100644 --- a/editor/import/SCsub +++ b/editor/import/SCsub @@ -1,5 +1,5 @@ #!/usr/bin/env python -Import('env') +Import("env") env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/plugins/SCsub b/editor/plugins/SCsub index 2b1e889fb0..359d04e5df 100644 --- a/editor/plugins/SCsub +++ b/editor/plugins/SCsub @@ -1,5 +1,5 @@ #!/usr/bin/env python -Import('env') +Import("env") env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/translations/extract.py b/editor/translations/extract.py index 10859c7c90..749bad5fff 100755 --- a/editor/translations/extract.py +++ b/editor/translations/extract.py @@ -10,23 +10,23 @@ import sys line_nb = False for arg in sys.argv[1:]: - if (arg == "--with-line-nb"): + if arg == "--with-line-nb": print("Enabling line numbers in the context locations.") line_nb = True else: os.sys.exit("Non supported argument '" + arg + "'. Aborting.") -if (not os.path.exists("editor")): +if not os.path.exists("editor"): os.sys.exit("ERROR: This script should be started from the root of the git repo.") matches = [] -for root, dirnames, filenames in os.walk('.'): +for root, dirnames, filenames in os.walk("."): dirnames[:] = [d for d in dirnames if d not in ["thirdparty"]] - for filename in fnmatch.filter(filenames, '*.cpp'): + for filename in fnmatch.filter(filenames, "*.cpp"): matches.append(os.path.join(root, filename)) - for filename in fnmatch.filter(filenames, '*.h'): + for filename in fnmatch.filter(filenames, "*.h"): matches.append(os.path.join(root, filename)) matches.sort() @@ -51,52 +51,54 @@ msgstr "" "Content-Transfer-Encoding: 8-bit\\n" """ + def process_file(f, fname): global main_po, unique_str, unique_loc l = f.readline() lc = 1 - while (l): + while l: - patterns = ['RTR(\"', 'TTR(\"', 'TTRC(\"'] + patterns = ['RTR("', 'TTR("', 'TTRC("'] idx = 0 pos = 0 - while (pos >= 0): + while pos >= 0: pos = l.find(patterns[idx], pos) - if (pos == -1): - if (idx < len(patterns) - 1): + if pos == -1: + if idx < len(patterns) - 1: idx += 1 pos = 0 continue pos += len(patterns[idx]) msg = "" - while (pos < len(l) and (l[pos] != '"' or l[pos - 1] == '\\')): + while pos < len(l) and (l[pos] != '"' or l[pos - 1] == "\\"): msg += l[pos] pos += 1 - location = os.path.relpath(fname).replace('\\', '/') - if (line_nb): + location = os.path.relpath(fname).replace("\\", "/") + if line_nb: location += ":" + str(lc) - if (not msg in unique_str): + if not msg in unique_str: main_po += "\n#: " + location + "\n" main_po += 'msgid "' + msg + '"\n' main_po += 'msgstr ""\n' unique_str.append(msg) unique_loc[msg] = [location] - elif (not location in unique_loc[msg]): + elif not location in unique_loc[msg]: # Add additional location to previous occurrence too msg_pos = main_po.find('\nmsgid "' + msg + '"') - if (msg_pos == -1): + if msg_pos == -1: print("Someone apparently thought writing Python was as easy as GDScript. Ping Akien.") - main_po = main_po[:msg_pos] + ' ' + location + main_po[msg_pos:] + main_po = main_po[:msg_pos] + " " + location + main_po[msg_pos:] unique_loc[msg].append(location) l = f.readline() lc += 1 + print("Updating the editor.pot template...") for fname in matches: @@ -106,7 +108,7 @@ for fname in matches: with open("editor.pot", "w") as f: f.write(main_po) -if (os.name == "posix"): +if os.name == "posix": print("Wrapping template at 79 characters for compatibility with Weblate.") os.system("msgmerge -w79 editor.pot editor.pot > editor.pot.wrap") shutil.move("editor.pot.wrap", "editor.pot") @@ -114,7 +116,7 @@ if (os.name == "posix"): shutil.move("editor.pot", "editor/translations/editor.pot") # TODO: Make that in a portable way, if we care; if not, kudos to Unix users -if (os.name == "posix"): +if os.name == "posix": added = subprocess.check_output(r"git diff editor/translations/editor.pot | grep \+msgid | wc -l", shell=True) removed = subprocess.check_output(r"git diff editor/translations/editor.pot | grep \\\-msgid | wc -l", shell=True) print("\n# Template changes compared to the staged status:") |