list-of-demos / tools /add_creation_timestamps.py
hysts's picture
hysts HF staff
Update
968af70
raw
history blame
1.31 kB
#!/usr/bin/env python
from __future__ import annotations
import pathlib
import tempfile
import git
import tqdm.auto
import yaml
repo_dir = pathlib.Path(__file__).parents[1]
yaml_path = repo_dir / 'list.yaml'
def add_creation_timestamps() -> None:
with open(yaml_path) as f:
data = yaml.safe_load(f)
urls = list(data.keys())
for url in tqdm.auto.tqdm(urls):
info = data[url]
if 'created' in info:
continue
with tempfile.TemporaryDirectory() as temp_dir:
repo = git.Repo.clone_from(url, temp_dir)
commits = list(repo.iter_commits())
initial_commit = commits[-1]
date = initial_commit.authored_datetime
date_str = date.strftime('%Y-%m-%dT%H:%M:%S.000Z')
info['created'] = date_str
with open(yaml_path, 'w') as f:
yaml.dump(data, f)
def sort_by_creation_date() -> None:
with open(yaml_path) as f:
data = yaml.safe_load(f)
keys = sorted(data.keys(), key=lambda x: data[x]['created'])
sorted_data = dict()
for key in keys[::-1]:
sorted_data[key] = data[key]
with open(yaml_path, 'w') as f:
yaml.dump(sorted_data, f, sort_keys=False)
if __name__ == '__main__':
add_creation_timestamps()
sort_by_creation_date()