Commit 64fc6714b31 by Raphaël Gomès

Change `yaml.load` to `yaml.safe_load` and refactor into a util.

Both benchmarks/ and scmperf_lib/ make use of PyYAML to load yaml
files. This library has used an unsafe (i.e. can load yaml bombs, etc.)
loader by default since its inception and has been urging people to move
to `safe_load` instead.

We could have still used `load` by switching to SafeLoader, but the refactor
shrunk the code so much that it would not really be an improvement.
parent c21323ce059
......@@ -21,13 +21,12 @@ BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir
STRIP_VARIANTS_PATH = os.path.join(BASEDIR, "partial-sets.yaml")
def read_configuration(config_path):
# TODO refactor into single util (3 copies of this function exist)
def read_yaml_file(config_path):
with open(config_path) as config_file:
return yaml.load(config_file.read())
return yaml.safe_load(config_file.read())
repodir = read_configuration(os.path.join(BASEDIR, "config.yaml")).get('repodir')
repodir = read_yaml_file(os.path.join(BASEDIR, "config.yaml")).get('repodir')
if repodir:
REPOS_DIR = os.path.abspath(repodir)
......@@ -108,8 +107,7 @@ def get_repo_variants(repo_dir):
# Parse the benchrepos files
for benchrepo in benchrepos_files:
with open(join(repo_dir, benchrepo)) as f:
data = yaml.load(f.read())
data = read_yaml_file(join(repo_dir, benchrepo))
reference_repo = data["reference-repo"]
repo_id = reference_repo["id"]
......
......@@ -3,12 +3,8 @@
import json
import os
import subprocess
import yaml
def read_configuration(config_path):
with open(config_path) as config_file:
return yaml.load(config_file.read())
from benchmarks.utils import read_yaml_file
def generate_skip_file(config):
......@@ -34,5 +30,5 @@ def generate_skip_file(config):
json.dump(skip, f)
config = read_configuration("config.yaml")
config = read_yaml_file("config.yaml")
skip = generate_skip_file(config)
......@@ -4,12 +4,9 @@ import errno
import os
import subprocess
from os.path import isdir, join
import yaml
import urllib
def read_configuration(config_path):
with open(config_path) as config_file:
return yaml.load(config_file.read())
from benchmarks.utils import read_yaml_file
def clone_repositories(config, partial_revset, repos_dir):
......@@ -57,8 +54,8 @@ def clone_repositories(config, partial_revset, repos_dir):
["hg", "--cwd", partialdir, "debugupdatecache"]
)
partial_revset = read_configuration("partial-sets.yaml")
partial_revset = read_yaml_file("partial-sets.yaml")
config = read_configuration("config.yaml")
config = read_yaml_file("config.yaml")
clone_repositories(config, partial_revset['partial-sets'], "repos")
......@@ -8,7 +8,7 @@ def read_yaml(config_path):
""" Read a yaml config file and returns the results as a dict
"""
with open(config_path) as config_file:
return yaml.load(config_file.read())
return yaml.safe_load(config_file.read())
def get_reference_hash(repo_id, hash_data):
......
......@@ -7,7 +7,6 @@ import sys
import subprocess
from argparse import ArgumentParser
from os.path import isdir, join
import yaml
import shutil
from os.path import abspath, basename, dirname, join
......
......@@ -19,6 +19,7 @@ from scmperf_lib import (
shell_exec,
check_format_data,
get_reference_hash,
read_yaml,
) # isort:skip
......@@ -140,8 +141,7 @@ def main(args):
reference_repo["repo-prefix"] = repo_prefix
# Inject partial sets into bench repo
with open(partial_sets_file_path, "r") as partial_sets_file:
contents = yaml.safe_load(partial_sets_file.read())
contents = read_yaml(partial_sets_file_path)
reference_repo["partial-sets"] = contents["partial-sets"]
......
......@@ -48,8 +48,7 @@ def _main(reference_archive_path, partial_sets_config_path, tmp_directory):
# Read the benchrepo file
print("Reading the original benchrepo file.")
with open(benchrepo_path) as benchrepo_file:
benchrepo_data = yaml.load(benchrepo_file.read())
benchrepo_data = read_yaml(benchrepo_path)
repo_id = benchrepo_data["reference-repo"]["id"]
# Support old benchrepo files without repo-prefix
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment