diff --git a/cuvis/General.py b/cuvis/General.py index b03a3ba..46f8c0f 100644 --- a/cuvis/General.py +++ b/cuvis/General.py @@ -1,17 +1,19 @@ import logging import os import platform +import pkg_resources from ._cuvis_il import cuvis_il from .cuvis_aux import SDKException from .cuvis_types import ComponentType +from pathlib import Path import cuvis.cuvis_types as internal from dataclasses import dataclass -def init(settings_path:str=".", log_path:str="", global_loglevel:int=logging.DEBUG): +def init(settings_path: str = ".", log_path: str = "", global_loglevel: int = logging.DEBUG): FORMAT = '%(asctime)s -- %(levelname)s: %(message)s' if os.path.exists(log_path): pass @@ -21,9 +23,9 @@ def init(settings_path:str=".", log_path:str="", global_loglevel:int=logging.DEB elif platform.system() == "Windows": log_path = "C:\\ProgramData\\cuvis" os.makedirs(log_path, exist_ok=True) - + if os.path.exists(log_path): - logging.basicConfig(filename= os.path.join(log_path, "cuvisSDK_python.log"), + logging.basicConfig(filename=os.path.join(log_path, "cuvisSDK_python.log"), format=FORMAT, encoding='utf-8', level=global_loglevel, @@ -36,21 +38,35 @@ def init(settings_path:str=".", log_path:str="", global_loglevel:int=logging.DEB if cuvis_il.status_ok != cuvis_il.cuvis_init(settings_path, internal.__CuvisLoglevel__[global_loglevel]): raise SDKException() + def shutdown(): cuvis_il.cuvis_shutdown() + def version() -> str: return cuvis_il.cuvis_version_swig() + +def sdk_version() -> str: + return version() + + +def wrapper_version() -> str: + pip_version = pkg_resources.require('cuvis')[0].version + with open(Path(__file__).parent.parent / "git-hash.txt", 'r') as f: + git_hash = f.readline() + return f'{pip_version} {git_hash}'.strip() + + def set_log_level(lvl): lvl_dict = {"info": {"cuvis": cuvis_il.loglevel_info, - "logging": logging.INFO}, + "logging": logging.INFO}, "debug": {"cuvis": cuvis_il.loglevel_debug, - "logging": logging.DEBUG}, + "logging": logging.DEBUG}, "error": {"cuvis": cuvis_il.loglevel_error, - "logging": logging.ERROR}, + "logging": logging.ERROR}, "fatal": {"cuvis": cuvis_il.loglevel_fatal, - "logging": logging.CRITICAL}, + "logging": logging.CRITICAL}, "warning": {"cuvis": cuvis_il.loglevel_warning, "logging": logging.WARNING}, } @@ -75,7 +91,7 @@ def _get_internal(self): ci.userfield = self.user_field ci.pixelformat = self.pixel_format return ci - + @classmethod def _from_internal(cls, ci): return cls(type=internal.__ComponentType__[ci.type], diff --git a/git-hash.txt b/git-hash.txt new file mode 100644 index 0000000..87edf79 --- /dev/null +++ b/git-hash.txt @@ -0,0 +1 @@ +unknown \ No newline at end of file diff --git a/setup.py b/setup.py index 21a324e..610b234 100644 --- a/setup.py +++ b/setup.py @@ -2,10 +2,11 @@ import platform import sys import io - +import subprocess from shutil import rmtree, copy from setuptools import setup, find_packages, Command from setuptools.command import develop +from pathlib import Path here = os.path.abspath(os.path.dirname(__file__)) @@ -34,6 +35,13 @@ Exception( 'CUVIS SDK does not seem to exist on this machine! Make sure that the environment variable CUVIS is set.') + +def get_git_commit_hash() -> str: + try: + return subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip() + except subprocess.CalledProcessError: + return 'unknown' + # taken from https://github.com/navdeep-G/setup.py/blob/master/setup.py @@ -67,7 +75,7 @@ def run(self): os.system('python setup.py sdist'.format(sys.executable)) self.status('Uploading the package to PyPI via Twine…') - os.system('twine upload -r testpypi dist/*') + os.system('twine upload dist/*') # self.status('Pushing git tags…') # os.system('git tag v{0}'.format(about['__version__'])) @@ -90,7 +98,8 @@ def __createManifest__(subdirs): current = os.path.dirname(__file__) relative_paths = [os.path.relpath(path, current) for path in subdirs] - single_files = [os.path.join(here, 'README.md')] + single_files = [os.path.join(here, 'README.md'), + os.path.join(here, 'git-hash.txt')] rel_single_files = [os.path.relpath(path, current) for path in single_files] @@ -116,6 +125,8 @@ def __createManifest__(subdirs): except FileNotFoundError: long_description = DESCRIPTION +with open(Path(__file__).parent / "git-hash.txt", 'w') as f: + f.write(f'{get_git_commit_hash()}\n') setup( name=NAME,