# Licensed under a 3-clause BSD style license - see LICENSE.rstimportsysfrompathlibimportPathfrom.importutilfrom.consoleimportlog# TODO: Some verification of the config values
[docs]def_get_config_path():""" Check the default config file path for all valid extensions. Raise if no file is found, or if multiple are found for the valid extensions. Return the path of the config file if exactly one is found. """extensions=['.json','.jsonc']path=Path('asv.conf.json')num_matches=0foreinextensions:p=path.with_suffix(e)ifp.exists():num_matches+=1path=pifnum_matches==0:raiseutil.UserError(f"No `asv.conf` file found for valid extensions: {extensions}.")elifnum_matches>1:raiseutil.UserError(f"Multiple `asv.conf` files found for valid extensions: {extensions}. "f"Please specify which file to use with `--config=FILEPATH`.")else:# exactly one valid config file foundreturnstr(path)
[docs]classConfig:""" Manages the configuration for a benchmark project. """
[docs]defload(cls,path=None):""" Load a configuration from a file. If no file is provided, defaults to `asv.conf` with valid extensions `['.json', '.jsonc']`. """ifpath:ifnotPath(path).exists():raiseutil.UserError(f"Config file {path} not found.")else:path=_get_config_path()d=util.load_json(path,cls.api_version,js_comments=True)try:returncls.from_json(d)exceptValueError:raiseutil.UserError(f"No repo specified in {path} config file.")
@classmethod
[docs]deffrom_json(cls,d):if'wheel_cache_size'ind:log.warning("`wheel_cache_size` has been renamed to `build_cache_size`."" Update your `asv.conf.json` accordingly.")d.setdefault('build_cache_size',d['wheel_cache_size'])conf=cls()conf.__dict__.update(d)ifnotgetattr(conf,"repo",None):raiseutil.UserError("No repo specified in config file.")ifnotgetattr(conf,"branches",[None]):# If 'branches' attribute is present, at least some must# be listed.raiseutil.UserError("No branches specified in config file.")returnconf