diff --git a/CHANGELOG.md b/CHANGELOG.md index d6f7c5f123942a22cc7de63424bd7fb7ea597569..cd37b71e60f243c5504fd61401e393c6e9eb1f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated ### +- getOriginUrlIn, getDiffIn, getBranchIn, getCommitIn (formerly apiutils) have been + moved to caosdb.utils.git_utils + ### Removed ### ### Fixed ### diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index a5a936c556dd065b56b60ff690baf9a1ce19a583..a46e30375b924d358448e73aece61562c36c700b 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -27,21 +27,20 @@ """ import logging -import sys -import tempfile import warnings from collections.abc import Iterable -from subprocess import call -from typing import Optional, Any, Dict, List +from typing import Any, Dict, List -from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER, - REFERENCE, TEXT, is_reference) -from caosdb.common.models import (Container, Entity, File, Property, Query, +from caosdb.common.datatype import is_reference +from caosdb.common.models import (Container, Entity, File, Property, Record, RecordType, execute_query, - get_config, SPECIAL_ATTRIBUTES) + SPECIAL_ATTRIBUTES) from caosdb.exceptions import CaosDBException +from caosdb.utils.git_utils import (get_origin_url_in, get_diff_in, + get_branch_in, get_commit_in) + logger = logging.getLogger(__name__) @@ -148,51 +147,35 @@ def retrieve_entities_with_ids(entities): def getOriginUrlIn(folder): - """return the Fetch URL of the git repository in the given folder.""" - with tempfile.NamedTemporaryFile(delete=False, mode="w") as t: - call(["git", "remote", "show", "origin"], stdout=t, cwd=folder) - with open(t.name, "r") as t: - urlString = "Fetch URL:" - - for line in t.readlines(): - if urlString in line: - return line[line.find(urlString) + len(urlString):].strip() - - return None + warnings.warn(""" + This function is deprecated and will be removed with the next release. + Please use the caosdb.utils.git_utils.get_origin_url_in instead.""", + DeprecationWarning) + return get_origin_url_in(folder) def getDiffIn(folder, save_dir=None): - """returns the name of a file where the out put of "git diff" in the given - folder is stored.""" - with tempfile.NamedTemporaryFile(delete=False, mode="w", dir=save_dir) as t: - call(["git", "diff"], stdout=t, cwd=folder) - - return t.name + warnings.warn(""" + This function is deprecated and will be removed with the next release. + Please use the caosdb.utils.git_utils.get_diff_in instead.""", + DeprecationWarning) + return get_diff_in(folder, save_dir) def getBranchIn(folder): - """returns the current branch of the git repository in the given folder. - - The command "git branch" is called in the given folder and the - output is returned - """ - with tempfile.NamedTemporaryFile(delete=False, mode="w") as t: - call(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=t, cwd=folder) - with open(t.name, "r") as t: - return t.readline().strip() + warnings.warn(""" + This function is deprecated and will be removed with the next release. + Please use the caosdb.utils.git_utils.get_branch_in instead.""", + DeprecationWarning) + return get_branch_in(folder) def getCommitIn(folder): - """returns the commit hash in of the git repository in the given folder. - - The command "git log -1 --format=%h" is called in the given folder - and the output is returned - """ - - with tempfile.NamedTemporaryFile(delete=False, mode="w") as t: - call(["git", "log", "-1", "--format=%h"], stdout=t, cwd=folder) - with open(t.name, "r") as t: - return t.readline().strip() + warnings.warn(""" + This function is deprecated and will be removed with the next release. + Please use the caosdb.utils.git_utils.get_commit_in instead.""", + DeprecationWarning) + return get_commit_in(folder) def compare_entities(old_entity: Entity, new_entity: Entity, compare_referenced_records: bool = False): diff --git a/src/caosdb/utils/git_utils.py b/src/caosdb/utils/git_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..98408fe22023e182fe9c9906a17f7d1a414dc5b3 --- /dev/null +++ b/src/caosdb/utils/git_utils.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +# +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2018 Research Group Biomedical Physics, +# Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com> +# Copyright (C) 2020-2022 IndiScale GmbH <info@indiscale.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +# ** end header +# +"""git-utils: Some functions for retrieving information about git repositories. + +""" + +import logging +import tempfile + +from subprocess import call + +logger = logging.getLogger(__name__) + + + + +def get_origin_url_in(folder: str): + """return the Fetch URL of the git repository in the given folder.""" + with tempfile.NamedTemporaryFile(delete=False, mode="w") as t: + call(["git", "remote", "show", "origin"], stdout=t, cwd=folder) + with open(t.name, "r") as t: + urlString = "Fetch URL:" + + for line in t.readlines(): + if urlString in line: + return line[line.find(urlString) + len(urlString):].strip() + + return None + + +def get_diff_in(folder: str, save_dir=None): + """returns the name of a file where the out put of "git diff" in the given + folder is stored.""" + with tempfile.NamedTemporaryFile(delete=False, mode="w", dir=save_dir) as t: + call(["git", "diff"], stdout=t, cwd=folder) + + return t.name + + +def get_branch_in(folder: str): + """returns the current branch of the git repository in the given folder. + + The command "git branch" is called in the given folder and the + output is returned + """ + with tempfile.NamedTemporaryFile(delete=False, mode="w") as t: + call(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=t, cwd=folder) + with open(t.name, "r") as t: + return t.readline().strip() + + +def get_commit_in(folder: str): + """returns the commit hash in of the git repository in the given folder. + + The command "git log -1 --format=%h" is called in the given folder + and the output is returned + """ + + with tempfile.NamedTemporaryFile(delete=False, mode="w") as t: + call(["git", "log", "-1", "--format=%h"], stdout=t, cwd=folder) + with open(t.name, "r") as t: + return t.readline().strip()