rdmtools/trr379_rdmtools/update_jtrack_study_dataset.py

87 lines
2.7 KiB
Python

# /// script
# requires-python = ">=3.11"
# dependencies = [
# "datalad-core @ git+https://hub.datalad.org/datalad/datalad-core@main",
# "trr379-rdmtools @ git+https://hub.trr379.de/q02/rdmtools@main",
# ]
# ///
# This script can be used in a cronjob. It is written to run silently
# whenever there is no error.
from datalad_core.runners import call_git
from pathlib import Path
import rich_click as click
from shutil import (
copytree,
ignore_patterns,
rmtree,
)
import tempfile
from trr379_rdmtools.utils import has_staged_changes
@click.command()
@click.argument(
'source_dir',
type=click.Path(
exists=True, file_okay=False, dir_okay=True, readable=True,
path_type=Path,
),
help="Directory where the JTrack data data resides."
)
@click.argument(
'repo_url',
help="URL of the repository to update.",
)
def update_jtrack_study_dataset(
source_dir: Path,
repo_url: str,
) -> None:
"""Ingest a JTrack study and push it to a Forgejo site
"""
with tempfile.TemporaryDirectory() as wdir:
wpath = Path(wdir)
call_git(['clone', '-q', '--depth', '1', repo_url, wdir])
call_git(['config', 'annex.private', 'true'], cwd=wdir)
# we need to fetch the git-annex branch to avoid it being created
# after the previous shallow clone
call_git(['fetch', '-q', 'origin', 'git-annex:git-annex', '--depth', '1'],
cwd=wdir)
call_git(['annex', 'init', '-q', '--no-autoenable'], cwd=wdir)
# wipe out all content
for p in wpath.iterdir():
if not p.is_dir() or p.name.startswith('.'):
continue
rmtree(p)
copytree(
source_dir,
wpath,
dirs_exist_ok=True,
# ignore anything that starts with a dot. This would be
# incomplete transfers. JTrack authors confirm that no
# data file has a leading dot in its name.
ignore=ignore_patterns('.*'),
)
# add anything that exists now, this will recreate the keys
# that already existed
call_git(['annex', 'add', '-q', '.'], cwd=wdir)
# now deal with deletions
call_git(['add', '-u'], cwd=wdir)
if not has_staged_changes(wpath):
# we are done, if there was no change, there will be nothing to
# push
return
call_git(
['-c', 'commit.gpgsign=false', 'commit', '-q', '-m', 'auto-ingest'],
cwd=wdir,
)
call_git(['annex', 'copy', '-q', '-t', 'origin', '.'], cwd=wdir)
# we need not push a git-annex branch, because the local annex is private
call_git(['push', '-q'], cwd=wdir)
if __name__ == "__main__":
update_jtrack_study_dataset()