File size: 1,798 Bytes
fc18dbd
 
 
 
 
 
 
 
e795e46
 
fc18dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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