From c2a56125a46071c018192a013140d9cee1c6cf77 Mon Sep 17 00:00:00 2001 From: Ulrich Date: Mon, 5 Sep 2022 18:48:35 +0200 Subject: [PATCH] git tool created --- .idea/vcs.xml | 6 --- test/test_01date.py | 14 ++++++- test/test_09git.py | 60 ++++++++++++++++++++++++++++++ utils/date_tool.py | 27 +++++++++++++- utils/git_tool.py | 89 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 8 deletions(-) delete mode 100644 .idea/vcs.xml create mode 100644 test/test_09git.py create mode 100644 utils/git_tool.py diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/test/test_01date.py b/test/test_01date.py index ea5dcd6..de47f5f 100644 --- a/test/test_01date.py +++ b/test/test_01date.py @@ -5,7 +5,7 @@ import datetime import utils.date_tool TEST_FUNCTIONS = ["test_dateformat", "test_parseFormula", "test_parseDate"] -#TEST_FUNCTIONS = ["test_parseFormula"] +TEST_FUNCTIONS = ["test_parseDate"] verbose = True @@ -87,6 +87,18 @@ class MyTestCase(unittest.TestCase): if verbose: print(str(res)) self.assertEqual(res[0], 2013) self.assertEqual(res[3], 0) + res = utils.date_tool.parseDate("Wed May 18 23:32:55 2022 +0200") + if verbose: print(str(res)) + self.assertEqual(res[0], 2022) + self.assertEqual(res[1], 5) + res = utils.date_tool.parseDate("Mit Dez 18 23:32:55 2021 +0200") + if verbose: print(str(res)) + self.assertEqual(res[0], 2021) + self.assertEqual(res[1], 12) + res = utils.date_tool.parseDate("Sun Sep 4 15:12:21 2022 +0200") + if verbose: print(str(res)) + self.assertEqual(res[0], 2022) + self.assertEqual(res[1], 9) MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) if __name__ == '__main__': diff --git a/test/test_09git.py b/test/test_09git.py new file mode 100644 index 0000000..81877f4 --- /dev/null +++ b/test/test_09git.py @@ -0,0 +1,60 @@ +import unittest +import os +import inspect +import utils.path_tool +import basic.message +import basic.program +import basic.constants as B +import test.constants +import test.testtools +import utils.path_const as P +import utils.git_tool + +HOME_PATH = test.constants.HOME_PATH +OS_SYSTEM = test.constants.OS_SYSTEM + +# here you can select single testfunction for developping the tests +TEST_FUNCTIONS = ["test_01run", "test_02status", "test_03log"] +#TEST_FUNCTIONS = [ "test_01status"] +verbose = False + +class MyTestCase(unittest.TestCase): + mymsg = "--------------------------------------------------------------" + + def test_01run(self): + global mymsg + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + job = test.testtools.getJob() + utils.git_tool.runGit(job, B.ATTR_PATH_PROGRAM, "git status") + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + def test_02status(self): + global mymsg + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + job = test.testtools.getJob() + utils.git_tool.gitStatus(job, B.ATTR_PATH_PROGRAM) + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + def test_03log(self): + global mymsg + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + job = test.testtools.getJob() + utils.git_tool.gitLog(job, B.ATTR_PATH_COMPS) + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + def test_zzz(self): + if verbose: print(MyTestCase.mymsg) + + +if __name__ == '__main__': + verbose = True + unittest.main() diff --git a/utils/date_tool.py b/utils/date_tool.py index 0533d79..ad74882 100644 --- a/utils/date_tool.py +++ b/utils/date_tool.py @@ -13,7 +13,8 @@ F_DIR = "%Y-%m-%d_%H-%M-%S" F_DB_DATE = "%Y-%m-%d" F_DE = "%d.%m.%Y" F_N8 = "%Y%m%d" - +MONTH_EN = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"] +MONTH_DE = ["jan", "feb", "mar", "apr", "mai", "jun", "jul", "aug", "sep", "okt", "nov", "dez"] def getActdate(format): return getFormatdate(datetime.datetime.now(), format) @@ -85,6 +86,19 @@ def parseFormula(instring): print("re matcht nicht") return (year, mon, day, hour, min, sec) +def getMonthInt(instring): + i = 0 + j = 0 + for l in [MONTH_EN, MONTH_DE]: + i = 0 + for m in l: + i += 1 + if instring.lower() == m: + j = i + break + if j > 0: + break + return j def parseDate(instring): """ @@ -98,6 +112,7 @@ def parseDate(instring): hour = 0 min = 0 sec = 0 + print(instring) if instring[0:2] == "{(" and instring[-2:] == ")}": return parseFormula(instring) if len(instring) > 8: @@ -120,6 +135,16 @@ def parseDate(instring): mon = int(res.group(2)) day = int(res.group(1)) return (year, mon, day, hour, min, sec) + if re.match(r"\w{3} \w{3} \d{1,2} \d{1,2}[:]\d{1,2}[:]\d{2} \d{4}", instring.strip()): + res = re.search(r"\w{3} (\w{3}) (\d{1,2}) (\d{1,2})[:](\d{1,2})[:](\d{2}) (\d{4})", instring.strip()) + month = res.group(1) + mon = getMonthInt(month) + day = int(res.group(2)) + hour = int(res.group(3)) + min = int(res.group(4)) + sec = int(res.group(5)) + year = int(res.group(6)) + return (year, mon, day, hour, min, sec) if re.match(r"\d{8}", instring): year = instring[0:4] mon = instring[4:6] diff --git a/utils/git_tool.py b/utils/git_tool.py new file mode 100644 index 0000000..0883b7e --- /dev/null +++ b/utils/git_tool.py @@ -0,0 +1,89 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# --------------------------------------------------------------------------------------------------------- +# Author : Ulrich Carmesin +# Source : gitea.ucarmesin.de +# --------------------------------------------------------------------------------------------------------- +import os +import re +import subprocess +import sys +import basic.toolHandling +import utils.data_const as D +import basic.constants as B +import basic.text_const as T +import utils.date_tool + +DEFAULT_CNT_COMMITS = 10 +COMMIT_ID = "commit" +COMMIT_AUTHOR = "author" +COMMIT_DATE = "date" +COMMIT_COMMENT = "comment" + +def runGit(job, repo, cmd): + cdpath = "" + if os.path.isdir(repo): + cdpath = repo + elif repo in job.conf.confs[B.SUBJECT_PATH]: + cdpath = job.conf.confs[B.SUBJECT_PATH][repo] + else: + raise Exception(T.EXP_PATH_MISSING, repo) + os.chdir(cdpath) + text = "" + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + btext = process.communicate()[0] + text = btext.decode('utf-8') + return text + +def gitStatus(job, repo): + text = runGit(job, repo, "git status") + +def gitLog(job, repo, arg="", cnt=DEFAULT_CNT_COMMITS): + """ + extracts the last cnt commits into th structure + :param job: + :param repo: + :param cnt: + :return: [ {commit: "", author: ] + """ + text = runGit(job, repo, "git log") + if len(arg) > 1: + arg = " -- "+arg + else: + arg = "" + text = runGit(job, repo, "git log --pretty=format:\"%H | %cn | %cd | %s\""+arg) + print(text) + logs = [] + for l in text.split("\n"): + res = {} + a = l.split("|") + res[COMMIT_ID] = a[0].strip() + res[COMMIT_AUTHOR] = a[1].strip() + cdate = utils.date_tool.parseDate(a[2].strip()) + res[COMMIT_DATE] = utils.date_tool.getFormatDatetupel(cdate, utils.date_tool.F_DIR) + res[COMMIT_COMMENT] = a[3].strip() + logs.append(res) + print(str(logs)) + + +def gitCommits(job, repo, arg=""): + if len(arg) > 1: + arg = " -- "+arg + else: + arg = "" + text = runGit(job, repo, "git log --pretty=format:\"%H | %cn | %cd | %s\""+arg) + print(text) + +def gitPull(job, repo): + if "git" not in job.conf.confs[B.SUBJECT_TOOL] or repo not in job.conf.confs[B.SUBJECT_TOOL]["git"]: + raise Exception(T.EXP_CONFIG_MISSING, "tool.git."+repo) + #print(str(job.conf.confs[B.SUBJECT_TOOL]["git"][repo])) + master = job.conf.confs[B.SUBJECT_TOOL]["git"][repo]["master"] + remote = job.conf.confs[B.SUBJECT_TOOL]["git"][repo]["remote"] + text = runGit(job, repo, "git status") + if "Commit vor" in text: + print("Das Repository "+repo+" bitte zuerst pushen \n") + #print(text+"\"-------------------------------------\n") + else: + print("Repository "+repo+" kann gepullt werden.") + #runGit(job, repo, "git pull "+remote+" "+master) \ No newline at end of file