Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
7b62791e
Unverified
Commit
7b62791e
authored
3 years ago
by
Patrick Cloke
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Clean-up get_version_string (#11468)
parent
153194c7
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
changelog.d/11468.misc
+1
-0
1 addition, 0 deletions
changelog.d/11468.misc
synapse/util/versionstring.py
+25
-57
25 additions, 57 deletions
synapse/util/versionstring.py
with
26 additions
and
57 deletions
changelog.d/11468.misc
0 → 100644
+
1
−
0
View file @
7b62791e
Refactor `get_version_string` to fix-up types and duplicated code.
This diff is collapsed.
Click to expand it.
synapse/util/versionstring.py
+
25
−
57
View file @
7b62791e
# Copyright 2016 OpenMarket Ltd
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
...
...
@@ -29,10 +30,11 @@ def get_version_string(module: ModuleType) -> str:
If called on a module not in a git checkout will return `__version__`.
Args:
module (module)
module: The module to check the version of. Must declare a __version__
attribute.
Returns:
str
The module version (as a string).
"""
cached_version
=
version_cache
.
get
(
module
)
...
...
@@ -44,71 +46,37 @@ def get_version_string(module: ModuleType) -> str:
version_string
=
module
.
__version__
# type: ignore[attr-defined]
try
:
null
=
open
(
os
.
devnull
,
"
w
"
)
cwd
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
module
.
__file__
))
try
:
git_branch
=
(
subprocess
.
check_output
(
[
"
git
"
,
"
rev-parse
"
,
"
--abbrev-ref
"
,
"
HEAD
"
],
stderr
=
null
,
cwd
=
cwd
def
_run_git_command
(
prefix
:
str
,
*
params
:
str
)
->
str
:
try
:
result
=
(
subprocess
.
check_output
(
[
"
git
"
,
*
params
],
stderr
=
subprocess
.
DEVNULL
,
cwd
=
cwd
)
.
strip
()
.
decode
(
"
ascii
"
)
)
.
strip
()
.
decode
(
"
ascii
"
)
)
git_branch
=
"
b=
"
+
git_branch
except
(
subprocess
.
CalledProcessError
,
FileNotFoundError
):
# FileNotFoundError can arise when git is not installed
git_branch
=
""
try
:
git_tag
=
(
subprocess
.
check_output
(
[
"
git
"
,
"
describe
"
,
"
--exact-match
"
],
stderr
=
null
,
cwd
=
cwd
)
.
strip
()
.
decode
(
"
ascii
"
)
)
git_tag
=
"
t=
"
+
git_tag
except
(
subprocess
.
CalledProcessError
,
FileNotFoundError
):
git_tag
=
""
try
:
git_commit
=
(
subprocess
.
check_output
(
[
"
git
"
,
"
rev-parse
"
,
"
--short
"
,
"
HEAD
"
],
stderr
=
null
,
cwd
=
cwd
)
.
strip
()
.
decode
(
"
ascii
"
)
)
except
(
subprocess
.
CalledProcessError
,
FileNotFoundError
):
git_commit
=
""
try
:
dirty_string
=
"
-this_is_a_dirty_checkout
"
is_dirty
=
(
subprocess
.
check_output
(
[
"
git
"
,
"
describe
"
,
"
--dirty=
"
+
dirty_string
],
stderr
=
null
,
cwd
=
cwd
)
.
strip
()
.
decode
(
"
ascii
"
)
.
endswith
(
dirty_string
)
)
return
prefix
+
result
except
(
subprocess
.
CalledProcessError
,
FileNotFoundError
):
return
""
git_dirty
=
"
dirty
"
if
is_dirty
else
""
except
(
subprocess
.
CalledProcessError
,
FileNotFoundError
):
git_dirty
=
""
git_branch
=
_run_git_command
(
"
b=
"
,
"
rev-parse
"
,
"
--abbrev-ref
"
,
"
HEAD
"
)
git_tag
=
_run_git_command
(
"
t=
"
,
"
describe
"
,
"
--exact-match
"
)
git_commit
=
_run_git_command
(
""
,
"
rev-parse
"
,
"
--short
"
,
"
HEAD
"
)
dirty_string
=
"
-this_is_a_dirty_checkout
"
is_dirty
=
_run_git_command
(
""
,
"
describe
"
,
"
--dirty=
"
+
dirty_string
).
endswith
(
dirty_string
)
git_dirty
=
"
dirty
"
if
is_dirty
else
""
if
git_branch
or
git_tag
or
git_commit
or
git_dirty
:
git_version
=
"
,
"
.
join
(
s
for
s
in
(
git_branch
,
git_tag
,
git_commit
,
git_dirty
)
if
s
)
version_string
=
"
%s (%s)
"
%
(
# If the __version__ attribute doesn't exist, we'll have failed
# loudly above.
module
.
__version__
,
# type: ignore[attr-defined]
git_version
,
)
version_string
=
f
"
{
version_string
}
(
{
git_version
}
)
"
except
Exception
as
e
:
logger
.
info
(
"
Failed to check for git repository: %s
"
,
e
)
...
...
This diff is collapsed.
Click to expand it.
Tulir Asokan
@tulir
mentioned in commit
158d73eb
·
3 years ago
mentioned in commit
158d73eb
mentioned in commit 158d73ebdd61eef33831ae5f6990acf07244fc55
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment