add functionality to automatically register when cache has an old version of an archived file

This commit is contained in:
Justus Kuhlmann 2025-11-21 21:50:59 +01:00
commit c9fe09d9d6
Signed by: jkuhl
GPG key ID: 00ED992DD79B85A6
5 changed files with 66 additions and 28 deletions

View file

@ -1,6 +1,19 @@
from typing import Union, Optional
import os
import shutil
from .tools import record2name_key
from pyerrors import dump_object
import datalad.api as dl
import sqlite3
def get_version_hash(path, record):
db = os.path.join(path, "backlogger.db")
dl.get(db, dataset=path)
conn = sqlite3.connect(db)
c = conn.cursor()
c.execute(f"SELECT current_version FROM 'backlogs' WHERE path = '{record}'")
return c.fetchall()[0][0]
def drop_cache_files(path: str, fs: Optional[list[str]]=None):
@ -19,15 +32,27 @@ def cache_dir(path, file):
return cache_path
def cache_path(path, file, hash, key):
cache_path = os.path.join(cache_dir(path, file), hash, key)
def cache_path(path, file, sha_hash, key):
cache_path = os.path.join(cache_dir(path, file), key + "_" + sha_hash)
return cache_path
def is_in_cache(path, record, hash):
if os.file.exists(cache_path(path, file, hash, key)):
return True
else:
return False
def is_old_version(path, record):
version_hash = get_version_hash(path, record)
file, key = record2name_key(record)
meas_cache_path = os.path.join(cache_dir(path, file))
ls = []
for p, ds, fs in os.walk(meas_cache_path):
ls.extend(fs)
for filename in ls:
if key == filename.split("_")[0]:
if not version_hash == filename.split("_")[1][:-2]:
return True
else:
return False
def is_in_cache(path, record):
version_hash = get_version_hash(path, record)
file, key = record2name_key(record)
return os.path.exists(cache_path(path, file, version_hash, key) + ".p")