Ver código fonte

Use common version string util (#497)

* Require matrix-common
* Retrieve version number from package metadata

So that the pyproject.toml is the single source of version truth
David Robertson 2 anos atrás
pai
commit
bcdbe5e46f
6 arquivos alterados com 26 adições e 101 exclusões
  1. 1 0
      changelog.d/497.misc
  2. 21 1
      poetry.lock
  3. 1 0
      pyproject.toml
  4. 0 1
      sydent/__init__.py
  5. 3 2
      sydent/sydent.py
  6. 0 97
      sydent/util/versionstring.py

+ 1 - 0
changelog.d/497.misc

@@ -0,0 +1 @@
+Use matrix-common util to get a git-aware version number.

+ 21 - 1
poetry.lock

@@ -265,6 +265,22 @@ category = "main"
 optional = false
 python-versions = ">=3.6"
 
+[[package]]
+name = "matrix-common"
+version = "1.1.0"
+description = "Common utilities for Synapse, Sydent and Sygnal"
+category = "main"
+optional = false
+python-versions = ">=3.6"
+
+[package.dependencies]
+attrs = "*"
+importlib-metadata = {version = ">=1.4", markers = "python_version < \"3.8\""}
+
+[package.extras]
+dev = ["tox", "twisted", "aiounittest", "mypy (==0.910)", "black (==21.9b0)", "flake8 (==4.0.1)", "isort (==5.9.3)"]
+test = ["tox", "twisted", "aiounittest"]
+
 [[package]]
 name = "matrix_is_tester"
 version = "0.1"
@@ -765,7 +781,7 @@ test = ["zope.i18nmessageid", "zope.testing", "zope.testrunner"]
 [metadata]
 lock-version = "1.1"
 python-versions = "^3.7"
-content-hash = "810634e15a0766c7ad2c5dfe0e78cb21884118cc1b1631c04160894f2fffe530"
+content-hash = "79858d88e1876e60f0eb4fc2ed191c6321ececcaef3c2232236ccf018b214920"
 
 [metadata.files]
 appdirs = [
@@ -1004,6 +1020,10 @@ markupsafe = [
     {file = "MarkupSafe-2.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:693ce3f9e70a6cf7d2fb9e6c9d8b204b6b39897a2c4a1aa65728d5ac97dcc1d8"},
     {file = "MarkupSafe-2.0.1.tar.gz", hash = "sha256:594c67807fb16238b30c44bdf74f36c02cdf22d1c8cda91ef8a0ed8dabf5620a"},
 ]
+matrix-common = [
+    {file = "matrix_common-1.1.0-py3-none-any.whl", hash = "sha256:5d6dfd777503b2f3a031b566e6af25b6e95f9c0818ef57d954c3190fce5eb407"},
+    {file = "matrix_common-1.1.0.tar.gz", hash = "sha256:a8238748afc2b37079818367fed5156f355771b07c8ff0a175934f47e0ff3276"},
+]
 matrix_is_tester = []
 mccabe = [
     {file = "mccabe-0.6.1-py2.py3-none-any.whl", hash = "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42"},

+ 1 - 0
pyproject.toml

@@ -99,6 +99,7 @@ python = "^3.7"
 attrs = ">=19.1.0"
 jinja2 = ">=3.0.0"
 netaddr = ">=0.7.0"
+matrix-common = "^1.1.0"
 phonenumbers = ">=8.12.32"
 pynacl = ">=1.2.1"
 pyOpenSSL = ">=16.0.0"

+ 0 - 1
sydent/__init__.py

@@ -1 +0,0 @@
-__version__ = "2.5.1"

+ 3 - 2
sydent/sydent.py

@@ -23,6 +23,7 @@ from typing import Optional
 
 import attr
 import twisted.internet.reactor
+from matrix_common.versionstring import get_distribution_version_string
 from signedjson.types import SigningKey
 from twisted.internet import address, task
 from twisted.internet.interfaces import (
@@ -81,7 +82,6 @@ from sydent.replication.pusher import Pusher
 from sydent.threepid.bind import ThreepidBinder
 from sydent.util.hash import sha256_and_url_safe_base64
 from sydent.util.tokenutils import generateAlphanumericTokenOfLength
-from sydent.util.versionstring import get_version_string
 from sydent.validators.emailvalidator import EmailValidator
 from sydent.validators.msisdnvalidator import MsisdnValidator
 
@@ -119,7 +119,8 @@ class Sydent:
             import sentry_sdk
 
             sentry_sdk.init(
-                dsn=self.config.general.sentry_dsn, release=get_version_string()
+                dsn=self.config.general.sentry_dsn,
+                release=get_distribution_version_string("matrix-sydent"),
             )
             with sentry_sdk.configure_scope() as scope:
                 scope.set_tag("sydent_server_name", self.config.general.server_name)

+ 0 - 97
sydent/util/versionstring.py

@@ -1,97 +0,0 @@
-import logging
-import os
-import subprocess
-
-import sydent
-
-logger = logging.getLogger(__name__)
-
-
-def get_version_string() -> str:
-    """Calculate a git-aware version string for sydent.
-
-    The version string is `sydent@x.y.z`, where `x.y.z` comes from
-    `sydent.__version__`. If the `sydent` module belongs to a git repository on
-    disk, we the current branch, tag and commit hash, plus a flag to indicate
-    if the working tree is dirty.
-
-    An example probably illustrates best. Testing locally, I get
-    `sydent@2.4.6 (b=dmr/version-in-sentry,05e1fa8,dirty)`.
-    If this wasn't done from within the repository, I'd just get `sydent@2.4.6`.
-
-    Implementation nicked from Synapse.
-    """
-    version_string = sydent.__version__
-
-    try:
-        cwd = os.path.dirname(os.path.abspath(sydent.__file__))
-
-        try:
-            git_branch = (
-                subprocess.check_output(
-                    ["git", "rev-parse", "--abbrev-ref", "HEAD"],
-                    stderr=subprocess.DEVNULL,
-                    cwd=cwd,
-                )
-                .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=subprocess.DEVNULL,
-                    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=subprocess.DEVNULL,
-                    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=subprocess.DEVNULL,
-                    cwd=cwd,
-                )
-                .strip()
-                .decode("ascii")
-                .endswith(dirty_string)
-            )
-
-            git_dirty = "dirty" if is_dirty else ""
-        except (subprocess.CalledProcessError, FileNotFoundError):
-            git_dirty = ""
-
-        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 = f"sydent@{sydent.__version__} ({git_version})"
-    except Exception as e:
-        logger.info("Failed to check for git repository: %s", e)
-
-    return version_string