X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2Fsystem.py;h=6b793397f12e8280bece2dd60d28b37caaadbe4c;hb=d828b7da029994130b3717855155e49d58ee87ea;hp=1969bacaa22c914738a333cadc5cfb8607252b38;hpb=96f2d4485beb23ea58cbeced0b357e82ea5544bb;p=tools%2Fsat.git diff --git a/src/system.py b/src/system.py index 1969bac..6b79339 100644 --- a/src/system.py +++ b/src/system.py @@ -21,10 +21,14 @@ In this file : all functions that do a system call, like open a browser or an editor, or call a git command ''' -import subprocess import os +import subprocess +import time import tarfile +import debug as DBG +import utilsSat as UTS + from . import printcolors def show_in_editor(editor, filePath, logger): @@ -50,40 +54,131 @@ def show_in_editor(editor, filePath, logger): logger.write(printcolors.printcError(_("Unable to edit file %s\n") % filePath), 1) - -def git_extract(from_what, tag, where, logger): - '''Extracts sources from a git repository. - - :param from_what str: The remote git repository. - :param tag str: The tag. - :param where str: The path where to extract. - :param logger Logger: The logger instance to use. - :return: True if the extraction is successful - :rtype: boolean +def git_describe(repo_path): + '''Use git describe --tags command to return tag description of the git repository" + :param repo_path str: The git repository to describe ''' - if not where.exists(): - where.make() - if tag == "master" or tag == "HEAD": - command = "git clone %(remote)s %(where)s" % \ - { 'remote': from_what, 'tag': tag, 'where': str(where) } + git_cmd="cd %s;git describe --tags" % repo_path + p = subprocess.Popen(git_cmd, shell=True, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + p.wait() + if p.returncode != 0: + return False else: - # NOTICE: this command only works with recent version of git - # because --work-tree does not work with an absolute path - where_git = os.path.join( str(where), ".git" ) - command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \ - "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s" - command = command % {'remote': from_what, - 'tag': tag, - 'where': str(where), - 'where_git': where_git } + return p.stdout.readlines()[0].strip() - logger.write(command + "\n", 5) - logger.logTxtFile.write("\n" + command + "\n") - logger.logTxtFile.flush() - res = subprocess.call(command, cwd=str(where.dir()), shell=True, - stdout=logger.logTxtFile, stderr=subprocess.STDOUT) - return (res == 0) +def git_extract(from_what, tag, where, logger, environment=None): + '''Extracts sources from a git repository. + + :param from_what str: The remote git repository. + :param tag str: The tag. + :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_extract", [from_what, tag, str(where)]) + if not where.exists(): + where.make() + if tag == "master" or tag == "HEAD": + cmd = r""" +set -x +git clone %(remote)s %(where)s +""" + cmd = cmd % {'remote': from_what, 'tag': tag, 'where': str(where)} + else: + # NOTICE: this command only works with recent version of git + # because --work-tree does not work with an absolute path + where_git = os.path.join(str(where), ".git") + + cmd = r""" +set -x +rmdir %(where)s +git clone %(remote)s %(where)s && \ +git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s +""" + cmd = cmd % {'remote': from_what, + 'tag': tag, + 'where': str(where), + 'where_git': where_git} + + + 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, where, sub_dir, logger, environment=None): + '''Extracts sources from a subtree sub_dir of a git repository. + + :param from_what str: The remote git repository. + :param tag str: The tag. + :param where str: The path where to extract. + :param sub_dir str: The relative path of subtree 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 + ''' + strWhere = str(where) + tmpWhere = strWhere + '_tmp' + parentWhere = os.path.dirname(strWhere) + if not os.path.exists(parentWhere): + logger.error("not existing directory: %s" % parentWhere) + return False + if os.path.isdir(strWhere): + logger.error("do not override existing directory: %s" % strWhere) + return False + aDict = {'remote': from_what, + 'tag': tag, + 'sub_dir': sub_dir, + 'where': strWhere, + 'parentWhere': parentWhere, + 'tmpWhere': tmpWhere, + } + DBG.write("git_extract_sub_dir", aDict) + + cmd = r""" +set -x +export tmpDir=%(tmpWhere)s && \ +rm -rf $tmpDir +git clone %(remote)s $tmpDir && \ +cd $tmpDir && \ +git checkout %(tag)s && \ +mv %(sub_dir)s %(where)s && \ +git log -1 > %(where)s/README_git_log.txt && \ +rm -rf $tmpDir +""" % aDict + DBG.write("cmd", cmd) + + for nbtry in range(0,3): # retries case of network problem + rc = UTS.Popen(cmd, cwd=parentWhere, env=environment.environ.environ, logger=logger) + if rc.isOk(): break + time.sleep(30) # wait a little + + return rc.isOk() def archive_extract(from_what, where, logger): '''Extracts sources from an archive. @@ -104,7 +199,7 @@ def archive_extract(from_what, where, logger): return False, None def cvs_extract(protocol, user, server, base, tag, product, where, - logger, checkout=False): + logger, checkout=False, environment=None): '''Extracts sources from a cvs repository. :param protocol str: The cvs protocol. @@ -116,6 +211,8 @@ def cvs_extract(protocol, user, server, base, tag, product, where, :param where str: The path where to extract. :param logger Logger: The logger instance to use. :param checkout boolean: If true use checkout cvs. + :param environment src.environment.Environ: The environment to source when + extracting. :return: True if the extraction is successful :rtype: boolean ''' @@ -147,11 +244,21 @@ def cvs_extract(protocol, user, server, base, tag, product, where, logger.logTxtFile.write("\n" + command + "\n") logger.logTxtFile.flush() - res = subprocess.call(command, cwd=str(where.dir()), shell=True, - stdout=logger.logTxtFile, stderr=subprocess.STDOUT) + res = subprocess.call(command, + cwd=str(where.dir()), + env=environment.environ.environ, + shell=True, + stdout=logger.logTxtFile, + stderr=subprocess.STDOUT) return (res == 0) -def svn_extract(user, from_what, tag, where, logger, checkout=False): +def svn_extract(user, + from_what, + tag, + where, + logger, + checkout=False, + environment=None): '''Extracts sources from a svn repository. :param user str: The user to be used. @@ -160,6 +267,8 @@ def svn_extract(user, from_what, tag, where, logger, checkout=False): :param where str: The path where to extract. :param logger Logger: The logger instance to use. :param checkout boolean: If true use checkout svn. + :param environment src.environment.Environ: The environment to source when + extracting. :return: True if the extraction is successful :rtype: boolean ''' @@ -187,6 +296,10 @@ def svn_extract(user, from_what, tag, where, logger, checkout=False): logger.write(command + "\n", 5) logger.logTxtFile.write("\n" + command + "\n") logger.logTxtFile.flush() - res = subprocess.call(command, cwd=str(where.dir()), shell=True, - stdout=logger.logTxtFile, stderr=subprocess.STDOUT) - return (res == 0) \ No newline at end of file + res = subprocess.call(command, + cwd=str(where.dir()), + env=environment.environ.environ, + shell=True, + stdout=logger.logTxtFile, + stderr=subprocess.STDOUT) + return (res == 0)