|
import os |
|
import subprocess |
|
import datetime |
|
from pathlib import Path |
|
import shutil |
|
|
|
|
|
|
|
items = ['spaces', 'models', 'datasets', 'daily_papers'] |
|
REPO_URL = "https://huggingface.co/datasets/cfahlgren1/hub-stats" |
|
|
|
for item in items: |
|
FILE_PATH = item+".parquet" |
|
DEST_DIR = item |
|
CLONE_DIR = "temp_repo" |
|
|
|
|
|
|
|
try: |
|
subprocess.run(["git", "lfs", "install"], check=True) |
|
print("Git LFS installed successfully") |
|
except Exception as e: |
|
print(f"Warning: Git LFS installation might have failed: {e}") |
|
print("Continuing anyway...") |
|
|
|
|
|
if not os.path.exists(CLONE_DIR): |
|
print(f"Cloning repository with Git LFS: {REPO_URL}") |
|
subprocess.run(["git", "lfs", "clone", REPO_URL, CLONE_DIR], check=True) |
|
else: |
|
print(f"Repository already exists at {CLONE_DIR}") |
|
|
|
subprocess.run(["git", "-C", CLONE_DIR, "checkout", "main"], check=True) |
|
subprocess.run(["git", "-C", CLONE_DIR, "pull"], check=True) |
|
|
|
|
|
print(f"Finding commits for file: {FILE_PATH}") |
|
git_log_cmd = ["git", "-C", CLONE_DIR, "log", "--pretty=format:%H|%ct", "--follow", FILE_PATH] |
|
result = subprocess.run(git_log_cmd, capture_output=True, text=True, check=True) |
|
commits_info = result.stdout.strip().split('\n') |
|
|
|
if not commits_info or commits_info[0] == '': |
|
print("No commits found for the file.") |
|
exit() |
|
|
|
|
|
commits = [] |
|
for commit_line in commits_info: |
|
if '|' in commit_line: |
|
hexsha, timestamp = commit_line.split('|') |
|
commits.append((hexsha, int(timestamp))) |
|
|
|
|
|
commits.sort(key=lambda x: x[1]) |
|
|
|
|
|
start_date = datetime.datetime.fromtimestamp(commits[0][1]).date() |
|
end_date = datetime.date.today() |
|
|
|
Path(DEST_DIR).mkdir(parents=True, exist_ok=True) |
|
|
|
current_date = start_date |
|
week = datetime.timedelta(weeks=1) |
|
|
|
print(f"Processing weekly snapshots from {start_date} to {end_date}") |
|
|
|
while current_date < end_date: |
|
next_date = current_date + week |
|
next_date_timestamp = int(datetime.datetime.combine(next_date, datetime.time.min).timestamp()) |
|
|
|
|
|
weekly_commit = None |
|
for hexsha, timestamp in reversed(commits): |
|
if timestamp < next_date_timestamp: |
|
weekly_commit = hexsha |
|
commit_date = datetime.datetime.fromtimestamp(timestamp).date() |
|
break |
|
|
|
if weekly_commit: |
|
try: |
|
print(f"Processing week of {current_date} using commit {weekly_commit[:8]}") |
|
|
|
|
|
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) |
|
|
|
|
|
subprocess.run(["git", "-C", CLONE_DIR, "checkout", weekly_commit], check=True) |
|
|
|
|
|
subprocess.run(["git", "-C", CLONE_DIR, "lfs", "pull"], check=True) |
|
|
|
|
|
source_file = os.path.join(CLONE_DIR, FILE_PATH) |
|
|
|
if os.path.exists(source_file): |
|
shutil.copy2(source_file, output_path) |
|
file_size = os.path.getsize(output_path) / (1024 * 1024) |
|
print(f"Saved: {output_path} ({file_size:.2f} MB)") |
|
|
|
|
|
with open(output_path, 'rb') as f: |
|
header = f.read(4) |
|
if header != b'PAR1': |
|
print(f"WARNING: File doesn't start with Parquet magic bytes. Size: {file_size:.2f} MB") |
|
if file_size < 1: |
|
print("This is likely still an LFS pointer and not the actual file") |
|
else: |
|
print(f"ERROR: Source file not found at {source_file}") |
|
|
|
except Exception as e: |
|
print(f"Error processing commit {weekly_commit}: {e}") |
|
else: |
|
print(f"No commit found for week of {current_date}") |
|
|
|
current_date = next_date |
|
|
|
|
|
try: |
|
subprocess.run(["git", "-C", CLONE_DIR, "checkout", "main"], check=True) |
|
print("Returned to main branch") |
|
except Exception as e: |
|
print(f"Warning: Couldn't return to main branch: {e}") |