Skip to content

Commit eb7bbde

Browse files
authored
🛠 Fix get_version in setup.py to avoid hard-coding version. (#229)
1 parent cda30d9 commit eb7bbde

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

setup.py

+25-14
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,30 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions
1515
# and limitations under the License.
16-
from pathlib import Path
16+
import os
17+
from importlib.util import module_from_spec, spec_from_file_location
1718
from typing import List
1819

1920
from setuptools import find_packages, setup
2021

2122

23+
def load_module(name: str = "anomalib/__init__.py"):
24+
"""Load Python Module.
25+
26+
Args:
27+
name (str, optional): Name of the module to load.
28+
Defaults to "anomalib/__init__.py".
29+
30+
Returns:
31+
_type_: _description_
32+
"""
33+
location = os.path.join(os.path.dirname(__file__), name)
34+
spec = spec_from_file_location(name=name, location=location)
35+
module = module_from_spec(spec) # type: ignore
36+
spec.loader.exec_module(module) # type: ignore
37+
return module
38+
39+
2240
def get_version() -> str:
2341
"""Get version from `anomalib.__init__`.
2442
@@ -28,21 +46,15 @@ def get_version() -> str:
2846
the value assigned to it.
2947
3048
Example:
31-
>>> # Assume that __version__ = "0.2.1"
49+
>>> # Assume that __version__ = "0.2.6"
3250
>>> get_version()
33-
"0.2.1"
51+
"0.2.6"
3452
3553
Returns:
36-
str: Version number of `anomalib` package.
54+
str: `anomalib` version.
3755
"""
38-
39-
with open(Path.cwd() / "anomalib" / "__init__.py", "r", encoding="utf8") as file:
40-
lines = file.readlines()
41-
for line in lines:
42-
line = line.strip()
43-
if line.startswith("__version__"):
44-
version = line.replace("__version__ = ", "")
45-
56+
anomalib = load_module(name="anomalib/__init__.py")
57+
version = anomalib.__version__
4658
return version
4759

4860

@@ -85,8 +97,7 @@ def get_required_packages(requirement_files: List[str]) -> List[str]:
8597

8698
setup(
8799
name="anomalib",
88-
# TODO: https://github.com/openvinotoolkit/anomalib/issues/36
89-
version="0.2.6",
100+
version=get_version(),
90101
author="Intel OpenVINO",
91102
author_email="[email protected]",
92103
description="anomalib - Anomaly Detection Library",

0 commit comments

Comments
 (0)