From 56063b96fdb3946b6bc6742e2b9890c44ce63eb1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 14 Oct 2023 07:44:18 +0530 Subject: [PATCH] Move gen scripts into their own package --- gen/README.rst | 4 ++ gen/__init__.py | 0 gen/__main__.py | 39 ++++++++++++++++++++ gen-apc-parsers.py => gen/apc_parsers.py | 10 ++++- gen-config.py => gen/config.py | 8 +++- gen-go-code.py => gen/go_code.py | 7 +++- gen-key-constants.py => gen/key_constants.py | 8 +++- gen-srgb-lut.py => gen/srgb_lut.py | 7 +++- gen-wcwidth.py => gen/wcwidth.py | 27 ++++++++------ kitty/parse-graphics-command.h | 2 +- pyproject.toml | 2 +- setup.py | 2 +- 12 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 gen/README.rst create mode 100644 gen/__init__.py create mode 100644 gen/__main__.py rename gen-apc-parsers.py => gen/apc_parsers.py (97%) rename gen-config.py => gen/config.py (91%) rename gen-go-code.py => gen/go_code.py (99%) rename gen-key-constants.py => gen/key_constants.py (98%) rename gen-srgb-lut.py => gen/srgb_lut.py (85%) rename gen-wcwidth.py => gen/wcwidth.py (98%) diff --git a/gen/README.rst b/gen/README.rst new file mode 100644 index 000000000..6fb0d89a0 --- /dev/null +++ b/gen/README.rst @@ -0,0 +1,4 @@ +Scripts to generate code for various things like keys, mouse cursors, unicode +data etc. Some of these generate code that is checked into version control. +Some generate ephemeral code used during builds. Ephemeral code is in files +with a _generated.[h|go|c] extension. diff --git a/gen/__init__.py b/gen/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/gen/__main__.py b/gen/__main__.py new file mode 100644 index 000000000..276d4db3f --- /dev/null +++ b/gen/__main__.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# License: GPLv3 Copyright: 2023, Kovid Goyal + + +import os +import sys + + +def main(args: list[str]=sys.argv) -> None: + os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + sys.path.insert(0, os.getcwd()) + if len(args) == 1: + raise SystemExit('usage: python gen which') + which = args[1] + del args[1] + if which == 'apc-parsers': + from gen.apc_parsers import main + main(args) + elif which == 'config': + from gen.config import main + main(args) + elif which == 'srgb-lut': + from gen.srgb_lut import main + main(args) + elif which == 'key-constants': + from gen.key_constants import main + main(args) + elif which == 'go-code': + from gen.go_code import main + main(args) + elif which == 'wcwidth': + from gen.wcwidth import main + main(args) + else: + raise SystemExit(f'Unknown which: {which}') + + +if __name__ == '__main__': + main() diff --git a/gen-apc-parsers.py b/gen/apc_parsers.py similarity index 97% rename from gen-apc-parsers.py rename to gen/apc_parsers.py index 835e1230b..757bb2a67 100755 --- a/gen-apc-parsers.py +++ b/gen/apc_parsers.py @@ -3,6 +3,7 @@ import os import subprocess +import sys from collections import defaultdict from typing import Any, DefaultDict, Dict, FrozenSet, List, Tuple, Union @@ -281,4 +282,11 @@ def graphics_parser() -> None: write_header(text, 'kitty/parse-graphics-command.h') -graphics_parser() +def main(args: list[str]=sys.argv) -> None: + graphics_parser() + + +if __name__ == '__main__': + import runpy + m = runpy.run_path(os.path.dirname(os.path.abspath(__file__))) + m['main']([sys.executable, 'apc-parsers']) diff --git a/gen-config.py b/gen/config.py similarity index 91% rename from gen-config.py rename to gen/config.py index 97935410b..8b33eb7b8 100755 --- a/gen-config.py +++ b/gen/config.py @@ -2,8 +2,10 @@ # License: GPLv3 Copyright: 2021, Kovid Goyal +import os import re import subprocess +import sys from typing import List from kitty.conf.generate import write_output @@ -33,7 +35,7 @@ def patch_color_list(path: str, colors: List[str], name: str, spc: str = ' ') subprocess.check_call(['gofmt', '-w', path]) -def main() -> None: +def main(args: list[str]=sys.argv) -> None: from kitty.options.definition import definition write_output('kitty', definition) nullable_colors = [] @@ -51,4 +53,6 @@ def main() -> None: if __name__ == '__main__': - main() + import runpy + m = runpy.run_path(os.path.dirname(os.path.abspath(__file__))) + m['main']([sys.executable, 'config']) diff --git a/gen-go-code.py b/gen/go_code.py similarity index 99% rename from gen-go-code.py rename to gen/go_code.py index a8c1564b7..4e77a7eec 100755 --- a/gen-go-code.py +++ b/gen/go_code.py @@ -834,7 +834,7 @@ def generate_ssh_kitten_data() -> None: write_compressed_data(buf.getvalue(), d) -def main() -> None: +def main(args: list[str]=sys.argv) -> None: with replace_if_needed('constants_generated.go') as f: f.write(generate_constants()) with replace_if_needed('tools/utils/style/color-names_generated.go') as f: @@ -860,4 +860,7 @@ def main() -> None: if __name__ == '__main__': - main() # }}} + import runpy + m = runpy.run_path(os.path.dirname(os.path.abspath(__file__))) + m['main']([sys.executable, 'go-code']) +# }}} diff --git a/gen-key-constants.py b/gen/key_constants.py similarity index 98% rename from gen-key-constants.py rename to gen/key_constants.py index 2c8a5a739..b22d43b95 100755 --- a/gen-key-constants.py +++ b/gen/key_constants.py @@ -1,7 +1,9 @@ #!/usr/bin/env python # License: GPLv3 Copyright: 2021, Kovid Goyal +import os import string +import sys from pprint import pformat from typing import Any, Dict, List, Union @@ -417,7 +419,7 @@ def generate_macos_mapping() -> None: patch_file('glfw/cocoa_window.m', 'functional to macu', '\n'.join(lines)) -def main() -> None: +def main(args: list[str]=sys.argv) -> None: generate_glfw_header() generate_xkb_mapping() generate_functional_table() @@ -427,4 +429,6 @@ def main() -> None: if __name__ == '__main__': - main() + import runpy + m = runpy.run_path(os.path.dirname(os.path.abspath(__file__))) + m['main']([sys.executable, 'key-constants']) diff --git a/gen-srgb-lut.py b/gen/srgb_lut.py similarity index 85% rename from gen-srgb-lut.py rename to gen/srgb_lut.py index 4ba33cef8..53840dc61 100755 --- a/gen-srgb-lut.py +++ b/gen/srgb_lut.py @@ -2,6 +2,7 @@ # vim:fileencoding=utf-8 import os +import sys from functools import lru_cache from typing import List @@ -41,11 +42,13 @@ def generate_srgb_gamma(declaration: str = 'static const GLfloat srgb_lut[256] = return "\n".join(lines) -def main() -> None: +def main(args: list[str]=sys.argv) -> None: c = generate_srgb_gamma() with open(os.path.join('kitty', 'srgb_gamma.h'), 'w') as f: f.write(f'{c}\n') if __name__ == '__main__': - main() + import runpy + m = runpy.run_path(os.path.dirname(os.path.abspath(__file__))) + m['main']([sys.executable, 'srgb-lut']) diff --git a/gen-wcwidth.py b/gen/wcwidth.py similarity index 98% rename from gen-wcwidth.py rename to gen/wcwidth.py index 7f5bb66b2..4e33b6390 100755 --- a/gen-wcwidth.py +++ b/gen/wcwidth.py @@ -26,8 +26,6 @@ from typing import ( ) from urllib.request import urlopen -os.chdir(os.path.dirname(os.path.abspath(__file__))) - non_characters = frozenset(range(0xfffe, 0x10ffff, 0x10000)) non_characters |= frozenset(range(0xffff, 0x10ffff + 1, 0x10000)) non_characters |= frozenset(range(0xfdd0, 0xfdf0)) @@ -584,12 +582,19 @@ def gen_rowcolumn_diacritics() -> None: subprocess.check_call(['gofmt', '-w', '-s', go_file]) -parse_ucd() -parse_prop_list() -parse_emoji() -parse_eaw() -gen_ucd() -gen_wcwidth() -gen_emoji() -gen_names() -gen_rowcolumn_diacritics() +def main(args: list[str]=sys.argv) -> None: + parse_ucd() + parse_prop_list() + parse_emoji() + parse_eaw() + gen_ucd() + gen_wcwidth() + gen_emoji() + gen_names() + gen_rowcolumn_diacritics() + + +if __name__ == '__main__': + import runpy + m = runpy.run_path(os.path.dirname(os.path.abspath(__file__))) + m['main']([sys.executable, 'wcwidth']) diff --git a/kitty/parse-graphics-command.h b/kitty/parse-graphics-command.h index 12acdbc26..e3735797f 100644 --- a/kitty/parse-graphics-command.h +++ b/kitty/parse-graphics-command.h @@ -1,4 +1,4 @@ -// This file is generated by gen-apc-parsers.py do not edit! +// This file is generated by apc_parsers.py do not edit! #pragma once diff --git a/pyproject.toml b/pyproject.toml index 90f20d628..90450f5b4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.mypy] -files = 'kitty,kittens,glfw,*.py,docs/conf.py' +files = 'kitty,kittens,glfw,*.py,docs/conf.py,gen' no_implicit_optional = true sqlite_cache = true cache_fine_grained = true diff --git a/setup.py b/setup.py index ed40f9028..b85b59a2b 100755 --- a/setup.py +++ b/setup.py @@ -939,7 +939,7 @@ def update_go_generated_files(args: Options, kitty_exe: str) -> None: env = os.environ.copy() env['ASAN_OPTIONS'] = 'detect_leaks=0' - cp = subprocess.run([kitty_exe, '+launch', os.path.join(src_base, 'gen-go-code.py')], stdout=subprocess.PIPE, env=env) + cp = subprocess.run([kitty_exe, '+launch', os.path.join(src_base, 'gen/go_code.py')], stdout=subprocess.PIPE, env=env) if cp.returncode != 0: raise SystemExit(cp.returncode)