Repository navigation
feat: add canonical deterministic directory-level hashing #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,7 +39,7 @@ def extract_text_from_xml(input_path): | |||||||||||||||||||
| project_root = Path.cwd() | ||||||||||||||||||||
| output_dir = project_root / "data" / "processed" | ||||||||||||||||||||
| output_dir.mkdir(parents=True, exist_ok=True) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| output_path = output_dir / "wiki_clean.txt" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| with bz2.open(input_path, "rb") as f: | ||||||||||||||||||||
|
|
@@ -58,7 +58,7 @@ def extract_text_from_xml(input_path): | |||||||||||||||||||
| elem.clear() | ||||||||||||||||||||
| logger.info("Preprocessing complete. Output saved to %s", output_path) | ||||||||||||||||||||
| generate_manifest(input_path,output_path) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # generate data manifest | ||||||||||||||||||||
| def generate_manifest(raw_path, processed_path): | ||||||||||||||||||||
| raw_path = Path(raw_path) | ||||||||||||||||||||
|
|
@@ -114,6 +114,66 @@ def compute_sha256(file_path: Union[str, Path]) -> str: | |||||||||||||||||||
|
|
||||||||||||||||||||
| return sha256.hexdigest() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def compute_directory_hash(directory_path: Union[str, Path]) -> str: | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| Compute a deterministic SHA256 hash of a directory. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| The hash incorporates: | ||||||||||||||||||||
| - Relative file paths (normalized to POSIX style) | ||||||||||||||||||||
| - File contents (via compute_sha256) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Hidden files and files inside hidden directories (starting with '.') | ||||||||||||||||||||
| are ignored. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Parameters | ||||||||||||||||||||
| ---------- | ||||||||||||||||||||
| directory_path : Union[str, Path] | ||||||||||||||||||||
| Path to directory to hash. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Returns | ||||||||||||||||||||
| ------- | ||||||||||||||||||||
| str | ||||||||||||||||||||
| SHA256 hex digest representing directory contents. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| directory = Path(directory_path) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if not directory.exists(): | ||||||||||||||||||||
| raise FileNotFoundError(f"Path not found: {directory}") | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if not directory.is_dir(): | ||||||||||||||||||||
| raise NotADirectoryError(f"Path is not a directory: {directory}") | ||||||||||||||||||||
|
|
||||||||||||||||||||
| sha256 = hashlib.sha256() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| entries = [] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for f in directory.rglob("*"): | ||||||||||||||||||||
| if not f.is_file(): | ||||||||||||||||||||
| continue | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+150
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat openverifiablellm/utils.py | head -200 | tail -100Repository: AOSSIE-Org/OpenVerifiableLLM Length of output: 2693 🏁 Script executed: rg -n 'compute_directory_hash|is_file|is_symlink|symlink' openverifiablellm/utils.py -A 2 -B 1Repository: AOSSIE-Org/OpenVerifiableLLM Length of output: 325 🏁 Script executed: find tests -name '*.py' -type f | xargs rg -l 'compute_directory_hash|symlink' 2>/dev/nullRepository: AOSSIE-Org/OpenVerifiableLLM Length of output: 100 🏁 Script executed: rg -n 'symlink|test.*hash' tests/ -A 3 -B 1Repository: AOSSIE-Org/OpenVerifiableLLM Length of output: 1843 🏁 Script executed: cat tests/test_directory_hash.pyRepository: AOSSIE-Org/OpenVerifiableLLM Length of output: 3503 🏁 Script executed: python3 <<'EOF'
from pathlib import Path
import tempfile
import os
# Test is_file() behavior with symlinks
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir_path = Path(tmpdir)
# Create a regular file
regular_file = tmpdir_path / "regular.txt"
regular_file.write_text("content")
# Create a symlink to the regular file
symlink_file = tmpdir_path / "symlink.txt"
symlink_file.symlink_to(regular_file)
# Test is_file() behavior
print(f"regular_file.is_file(): {regular_file.is_file()}")
print(f"symlink_file.is_file(): {symlink_file.is_file()}")
print(f"symlink_file.is_symlink(): {symlink_file.is_symlink()}")
# Test what rglob includes
print("\nFiles from rglob('*'):")
for f in tmpdir_path.rglob("*"):
print(f" {f.name}: is_file={f.is_file()}, is_symlink={f.is_symlink()}")
EOFRepository: AOSSIE-Org/OpenVerifiableLLM Length of output: 276 🏁 Script executed: rg -n 'is_symlink|symlink' tests/test_directory_hash.pyRepository: AOSSIE-Org/OpenVerifiableLLM Length of output: 54 Exclude symlinked files from directory hashing. At line 151, The function currently provides no test coverage for symlink behavior. ♻️ Proposed fix for f in directory.rglob("*"):
+ if f.is_symlink():
+ continue
if not f.is_file():
continue📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| relative = f.relative_to(directory) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if any(part.startswith(".") for part in relative.parts): | ||||||||||||||||||||
| continue | ||||||||||||||||||||
|
|
||||||||||||||||||||
| rel_posix = relative.as_posix() | ||||||||||||||||||||
| entries.append((rel_posix, f)) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| entries_sorted = sorted(entries, key=lambda e: e[0]) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for relative_path, file in entries_sorted: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Include relative path in hash | ||||||||||||||||||||
| sha256.update(relative_path.encode("utf-8")) | ||||||||||||||||||||
| sha256.update(b"\0") # delimiter | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Include file content hash | ||||||||||||||||||||
| file_hash = compute_sha256(file) | ||||||||||||||||||||
| sha256.update(file_hash.encode("utf-8")) | ||||||||||||||||||||
| sha256.update(b"\0") # delimiter | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return sha256.hexdigest() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def extract_dump_date(filename: str): | ||||||||||||||||||||
| parts = filename.split("-") | ||||||||||||||||||||
| for part in parts: | ||||||||||||||||||||
|
|
@@ -148,7 +208,7 @@ def clean_wikitext(text: str) -> str: | |||||||||||||||||||
| if len(sys.argv) < 2: | ||||||||||||||||||||
| print("Usage: python -m openverifiablellm.utils <input_dump>") | ||||||||||||||||||||
| sys.exit(1) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| logging.basicConfig( | ||||||||||||||||||||
| level=logging.INFO, | ||||||||||||||||||||
| format="%(levelname)s - %(message)s" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,117 @@ | ||||||||||||||||||||
| import pytest | ||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||
| from openverifiablellm.utils import compute_directory_hash | ||||||||||||||||||||
|
||||||||||||||||||||
| import hashlib | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
||||||||||||||||||||
| def test_directory_hash_is_deterministic(tmp_path): | ||||||||||||||||||||
| # Create files | ||||||||||||||||||||
| file1 = tmp_path / "a.txt" | ||||||||||||||||||||
| file2 = tmp_path / "b.txt" | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+6
to
+10
|
||||||||||||||||||||
| def test_directory_hash_is_deterministic(tmp_path): | |
| # Create files | |
| file1 = tmp_path / "a.txt" | |
| file2 = tmp_path / "b.txt" | |
| # --------------- compute_directory_hash tests ------------------------------------ | |
| def test_directory_hash_is_deterministic(tmp_path): | |
| # Create files | |
| file1 = tmp_path / "a.txt" | |
| file2 = tmp_path / "b.txt" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider moving exception messages into custom exception classes (Ruff TRY003).
Ruff flags both lines for carrying long message strings inline rather than inside a dedicated exception subclass. For a library utility, a lightweight fix is to define small subclasses or use shorter messages.
♻️ Minimal fix — shorter inline messages (no new classes needed)
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.15.2)
[warning] 141-141: Avoid specifying long messages outside the exception class
(TRY003)
[warning] 144-144: Avoid specifying long messages outside the exception class
(TRY003)
🤖 Prompt for AI Agents