# Buggy code
def __get_version(version, version_info):
    """
    If we can get a version provided at installation time or from Git, use
    that instead, otherwise we carry on.
    """
    try:
        # Try to import the version information provided at install time
        from salt._version import __version__, __version_info__  # pylint: disable=E0611

        return __version__, __version_info__
    except ImportError:
        pass

    # This might be a 'python setup.py develop' installation type. Let's
    # discover the version information at runtime.
    import os
    import warnings
    import subprocess

    try:
        cwd = os.path.abspath(os.path.dirname(__file__))
    except NameError:
        # We're most likely being frozen and __file__ triggered this NameError
        # Let's work around that
        import inspect

        cwd = os.path.abspath(os.path.dirname(inspect.getsourcefile(__get_version)))

    try:
        kwargs = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)

        if not sys.platform.startswith("win"):
            # Let's not import `salt.utils` for the above check
            kwargs["close_fds"] = True

        process = subprocess.Popen(
            ["git", "describe", "--tags", "--match", "v[0-9]*"], **kwargs
        )
        out, err = process.communicate()
        out = out.strip()
        err = err.strip()

        if not out or err:
            return version, version_info

        parsed_version = SaltStackVersion.parse(out)

        if parsed_version.info > version_info:
            warnings.warn(
                "The parsed version info, `{0}`, is bigger than the one "
                "defined in the file, `{1}`. Missing version bump?".format(
                    parsed_version.info, version_info
                ),
                UserWarning,
                stacklevel=2,
            )
            return version, version_info
        elif parsed_version.info < version_info:
            warnings.warn(
                "The parsed version info, `{0}`, is lower than the one "
                "defined in the file, `{1}`."
                "In order to get the proper salt version with the git hash "
                "you need to update salt's local git tags. Something like: "
                "'git fetch --tags' or 'git fetch --tags upstream' if "
                "you followed salt's contribute documentation. The version "
                "string WILL NOT include the git hash.".format(
                    parsed_version.info, version_info
                ),
                UserWarning,
                stacklevel=2,
            )
            return version, version_info
        return parsed_version.string, parsed_version.info
    except OSError as os_err:
        if os_err.errno != 2:
            # If the errno is not 2(The system cannot find the file
            # specified), raise the exception so it can be catch by the
            # developers
            raise
    return version, version_info


# Traceback (ValueError)
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import salt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/wari/venv/local/lib/python2.7/site-packages/salt-0.17.1-py2.7.egg/salt/__init__.py", line 27, in <module>
from salt.version import __version__
File "/home/wari/venv/local/lib/python2.7/site-packages/salt-0.17.1-py2.7.egg/salt/version.py", line 269, in <module>
__version__, __version_info__ = __get_version(__version__, __version_info__)
File "/home/wari/venv/local/lib/python2.7/site-packages/salt-0.17.1-py2.7.egg/salt/version.py", line 229, in __get_version
parsed_version = SaltStackVersion.parse(out)
File "/home/wari/venv/local/lib/python2.7/site-packages/salt-0.17.1-py2.7.egg/salt/version.py", line 83, in parse
'Unable to parse version string: {0!r}'.format(version_string)
ValueError: Unable to parse version string: 'v20131009-9-g40fa813'

# Fixed code
def __get_version(version, version_info):
    """
    If we can get a version provided at installation time or from Git, use
    that instead, otherwise we carry on.
    """
    try:
        # Try to import the version information provided at install time
        from salt._version import __version__, __version_info__  # pylint: disable=E0611

        return __version__, __version_info__
    except ImportError:
        pass

    # This might be a 'python setup.py develop' installation type. Let's
    # discover the version information at runtime.
    import os
    import warnings
    import subprocess

    try:
        cwd = os.path.abspath(os.path.dirname(__file__))
    except NameError:
        # We're most likely being frozen and __file__ triggered this NameError
        # Let's work around that
        import inspect

        cwd = os.path.abspath(os.path.dirname(inspect.getsourcefile(__get_version)))

    if not os.path.exists(os.path.join(os.path.dirname(cwd), ".git")):
        # This is not a Salt git checkout!!! Don't even try to parse...
        return version, version_info

    try:
        kwargs = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)

        if not sys.platform.startswith("win"):
            # Let's not import `salt.utils` for the above check
            kwargs["close_fds"] = True

        process = subprocess.Popen(
            ["git", "describe", "--tags", "--match", "v[0-9]*"], **kwargs
        )
        out, err = process.communicate()
        out = out.strip()
        err = err.strip()

        if not out or err:
            return version, version_info

        parsed_version = SaltStackVersion.parse(out)

        if parsed_version.info > version_info:
            warnings.warn(
                "The parsed version info, `{0}`, is bigger than the one "
                "defined in the file, `{1}`. Missing version bump?".format(
                    parsed_version.info, version_info
                ),
                UserWarning,
                stacklevel=2,
            )
            return version, version_info
        elif parsed_version.info < version_info:
            warnings.warn(
                "The parsed version info, `{0}`, is lower than the one "
                "defined in the file, `{1}`."
                "In order to get the proper salt version with the git hash "
                "you need to update salt's local git tags. Something like: "
                "'git fetch --tags' or 'git fetch --tags upstream' if "
                "you followed salt's contribute documentation. The version "
                "string WILL NOT include the git hash.".format(
                    parsed_version.info, version_info
                ),
                UserWarning,
                stacklevel=2,
            )
            return version, version_info
        return parsed_version.string, parsed_version.info
    except OSError as os_err:
        if os_err.errno != 2:
            # If the errno is not 2(The system cannot find the file
            # specified), raise the exception so it can be catch by the
            # developers
            raise
    return version, version_info