From d96df4025323e0173d32b5bba9086fea28c8444a Mon Sep 17 00:00:00 2001 From: Harshil Patel Date: Tue, 1 Aug 2023 16:22:44 -0700 Subject: [PATCH 1/2] stdlib: Added support for JSON via env variables. Change-Id: I5791e6d51b3b9f68eb212a46c4cd0add23668340 Co-authored-by: Kunal Pai --- src/python/gem5/resources/client.py | 33 ++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/python/gem5/resources/client.py b/src/python/gem5/resources/client.py index ab8262bf92..c43dd76ac5 100644 --- a/src/python/gem5/resources/client.py +++ b/src/python/gem5/resources/client.py @@ -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,6 +53,14 @@ 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." + ) + # First check if the config file path is provided in the environment variable if "GEM5_CONFIG" in os.environ: config_file_path = Path(os.environ["GEM5_CONFIG"]) @@ -68,6 +76,29 @@ 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']}" + ) + # If the GEM5_RESOURCE_JSON is set, use it as the only source + elif "GEM5_RESOURCE_JSON" in os.environ: + json_source = { + "url": os.environ["GEM5_RESOURCE_JSON"], + "isMongo": False, + } + gem5_config["sources"] = {"GEM5_RESOURCE_JSON": json_source} + warn( + f"No config sources are used, Using resources from {os.environ['GEM5_RESOURCE_JSON']}" + ) clientwrapper = ClientWrapper(gem5_config) return clientwrapper From 32b7ffc4546fd53bc50487d1899188844177bb1d Mon Sep 17 00:00:00 2001 From: Harshil Patel Date: Tue, 1 Aug 2023 17:22:35 -0700 Subject: [PATCH 2/2] stdlib: fixed warning message Change-Id: I04ef23529d7afc5d46fbba7558279ec08acd629a Co-authored-by: paikunal --- src/python/gem5/resources/client.py | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/python/gem5/resources/client.py b/src/python/gem5/resources/client.py index c43dd76ac5..571a8254e0 100644 --- a/src/python/gem5/resources/client.py +++ b/src/python/gem5/resources/client.py @@ -60,9 +60,31 @@ def _get_clientwrapper(): 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") @@ -89,16 +111,7 @@ def _get_clientwrapper(): inform( f"Appending resources from {os.environ['GEM5_RESOURCE_JSON_APPEND']}" ) - # If the GEM5_RESOURCE_JSON is set, use it as the only source - elif "GEM5_RESOURCE_JSON" in os.environ: - json_source = { - "url": os.environ["GEM5_RESOURCE_JSON"], - "isMongo": False, - } - gem5_config["sources"] = {"GEM5_RESOURCE_JSON": json_source} - warn( - f"No config sources are used, Using resources from {os.environ['GEM5_RESOURCE_JSON']}" - ) + clientwrapper = ClientWrapper(gem5_config) return clientwrapper