stdlib,resources: Enable loading of local Resources data via JSON file path (#150)

This commit is contained in:
Bobby R. Bruce
2023-08-02 15:49:47 -07:00
committed by GitHub

View File

@@ -30,7 +30,7 @@ import os
from typing import Optional, Dict, List
from .client_api.client_wrapper import ClientWrapper
from gem5.gem5_default_config import config
from m5.util import inform
from m5.util import inform, warn
from _m5 import core
@@ -53,8 +53,38 @@ clientwrapper = None
def _get_clientwrapper():
global clientwrapper
if clientwrapper is None:
if (
"GEM5_RESOURCE_JSON" in os.environ
and "GEM5_RESOURCE_JSON_APPEND" in os.environ
):
raise Exception(
"Both GEM5_RESOURCE_JSON and GEM5_RESOURCE_JSON_APPEND are set. Please set only one of them."
)
gem5_config = {}
# If the GEM5_RESOURCE_JSON is set, use it as the only source
if "GEM5_RESOURCE_JSON" in os.environ:
json_source = {
"url": os.environ["GEM5_RESOURCE_JSON"],
"isMongo": False,
}
gem5_config["sources"] = {"GEM5_RESOURCE_JSON": json_source}
if "GEM5_CONFIG" in os.environ:
warn(
f"Both GEM5_CONFIG and GEM5_RESOURCE_JSON are set.\n"
f"GEM5_CONFIG will be ignored in favor of the GEM5_RESOURCE_JSON environment variable."
)
elif (Path().cwd().resolve() / "gem5-config.json").exists():
warn(
f"Both gem5-config.json and GEM5_RESOURCE_JSON are set.\n"
f"gem5-config.json will be ignored in favor of the GEM5_RESOURCE_JSON environment variable."
)
else:
warn(
f"GEM5_RESOURCE_JSON is set.\n"
f"gem5-default-config will be ignored in favor of the GEM5_RESOURCE_JSON environment variable."
)
# First check if the config file path is provided in the environment variable
if "GEM5_CONFIG" in os.environ:
elif "GEM5_CONFIG" in os.environ:
config_file_path = Path(os.environ["GEM5_CONFIG"])
gem5_config = getFileContent(config_file_path)
inform("Using config file specified by $GEM5_CONFIG")
@@ -68,6 +98,20 @@ def _get_clientwrapper():
else:
gem5_config = config
inform("Using default config")
# If the GEM5_RESOURCE_JSON_APPEND is set, append the resources to the gem5_config
if "GEM5_RESOURCE_JSON_APPEND" in os.environ:
json_source = {
"url": os.environ["GEM5_RESOURCE_JSON_APPEND"],
"isMongo": False,
}
gem5_config["sources"].update(
{"GEM5_RESOURCE_JSON_APPEND": json_source}
)
inform(
f"Appending resources from {os.environ['GEM5_RESOURCE_JSON_APPEND']}"
)
clientwrapper = ClientWrapper(gem5_config)
return clientwrapper