Skip to content

Commit f33ed60

Browse files
committed
more robust check of git version
1 parent 5f830a5 commit f33ed60

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/hpc_connect/util/dynamic_version.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ def version_components_from_git(full: bool = False) -> tuple[int, int, int, str]
1515
dir = os.path.dirname(dir)
1616
if dir == os.path.sep:
1717
raise GitRepoNotFoundError(os.path.dirname(__file__))
18-
out = subprocess.check_output(
19-
["git", "-C", dir, "log", "-1", "--pretty=format:%ad %h", "--date=short"],
20-
encoding="utf-8",
21-
)
18+
try:
19+
out = subprocess.check_output(
20+
["git", "-C", dir, "log", "-1", "--pretty=format:%ad %h", "--date=short"],
21+
encoding="utf-8",
22+
)
23+
except Exception as e:
24+
raise CannotDetermineVersionFromGitError(e.args[0]) from None
25+
2226
parts = out.split()
2327
date, local = parts[:2]
2428
major, minor, micro = [int(_) for _ in date.split("-")]
@@ -34,6 +38,10 @@ class GitRepoNotFoundError(Exception):
3438
pass
3539

3640

41+
class CannotDetermineVersionFromGitError(Exception):
42+
pass
43+
44+
3745
def write_version_file(file: TextIO, major: int, minor: int, micro: int, local: str) -> None:
3846
file.write(
3947
f"""# Version file automatically generated by hpc_connect
@@ -43,8 +51,9 @@ def write_version_file(file: TextIO, major: int, minor: int, micro: int, local:
4351
4452
def __getattr__(name):
4553
46-
from hpc_connect.util.dynamic_version import version_components_from_git
54+
from hpc_connect.util.dynamic_version import CannotDetermineVersionFromGitError
4755
from hpc_connect.util.dynamic_version import GitRepoNotFoundError
56+
from hpc_connect.util.dynamic_version import version_components_from_git
4857
4958
if name not in ("version", "__version__", "version_info", "__version_info__"):
5059
raise AttributeError(name)
@@ -54,7 +63,7 @@ def __getattr__(name):
5463
return f"{{major}}.{{minor}}.{{micro}}+{{local}}"
5564
else:
5665
return (major, minor, micro, local)
57-
except GitRepoNotFoundError:
66+
except (GitRepoNotFoundError, CannotDetermineVersionFromGitError):
5867
if name in ("version", "__version__"):
5968
return __static_version__
6069
else:

0 commit comments

Comments
 (0)