Skip to content

Commit e046cd5

Browse files
Switch to importlib
Removes the deprecated `pkg_resources` module in favour of `importlib.metadata`. This also fixes installations by `pipx` etc throwing a "could not determine version" error.
1 parent 45aae5d commit e046cd5

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

pros/common/utils.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@ def get_version():
2727
except:
2828
pass
2929
try:
30-
import pkg_resources
30+
from importlib.metadata import distributions
3131
except ImportError:
3232
pass
3333
else:
3434
import pros.cli.main
3535
module = pros.cli.main.__name__
36-
for dist in pkg_resources.working_set:
37-
scripts = dist.get_entry_map().get('console_scripts') or {}
38-
for script_name, entry_point in iter(scripts.items()):
39-
if entry_point.module_name == module:
40-
ver = dist.version
41-
if ver is not None:
42-
return ver
36+
for dist in distributions():
37+
for entry_point in dist.entry_points:
38+
if entry_point.group == "console_scripts":
39+
if entry_point.module == module:
40+
if dist.version is not None:
41+
return dist.version
4342
raise RuntimeError('Could not determine version')
4443

4544

pros/upgrade/manifests/upgrade_manifest_v2.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from enum import Enum
33
from typing import *
44

5+
from importlib.metadata import distributions
6+
57
from pros.common import logger
68
from .upgrade_manifest_v1 import UpgradeManifestV1
79
from ..instructions import UpgradeInstruction, UpgradeResult, NothingInstruction
@@ -27,7 +29,6 @@ def __init__(self):
2729
self.platform_instructions: Dict[PlatformsV2, UpgradeInstruction] = {}
2830

2931
self._platform: 'PlatformsV2' = None
30-
3132
self._last_file: Optional[str] = None
3233

3334
@property
@@ -50,11 +51,12 @@ def platform(self) -> 'PlatformsV2':
5051
self._platform = PlatformsV2.MacOS
5152
else:
5253
try:
53-
from pip._vendor import pkg_resources
54-
results = [p for p in pkg_resources.working_set if p.project_name.startswith('pros-cli')]
55-
if any(results):
56-
self._platform = PlatformsV2.Pip
57-
except ImportError:
54+
for dist in distributions():
55+
name = (dist.metadata.get("Name") or "").lower()
56+
if name.startswith("pros-cli"):
57+
self._platform = PlatformsV2.Pip
58+
break
59+
except Exception:
5860
pass
5961
if not self._platform:
6062
self._platform = PlatformsV2.Unknown
@@ -65,7 +67,9 @@ def can_perform_upgrade(self) -> bool:
6567
return True
6668

6769
def perform_upgrade(self) -> UpgradeResult:
68-
instructions: UpgradeInstruction = self.platform_instructions.get(self.platform, NothingInstruction())
70+
instructions: UpgradeInstruction = self.platform_instructions.get(
71+
self.platform, NothingInstruction()
72+
)
6973
logger(__name__).debug(self.__dict__)
7074
logger(__name__).debug(f'Platform: {self.platform}')
7175
logger(__name__).debug(instructions.__dict__)

0 commit comments

Comments
 (0)