def conda_check_versions_aligned(): # Next problem. If we use conda to provide our git or otherwise do not # have it on PATH and if we also have no .version file then conda is # unable to figure out its version without throwing an exception. The # tests this broke most badly (test_activate.py) have a workaround of # installing git into one of the conda prefixes that gets used but it # is slow. Instead write .version if it does not exist, and also fix # it if it disagrees. import conda version_file = normpath(join(dirname(conda.__file__), '.version')) version_from_file = open(version_file, 'rt').read().split('\n')[0] if isfile(version_file) else None git_exe = 'git.exe' if sys.platform == 'win32' else 'git' version_from_git = None for pe in os.environ.get('PATH', '').split(os.pathsep): if isfile(join(pe, git_exe)): try: version_from_git = check_output(join(pe, git_exe) + ' describe --tags --long').decode('utf-8').split('\n')[0] from conda._vendor.auxlib.packaging import _get_version_from_git_tag version_from_git = _get_version_from_git_tag(version_from_git) break except: continue if not version_from_git: print("WARNING :: Could not check versions.") if version_from_git and version_from_git != version_from_file: print("WARNING :: conda/.version ({}) and git describe ({}) disagree, rewriting .version".format( version_from_git, version_from_file)) with open(version_file, 'w') as fh: fh.write(version_from_git)
def conda_check_versions_aligned(): # Next problem. If we use conda to provide our git or otherwise do not # have it on PATH and if we also have no .version file then conda is # unable to figure out its version without throwing an exception. The # tests this broke most badly (test_activate.py) have a workaround of # installing git into one of the conda prefixes that gets used but it # is slow. Instead write .version if it does not exist, and also fix # it if it disagrees. version_file = normpath(join(dirname(dirname(__file__)), 'conda', '.version')) version_from_file = open(version_file, 'rt').read().split('\n')[0] if isfile(version_file) else None git_exe = 'git.exe' if sys.platform == 'win32' else 'git' version_from_git = None for pe in os.environ.get('PATH', '').split(os.pathsep): if isfile(join(pe, git_exe)): try: version_from_git = check_output(join(pe, git_exe) + ' describe --tags --long').decode('utf-8').split('\n')[0] from conda._vendor.auxlib.packaging import _get_version_from_git_tag version_from_git = _get_version_from_git_tag(version_from_git) break except: continue if not version_from_git: print("WARNING :: Could not check versions.") if version_from_git and version_from_git != version_from_file: print("WARNING :: conda/.version ({}) and git describe ({}) disagree, rewriting .version".format( version_from_git, version_from_file)) with open(version_file, 'w') as fh: fh.write(version_from_git)
def get_git_info(git_exe, repo, debug): """ Given a repo to a git repo, return a dictionary of: GIT_DESCRIBE_TAG GIT_DESCRIBE_TAG_PEP440 GIT_DESCRIBE_NUMBER GIT_DESCRIBE_HASH GIT_FULL_HASH GIT_BUILD_STR from the output of git describe. :return: """ d = {} log = utils.get_logger(__name__) if debug: stderr = None else: FNULL = open(os.devnull, 'w') stderr = FNULL # grab information from describe env = os.environ.copy() env['GIT_DIR'] = repo keys = ["GIT_DESCRIBE_TAG", "GIT_DESCRIBE_NUMBER", "GIT_DESCRIBE_HASH"] try: output = utils.check_output_env( [git_exe, "describe", "--tags", "--long", "HEAD"], env=env, cwd=os.path.dirname(repo), stderr=stderr).splitlines()[0] output = output.decode('utf-8') parts = output.rsplit('-', 2) if len(parts) == 3: d.update(dict(zip(keys, parts))) from conda._vendor.auxlib.packaging import _get_version_from_git_tag d['GIT_DESCRIBE_TAG_PEP440'] = str(_get_version_from_git_tag(output)) except subprocess.CalledProcessError: msg = ("Failed to obtain git tag information.\n" "Consider using annotated tags if you are not already " "as they are more reliable when used with git describe.") log.debug(msg) # If there was no tag reachable from HEAD, the above failed and the short hash is not set. # Try to get the short hash from describing with all refs (not just the tags). if "GIT_DESCRIBE_HASH" not in d: try: output = utils.check_output_env( [git_exe, "describe", "--all", "--long", "HEAD"], env=env, cwd=os.path.dirname(repo), stderr=stderr).splitlines()[0] output = output.decode('utf-8') parts = output.rsplit('-', 2) if len(parts) == 3: # Don't save GIT_DESCRIBE_TAG and GIT_DESCRIBE_NUMBER because git (probably) # described a branch. We just want to save the short hash. d['GIT_DESCRIBE_HASH'] = parts[-1] except subprocess.CalledProcessError as error: log.debug("Error obtaining git commit information. Error was: ") log.debug(str(error)) try: # get the _full_ hash of the current HEAD output = utils.check_output_env([git_exe, "rev-parse", "HEAD"], env=env, cwd=os.path.dirname(repo), stderr=stderr).splitlines()[0] output = output.decode('utf-8') d['GIT_FULL_HASH'] = output except subprocess.CalledProcessError as error: log.debug("Error obtaining git commit information. Error was: ") log.debug(str(error)) # set up the build string if "GIT_DESCRIBE_NUMBER" in d and "GIT_DESCRIBE_HASH" in d: d['GIT_BUILD_STR'] = '{}_{}'.format(d["GIT_DESCRIBE_NUMBER"], d["GIT_DESCRIBE_HASH"]) # issues on Windows with the next line of the command prompt being recorded here. assert not any("\n" in value for value in d.values()) return d