|
import os |
|
import subprocess |
|
import datetime |
|
from pathlib import Path |
|
from git import Repo |
|
|
|
|
|
REPO_URL="https://huggingface.co/datasets/cfahlgren1/hub-stats" |
|
FILE_PATH="<item>.parquet" |
|
DEST_DIR="weekly_snapshots/<item>" |
|
CLONE_DIR="temp_repo" |
|
|
|
|
|
|
|
if not os.path.exists(CLONE_DIR): |
|
Repo.clone_from(REPO_URL, CLONE_DIR) |
|
|
|
repo = Repo(CLONE_DIR) |
|
assert not repo.bare |
|
|
|
|
|
commits = list(repo.iter_commits(paths=FILE_PATH, reverse=True)) |
|
|
|
if not commits: |
|
print("No commits found for the file.") |
|
exit() |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|