panxy1019's picture
Add files using upload-large-folder tool
128036e verified
Raw History Blame Contribute Delete
2.43 kB
"""Join the two release trees and restore scientific aliases."""
import argparse
import csv
import hashlib
import os
from pathlib import Path
import shutil
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--release-root", type=Path, default=Path(__file__).resolve().parents[2])
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
root = args.release_root.resolve(strict=True)
output = args.output.resolve()
assert not output.is_relative_to(root), "Choose a restored workspace outside the release tree"
output.mkdir(parents=True, exist_ok=True)
metadata = root / "code_and_checkpoints/release_metadata"
count = 0
with (metadata / "FILE_MANIFEST.csv").open(encoding="utf-8", newline="") as stream:
for row in csv.DictReader(stream):
source = root / row["destination_relative_path"]
target = output / row["relative_path"]
assert source.resolve(strict=True).is_relative_to(root)
assert target.resolve().is_relative_to(output)
target.parent.mkdir(parents=True, exist_ok=True)
if target.exists():
assert target.stat().st_size == int(row["bytes"]), target
with target.open("rb") as existing:
assert hashlib.file_digest(existing, "sha256").hexdigest() == row["sha256"], target
else:
try:
os.link(source, target)
except OSError:
shutil.copy2(source, target)
count += 1
alias_count = 0
with (metadata / "SYMLINK_ALIASES.csv").open(encoding="utf-8", newline="") as stream:
for row in csv.DictReader(stream):
target = output / row["alias_relative_path"]
referent = output / row["target_relative_path"]
assert target.parent.resolve().is_relative_to(output)
assert referent.resolve(strict=True).is_relative_to(output)
target.parent.mkdir(parents=True, exist_ok=True)
if os.path.lexists(target):
assert target.resolve(strict=True) == referent.resolve(strict=True), target
else:
target.symlink_to(os.path.relpath(referent, target.parent))
alias_count += 1
print(f"RESTORED files={count} aliases={alias_count} output={output}")
if __name__ == "__main__":
main()