1
1
# Copyright 2016 OpenMarket Ltd
2
+ # Copyright 2021 The Matrix.org Foundation C.I.C.
2
3
#
3
4
# Licensed under the Apache License, Version 2.0 (the "License");
4
5
# you may not use this file except in compliance with the License.
@@ -29,10 +30,11 @@ def get_version_string(module: ModuleType) -> str:
29
30
If called on a module not in a git checkout will return `__version__`.
30
31
31
32
Args:
32
- module (module)
33
+ module: The module to check the version of. Must declare a __version__
34
+ attribute.
33
35
34
36
Returns:
35
- str
37
+ The module version (as a string).
36
38
"""
37
39
38
40
cached_version = version_cache .get (module )
@@ -44,71 +46,37 @@ def get_version_string(module: ModuleType) -> str:
44
46
version_string = module .__version__ # type: ignore[attr-defined]
45
47
46
48
try :
47
- null = open (os .devnull , "w" )
48
49
cwd = os .path .dirname (os .path .abspath (module .__file__ ))
49
50
50
- try :
51
- git_branch = (
52
- subprocess .check_output (
53
- ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ], stderr = null , cwd = cwd
51
+ def _run_git_command (prefix : str , * params : str ) -> str :
52
+ try :
53
+ result = (
54
+ subprocess .check_output (
55
+ ["git" , * params ], stderr = subprocess .DEVNULL , cwd = cwd
56
+ )
57
+ .strip ()
58
+ .decode ("ascii" )
54
59
)
55
- .strip ()
56
- .decode ("ascii" )
57
- )
58
- git_branch = "b=" + git_branch
59
- except (subprocess .CalledProcessError , FileNotFoundError ):
60
- # FileNotFoundError can arise when git is not installed
61
- git_branch = ""
62
-
63
- try :
64
- git_tag = (
65
- subprocess .check_output (
66
- ["git" , "describe" , "--exact-match" ], stderr = null , cwd = cwd
67
- )
68
- .strip ()
69
- .decode ("ascii" )
70
- )
71
- git_tag = "t=" + git_tag
72
- except (subprocess .CalledProcessError , FileNotFoundError ):
73
- git_tag = ""
74
-
75
- try :
76
- git_commit = (
77
- subprocess .check_output (
78
- ["git" , "rev-parse" , "--short" , "HEAD" ], stderr = null , cwd = cwd
79
- )
80
- .strip ()
81
- .decode ("ascii" )
82
- )
83
- except (subprocess .CalledProcessError , FileNotFoundError ):
84
- git_commit = ""
85
-
86
- try :
87
- dirty_string = "-this_is_a_dirty_checkout"
88
- is_dirty = (
89
- subprocess .check_output (
90
- ["git" , "describe" , "--dirty=" + dirty_string ], stderr = null , cwd = cwd
91
- )
92
- .strip ()
93
- .decode ("ascii" )
94
- .endswith (dirty_string )
95
- )
60
+ return prefix + result
61
+ except (subprocess .CalledProcessError , FileNotFoundError ):
62
+ return ""
96
63
97
- git_dirty = "dirty" if is_dirty else ""
98
- except (subprocess .CalledProcessError , FileNotFoundError ):
99
- git_dirty = ""
64
+ git_branch = _run_git_command ("b=" , "rev-parse" , "--abbrev-ref" , "HEAD" )
65
+ git_tag = _run_git_command ("t=" , "describe" , "--exact-match" )
66
+ git_commit = _run_git_command ("" , "rev-parse" , "--short" , "HEAD" )
67
+
68
+ dirty_string = "-this_is_a_dirty_checkout"
69
+ is_dirty = _run_git_command ("" , "describe" , "--dirty=" + dirty_string ).endswith (
70
+ dirty_string
71
+ )
72
+ git_dirty = "dirty" if is_dirty else ""
100
73
101
74
if git_branch or git_tag or git_commit or git_dirty :
102
75
git_version = "," .join (
103
76
s for s in (git_branch , git_tag , git_commit , git_dirty ) if s
104
77
)
105
78
106
- version_string = "%s (%s)" % (
107
- # If the __version__ attribute doesn't exist, we'll have failed
108
- # loudly above.
109
- module .__version__ , # type: ignore[attr-defined]
110
- git_version ,
111
- )
79
+ version_string = f"{ version_string } ({ git_version } )"
112
80
except Exception as e :
113
81
logger .info ("Failed to check for git repository: %s" , e )
114
82
0 commit comments