-
Notifications
You must be signed in to change notification settings - Fork 121
repair corrupt packages #3873
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
Draft
LKuemmel
wants to merge
5
commits into
openWB:master
Choose a base branch
from
LKuemmel:repair_corrupt_packages
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
repair corrupt packages #3873
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import base64 | ||
| import csv | ||
| import hashlib | ||
| import importlib.metadata | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| REQUIREMENTS = Path(__file__).resolve().parents[1] / "requirements.txt" | ||
| MAIN_LOG = Path(__file__).resolve().parents[1] / "ramdisk" / "main.log" | ||
|
|
||
|
|
||
| def write_log(message: str = "") -> None: | ||
| MAIN_LOG.parent.mkdir(parents=True, exist_ok=True) | ||
| with MAIN_LOG.open("a", encoding="utf-8") as log_file: | ||
| log_file.write(f"{message}\n") | ||
|
|
||
|
|
||
| def check_packages() -> dict[str, list[str]]: | ||
| """Check installed package files against their RECORD hashes.""" | ||
| broken = {} | ||
|
|
||
| for dist in importlib.metadata.distributions(): | ||
| package = dist._path.name.removesuffix(".dist-info") | ||
| record = dist.read_text("RECORD") | ||
|
|
||
| if not record: | ||
| continue | ||
|
|
||
| for row in csv.reader(record.splitlines()): | ||
| if len(row) < 3 or not row[1] or not row[2]: | ||
| continue | ||
|
|
||
| path = Path(dist.locate_file(row[0])) | ||
|
|
||
| if not path.is_file(): | ||
| broken.setdefault(package, []).append(str(path)) | ||
| continue | ||
|
|
||
| algorithm, expected_hash = row[1].split("=", 1) | ||
|
|
||
| if algorithm != "sha256": | ||
| continue | ||
|
|
||
| digest = hashlib.sha256(path.read_bytes()).digest() | ||
|
LKuemmel marked this conversation as resolved.
Outdated
|
||
| actual_hash = ( | ||
| base64.urlsafe_b64encode(digest) | ||
| .rstrip(b"=") | ||
| .decode() | ||
| ) | ||
|
|
||
| if actual_hash != expected_hash: | ||
| broken.setdefault(package, []).append(str(path)) | ||
|
|
||
| return broken | ||
|
|
||
|
|
||
| def install_requirements() -> None: | ||
| subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-m", | ||
| "pip", | ||
| "install", | ||
| "--only-binary", | ||
| ":all:", | ||
| "-r", | ||
| str(REQUIREMENTS), | ||
| ], | ||
| check=True, | ||
| ) | ||
|
|
||
|
|
||
| def reinstall(packages: set[str]) -> None: | ||
| if not packages: | ||
| return | ||
|
|
||
| subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-m", | ||
| "pip", | ||
| "install", | ||
| "--only-binary", | ||
| ":all:", | ||
| "--force-reinstall", | ||
| *sorted(packages), | ||
| ], | ||
| check=True, | ||
| ) | ||
|
|
||
|
|
||
| def print_broken(broken: dict[str, list[str]]) -> None: | ||
| for package, files in sorted(broken.items()): | ||
| write_log(f"CORRUPT: {package}") | ||
| for path in files: | ||
| write_log(f" {path}") | ||
|
|
||
|
|
||
| def main() -> int: | ||
| write_log("Installing Python packages...") | ||
| install_requirements() | ||
|
|
||
| write_log("Checking package integrity...") | ||
| broken = check_packages() | ||
|
|
||
| if not broken: | ||
| write_log("All packages are intact.") | ||
| return 0 | ||
|
|
||
| write_log("\nCorrupted packages:") | ||
| print_broken(broken) | ||
|
|
||
| write_log("\nReinstalling corrupted packages...") | ||
| reinstall(set(broken)) | ||
|
|
||
| write_log("\nChecking package integrity after reinstall...") | ||
| broken = check_packages() | ||
|
|
||
| if broken: | ||
| write_log("\nERROR: The following packages are still corrupted:") | ||
| print_broken(broken) | ||
| return 1 | ||
|
|
||
| write_log("All packages are intact after reinstall.") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.