From 2510856781eecafdfa113d8d6df48851134cd12d Mon Sep 17 00:00:00 2001 From: SONOLET Aymeric Date: Mon, 4 Dec 2023 16:19:11 +0100 Subject: [PATCH] Add git checkout and pull system call --- src/system.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/system.py b/src/system.py index 17a7c06..2ac5470 100644 --- a/src/system.py +++ b/src/system.py @@ -196,6 +196,78 @@ exit $res return rc.isOk() +def git_checkout(tag, git_options, where, logger, environment=None): + '''Checkout sources from a git repository. +87 + :param from_what str: The remote git repository. + :param tag str: The tag. + :param git_options str: git options + :param where str: The path where to extract. + :param logger Logger: The logger instance to use. + :param environment src.environment.Environ: The environment to source when extracting. + :return: True if the extraction is successful + :rtype: boolean + ''' + DBG.write("git_checkout", [tag, str(where)]) + if not where.exists(): + where.make() + where_git = os.path.join(str(where), ".git") + cmd = r""" +git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(git_options)s --guess %(tag)s +""" + cmd = cmd % {'git_options': git_options, 'tag': tag, 'where': str(where), 'where_git': where_git} + + cmd=cmd.replace('date_format', '"%ai"') + cmd=cmd.replace('--no-guess ', '') + launch_command(cmd, logger, where, environment) + +def git_pull(from_what, tag, git_options, where, logger, environment=None): + '''Checkout sources from a git repository. +87 + :param from_what str: The remote git repository. + :param tag str: The tag. + :param git_options str: git options + :param where str: The path where to extract. + :param logger Logger: The logger instance to use. + :param environment src.environment.Environ: The environment to source when extracting. + :return: True if the extraction is successful + :rtype: boolean + ''' + DBG.write("git_pull", [from_what, tag, str(where)]) + if not where.exists(): + where.make() + where_git = os.path.join(str(where), ".git") + cmd = r""" +git --git-dir=%(where_git)s --work-tree=%(where)s pull $(git_options)s --ff-only %(remote)s %(tag)s +""" + cmd = cmd % {'git_options': git_options, 'remote': from_what, 'tag': tag, 'where': str(where), 'where_git': where_git} + + cmd=cmd.replace('date_format', '"%ai"') + launch_command(cmd, logger, where, environment) + + +def launch_command(cmd, logger, where, environment): + logger.logTxtFile.write("\n" + cmd + "\n") + logger.logTxtFile.flush() + + DBG.write("cmd", cmd) + # git commands may fail sometimes for various raisons + # (big module, network troubles, tuleap maintenance) + # therefore we give several tries + i_try = 0 + max_number_of_tries = 3 + sleep_delay = 30 # seconds + while (True): + i_try += 1 + rc = UTS.Popen(cmd, cwd=str(where.dir()), env=environment.environ.environ, logger=logger) + if rc.isOk() or (i_try>=max_number_of_tries): + break + logger.write('\ngit command failed! Wait %d seconds and give an other try (%d/%d)\n' % \ + (sleep_delay, i_try + 1, max_number_of_tries), 3) + time.sleep(sleep_delay) # wait a little + + return rc.isOk() + def git_extract_sub_dir(from_what, tag, git_options, where, sub_dir, logger, environment=None): '''Extracts sources from a subtree sub_dir of a git repository. -- 2.39.2