diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2023-04-26 10:04:00 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2023-06-26 21:18:05 +0200 |
commit | 0d48dc778aae6f904dc4dfb8ca2c721d79f6fbe0 (patch) | |
tree | 47ef3b801706c7bef652506ed06dce7881216bf9 | |
parent | b655ccb9aa419e26cdab072ebe7aec0d1f22ed6d (diff) |
build: Use CSafeLoader if available
The CSafeLoader uses the C libyaml libary to considerably speed up the
loading of YAML files.
-rwxr-xr-x | wscript | 39 |
1 files changed, 23 insertions, 16 deletions
@@ -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: |