diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 51828f36..13c5af4c 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -9,7 +9,6 @@ import sys import time -import pkg_resources import pyqrcode import yaml from google.protobuf.json_format import MessageToDict @@ -18,6 +17,7 @@ import meshtastic.test import meshtastic.util from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware +from meshtastic.version import get_active_version from meshtastic.__init__ import BROADCAST_ADDR from meshtastic.ble_interface import BLEInterface from meshtastic.globals import Globals @@ -1399,7 +1399,7 @@ def initParser(): parser.set_defaults(deprecated=None) - the_version = pkg_resources.get_distribution("meshtastic").version + the_version = get_active_version() parser.add_argument("--version", action="version", version=f"{the_version}") parser.add_argument( diff --git a/meshtastic/util.py b/meshtastic/util.py index 7a5dd100..0c2fd448 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -12,12 +12,13 @@ import traceback from queue import Queue -import pkg_resources +import packaging.version as pkg_version import requests import serial import serial.tools.list_ports from meshtastic.supported_device import supported_devices +from meshtastic.version import get_active_version """Some devices such as a seger jlink we never want to accidentally open""" blacklistVids = dict.fromkeys([0x1366]) @@ -269,7 +270,7 @@ def support_info(): print(f" Machine: {platform.uname().machine}") print(f" Encoding (stdin): {sys.stdin.encoding}") print(f" Encoding (stdout): {sys.stdout.encoding}") - the_version = pkg_resources.get_distribution("meshtastic").version + the_version = get_active_version() pypi_version = check_if_newer_version() if pypi_version: print( @@ -599,9 +600,15 @@ def check_if_newer_version(): pypi_version = data["info"]["version"] except Exception: pass - act_version = pkg_resources.get_distribution("meshtastic").version - if pypi_version and pkg_resources.parse_version( - pypi_version - ) <= pkg_resources.parse_version(act_version): + act_version = get_active_version() + + try: + parsed_act_version = pkg_version.parse(act_version) + parsed_pypi_version = pkg_version.parse(pypi_version) + except pkg_version.InvalidVersion: + return pypi_version + + if parsed_pypi_version <= parsed_act_version: return None + return pypi_version diff --git a/meshtastic/version.py b/meshtastic/version.py new file mode 100644 index 00000000..bdc69eb4 --- /dev/null +++ b/meshtastic/version.py @@ -0,0 +1,11 @@ +import sys +try: + from importlib.metadata import version +except: + import pkg_resources + +def get_active_version(): + if "importlib.metadata" in sys.modules: + return version("meshtastic") + else: + return pkg_resources.get_distribution("meshtastic").version diff --git a/requirements.txt b/requirements.txt index 099eb9d0..524cd9da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,3 +19,4 @@ pytap2 pdoc3 pypubsub bleak +packaging diff --git a/setup.py b/setup.py index 893824fb..03f3455b 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,7 @@ "timeago>=1.0.15", "pyyaml", "bleak>=0.21.1", + "packaging", ], extras_require={"tunnel": ["pytap2>=2.0.0"]}, python_requires=">=3.7",