scripts: Loader change

CSafeLoader used instead of yaml.safe_load and SafeLoader.
C implementation is faster.

Signed-off-by: Lukasz Mrugala <lukaszx.mrugala@intel.com>
This commit is contained in:
Lukasz Mrugala 2024-04-19 10:37:20 +00:00 committed by Alberto Escolar
commit b2f43210de
7 changed files with 38 additions and 14 deletions

View file

@ -15,9 +15,14 @@ import yaml
import list_hardware
from list_hardware import unique_paths
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
BOARD_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'board-schema.yml')
with open(BOARD_SCHEMA_PATH, 'r') as f:
board_schema = yaml.safe_load(f.read())
board_schema = yaml.load(f.read(), Loader=SafeLoader)
BOARD_YML = 'board.yml'
@ -178,7 +183,7 @@ def load_v2_boards(board_name, board_yml, systems):
boards = []
if board_yml.is_file():
with board_yml.open('r') as f:
b = yaml.safe_load(f.read())
b = yaml.load(f.read(), Loader=SafeLoader)
try:
pykwalify.core.Core(source_data=b, schema_data=board_schema).validate()

View file

@ -12,14 +12,19 @@ from typing import List
import yaml
import re
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
SOC_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'soc-schema.yml')
with open(SOC_SCHEMA_PATH, 'r') as f:
soc_schema = yaml.safe_load(f.read())
soc_schema = yaml.load(f.read(), Loader=SafeLoader)
ARCH_SCHEMA_PATH = str(Path(__file__).parent / 'schemas' / 'arch-schema.yml')
with open(ARCH_SCHEMA_PATH, 'r') as f:
arch_schema = yaml.safe_load(f.read())
arch_schema = yaml.load(f.read(), Loader=SafeLoader)
SOC_YML = 'soc.yml'
ARCHS_YML_PATH = PurePath('arch/archs.yml')
@ -35,7 +40,7 @@ class Systems:
return
try:
data = yaml.safe_load(soc_yaml)
data = yaml.load(soc_yaml, Loader=SafeLoader)
pykwalify.core.Core(source_data=data,
schema_data=soc_schema).validate()
except (yaml.YAMLError, pykwalify.errors.SchemaError) as e:
@ -188,7 +193,7 @@ def find_v2_archs(args):
if Path(archs_yml).is_file():
with Path(archs_yml).open('r') as f:
archs = yaml.safe_load(f.read())
archs = yaml.load(f.read(), Loader=SafeLoader)
try:
pykwalify.core.Core(source_data=archs, schema_data=arch_schema).validate()

View file

@ -5,11 +5,16 @@ from __future__ import annotations
import logging
import re
import yaml
from pathlib import Path
from yaml import safe_load
from dataclasses import dataclass, field
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
logger = logging.getLogger(__name__)
@ -88,7 +93,7 @@ class QuarantineData:
def load_data_from_yaml(cls, filename: str | Path) -> QuarantineData:
"""Load quarantine from yaml file."""
with open(filename, 'r', encoding='UTF-8') as yaml_fd:
qlist_raw_data: list[dict] = safe_load(yaml_fd)
qlist_raw_data: list[dict] = yaml.load(yaml_fd, Loader=SafeLoader)
try:
if not qlist_raw_data:
# in case of loading empty quarantine file

View file

@ -46,6 +46,11 @@ from twisterlib.platform import Platform
from twisterlib.testplan import change_skip_to_error_if_integration
from twisterlib.harness import HarnessImporter, Pytest
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
logger = logging.getLogger('twister')
logger.setLevel(logging.DEBUG)
import expr_parser
@ -861,7 +866,7 @@ class ProjectBuilder(FilterBuilder):
return []
with open(runners_file_path, 'r') as file:
runners_content: dict = yaml.safe_load(file)
runners_content: dict = yaml.load(file, Loader=SafeLoader)
if 'config' not in runners_content:
return []
@ -901,7 +906,7 @@ class ProjectBuilder(FilterBuilder):
with open(runners_file_path, 'rt') as file:
runners_content_text = file.read()
runners_content_yaml: dict = yaml.safe_load(runners_content_text)
runners_content_yaml: dict = yaml.load(runners_content_text, Loader=SafeLoader)
if 'config' not in runners_content_yaml:
return

View file

@ -16,7 +16,6 @@ import logging
import copy
import shutil
import random
import snippets
from pathlib import Path
from argparse import Namespace

View file

@ -1805,7 +1805,7 @@ def test_projectbuilder_get_binaries_from_runners(
with mock.patch('os.path.exists', mock_exists), \
mock.patch('builtins.open', mock.mock_open()), \
mock.patch('yaml.safe_load', return_value=runners_content):
mock.patch('yaml.load', return_value=runners_content):
if domain:
bins = pb._get_binaries_from_runners(domain)
else:

View file

@ -28,6 +28,11 @@ import pykwalify.core
from pathlib import Path, PurePath
from collections import namedtuple
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
METADATA_SCHEMA = '''
## A pykwalify schema for basic validation of the structure of a
## metadata YAML file.
@ -156,7 +161,7 @@ BLOB_PRESENT = 'A'
BLOB_NOT_PRESENT = 'D'
BLOB_OUTDATED = 'M'
schema = yaml.safe_load(METADATA_SCHEMA)
schema = yaml.load(METADATA_SCHEMA, Loader=SafeLoader)
def validate_setting(setting, module_path, filename=None):
@ -180,7 +185,7 @@ def process_module(module):
module_path / MODULE_YML_PATH.with_suffix('.yaml')]:
if Path(module_yml).is_file():
with Path(module_yml).open('r') as f:
meta = yaml.safe_load(f.read())
meta = yaml.load(f.read(), Loader=SafeLoader)
try:
pykwalify.core.Core(source_data=meta, schema_data=schema)\