Skip to content

Commit

Permalink
🛠 Fix get_version in setup.py to avoid hard-coding version. (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
samet-akcay committed Apr 13, 2022
1 parent cda30d9 commit eb7bbde
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,30 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
from pathlib import Path
import os
from importlib.util import module_from_spec, spec_from_file_location
from typing import List

from setuptools import find_packages, setup


def load_module(name: str = "anomalib/__init__.py"):
"""Load Python Module.
Args:
name (str, optional): Name of the module to load.
Defaults to "anomalib/__init__.py".
Returns:
_type_: _description_
"""
location = os.path.join(os.path.dirname(__file__), name)
spec = spec_from_file_location(name=name, location=location)
module = module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore
return module


def get_version() -> str:
"""Get version from `anomalib.__init__`.
Expand All @@ -28,21 +46,15 @@ def get_version() -> str:
the value assigned to it.
Example:
>>> # Assume that __version__ = "0.2.1"
>>> # Assume that __version__ = "0.2.6"
>>> get_version()
"0.2.1"
"0.2.6"
Returns:
str: Version number of `anomalib` package.
str: `anomalib` version.
"""

with open(Path.cwd() / "anomalib" / "__init__.py", "r", encoding="utf8") as file:
lines = file.readlines()
for line in lines:
line = line.strip()
if line.startswith("__version__"):
version = line.replace("__version__ = ", "")

anomalib = load_module(name="anomalib/__init__.py")
version = anomalib.__version__
return version


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

setup(
name="anomalib",
# TODO: https://github.com/openvinotoolkit/anomalib/issues/36
version="0.2.6",
version=get_version(),
author="Intel OpenVINO",
author_email="help@openvino.intel.com",
description="anomalib - Anomaly Detection Library",
Expand Down

0 comments on commit eb7bbde

Please sign in to comment.