parameters: (
      resource_name: str,
      to_path: str,
      unzip: bool = True,
      download_md5_mismatch: bool = True,
  )
docstring: Obtains a gem5 resource and stored it to a specified location. If the
      specified resource is already at the location, no action is taken.
  
      :param resource_name: The resource to be obtained.
  
      :param to_path: The location in the file system the resource is to be
      stored. The filename should be included.
  
      :param unzip: If true, gzipped resources will be unzipped prior to saving
      to `to_path`. True by default.
  
      :param download_md5_mismatch: If a resource is present with an incorrect
      hash (e.g., an outdated version of the resource is present), `get_resource`
      will delete this local resource and re-download it if this parameter is
      True. True by default.
  
      :raises Exception: An exception is thrown if a file is already present at
      `to_path` but it does not have the correct md5 sum. An exception will also
      be thrown is a directory is present at `to_path`
docstring_summary: Obtains a gem5 resource and stored it to a specified location. If the
      specified resource is already at the location, no action is taken.
docstring_tokens: Obtains a gem5 resource and stored it to a specified location . If the specified resource is already at the location no action is taken .
function: def get_resource(
      resource_name: str,
      to_path: str,
      unzip: bool = True,
      download_md5_mismatch: bool = True,
  ) -> None:
      """
      Obtains a gem5 resource and stored it to a specified location. If the
      specified resource is already at the location, no action is taken.
  
      :param resource_name: The resource to be obtained.
  
      :param to_path: The location in the file system the resource is to be
      stored. The filename should be included.
  
      :param unzip: If true, gzipped resources will be unzipped prior to saving
      to `to_path`. True by default.
  
      :param download_md5_mismatch: If a resource is present with an incorrect
      hash (e.g., an outdated version of the resource is present), `get_resource`
      will delete this local resource and re-download it if this parameter is
      True. True by default.
  
      :raises Exception: An exception is thrown if a file is already present at
      `to_path` but it does not have the correct md5 sum. An exception will also
      be thrown is a directory is present at `to_path`
      """
  
      # We apply a lock for a specific resource. This is to avoid circumstances
      # where multiple instances of gem5 are running and trying to obtain the
      # same resources at once. The timeout here is somewhat arbitarily put at 15
      # minutes.Most resources should be downloaded and decompressed in this
      # timeframe, even on the most constrained of systems.
      with FileLock("{}.lock".format(to_path), timeout=900):
  
          resource_json = get_resources_json_obj(resource_name)
  
          if os.path.exists(to_path):
  
              if not os.path.isfile(to_path):
                  raise Exception(
                      "There is a directory at '{}'.".format(to_path)
                  )
  
              if _get_md5(to_path) == resource_json["md5sum"]:
                  # In this case, the file has already been download, no need to
                  # do so again.
                  return
              elif download_md5_mismatch:
                  os.remove(to_path)
              else:
                  raise Exception(
                      "There already a file present at '{}' but "
                      "its md5 value is invalid.".format(to_path)
                  )
  
          download_dest = to_path
  
          # This if-statement is remain backwards compatable with the older,
          # string-based way of doing things. It can be refactored away over
          # time:
          # https://gem5-review.googlesource.com/c/public/gem5-resources/+/51168
          if isinstance(resource_json["is_zipped"], str):
              run_unzip = unzip and resource_json["is_zipped"].lower() == "true"
          elif isinstance(resource_json["is_zipped"], bool):
              run_unzip = unzip and resource_json["is_zipped"]
          else:
              raise Exception(
                  "The resource.json entry for '{}' has a value for the "
                  "'is_zipped' field which is neither a string or a boolean."
                  .format(
                      resource_name
                  )
              )
  
          if run_unzip:
              download_dest += ".gz"
  
          # TODO: Might be nice to have some kind of download status bar here.
          # TODO: There might be a case where this should be silenced.
          print(
              "Resource '{}' was not found locally. Downloading to '{}'..."
              .format(
                  resource_name, download_dest
              )
          )
  
          # Get the URL. The URL may contain '{url_base}' which needs replaced
          # with the correct value.
          url = resource_json["url"].format(url_base=_get_url_base())
  
          _download(url=url, download_to=download_dest)
          print("Finished downloading resource '{}'.".format(resource_name))
  
          if run_unzip:
              print(
                  "Decompressing resource '{}' ('{}')...".format(
                      resource_name, download_dest
                  )
              )
              with gzip.open(download_dest, "rb") as f:
                  with open(to_path, "wb") as o:
                      shutil.copyfileobj(f, o)
              os.remove(download_dest)
              print(
                  "Finished decompressing resource '{}'.".format(resource_name)
              )
function_tokens: def get_resource ( resource_name : str , to_path : str , unzip : bool = True , download_md5_mismatch : bool = True , ) -> None : # We apply a lock for a specific resource. This is to avoid circumstances # where multiple instances of gem5 are running and trying to obtain the # same resources at once. The timeout here is somewhat arbitarily put at 15 # minutes.Most resources should be downloaded and decompressed in this # timeframe, even on the most constrained of systems. with FileLock ( "{}.lock" . format ( to_path ) , timeout = 900 ) : resource_json = get_resources_json_obj ( resource_name ) if os . path . exists ( to_path ) : if not os . path . isfile ( to_path ) : raise Exception ( "There is a directory at '{}'." . format ( to_path ) ) if _get_md5 ( to_path ) == resource_json [ "md5sum" ] : # In this case, the file has already been download, no need to # do so again. return elif download_md5_mismatch : os . remove ( to_path ) else : raise Exception ( "There already a file present at '{}' but " "its md5 value is invalid." . format ( to_path ) ) download_dest = to_path # This if-statement is remain backwards compatable with the older, # string-based way of doing things. It can be refactored away over # time: # https://gem5-review.googlesource.com/c/public/gem5-resources/+/51168 if isinstance ( resource_json [ "is_zipped" ] , str ) : run_unzip = unzip and resource_json [ "is_zipped" ] . lower ( ) == "true" elif isinstance ( resource_json [ "is_zipped" ] , bool ) : run_unzip = unzip and resource_json [ "is_zipped" ] else : raise Exception ( "The resource.json entry for '{}' has a value for the " "'is_zipped' field which is neither a string or a boolean." . format ( resource_name ) ) if run_unzip : download_dest += ".gz" # TODO: Might be nice to have some kind of download status bar here. # TODO: There might be a case where this should be silenced. print ( "Resource '{}' was not found locally. Downloading to '{}'..." . format ( resource_name , download_dest ) ) # Get the URL. The URL may contain '{url_base}' which needs replaced # with the correct value. url = resource_json [ "url" ] . format ( url_base = _get_url_base ( ) ) _download ( url = url , download_to = download_dest ) print ( "Finished downloading resource '{}'." . format ( resource_name ) ) if run_unzip : print ( "Decompressing resource '{}' ('{}')..." . format ( resource_name , download_dest ) ) with gzip . open ( download_dest , "rb" ) as f : with open ( to_path , "wb" ) as o : shutil . copyfileobj ( f , o ) os . remove ( download_dest ) print ( "Finished decompressing resource '{}'." . format ( resource_name ) )