diff options
author | Munyoki Kilyungi | 2024-04-17 14:33:20 +0300 |
---|---|---|
committer | BonfaceKilz | 2024-04-30 12:18:58 +0300 |
commit | e4cc7ffce07f8c33f42e2ec0257277897217c98b (patch) | |
tree | 69a121b22f3189fbfc85d0482474ce9abeb4b278 /gn3/db | |
parent | f389570cc325a712883b8895d3f3568ba766d55e (diff) | |
download | genenetwork3-e4cc7ffce07f8c33f42e2ec0257277897217c98b.tar.gz |
Add a method for saving a dataset's metadata to git.
* gn3/db/datasets.py (retrieve_metadata): New function.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/db')
-rw-r--r-- | gn3/db/datasets.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/gn3/db/datasets.py b/gn3/db/datasets.py index ef9c6be..fef01a5 100644 --- a/gn3/db/datasets.py +++ b/gn3/db/datasets.py @@ -5,9 +5,12 @@ import json from typing import Any from pathlib import Path +from pymonad.either import Either, Left, Right from flask import current_app as app +from gn3.commands import monadic_run_cmd + def retrieve_sample_list(group: str, inc_par: bool = True, inc_f1: bool = True): """ Get the sample list for a group (a category that datasets belong to) @@ -372,3 +375,36 @@ def retrieve_metadata(name: str) -> dict: with __file.open() as _f: result[__subject.get(__file.stem, f"gn:{__file.stem}")] = _f.read() return result + + +def save_metadata( + git_dir: str, output: str, + author: str, content: str, msg: str +) -> Either: + """Save dataset metadata to git""" + def __write__(): + try: + with Path(output).open(mode="w", encoding="utf8") as file_: + file_.write(content) + return Right(0) + except PermissionError as excpt: + return Left({ + "command": "Permission required to write to this file!", + "error": str(excpt) + }) + except IOError as excpt: + return Left({ + "command": "IOError when writing to this file!", + "error": str(excpt) + }) + return ( + monadic_run_cmd(f"git -C {git_dir} reset --hard origin".split(" ")) + .then(lambda _: monadic_run_cmd(f"git -C {git_dir} pull".split(" "))) + .then(lambda _: __write__()) + .then(lambda _: monadic_run_cmd(f"git -C {git_dir} add .".split(" "))) + .then(lambda _: monadic_run_cmd( + f"git -C {git_dir} commit -m".split(" ") + [ + f'{msg}', f"--author='{author}'", "--no-gpg-sign" + ])) + .then(lambda _: monadic_run_cmd(f"git -C {git_dir} \ +push origin master --dry-run".split(" ")))) |