Skip to content

Use distlib to emulate pkg_resources #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ dist/*
docs/_build/*
build/*
*.pyc
*.pyo
*.pyo
pip-log.txt
pip.log
*.~
.tox

env/*
2 changes: 1 addition & 1 deletion pip/baseparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys
import optparse
import pkg_resources
from pip.vendor.distlib import pkg_resources
import os
import textwrap
from distutils.util import strtobool
Expand Down
2 changes: 1 addition & 1 deletion pip/commands/freeze.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
import sys
import pkg_resources
from pip.vendor.distlib import pkg_resources
import pip
from pip.req import InstallRequirement
from pip.log import logger
Expand Down
2 changes: 1 addition & 1 deletion pip/commands/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import textwrap
import pkg_resources
from pip.vendor.distlib import pkg_resources
import pip.download
from pip.basecommand import Command, SUCCESS
from pip.util import get_terminal_size
Expand Down
2 changes: 1 addition & 1 deletion pip/commands/show.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import pkg_resources
from pip.vendor.distlib import pkg_resources
from pip.basecommand import Command
from pip.log import logger

Expand Down
3 changes: 1 addition & 2 deletions pip/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import gzip
import mimetypes
import posixpath
import pkg_resources
from pip.vendor.distlib import pkg_resources
import random
import socket
import ssl
Expand Down Expand Up @@ -818,4 +818,3 @@ def string_range(last):
yield result
if result == last:
return

6 changes: 3 additions & 3 deletions pip/req.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from email.parser import FeedParser
import os
import imp
import pkg_resources
from pip.vendor.distlib import pkg_resources
import re
import sys
import shutil
Expand Down Expand Up @@ -428,7 +428,6 @@ def uninstall(self, auto_confirm=False):
if not self.check_if_exists():
raise UninstallationError("Cannot uninstall requirement %s, not installed" % (self.name,))
dist = self.satisfied_by or self.conflicts_with

paths_to_remove = UninstallPathSet(dist)

pip_egg_info_path = os.path.join(dist.location,
Expand Down Expand Up @@ -1095,7 +1094,8 @@ def prepare_files(self, finder, force_root_egg_info=False, bundle=False):
elif is_wheel:
req_to_install.source_dir = location
req_to_install.url = url.url
dist = list(pkg_resources.find_distributions(location))[0]
dists = list(pkg_resources.find_distributions(location))
dist = dists[0]
if not req_to_install.req:
req_to_install.req = dist.as_requirement()
self.add_requirement(req_to_install)
Expand Down
2 changes: 1 addition & 1 deletion pip/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import stat
import re
import posixpath
import pkg_resources
from pip.vendor.distlib import pkg_resources
import zipfile
import tarfile
import subprocess
Expand Down
4 changes: 2 additions & 2 deletions pip/vendor/distlib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ def parse_requires(req_path):
return metadata

def __repr__(self):
return '<EggInfoDistribution %r %s at %r>' % (
self.name, self.version, self.path)
return '<%s %r %s at %r>' % (
self.__class__.__name__, self.name, self.version, self.path)

def __str__(self):
return "%s %s" % (self.name, self.version)
Expand Down
29 changes: 15 additions & 14 deletions pip/vendor/distlib/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
"""Implementation of the Metadata for Python packages PEPs.

Supports all metadata formats (1.0, 1.1, 1.2, and 2.0 experimental).
Supports all metadata formats (1.0, 1.1, 1.2, and 1.3 experimental).
"""
from __future__ import unicode_literals

Expand All @@ -22,6 +22,7 @@

logger = logging.getLogger(__name__)


class MetadataMissingError(DistlibException):
"""A required metadata is missing"""

Expand Down Expand Up @@ -125,7 +126,7 @@ def _version2fieldlist(version):
return _314_FIELDS
elif version == '1.2':
return _345_FIELDS
elif version == '2.0':
elif version == '1.3':
return _426_FIELDS
raise MetadataUnrecognizedVersionError(version)

Expand All @@ -144,7 +145,7 @@ def _has_marker(keys, markers):
continue
keys.append(key)

possible_versions = ['1.0', '1.1', '1.2', '2.0']
possible_versions = ['1.0', '1.1', '1.2', '1.3']

# first let's try to see if a field is not part of one of the version
for key in keys:
Expand All @@ -154,8 +155,8 @@ def _has_marker(keys, markers):
possible_versions.remove('1.1')
if key not in _345_FIELDS and '1.2' in possible_versions:
possible_versions.remove('1.2')
if key not in _426_FIELDS and '2.0' in possible_versions:
possible_versions.remove('2.0')
if key not in _426_FIELDS and '1.3' in possible_versions:
possible_versions.remove('1.3')

# possible_version contains qualified versions
if len(possible_versions) == 1:
Expand All @@ -166,16 +167,16 @@ def _has_marker(keys, markers):
# let's see if one unique marker is found
is_1_1 = '1.1' in possible_versions and _has_marker(keys, _314_MARKERS)
is_1_2 = '1.2' in possible_versions and _has_marker(keys, _345_MARKERS)
is_2_0 = '2.0' in possible_versions and _has_marker(keys, _426_MARKERS)
if int(is_1_1) + int(is_1_2) + int(is_2_0) > 1:
raise MetadataConflictError('You used incompatible 1.1/1.2/2.0 fields')
is_1_3 = '1.3' in possible_versions and _has_marker(keys, _426_MARKERS)
if int(is_1_1) + int(is_1_2) + int(is_1_3) > 1:
raise MetadataConflictError('You used incompatible 1.1/1.2/1.3 fields')

# we have the choice, 1.0, or 1.2, or 2.0
# we have the choice, 1.0, or 1.2, or 1.3
# - 1.0 has a broken Summary field but works with all tools
# - 1.1 is to avoid
# - 1.2 fixes Summary but has little adoption
# - 2.0 adds more features and is very new
if not is_1_1 and not is_1_2 and not is_2_0:
# - 1.3 adds more features and is very new
if not is_1_1 and not is_1_2 and not is_1_3:
# we couldn't find any specific marker
if PKG_INFO_PREFERRED_VERSION in possible_versions:
return PKG_INFO_PREFERRED_VERSION
Expand All @@ -184,7 +185,7 @@ def _has_marker(keys, markers):
if is_1_2:
return '1.2'

return '2.0'
return '1.3'

_ATTR2FIELD = {
'metadata_version': 'Metadata-Version',
Expand Down Expand Up @@ -275,8 +276,8 @@ def __init__(self, path=None, platform_dependent=False,
def set_metadata_version(self):
self._fields['Metadata-Version'] = _best_version(self._fields)

def _write_field(self, file, name, value):
file.write('%s: %s\n' % (name, value))
def _write_field(self, fileobj, name, value):
fileobj.write('%s: %s\n' % (name, value))

def __getitem__(self, name):
return self.get(name)
Expand Down
Loading