From f2d4643bf52b9aa6f6d6d8d66f8828f696d8e993 Mon Sep 17 00:00:00 2001 From: Peter Eisenmann Date: Fri, 29 Mar 2024 01:31:05 +0100 Subject: [PATCH 1/7] gir: batch-add user typelib search paths --- blueprintcompiler/gir.py | 5 +++-- blueprintcompiler/main.py | 8 +++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index 142bbd2..a33bb26 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -37,8 +37,9 @@ _xml_cache = {} _user_search_paths = [] -def add_typelib_search_path(path: str): - _user_search_paths.append(path) +def add_user_typelib_paths(paths: list): + global _user_search_paths + _user_search_paths += paths def get_namespace(namespace: str, version: str) -> "Namespace": diff --git a/blueprintcompiler/main.py b/blueprintcompiler/main.py index 5b4cad8..aef3ad1 100644 --- a/blueprintcompiler/main.py +++ b/blueprintcompiler/main.py @@ -26,7 +26,7 @@ import typing as T from . import formatter, interactive_port, parser, tokenizer from .errors import CompileError, CompilerBugError, PrintableError, report_bug -from .gir import add_typelib_search_path +from .gir import add_user_typelib_paths from .lsp import LanguageServer from .outputs import XmlOutput from .utils import Colors @@ -135,8 +135,7 @@ class BlueprintApp: def cmd_compile(self, opts): if opts.typelib_path != None: - for typelib_path in opts.typelib_path: - add_typelib_search_path(typelib_path) + add_user_typelib_paths(opts.typelib_path) data = opts.input.read() try: @@ -156,8 +155,7 @@ class BlueprintApp: def cmd_batch_compile(self, opts): if opts.typelib_path != None: - for typelib_path in opts.typelib_path: - add_typelib_search_path(typelib_path) + add_user_typelib_paths(opts.typelib_path) for file in opts.inputs: data = file.read() From c5949de543bbf201c0e935ef81024deca8024e3c Mon Sep 17 00:00:00 2001 From: Peter Eisenmann Date: Fri, 29 Mar 2024 01:52:53 +0100 Subject: [PATCH 2/7] gir: only initialize search paths once --- blueprintcompiler/gir.py | 47 ++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index a33bb26..f4bf4e3 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -35,6 +35,10 @@ _namespace_cache: T.Dict[str, "Namespace"] = {} _xml_cache = {} _user_search_paths = [] +_typelib_search_paths_initalized: bool = False +_typelib_search_paths: list[str] = [] +_gir_search_paths_initalized: bool = False +_gir_search_paths: list[str] = [] def add_user_typelib_paths(paths: list): @@ -42,13 +46,39 @@ def add_user_typelib_paths(paths: list): _user_search_paths += paths +def collect_typelib_search_paths(): + global _typelib_search_paths_initalized + if _typelib_search_paths_initalized: + return + + global _typelib_search_paths + _typelib_search_paths += [ + *_user_search_paths, + *GIRepository.Repository.get_search_path(), + ] + _typelib_search_paths_initalized = True + + +def collect_gir_search_paths(): + global _gir_search_paths_initalized + if _gir_search_paths_initalized: + return + + global _gir_search_paths + if data_paths := os.environ.get("XDG_DATA_DIRS"): + _gir_search_paths += [ + os.path.join(path, "gir-1.0") for path in data_paths.split(os.pathsep) + ] + _gir_search_paths_initalized = True + + def get_namespace(namespace: str, version: str) -> "Namespace": - search_paths = [*GIRepository.Repository.get_search_path(), *_user_search_paths] + collect_typelib_search_paths() filename = f"{namespace}-{version}.typelib" if filename not in _namespace_cache: - for search_path in search_paths: + for search_path in _typelib_search_paths: path = os.path.join(search_path, filename) if os.path.exists(path) and os.path.isfile(path): @@ -61,7 +91,7 @@ def get_namespace(namespace: str, version: str) -> "Namespace": if filename not in _namespace_cache: raise CompileError( f"Namespace {namespace}-{version} could not be found", - hints=["search path: " + os.pathsep.join(search_paths)], + hints=["search path: " + os.pathsep.join(_typelib_search_paths)], ) return _namespace_cache[filename] @@ -94,17 +124,12 @@ def get_available_namespaces() -> T.List[T.Tuple[str, str]]: def get_xml(namespace: str, version: str): - search_paths = [] - - if data_paths := os.environ.get("XDG_DATA_DIRS"): - search_paths += [ - os.path.join(path, "gir-1.0") for path in data_paths.split(os.pathsep) - ] + collect_gir_search_paths() filename = f"{namespace}-{version}.gir" if filename not in _xml_cache: - for search_path in search_paths: + for search_path in _gir_search_paths: path = os.path.join(search_path, filename) if os.path.exists(path) and os.path.isfile(path): @@ -114,7 +139,7 @@ def get_xml(namespace: str, version: str): if filename not in _xml_cache: raise CompileError( f"GObject introspection file '{namespace}-{version}.gir' could not be found", - hints=["search path: " + os.pathsep.join(search_paths)], + hints=["search path: " + os.pathsep.join(_gir_search_paths)], ) return _xml_cache[filename] From ca4ef1db88353e48c5f246c7b79c595fbecc98f0 Mon Sep 17 00:00:00 2001 From: Peter Eisenmann Date: Fri, 29 Mar 2024 02:11:06 +0100 Subject: [PATCH 3/7] gir: use typelib paths for namespaces --- blueprintcompiler/gir.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index f4bf4e3..cb73961 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -104,12 +104,8 @@ def get_available_namespaces() -> T.List[T.Tuple[str, str]]: if len(_available_namespaces): return _available_namespaces - search_paths: list[str] = [ - *GIRepository.Repository.get_search_path(), - *_user_search_paths, - ] - - for search_path in search_paths: + collect_typelib_search_paths() + for search_path in _typelib_search_paths: try: filenames = os.listdir(search_path) except FileNotFoundError: From 9429ac5f413a4b7e8e7ac849267975db42ee260f Mon Sep 17 00:00:00 2001 From: Peter Eisenmann Date: Fri, 29 Mar 2024 02:14:00 +0100 Subject: [PATCH 4/7] gir: use GLib to get gir base paths --- blueprintcompiler/gir.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index cb73961..e57fe50 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -25,7 +25,8 @@ from functools import cached_property import gi # type: ignore gi.require_version("GIRepository", "2.0") -from gi.repository import GIRepository # type: ignore +gi.require_version("GLib", "2.0") +from gi.repository import GIRepository, GLib # type: ignore from . import typelib, xml_reader from .errors import CompileError, CompilerBugError @@ -64,11 +65,13 @@ def collect_gir_search_paths(): if _gir_search_paths_initalized: return + base_paths = [ + GLib.get_user_data_dir(), + *GLib.get_system_data_dirs(), + ] + global _gir_search_paths - if data_paths := os.environ.get("XDG_DATA_DIRS"): - _gir_search_paths += [ - os.path.join(path, "gir-1.0") for path in data_paths.split(os.pathsep) - ] + _gir_search_paths = [os.path.join(path, "gir-1.0") for path in base_paths] _gir_search_paths_initalized = True From 077610b8772f6eb8a8a86a77ad9774875b27b24a Mon Sep 17 00:00:00 2001 From: Peter Eisenmann Date: Fri, 29 Mar 2024 02:14:44 +0100 Subject: [PATCH 5/7] gir: add search fallback paths --- blueprintcompiler/gir.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index e57fe50..d2849a0 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -56,6 +56,11 @@ def collect_typelib_search_paths(): _typelib_search_paths += [ *_user_search_paths, *GIRepository.Repository.get_search_path(), + # fallback paths + "/usr/local/lib/girepository-1.0", + "/usr/local/lib64/girepository-1.0", + "/usr/lib/girepository-1.0", + "/usr/lib64/girepository-1.0", ] _typelib_search_paths_initalized = True @@ -68,6 +73,9 @@ def collect_gir_search_paths(): base_paths = [ GLib.get_user_data_dir(), *GLib.get_system_data_dirs(), + # fallback paths + "/usr/local/share/", + "/usr/share/", ] global _gir_search_paths From 6acf0fe5a0f1573962cd14ff88435db94ce4a109 Mon Sep 17 00:00:00 2001 From: James Westman Date: Mon, 9 Dec 2024 18:59:25 -0600 Subject: [PATCH 6/7] tests: Test deprecations separately Libraries can add new deprecations, or the environment you're running the tests in might have old libraries where the things we test aren't deprecated yet. Move the deprecations test into its own module with its own code, so it can check library versions and skip the test if it won't work. --- blueprintcompiler/language/expression.py | 18 ++++ tests/sample_errors/deprecations.blp | 9 -- tests/sample_errors/deprecations.err | 1 - tests/test_deprecations.py | 110 +++++++++++++++++++++++ 4 files changed, 128 insertions(+), 10 deletions(-) delete mode 100644 tests/sample_errors/deprecations.blp delete mode 100644 tests/sample_errors/deprecations.err create mode 100644 tests/test_deprecations.py diff --git a/blueprintcompiler/language/expression.py b/blueprintcompiler/language/expression.py index 558392c..ae9c399 100644 --- a/blueprintcompiler/language/expression.py +++ b/blueprintcompiler/language/expression.py @@ -171,6 +171,24 @@ class LookupOp(InfixExpr): did_you_mean=(self.property_name, self.lhs.type.properties.keys()), ) + @validate("property") + def property_deprecated(self): + if self.lhs.type is None or not ( + isinstance(self.lhs.type, gir.Class) + or isinstance(self.lhs.type, gir.Interface) + ): + return + + if property := self.lhs.type.properties.get(self.property_name): + if property.deprecated: + hints = [] + if property.deprecated_doc: + hints.append(property.deprecated_doc) + raise DeprecatedWarning( + f"{property.signature} is deprecated", + hints=hints, + ) + class CastExpr(InfixExpr): grammar = [ diff --git a/tests/sample_errors/deprecations.blp b/tests/sample_errors/deprecations.blp deleted file mode 100644 index f67f002..0000000 --- a/tests/sample_errors/deprecations.blp +++ /dev/null @@ -1,9 +0,0 @@ -using Gtk 4.0; - -Dialog { - use-header-bar: 1; -} - -Window { - keys-changed => $on_window_keys_changed(); -} diff --git a/tests/sample_errors/deprecations.err b/tests/sample_errors/deprecations.err deleted file mode 100644 index e3abd61..0000000 --- a/tests/sample_errors/deprecations.err +++ /dev/null @@ -1 +0,0 @@ -3,1,6,Gtk.Dialog is deprecated \ No newline at end of file diff --git a/tests/test_deprecations.py b/tests/test_deprecations.py new file mode 100644 index 0000000..0ffe726 --- /dev/null +++ b/tests/test_deprecations.py @@ -0,0 +1,110 @@ +# test_samples.py +# +# Copyright 2024 James Westman +# +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see . +# +# SPDX-License-Identifier: LGPL-3.0-or-later + + +import unittest + +import gi + +gi.require_version("Gtk", "4.0") +from gi.repository import Gtk + +from blueprintcompiler import parser, tokenizer +from blueprintcompiler.errors import DeprecatedWarning, PrintableError + +# Testing deprecation warnings requires special handling because libraries can add deprecations with new versions, +# causing tests to break if we're not careful. + + +class TestDeprecations(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.gtkVersion = f"{Gtk.get_major_version()}.{Gtk.get_minor_version()}.{Gtk.get_micro_version()}" + + def assertDeprecation(self, blueprint: str, message: str): + try: + tokens = tokenizer.tokenize(blueprint) + _ast, errors, warnings = parser.parse(tokens) + + self.assertIsNone(errors) + self.assertEqual(len(warnings), 1) + self.assertIsInstance(warnings[0], DeprecatedWarning) + self.assertEqual(warnings[0].message, message) + except PrintableError as e: # pragma: no cover + e.pretty_print("", blueprint) + raise AssertionError() + + def test_class_deprecation(self): + if Gtk.check_version(4, 10, 0) is not None: + self.skipTest(f"Gtk.Dialog is not deprecated in GTK {self.gtkVersion}") + + blueprint = """ + using Gtk 4.0; + + Dialog { + use-header-bar: 1; + } + """ + message = "Gtk.Dialog is deprecated" + + self.assertDeprecation(blueprint, message) + + def test_property_deprecation(self): + self.skipTest( + "gobject-introspection does not currently write property deprecations to the typelib. See ." + ) + + if Gtk.check_version(4, 4, 0) is not None: + self.skipTest( + f"Gtk.DropTarget:drop is not deprecated in GTK {self.gtkVersion}" + ) + + blueprint = """ + using Gtk 4.0; + + $MyObject { + a: bind drop_target.drop; + } + + DropTarget drop_target { + } + """ + + message = "Gtk.DropTarget:drop is deprecated" + + self.assertDeprecation(blueprint, message) + + def test_signal_deprecation(self): + if Gtk.check_version(4, 10, 0) is not None: + self.skipTest( + f"Gtk.Window::keys-changed is not deprecated in GTK {self.gtkVersion}" + ) + + blueprint = """ + using Gtk 4.0; + + Window { + keys-changed => $handler(); + } + """ + + message = "signal Gtk.Window::keys-changed () is deprecated" + + self.assertDeprecation(blueprint, message) From 778a979714f8eecbbe47953be8b23b6f2fe5a752 Mon Sep 17 00:00:00 2001 From: Luoyayu Date: Sat, 7 Dec 2024 07:36:14 +0000 Subject: [PATCH 7/7] lsp: Fix format of JSON-RPC content part ending with \r\n --- blueprintcompiler/lsp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blueprintcompiler/lsp.py b/blueprintcompiler/lsp.py index 25b289f..0659154 100644 --- a/blueprintcompiler/lsp.py +++ b/blueprintcompiler/lsp.py @@ -149,7 +149,7 @@ class LanguageServer: def _send(self, data): data["jsonrpc"] = "2.0" - line = json.dumps(data, separators=(",", ":")) + "\r\n" + line = json.dumps(data, separators=(",", ":")) printerr("output: " + line) sys.stdout.write( f"Content-Length: {len(line.encode())}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}"