# Buggy code
def _check_pkg_version_format(pkg):
    """
    Takes a package name and version specification (if any) and checks it using
    the pip library.
    """

    ret = {"result": False, "comment": None, "prefix": None, "version_spec": None}

    if not HAS_PIP:
        ret["comment"] = (
            "An importable pip module is required but could not be found on "
            "your system. This usually means that the system"
            "s pip package "
            "is not installed properly."
        )

        return ret

    from_vcs = False
    try:
        # Get the requirement object from the pip library
        try:
            # With pip < 1.2, the __version__ attribute does not exist and
            # vcs+URL urls are not properly parsed.
            # The next line is meant to trigger an AttributeError and
            # handle lower pip versions
            logger.debug("Installed pip version: {0}".format(pip.__version__))
            install_req = pip.req.InstallRequirement.from_line(pkg)
        except AttributeError:
            logger.debug("Installed pip version is lower than 1.2")
            supported_vcs = ("git", "svn", "hg", "bzr")
            if pkg.startswith(supported_vcs):
                for vcs in supported_vcs:
                    if pkg.startswith(vcs):
                        from_vcs = True
                        install_req = pip.req.InstallRequirement.from_line(
                            pkg.split("{0}+".format(vcs))[-1]
                        )
                        break
            else:
                install_req = pip.req.InstallRequirement.from_line(pkg)
    except ValueError as exc:
        ret["result"] = False
        if not from_vcs and "=" in pkg and "==" not in pkg:
            ret["comment"] = (
                "Invalid version specification in package {0}. '=' is "
                "not supported, use '==' instead.".format(pkg)
            )
            return ret
        ret["comment"] = "pip raised an exception while parsing {0!r}: {1}".format(
            pkg, exc
        )
        return ret

    if install_req.req is None:
        # This is most likely an url and there's no way to know what will
        # be installed before actually installing it.
        ret["result"] = True
        ret["prefix"] = ""
        ret["version_spec"] = []
    else:
        ret["result"] = True
        ret["prefix"] = install_req.req.project_name
        ret["version_spec"] = install_req.req.specs

    return ret


# Traceback (ConfigParser.MissingSectionHeaderError)
----------
ID: install_tbzroottools
Function: pip.installed
Name: package
Result: None
Comment: Error installing 'package': Traceback (most recent call last):
File "/usr/bin/pip2", line 9, in <module>
load_entry_point('pip==7.1.2', 'console_scripts', 'pip2')()
File "/usr/lib/python2.7/site-packages/pip/__init__.py", line 210, in main
cmd_name, cmd_args = parseopts(args)
File "/usr/lib/python2.7/site-packages/pip/__init__.py", line 146, in parseopts
parser = create_main_parser()
File "/usr/lib/python2.7/site-packages/pip/__init__.py", line 124, in create_main_parser
parser = ConfigOptionParser(**parser_kw)
File "/usr/lib/python2.7/site-packages/pip/baseparser.py", line 147, in __init__
self.config.read(self.files)
File "/usr/lib64/python2.7/ConfigParser.py", line 305, in read
self._read(fp, filename)
File "/usr/lib64/python2.7/ConfigParser.py", line 512, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: /root/.config/pip/pip.conf, line: 1
'bal]\n'
Started: 00:38:47.277938
Duration: 386.45 ms
Changes:
----------

# Fixed code
def _check_pkg_version_format(pkg):
    """
    Takes a package name and version specification (if any) and checks it using
    the pip library.
    """

    ret = {"result": False, "comment": None, "prefix": None, "version_spec": None}

    if not HAS_PIP:
        ret["comment"] = (
            "An importable pip module is required but could not be found on "
            "your system. This usually means that the system's pip package "
            "is not installed properly."
        )

        return ret

    from_vcs = False
    try:
        # Get the requirement object from the pip library
        try:
            # With pip < 1.2, the __version__ attribute does not exist and
            # vcs+URL urls are not properly parsed.
            # The next line is meant to trigger an AttributeError and
            # handle lower pip versions
            logger.debug("Installed pip version: {0}".format(pip.__version__))
            install_req = pip.req.InstallRequirement.from_line(pkg)
        except AttributeError:
            logger.debug("Installed pip version is lower than 1.2")
            supported_vcs = ("git", "svn", "hg", "bzr")
            if pkg.startswith(supported_vcs):
                for vcs in supported_vcs:
                    if pkg.startswith(vcs):
                        from_vcs = True
                        install_req = pip.req.InstallRequirement.from_line(
                            pkg.split("{0}+".format(vcs))[-1]
                        )
                        break
            else:
                install_req = pip.req.InstallRequirement.from_line(pkg)
    except ValueError as exc:
        ret["result"] = False
        if not from_vcs and "=" in pkg and "==" not in pkg:
            ret["comment"] = (
                "Invalid version specification in package {0}. '=' is "
                "not supported, use '==' instead.".format(pkg)
            )
            return ret
        ret["comment"] = "pip raised an exception while parsing {0!r}: {1}".format(
            pkg, exc
        )
        return ret

    if install_req.req is None:
        # This is most likely an url and there's no way to know what will
        # be installed before actually installing it.
        ret["result"] = True
        ret["prefix"] = ""
        ret["version_spec"] = []
    else:
        ret["result"] = True
        ret["prefix"] = install_req.req.project_name
        ret["version_spec"] = install_req.req.specs

    return ret