From 0e23ebd9491bd239e8f1ca417b14679e02787a77 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 26 Apr 2023 10:04:00 +0200 Subject: build: Use CSafeLoader if available The CSafeLoader uses the C libyaml libary to considerably speed up the loading of YAML files. --- wscript | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/wscript b/wscript index e61b127e43..a68432345a 100755 --- a/wscript +++ b/wscript @@ -1184,7 +1184,7 @@ def must_update_item_cache(ctx, path, cache_file): return is_one_item_newer(ctx, path, mtime) -def load_from_yaml(load, ctx, data_by_uid, base, path): +def load_from_yaml(ctx, data_by_uid, base, path): try: names = os.listdir(path) except Exception as e: @@ -1194,11 +1194,11 @@ def load_from_yaml(load, ctx, data_by_uid, base, path): if name.endswith(".yml") and not name.startswith("."): uid = "/" + os.path.relpath(path2, base).replace(".yml", "") with open(path2, "r") as f: - data_by_uid[uid] = load(f.read()) + data_by_uid[uid] = load(f.read(), SafeLoader) else: mode = os.lstat(path2).st_mode if stat.S_ISDIR(mode): - load_from_yaml(load, ctx, data_by_uid, base, path2) + load_from_yaml(ctx, data_by_uid, base, path2) def load_items_in_directory(ctx, ctors, path): @@ -1217,19 +1217,7 @@ def load_items_in_directory(ctx, ctors, path): "Regenerate build specification cache (needs a couple of seconds)..." ) - # - # Do not use a system provided yaml module and instead import it from - # the project. This reduces the host system requirements to a simple - # Python 2.7 or 3 installation without extra modules. - # - if sys.version_info[0] == 2: - yaml_path = "yaml/lib" - else: - yaml_path = "yaml/lib3" - sys.path += [yaml_path] - from yaml import safe_load - - load_from_yaml(safe_load, ctx, data_by_uid, path, path) + load_from_yaml(ctx, data_by_uid, path, path) with open(cache_file, "wb") as f: pickle.dump(data_by_uid, f) else: @@ -1262,6 +1250,25 @@ def load_items(ctx, specs): load_items_in_directory(ctx, ctors, path) +try: + # + # Try to use the system-provided yaml module with libyaml support. + # + from yaml import load, CSafeLoader as SafeLoader +except ImportError: + # + # Fall back to the Python implementation provided by the project. This + # reduces the host system requirements to a simple Python 2.7 or 3 + # installation without extra modules. + # + if sys.version_info[0] == 2: + yaml_path = "yaml/lib" + else: + yaml_path = "yaml/lib3" + sys.path += [yaml_path] + from yaml import load, SafeLoader + + def load_items_from_options(ctx): specs = ctx.options.rtems_specs if specs is not None: -- cgit v1.2.3