Skip to content

Commit

Permalink
Filter to avoid manually selecting packages/distributions multiple times
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
  • Loading branch information
damnever committed Dec 16, 2024
1 parent 755aed7 commit 5ebdad4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 13 additions & 4 deletions pigar/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import importlib
import importlib.util
import importlib.machinery
from typing import NamedTuple, List, Dict, Any, Optional, Tuple
from typing import NamedTuple, List, Dict, Any, Optional, Tuple, Set, AnyStr
import asyncio

from .db import database
Expand Down Expand Up @@ -140,6 +140,7 @@ def __init__(self, project_root):
distributions=self._installed_dists.values()
)
self._requirements = _LocatableRequirements()
self._choosed_manually = dict()
self._uncertain_requirements = collections.defaultdict(
_LocatableRequirements
) # Multiple requirements for same import name.
Expand Down Expand Up @@ -394,15 +395,15 @@ def write_requirements(

if self._uncertain_requirements:
stream.write(
'\nWARNING(pigar): some manual fixes are required since pigar has found duplicate requirements for the same import name.\n'
'\n# WARNING(pigar): some manual fixes might be required as pigar has detected duplicate requirements for the same import name (possibly for different submodules).\n' # noqa: E501
)
uncertain_requirements = sorted(
self._uncertain_requirements.items(),
key=lambda item: item[0].lower()
)
for import_name, reqs in uncertain_requirements:
stream.write(
f'# WARNING(pigar): the following duplicate requirements are for import name: {import_name}\n'
f'# WARNING(pigar): the following duplicate requirements are for the import name: {import_name}\n' # noqa: E501
)
with_ref_comments_once = with_ref_comments
for _, req in reqs.sorted_items():
Expand Down Expand Up @@ -461,6 +462,10 @@ def _maybe_filter_distributions_with_same_import_name(
):
if dists_filter is None or len(distributions) <= 1:
return distributions
# We can use `functools.cache` in later versions of Python.
existing = self._choosed_manually.get(import_name, None)
if existing is not None:
return existing

assert (hasattr(distributions[0], 'name'))

Expand All @@ -481,7 +486,11 @@ def _maybe_filter_distributions_with_same_import_name(
best_match = casefold_match
if best_match is None and len(contains) == 1:
best_match = contains[0]
return dists_filter(import_name, locations, distributions, best_match)
choosed = dists_filter(
import_name, locations, distributions, best_match
)
self._choosed_manually[import_name] = choosed
return choosed


class LocalRequirementWithLatestVersion(NamedTuple):
Expand Down
7 changes: 3 additions & 4 deletions pigar/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ def test_generate(self):
cli, [
'gen', '--with-referenced-comments',
'--dont-show-differences', '--exclude-glob',
'**/tests/data/*', '--exclude-glob',
'**/_vendor/pip/*', '-f',
generated_requirement_file, project_path
'**/tests/data/*', '--exclude-glob', '**/_vendor/pip/*',
'-f', generated_requirement_file, project_path
]
)
self.assertEqual(result.exit_code, 0, result.output)
expected = self._read_filelines(expected_requirements)
actual = self._read_filelines(generated_requirement_file)
self.assertEqual(len(expected), len(actual))
self.assertEqual(len(expected), len(actual), actual)
for idx, line in enumerate(expected):
line2 = actual[idx]
if not line or line.startswith('#') or line.startswith('\n'):
Expand Down

0 comments on commit 5ebdad4

Please sign in to comment.