127 lines
4.6 KiB
Python
127 lines
4.6 KiB
Python
import sqlite3
|
|
import datalad.api as dl
|
|
import datalad.config as dlc
|
|
import os
|
|
from .git_tools import move_submodule
|
|
import shutil
|
|
from .find import _project_lookup_by_id
|
|
from .tools import list2str, str2list
|
|
from typing import Union
|
|
|
|
|
|
def create_project(path: str, uuid: str, owner: Union[str, None]=None, tags: Union[str, None]=None, aliases: Union[str, None]=None, code: Union[str, None]=None):
|
|
"""
|
|
Create a new project entry in the database.
|
|
|
|
Parameters
|
|
----------
|
|
path: str
|
|
The path to the backlogger folder.
|
|
uuid: str
|
|
The uuid of the project.
|
|
name: str (optional)
|
|
Costum name for the project (e.g. 'cA determination on exponential clover').
|
|
code: str (optional)
|
|
The code that was used to create the measurements.
|
|
"""
|
|
conn = sqlite3.connect(path + "/backlogger.db")
|
|
c = conn.cursor()
|
|
known_projects = c.execute("SELECT * FROM projects WHERE id=?", (uuid,))
|
|
if known_projects.fetchone():
|
|
raise ValueError("Project already imported, use update_project() instead.")
|
|
|
|
dl.unlock(path + "/backlogger.db", dataset=path)
|
|
alias_str = None
|
|
if aliases is not None:
|
|
alias_str = list2str(aliases)
|
|
tag_str = None
|
|
if tags is not None:
|
|
tag_str = list2str(tags)
|
|
c.execute("INSERT INTO projects (id, aliases, customTags, owner, code, created_at, updated_at) VALUES (?, ?, ?, ?, ?, datetime('now'), datetime('now'))", (uuid, alias_str, tag_str, owner, code))
|
|
conn.commit()
|
|
conn.close()
|
|
dl.save(path + "/backlogger.db", message="Added entry for project " + uuid + " to database", dataset=path)
|
|
|
|
|
|
def update_project_data(db, uuid, prop, value = None):
|
|
conn = sqlite3.connect(db)
|
|
c = conn.cursor()
|
|
c.execute(f"UPDATE projects SET '{prop}' = '{value}' WHERE id == '{uuid}'")
|
|
conn.commit()
|
|
conn.close()
|
|
return
|
|
|
|
|
|
def update_aliases(path, uuid, aliases):
|
|
known_data = _project_lookup_by_id(path + "/backlogger.db", uuid)[0]
|
|
known_aliases = known_data[1]
|
|
if aliases is None:
|
|
aliases = []
|
|
if known_aliases is None:
|
|
print(f"Project {uuid} is already imported, no known aliases.")
|
|
known_alias_list = []
|
|
else:
|
|
print(f"Project {uuid} is already imported, known by names: {known_aliases}")
|
|
known_alias_list = str2list(known_aliases)
|
|
new_alias_list = known_alias_list
|
|
for aka in aliases:
|
|
if aka not in known_alias_list:
|
|
new_alias_list.append(aka)
|
|
if not len(new_alias_list) == len(known_alias_list):
|
|
alias_str = list2str(new_alias_list)
|
|
update_project_data(path, uuid, "aliases", alias_str)
|
|
return
|
|
|
|
|
|
def import_project(path: str, url: str, owner: Union[str, None]=None, tags: Union[str, None]=None, aliases: Union[str, None]=None, code: Union[str, None]=None, isDataset: bool=True):
|
|
"""
|
|
Parameters
|
|
----------
|
|
|
|
url: str
|
|
The url of the project to import. This can be any url that datalad can handle.
|
|
path: str
|
|
The path to the backlogger folder.
|
|
aliases: list[str]
|
|
Custom name of the project, alias of the project.
|
|
code: str
|
|
Code that was used to create the measurements.
|
|
|
|
Import a datalad dataset into the backlogger.
|
|
|
|
Parameters
|
|
----------
|
|
path: str
|
|
The path to the backlogger directory.
|
|
url: str
|
|
The url of the project to import. This can be any url that datalad can handle.
|
|
Also supported are non-datalad datasets, which will be converted to datalad datasets,
|
|
in order to receive a uuid and have a consistent interface.
|
|
|
|
"""
|
|
tmp_path = path + '/projects/tmp'
|
|
if not isDataset:
|
|
dl.create(tmp_path, dataset=path)
|
|
shutil.copytree(url + "/*", path + '/projects/tmp/')
|
|
dl.save(tmp_path, dataset=path)
|
|
else:
|
|
dl.install(path=tmp_path, source=url, dataset=path)
|
|
tmp_ds = dl.Dataset(tmp_path)
|
|
conf = dlc.ConfigManager(tmp_ds)
|
|
uuid = conf.get("datalad.dataset.id")
|
|
if not uuid:
|
|
raise ValueError("The dataset does not have a uuid!")
|
|
if not os.path.exists(path + "/projects/" + uuid):
|
|
dl.unlock(path + "/backlogger.db", dataset=path)
|
|
create_project(path, uuid, owner, tags, aliases, code)
|
|
move_submodule(path, 'projects/tmp', 'projects/' + uuid)
|
|
os.mkdir(path + '/import_scripts/' + uuid)
|
|
dl.save([path + "/backlogger.db", path + '/projects/' + uuid], message="Import project from " + url, dataset=path)
|
|
else:
|
|
dl.drop(tmp_path, reckless='kill')
|
|
shutil.rmtree(tmp_path)
|
|
if aliases is not None:
|
|
update_aliases(path, uuid, aliases)
|
|
|
|
# make this more concrete
|
|
return uuid
|