"""Plugin for refreshing a locked requirements file."""fromconfigparserimportConfigParserfrompathlibimportPathfromtypingimportAny,Dict,Listimportclickimportpluggyfromedgetest.loggerimportget_loggerfromedgetest.schemaimportSchemafromedgetest.utilsimport_run_commandfromtomlkitimportloadLOG=get_logger(__name__)hookimpl=pluggy.HookimplMarker("edgetest")
[docs]@hookimpldefaddoption(schema:Schema):"""Add configuration options for pip-tools. Parameters ---------- schema : Schema The schema class. """schema.add_globaloption("pip_tools",{"type":"dict","schema":{"extras":{"type":"list","coerce":"listify","schema":{"type":"string"},},"index_url":{"type":"string"},},},)
[docs]defget_reqfile(ctx:click.Context)->Path:"""Get the requirements file to supply to ``uv``. Parameters ---------- ctx : click.Context The context for the CLI call. Returns ------- Path Path to the requirements file. """parser:AnyifPath(ctx.params["config"]).name=="setup.cfg":# Check for the install_requiresparser=ConfigParser()parser.read(Path(ctx.params["config"]))if"options"inparserandparser.get("options","install_requires"):reqfile=Path(ctx.params["config"])else:reqfile=Path(ctx.params["requirements"])elifPath(ctx.params["config"]).name=="pyproject.toml":withopen(Path(ctx.params["config"]))asbuf:parser=load(buf)if"dependencies"inparser["project"]:reqfile=Path(ctx.params["config"])else:reqfile=Path(ctx.params["requirements"])else:reqfile=Path(ctx.params["requirements"])returnreqfile
[docs]@hookimpl(tryfirst=True)defpost_run_hook(testers:List,conf:Dict):"""Refresh a locked requirements file based on the test output."""ctx=click.get_current_context()ifnotctx.params["export"]:LOG.info("Skipping ``uv pip compile`` as the requirements have not been updated.")eliftesters[-1].status:reqfile=get_reqfile(ctx=ctx)try:options=[]ifconf.get("pip_tools",{}).get("extras"):options+=[f"--extra={extra}"forextrainconf["pip_tools"]["extras"]]ifconf.get("pip_tools",{}).get("index_url"):options.append(f"--index-url={conf['pip_tools']['index_url']}")_run_command("uv","pip","compile","-U",*options,"--output-file=requirements.txt",str(reqfile),)exceptRuntimeError:LOG.info("Unable to update the locked requirements file")else:LOG.info("Skipping ``uv`` refresh as the tests didn't pass.")