Coverage for src/hatch_ci/tools.py : 92%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# see https://pypi.org/project/setuptools-github # copy of setuptools_github.tools
continue
config: dict[str, str], var: str, typ: Any = _NO, fallback: Any = _NO ) -> Any: """extract an option value from the hatch config dict
Eg. get_options(self.config, "key") """ raise ValidationError(f"cannot find variable '{var}' for plugin 'ci'") except Exception as exc: raise ValidationError(f"cannot convert to {typ=} the {value=}") from exc
else:
"""given a version str will bump it according to mode
Arguments: version: text in the N.M.O form mode: major, minor or micro
Returns: increased text
>>> bump_version("1.0.3", "micro") "1.0.4" >>> bump_version("1.0.3", "minor") "1.1.0" """
gdata: dict[str, Any], abort: bool = True ) -> tuple[set[str], set[str]]: "ref", "sha", "run_id", "run_number", } raise ToolsError( f"missing keys from gdata '{','.join(missing)}'", missing, extra, gdata )
version_file: str | Path, github_dump: str | None = None, record_path: Path | None = None, abort: bool = True, ) -> dict[str, str | None]: """extracts version information from github_dump and updates version_file in-place
This gather version, branch, commit and other info from:
- version_file (eg. the __init__.py file containing __version__) - github_dump *a json encoded dictionary in GITHUB_DUMP - a record file (eg. _build.py) - the git checkout (using git)
Args: version_file (str, Path): path to a file with a __version__ variable github_dump (str): the os.getenv("GITHUB_DUMP") value (a json structure) record: pull data from a _build.py file
Returns: dict[str,str|None]: a dict with the current config dict[str,str|None]: a dict with the github dump data
Example: for github data: { "ref": "refs/heads/beta/0.3.10", "run_id": "5904313530", "run_number": "98", "sha": "507c657056d1a66520ec6b219a64706e70b0ff15", } for data: { "branch": "beta/0.3.10", "build": "98", "current": "0.3.10", "ref": "refs/heads/beta/0.3.10", "runid": "5904313530", "sha": "507c657056d1a66520ec6b219a64706e70b0ff15", "version": "0.3.10b98", "workflow": "beta", } """
# we fill this structure getting data from these sources: # github_dump -> record_path (eg. _build.py) -> repo infos (eg. the git checkout) "version": code.get_module_var(version_file, "__version__"), "current": code.get_module_var(version_file, "__version__"), "ref": None, "branch": None, "sha": None, "build": None, "runid": None, "workflow": None, }
if abort: raise scm.InvalidGitRepoError( f"cannot figure out settings (no repo in {path}, " f"a GITHUB_DUMP or a _build.py file)" ) return data
"ref": mod.ref, "sha": mod.sha, "run_number": mod.build, "run_id": mod.runid, } "ref": repo.head.name, "sha": repo.head.target.hex, "run_number": 0, "run_id": 0, } else: raise RuntimeError( "Unable to retrieve data from GITHUB_DUMP or " "{record_path=} or {repo=}" )
# make sure we have all keys
# update/fill the data values
# setuptools double calls the update_version, # this fixes the issue raise InvalidVersionError(f"cannot parse current version '{current}'") f"building package for {current} from '{gdata['ref']}' " f"branch ({match.groupdict()} mismatch {match1.groupdict()})" ) else:
version_file: str | Path, github_dump: str | None = None, abort: bool = True ) -> str | None: """extracts version information from github_dump and updates version_file in-place
Args: version_file (str, Path): path to a file with a __version__ variable github_dump (str): the os.getenv("GITHUB_DUMP") value
Returns: str: the new version for the package """
version_file: str | Path, github_dump: str | None = None, record_path: Path | None = None, abort: bool = True, ) -> jinja2.Environment: """returns a context object"""
"dir": dir, "len": len, "sorted": sorted, "reversed": reversed, }
# def process( # version_file: str | Path, # github_dump: str | None = None, # record: str | Path = "_build.py", # paths: str | Path | list[str | Path] | None = None, # fixers: dict[str, str] | None = None, # backup: Callable[[Path | str], None] | None = None, # abort: bool = True, # ) -> dict[str, str | None]: # """get version from github_dump and updates version_file/paths # # Args: # version_file (str, Path): path to a file with __version__ variable # github_dump (str): the os.getenv("GITHUB_DUMP") value # paths (str, Path): path(s) to files jinja2 processeable # fixers (dict[str,str]): fixer dictionary # record: set to True will generate a _build.py sibling of version_file # # Returns: # str: the new version for the package # # Example: # {'branch': 'beta/0.3.1', # 'build': 0, # 'current': '0.3.1', # 'hash': 'c9e484a*', # 'version': '0.3.1b0', # 'runid': 0 # } # """ # from argparse import Namespace # from functools import partial # from urllib.parse import quote # # from jinja2 import Environment # # class Context(Namespace): # def items(self): # for name, value in self.__dict__.items(): # if name.startswith("_"): # continue # yield (name, value) # # record_path = (Path(version_file).parent / record).absolute() if record else None # data = get_data(version_file, github_dump, record_path, abort) # # if not backup: # # def backup(_: Path | str) -> None: # pass # # breakpoint() # # backup(version_file) # # # set_module_var(version_file, "__version__", data["version"]) # # set_module_var(version_file, "__hash__", (data["sha"] or "")[:7]) # # # env = Environment(autoescape=True) # # env.filters["urlquote"] = partial(quote, safe="") # # for path in list_of_paths(paths): # # txt = replace(path.read_text(), fixers) # # tmpl = env.from_string(txt) # # backup(path) # # path.write_text(tmpl.render(ctx=Context(**data))) # # # # if record_path: # # generate_build_record(record_path, data) # # return data # # |