Skip to content

Commit

Permalink
Properly handle .keep_dir files
Browse files Browse the repository at this point in the history
Handle ValueError when trying to parse .keep_dir files that hold
no package atom, also add some tests.

Signed-off-by: gcarq <[email protected]>
  • Loading branch information
gcarq committed Dec 23, 2024
1 parent 7ab9b8c commit d64913b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lostfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
import itertools
import os
import sys
from glob import glob

import portage
Expand Down Expand Up @@ -347,10 +348,9 @@ def process_directory(dirpath: str, filenames: list[str], strict: bool, tracked:
if not name.startswith(".keep_"):
continue

atom = resolve_pkg_from_keepfile(name)
if is_pkg_installed(atom):
raise IgnoreDirectory()
break
if atom := resolve_pkg_from_keepfile(name):
if is_pkg_installed(atom):
raise IgnoreDirectory()

if not strict:
paths = resolve_symlinks(dirpath)
Expand Down Expand Up @@ -383,14 +383,22 @@ def resolve_symlinks(*paths: str) -> set[str]:
return set(itertools.chain.from_iterable((path, os.path.realpath(path)) for path in paths))


def resolve_pkg_from_keepfile(filename: str) -> str:
def resolve_pkg_from_keepfile(filename: str) -> str | None:
"""
Returns the package atom from the given .keep file,
or None if the keepfile cannot be parsed.
for example: .keep_net-print_cups-0 -> net-print/cups
"""
_, category, remainder = filename.split("_", maxsplit=2)
package, _ = remainder.rsplit("-", maxsplit=1)
return f"{category}/{package}"
if filename == '.keep_dir':
return None

try:
_, category, remainder = filename.split("_", maxsplit=2)
package, _ = remainder.rsplit("-", maxsplit=1)
return f"{category}/{package}"
except ValueError:
print(f"Failed to parse package from keepfile: {filename}, this is a bug", file=sys.stderr)
return None


def is_pkg_installed(atom: str) -> bool:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ authors = [
{name = 'Michael Egger', email = '[email protected]'},
]

[dev-dependencies]
test = ["pytest"]

[project.scripts]
lostfiles = "lostfiles:main"

Expand Down
16 changes: 16 additions & 0 deletions tests/test_lostfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from unittest.mock import patch

from lostfiles import resolve_pkg_from_keepfile, should_ignore_path


def test_resolve_pkg_from_keepfile():
assert resolve_pkg_from_keepfile(".keep_net-print_cups-0") == "net-print/cups"
assert resolve_pkg_from_keepfile(".keep_sys-apps_systemd-0") == "sys-apps/systemd"
assert resolve_pkg_from_keepfile(".keep_dir") is None


@patch("lostfiles.IGNORED_PATHS", {"/ignored"})
def test_should_ignore_path(*args, **kwargs):
assert should_ignore_path("/ignored") is True
assert should_ignore_path("/not_ignored") is False
assert should_ignore_path("/not_ignored/.keep") is True

0 comments on commit d64913b

Please sign in to comment.