From f105f5e881bc0ee52f60ca207f702033ea57a4d3 Mon Sep 17 00:00:00 2001 From: Arthur Peters Date: Thu, 22 Apr 2021 11:55:56 -0500 Subject: [PATCH] scripts/version: Correctly handle cases where we cannot find the version file at all. Initiall mentioned here: https://github.com/KatanaGraph/katana/issues/70#issuecomment-824885029 --- scripts/katana_version/__init__.py | 10 ++++++++++ scripts/katana_version/__main__.py | 11 +++++++---- scripts/katana_version/version.py | 28 ++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/scripts/katana_version/__init__.py b/scripts/katana_version/__init__.py index bcfe09011c..eb2f465773 100644 --- a/scripts/katana_version/__init__.py +++ b/scripts/katana_version/__init__.py @@ -39,6 +39,7 @@ class Configuration: github_access: Tuple[str, ...] enterprise: Optional[Repo] open: Optional[Repo] + version_file: Optional[Path] dry_run: bool def __init__(self, args=Namespace()): @@ -68,6 +69,15 @@ def __init__(self, args=Namespace()): f"Failed to parse version provided in environment variable KATANA_VERSION: {environ.get('KATANA_VERSION')}" ) + self.version_file = None + if (katana_repo_path / CONFIG_VERSION_PATH).is_file(): + self.version_file = katana_repo_path / CONFIG_VERSION_PATH + else: + logger.error( + f"Version file does not exist in source: {katana_repo_path / CONFIG_VERSION_PATH}. " + "Your Katana source is incomplete. Using 0.0.0 as a stand-in for the missing version." + ) + self.open = None self.enterprise = None try: diff --git a/scripts/katana_version/__main__.py b/scripts/katana_version/__main__.py index 16794c6aff..81731b0d11 100644 --- a/scripts/katana_version/__main__.py +++ b/scripts/katana_version/__main__.py @@ -230,7 +230,7 @@ def bump_checks(args): git.switch(current_branch, config.enterprise, config.dry_run) git.switch(current_branch, config.open, config.dry_run) - prev_version, variant = get_explicit_version(git.HEAD, True, config.open, no_dev=False) + prev_version, variant = get_explicit_version(git.HEAD, True, config.open, config.version_file, no_dev=False) next_version = version.Version(args.next_version) check_branch_version(current_branch, kind, next_version, prev_version) @@ -300,7 +300,7 @@ def bump_subcommand(args): g = GithubFacade(config) - prev_version, variant = get_explicit_version(git.HEAD, True, config.open, no_dev=True) + prev_version, variant = get_explicit_version(git.HEAD, True, config.open, config.version_file, no_dev=True) next_version = version.Version(args.next_version) current_branch = git.get_branch_checked_out(config.open) @@ -535,7 +535,7 @@ def release_subcommand(args): # Perform the checks that bump will do first. That way we will fail before tagging if possible. bump_checks(args) # Set some arguments for tag. This is a bit of a hack, but not worth the engineering to fix. - ver, variant = get_explicit_version(git.HEAD, False, config.open, no_dev=True) + ver, variant = get_explicit_version(git.HEAD, False, config.open, config.version_file, no_dev=True) args.version = str(ver) args.require_upstream = True tag_subcommand(args) @@ -570,7 +570,7 @@ def release_branch_subcommand(args): git.switch("master", config.enterprise, config.dry_run) git.switch("master", config.open, config.dry_run) - prev_version, variant = get_explicit_version(git.HEAD, True, config.open, no_dev=True) + prev_version, variant = get_explicit_version(git.HEAD, True, config.open, config.version_file, no_dev=True) next_version = version.Version(args.next_version) rc_version = version.Version(f"{prev_version}rc1") @@ -599,6 +599,9 @@ def release_branch_subcommand(args): def check_clean(args, config): + if not config.open: + raise StateError("Action cannot run in a source tree that is not a git clone.") + is_dirty = git.is_dirty(config.open) or (config.has_enterprise and git.is_dirty(config.enterprise)) if not args.clean and is_dirty: raise StateError("Action only supported in clean repositories. (Stash your changes.)") diff --git a/scripts/katana_version/version.py b/scripts/katana_version/version.py index 72f424758e..8a004c196b 100644 --- a/scripts/katana_version/version.py +++ b/scripts/katana_version/version.py @@ -64,7 +64,9 @@ def get_version( if config.has_enterprise: ke_commit = git.simplify_merge_commit(ke_commit, config.enterprise) - k_explicit_version, variant = get_explicit_version(k_commit, use_working_copy, config.open, variant) + k_explicit_version, variant = get_explicit_version( + k_commit, use_working_copy, config.open, config.version_file, variant + ) ke_tag_version = None if config.has_enterprise and not git.is_dirty(config.enterprise): ke_tag_version = get_tag_version(ke_commit or "HEAD", config.enterprise) @@ -170,9 +172,11 @@ def branch_ahead_count(branch): return nearest_branch -def get_explicit_version(k_commit: str, use_working_copy: bool, repo, variant=None, no_dev=False): +def get_explicit_version(k_commit: str, use_working_copy: bool, repo, version_file, variant=None, no_dev=False): tag_version = get_tag_version(k_commit, repo) - explicit_version = tag_version or get_config_version(None if use_working_copy else k_commit, repo, no_dev=no_dev) + explicit_version = tag_version or get_config_version( + None if use_working_copy else k_commit, repo, version_file, no_dev=no_dev + ) if explicit_version.local and variant and variant != explicit_version.local: logger.warning( f"You are overriding the repository variant {explicit_version.local} with build-time variant {variant}." @@ -181,12 +185,20 @@ def get_explicit_version(k_commit: str, use_working_copy: bool, repo, variant=No return explicit_version, variant -def get_config_version(k_commit, repo: Repo, no_dev=False) -> version.Version: - if k_commit: - version_str = capture_command("git", *git.dir_arg(repo), "show", "-p", f"{k_commit}:{CONFIG_VERSION_PATH}") - else: - with open(repo.dir / CONFIG_VERSION_PATH, "rt") as version_file: +def get_config_version(k_commit, repo: Repo, version_file, no_dev=False) -> version.Version: + if repo: + if k_commit: + version_str = capture_command("git", *git.dir_arg(repo), "show", "-p", f"{k_commit}:{CONFIG_VERSION_PATH}") + else: + with open(repo.dir / CONFIG_VERSION_PATH, "rt") as version_file: + version_str = version_file.read() + elif version_file: + # We have no git information. Wing it. + with open(Path(__file__).parent.parent.parent / CONFIG_VERSION_PATH, "rt") as version_file: version_str = version_file.read() + else: + # We have no information. Something is really broken. Still don't crash to allow builds. + version_str = "0.0.0" ver = version.Version(version_str.strip()) if no_dev: