Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

93 righe
3.1 KiB

  1. from __future__ import annotations
  2. import importlib.metadata
  3. __all__ = ["tag", "version", "commit"]
  4. # ========= =========== ===================
  5. # release development
  6. # ========= =========== ===================
  7. # tag X.Y X.Y (upcoming)
  8. # version X.Y X.Y.dev1+g5678cde
  9. # commit X.Y 5678cde
  10. # ========= =========== ===================
  11. # When tagging a release, set `released = True`.
  12. # After tagging a release, set `released = False` and increment `tag`.
  13. released = True
  14. tag = version = commit = "15.0.1"
  15. if not released: # pragma: no cover
  16. import pathlib
  17. import re
  18. import subprocess
  19. def get_version(tag: str) -> str:
  20. # Since setup.py executes the contents of src/websockets/version.py,
  21. # __file__ can point to either of these two files.
  22. file_path = pathlib.Path(__file__)
  23. root_dir = file_path.parents[0 if file_path.name == "setup.py" else 2]
  24. # Read version from package metadata if it is installed.
  25. try:
  26. version = importlib.metadata.version("websockets")
  27. except ImportError:
  28. pass
  29. else:
  30. # Check that this file belongs to the installed package.
  31. files = importlib.metadata.files("websockets")
  32. if files:
  33. version_files = [f for f in files if f.name == file_path.name]
  34. if version_files:
  35. version_file = version_files[0]
  36. if version_file.locate() == file_path:
  37. return version
  38. # Read version from git if available.
  39. try:
  40. description = subprocess.run(
  41. ["git", "describe", "--dirty", "--tags", "--long"],
  42. capture_output=True,
  43. cwd=root_dir,
  44. timeout=1,
  45. check=True,
  46. text=True,
  47. ).stdout.strip()
  48. # subprocess.run raises FileNotFoundError if git isn't on $PATH.
  49. except (
  50. FileNotFoundError,
  51. subprocess.CalledProcessError,
  52. subprocess.TimeoutExpired,
  53. ):
  54. pass
  55. else:
  56. description_re = r"[0-9.]+-([0-9]+)-(g[0-9a-f]{7,}(?:-dirty)?)"
  57. match = re.fullmatch(description_re, description)
  58. if match is None:
  59. raise ValueError(f"Unexpected git description: {description}")
  60. distance, remainder = match.groups()
  61. remainder = remainder.replace("-", ".") # required by PEP 440
  62. return f"{tag}.dev{distance}+{remainder}"
  63. # Avoid crashing if the development version cannot be determined.
  64. return f"{tag}.dev0+gunknown"
  65. version = get_version(tag)
  66. def get_commit(tag: str, version: str) -> str:
  67. # Extract commit from version, falling back to tag if not available.
  68. version_re = r"[0-9.]+\.dev[0-9]+\+g([0-9a-f]{7,}|unknown)(?:\.dirty)?"
  69. match = re.fullmatch(version_re, version)
  70. if match is None:
  71. raise ValueError(f"Unexpected version: {version}")
  72. (commit,) = match.groups()
  73. return tag if commit == "unknown" else commit
  74. commit = get_commit(tag, version)