|
| 1 | +"""Implementation of the hook interface.""" |
| 2 | + |
| 3 | +import datetime |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +from typing import Dict, Optional |
| 7 | + |
| 8 | +from bumpversion.config.models import Config |
| 9 | +from bumpversion.ui import get_indented_logger |
| 10 | + |
| 11 | +PREFIX = "BVHOOK_" |
| 12 | + |
| 13 | +logger = get_indented_logger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def run_command(script: str, environment: Optional[dict] = None) -> subprocess.CompletedProcess: |
| 17 | + """Runs command-line programs using the shell.""" |
| 18 | + if not isinstance(script, str): |
| 19 | + raise TypeError(f"`script` must be a string, not {type(script)}") |
| 20 | + if environment and not isinstance(environment, dict): |
| 21 | + raise TypeError(f"`environment` must be a dict, not {type(environment)}") |
| 22 | + return subprocess.run(script, env=environment, encoding="utf-8", shell=True, text=True, capture_output=True) |
| 23 | + |
| 24 | + |
| 25 | +def base_env(config: Config) -> Dict[str, str]: |
| 26 | + """Provide the base environment variables.""" |
| 27 | + return { |
| 28 | + f"{PREFIX}NOW": datetime.datetime.now().isoformat(), |
| 29 | + f"{PREFIX}UTCNOW": datetime.datetime.now(datetime.timezone.utc).isoformat(), |
| 30 | + **os.environ, |
| 31 | + **scm_env(config), |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +def scm_env(config: Config) -> Dict[str, str]: |
| 36 | + """Provide the scm environment variables.""" |
| 37 | + scm = config.scm_info |
| 38 | + return { |
| 39 | + f"{PREFIX}COMMIT_SHA": scm.commit_sha or "", |
| 40 | + f"{PREFIX}DISTANCE_TO_LATEST_TAG": str(scm.distance_to_latest_tag) or "0", |
| 41 | + f"{PREFIX}IS_DIRTY": str(scm.dirty), |
| 42 | + f"{PREFIX}BRANCH_NAME": scm.branch_name or "", |
| 43 | + f"{PREFIX}SHORT_BRANCH_NAME": scm.short_branch_name or "", |
| 44 | + f"{PREFIX}CURRENT_VERSION": scm.current_version or "", |
| 45 | + f"{PREFIX}CURRENT_TAG": scm.current_tag or "", |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +def current_version_env(config: Config) -> Dict[str, str]: |
| 50 | + """Provide the current version environment variables.""" |
| 51 | + version_str = config.current_version |
| 52 | + version = config.version_config.parse(version_str) |
| 53 | + |
| 54 | + return {f"{PREFIX}CURRENT_{part.upper()}": version[part].value for part in version} |
| 55 | + |
| 56 | + |
| 57 | +def setup_hook_env(config: Config) -> Dict[str, str]: |
| 58 | + """Provide the environment dictionary for `setup_hook`s.""" |
| 59 | + return {**base_env(config), **scm_env(config), **current_version_env(config)} |
| 60 | + |
| 61 | + |
| 62 | +def run_setup_hooks(config: Config) -> None: |
| 63 | + """Run the setup hooks.""" |
| 64 | + env = setup_hook_env(config) |
| 65 | + if config.setup_hooks: |
| 66 | + logger.info("Running setup hooks:") |
| 67 | + else: |
| 68 | + logger.info("No setup hooks defined") |
| 69 | + return |
| 70 | + |
| 71 | + logger.indent() |
| 72 | + for script in config.setup_hooks: |
| 73 | + logger.debug(f"Running {script!r}") |
| 74 | + logger.indent() |
| 75 | + result = run_command(script, env) |
| 76 | + logger.debug(result.stdout) |
| 77 | + logger.debug(result.stderr) |
| 78 | + logger.debug(f"Exited with {result.returncode}") |
| 79 | + logger.indent() |
| 80 | + logger.dedent() |
| 81 | + |
| 82 | + |
| 83 | +def run_pre_commit_hooks(config: Config) -> None: |
| 84 | + """Run the pre-commit hooks.""" |
| 85 | + pass |
| 86 | + |
| 87 | + |
| 88 | +def run_post_commit_hooks(config: Config) -> None: |
| 89 | + """Run the post-commit hooks.""" |
| 90 | + pass |
0 commit comments