Skip to content

Commit 88fcb2d

Browse files
authored
Merge pull request #55 from advaitpatel/refactor/single-source-version
refactor(version): remove hardcoded __version__, resolve from setup.py only
2 parents f3a8a01 + 0e33142 commit 88fcb2d

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

docksec.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,31 @@
55
import argparse
66
from typing import NoReturn, Optional
77

8-
# Version - keep in sync with setup.py
9-
__version__ = "2026.2.23"
8+
def get_version() -> str:
9+
"""Return the installed package version.
10+
11+
Resolution order:
12+
1. importlib.metadata — works when installed via pip
13+
2. setup.py on disk — works when running from source
14+
3. 'unknown' — last resort fallback
15+
"""
16+
try:
17+
from importlib.metadata import version
18+
return version("docksec")
19+
except Exception: # package not installed; fall through to source fallback
20+
pass
1021

11-
def get_version():
12-
"""Get version from setup.py if available, otherwise use hardcoded version."""
1322
try:
1423
import re
1524
setup_path = os.path.join(os.path.dirname(__file__), 'setup.py')
16-
if os.path.exists(setup_path):
17-
with open(setup_path, 'r') as f:
18-
content = f.read()
19-
match = re.search(r'version="([^"]+)"', content)
20-
if match:
21-
return match.group(1)
22-
except:
25+
with open(setup_path, 'r') as f:
26+
match = re.search(r'version="([^"]+)"', f.read())
27+
if match:
28+
return match.group(1)
29+
except Exception: # setup.py missing or unreadable; fall through to unknown
2330
pass
24-
return __version__
31+
32+
return "unknown"
2533

2634
def main() -> None:
2735
"""

0 commit comments

Comments
 (0)