hub_weekly_snapshots / hub_download.py
evijit's picture
evijit HF Staff
Update hub_download.py
e795e46 verified
raw
history blame
1.8 kB
import os
import subprocess
import datetime
from pathlib import Path
from git import Repo
# --- CONFIGURATION ---
REPO_URL="https://huggingface.co/datasets/cfahlgren1/hub-stats"
FILE_PATH="<item>.parquet"
DEST_DIR="weekly_snapshots/<item>"
CLONE_DIR="temp_repo"
# ----------------------
# Clone repo if not already cloned
if not os.path.exists(CLONE_DIR):
Repo.clone_from(REPO_URL, CLONE_DIR)
repo = Repo(CLONE_DIR)
assert not repo.bare
# Get all commits that affected the file
commits = list(repo.iter_commits(paths=FILE_PATH, reverse=True))
if not commits:
print("No commits found for the file.")
exit()
# Get the date of the first commit
start_date = datetime.datetime.fromtimestamp(commits[0].committed_date).date()
end_date = datetime.date.today()
Path(DEST_DIR).mkdir(exist_ok=True)
current_date = start_date
week = datetime.timedelta(weeks=1)
while current_date < end_date:
next_date = current_date + week
# Find latest commit before next_date that touches the file
weekly_commit = None
for commit in reversed(commits):
commit_date = datetime.datetime.fromtimestamp(commit.committed_date).date()
if current_date <= commit_date < next_date:
weekly_commit = commit
break
if weekly_commit:
try:
blob = weekly_commit.tree / FILE_PATH
weekly_folder = Path(DEST_DIR) / str(current_date)
weekly_folder.mkdir(parents=True, exist_ok=True)
output_path = weekly_folder / os.path.basename(FILE_PATH)
with open(output_path, 'wb') as f:
f.write(blob.data_stream.read())
print(f"Saved: {output_path}")
except KeyError:
print(f"File not found in commit {weekly_commit.hexsha}")
current_date = next_date