PY-50

Check Files against a Manifest

  • Medium
  • Files and Paths
  • Python

Task

Write check_manifest(base_path, manifest, digest_file). base_path names a directory. manifest is a list of dictionaries with exactly path and digest. digest_file(path) is a supplied function that returns the digest string for one existing file.

Return a dictionary with exactly these keys:

Manifest paths use forward slashes and are relative to base_path. A path is unsafe when it is empty, absolute, or contains a .. part. Do not open an unsafe path. duplicates contains every path listed more than once.

For each unique safe manifest path, report it as missing when no regular file exists, or changed when digest_file differs from its declared digest. extra contains every regular file found recursively below base_path whose relative forward-slash path is not a unique safe manifest path. complete is true only when all five evidence sets are empty.

Example

If docs/a.txt matches, docs/b.txt is absent, and notes.txt also exists, the result has missing == {"docs/b.txt"}, extra == {"notes.txt"}, and complete == False. The supplied helper decides how digests are calculated; your function owns path and identity accounting.

Your implementation

Edit solution.py and keep this function signature:

You may import Path from pathlib. Because manifest paths use forward slashes, relative.split("/") exposes the parts needed for the .. check. Do not modify the manifest, write or delete files, print, or ask for input.