def get_current_version(project_name=None, project_dir=os.curdir,
                        repo_dir=None):
    """
    Retrieves the version of the package, checking in this order of priority:

    * From an environment variable named ${project_name}_VERSION (all in caps)
      if project_name was specified.
    * From the PKG-INFO file if inside a packaged distro.
    * From the git history.

    Args:
        project_name(str): Name of the project to get the version for, if none
            passed, will not use any environment variable override.

    Returns:
        str: Version for the package.

    Raises:
        RuntimeError: If the version could not be retrieved.
    """
    if project_name is not None:
        version_env_var = '%s_VERSION' % project_name.upper()

        if version_env_var in os.environ and os.environ[version_env_var]:
            return os.environ[version_env_var]

    version = None
    repo_dir = repo_dir or project_dir
    pkg_info_file = os.path.join(project_dir, 'PKG-INFO')
    if os.path.exists(pkg_info_file):
        with open(pkg_info_file) as info_fd:
            for line in info_fd.readlines():
                if line.startswith('Version: '):
                    version = line.split(' ', 1)[-1]

    if version is None:
        try:
            version = api.get_current_version(repo_path=repo_dir)
        except:
            pass

    if version is None:
        if project_name:
            try:
                distribution = pkg_resources.get_distribution(project_name)
                if distribution.has_version():
                    version = distribution.version
            except:
                pass

    if version is None:
        raise RuntimeError('Failed to get package version')

    # py3 compatibility step
    if not isinstance(version, str) and isinstance(version, bytes):
        version = api._to_str(version)

    return version
def get_current_version ( project_name = None , project_dir = os . curdir , repo_dir = None ) : if project_name is not None : version_env_var = '%s_VERSION' % project_name . upper ( ) if version_env_var in os . environ and os . environ [ version_env_var ] : return os . environ [ version_env_var ] version = None repo_dir = repo_dir or project_dir pkg_info_file = os . path . join ( project_dir , 'PKG-INFO' ) if os . path . exists ( pkg_info_file ) : with open ( pkg_info_file ) as info_fd : for line in info_fd . readlines ( ) : if line . startswith ( 'Version: ' ) : version = line . split ( ' ' , 1 ) [ - 1 ] if version is None : try : version = api . get_current_version ( repo_path = repo_dir ) except : pass if version is None : if project_name : try : distribution = pkg_resources . get_distribution ( project_name ) if distribution . has_version ( ) : version = distribution . version except : pass if version is None : raise RuntimeError ( 'Failed to get package version' ) if not isinstance ( version , str ) and isinstance ( version , bytes ) : version = api . _to_str ( version ) return version
Retrieves the version of the package, checking in this order of priority:

    * From an environment variable named ${project_name}_VERSION (all in caps)
      if project_name was specified.
    * From the PKG-INFO file if inside a packaged distro.
    * From the git history.

    Args:
        project_name(str): Name of the project to get the version for, if none
            passed, will not use any environment variable override.

    Returns:
        str: Version for the package.

    Raises:
        RuntimeError: If the version could not be retrieved.