Skip to content

Commit

Permalink
Avoid reading the wrong version.
Browse files Browse the repository at this point in the history
This was causing builds to fail on Read the Docs since sphinx-autobuild
added websockets as a dependency.
  • Loading branch information
aaugustin committed Jul 21, 2024
1 parent 41c42b8 commit eaa64c0
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/websockets/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ def get_version(tag: str) -> str:
file_path = pathlib.Path(__file__)
root_dir = file_path.parents[0 if file_path.name == "setup.py" else 2]

# Read version from git if available. This prevents reading stale
# information from src/websockets.egg-info after building a sdist.
# Read version from package metadata if it is installed.
try:
version = importlib.metadata.version("websockets")
except ImportError:
pass
else:
# Check that this file belongs to the installed package.
files = importlib.metadata.files("websockets")
if files:
version_file = [f for f in files if f.name == file_path.name][0]
if version_file.locate() == file_path:
return version

# Read version from git if available.
try:
description = subprocess.run(
["git", "describe", "--dirty", "--tags", "--long"],
Expand All @@ -61,12 +73,6 @@ def get_version(tag: str) -> str:
remainder = remainder.replace("-", ".") # required by PEP 440
return f"{tag}.dev{distance}+{remainder}"

# Read version from package metadata if it is installed.
try:
return importlib.metadata.version("websockets")
except ImportError:
pass

# Avoid crashing if the development version cannot be determined.
return f"{tag}.dev0+gunknown"

Expand Down

0 comments on commit eaa64c0

Please sign in to comment.