Skip to content

Commit 353a5bc

Browse files
author
Goran Jelic-Cizmek
committed
Updates
1 parent 7f7e444 commit 353a5bc

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

cpp/lib.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,39 @@ def forget_python_pkg(package):
3636
Args:
3737
package: name of the Python package to exclude
3838
"""
39-
import pkg_resources
40-
4139
try:
42-
dist = pkg_resources.get_distribution(package)
43-
except pkg_resources.DistributionNotFound:
40+
dist = importlib.metadata.distribution(package)
41+
except importlib.metadata.PackageNotFoundError:
42+
return
43+
44+
dist_location = None
45+
if dist.files:
46+
# Get the parent directory of the first file in the distribution
47+
first_file = next(iter(dist.files))
48+
dist_location = str(first_file.locate().parent.parent)
49+
else:
50+
# Fallback: try to get location from sys.path and package name
51+
try:
52+
spec = importlib.util.find_spec(package)
53+
if spec and spec.origin:
54+
# Get the parent directory containing the package
55+
dist_location = str(Path(spec.origin).parent.parent)
56+
except (AttributeError, ImportError):
57+
pass
58+
59+
if dist_location is None:
4460
return
61+
4562
PYTHONPATH = os.environ.get("PYTHONPATH")
46-
if PYTHONPATH is not None and dist.location in PYTHONPATH:
63+
if PYTHONPATH is not None and dist_location in PYTHONPATH:
4764
logging.debug(
4865
"Remove incompatible version of %s in PYTHONPATH: %s",
4966
package,
50-
dist.location,
67+
dist_location,
5168
)
52-
os.environ["PYTHONPATH"] = PYTHONPATH.replace(dist.location, "")
69+
os.environ["PYTHONPATH"] = PYTHONPATH.replace(dist_location, "")
5370
try:
54-
sys.path.remove(dist.location)
71+
sys.path.remove(dist_location)
5572
except ValueError:
5673
pass
5774

@@ -553,7 +570,9 @@ def requirement(self):
553570
name = self.name
554571

555572
import packaging.requirements
556-
return packaging.requirements.Requirement(f"{name} {self.user_config['version']}")
573+
return packaging.requirements.Requirement(
574+
f"{name} {self.user_config['version']}"
575+
)
557576

558577
@abc.abstractmethod
559578
def configure(self):
@@ -776,7 +795,11 @@ def find_tool_in_path(self, search_paths=None):
776795
if not paths:
777796
raise FileNotFoundError(f"Could not find tool {self}")
778797
all_paths = [(p, self.find_version(p)) for p in paths]
779-
paths = list(filter(lambda tpl: self.requirement.specifier.contains(tpl[1]), all_paths))
798+
paths = list(
799+
filter(
800+
lambda tpl: self.requirement.specifier.contains(tpl[1]), all_paths
801+
)
802+
)
780803
paths = list(sorted(paths, key=lambda tup: tup[1])) # sort by version
781804
if not paths:
782805
raise FileNotFoundError(

0 commit comments

Comments
 (0)