Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #81 from mardiros/fix/hyphens
Browse files Browse the repository at this point in the history
Fix issue #80: hyphen in the package name resolution
  • Loading branch information
mardiros authored Jun 28, 2017
2 parents 2198117 + 3ca7a5b commit c20e161
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
11 changes: 8 additions & 3 deletions pyshop/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def includeme(config):
"""
Pyramid includeme file for the :class:`pyramid.config.Configurator`
"""
settings = config.registry.settings

# config.add_renderer('json', JSONP())
# release file download
Expand All @@ -50,9 +51,13 @@ def includeme(config):
# i18n
config.add_translation_dirs('locale/')

pypi_url = settings.get('pyshop.pypi.url', 'https://pypi.python.org/pypi')
simple_url = settings.get(
'pyshop.pypi.simple_url', 'https://pypi.python.org/simple').rstrip('/')
# PyPI url for XML RPC service consume
pypi.set_proxy(config.registry.settings['pyshop.pypi.url'],
config.registry.settings.get('pyshop.pypi.transport_proxy'))
pypi.set_proxy(pypi_url,
simple_url,
settings.get('pyshop.pypi.transport_proxy'))

# Javascript + Media
config.add_static_view('static', 'static', cache_max_age=3600)
Expand Down Expand Up @@ -124,7 +129,7 @@ def includeme(config):

# Web Services

if asbool(config.registry.settings.get('pyshop.enable_xmlrpc', 'true')):
if asbool(settings.get('pyshop.enable_xmlrpc', 'true')):
config.add_view('pyshop.views.xmlrpc.PyPI', name='pypi')

# Backoffice Views
Expand Down
25 changes: 21 additions & 4 deletions pyshop/helpers/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
"""

import logging
try:
import xmlrpc.client as xmlrpc
except ImportError:
import xmlrpclib as xmlrpc

import requests


log = logging.getLogger(__name__)
proxy = None
PYPI_SIMPLE_URL = None


class RequestsTransport(xmlrpc.Transport):
Expand Down Expand Up @@ -86,10 +87,26 @@ def _build_url(self, host, handler):
return '%s://%s%s' % (self.scheme, host, handler)


def resolve_name(package_name):
""" Return the """
log.info('Resolving hyphenation of %s', package_name)
url = '{}/{}'.format(PYPI_SIMPLE_URL, package_name)
response = requests.get(url,
allow_redirects=False)
if 300 <= response.status_code < 400:
loc = response.headers['Location'].rstrip('/')
real_package_name = loc.rsplit('/', 1).pop()
log.info('Package %s is %s in upstream index',
package_name, real_package_name)
return real_package_name
else:
response.raise_for_status()


def set_proxy(proxy_url, transport_proxy=None):
def set_proxy(proxy_url, simple_url, transport_proxy=None):
"""Create the proxy to PyPI XML-RPC Server"""
global proxy
global proxy, PYPI_SIMPLE_URL
PYPI_SIMPLE_URL = simple_url
proxy = xmlrpc.ServerProxy(
proxy_url,
transport=RequestsTransport(proxy_url.startswith('https://')),
Expand Down
24 changes: 2 additions & 22 deletions pyshop/views/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,28 +264,8 @@ def _create_release_file(self, release, data):
)

def _search_package(self, package_name):
api = pypi.proxy

search_result = api.search({'name': package_name})
search_count = len(search_result)
if not search_count:
return None

package_name = package_name.lower().replace('-', '_')
search_result = [
pkg
for pkg in search_result
if pkg['name'].lower() == package_name or
pkg['name'].lower().replace('-', '_') == package_name
]
log.debug('Found %s, matched %s',
search_count, len(search_result))

if not search_result:
return None

package_name = search_result[0]['name']
pypi_versions = api.package_releases(package_name, True)
package_name = pypi.resolve_name(package_name)
pypi_versions = pypi.proxy.package_releases(package_name, True)
return to_unicode(package_name), pypi_versions

def render(self):
Expand Down

0 comments on commit c20e161

Please sign in to comment.