Commit 55742ce1 authored by PJ Eby's avatar PJ Eby

Allow ``file://`` URLs to be used as a package index. URLs that refer to

directories will use an internally-generated directory listing if there is
no ``index.html`` file in the directory.
(backport from trunk)

--HG--
branch : setuptools-0.6
extra : convert_revision : svn%3A6015fed2-1504-0410-9fe1-9d1591cc4771/sandbox/branches/setuptools-0.6%4050555
parent ac7808e6
...@@ -1106,6 +1106,10 @@ Release Notes/Change History ...@@ -1106,6 +1106,10 @@ Release Notes/Change History
* Fix not recognizing HTML 404 pages from package indexes. * Fix not recognizing HTML 404 pages from package indexes.
* Allow ``file://`` URLs to be used as a package index. URLs that refer to
directories will use an internally-generated directory listing if there is
no ``index.html`` file in the directory.
0.6b3 0.6b3
* Fix local ``--find-links`` eggs not being copied except with * Fix local ``--find-links`` eggs not being copied except with
``--always-copy``. ``--always-copy``.
......
"""PyPI and direct package downloading""" """PyPI and direct package downloading"""
import sys, os.path, re, urlparse, urllib2, shutil, random, socket import sys, os.path, re, urlparse, urllib2, shutil, random, socket, cStringIO
from pkg_resources import * from pkg_resources import *
from distutils import log from distutils import log
from distutils.errors import DistutilsError from distutils.errors import DistutilsError
...@@ -573,6 +573,8 @@ class PackageIndex(Environment): ...@@ -573,6 +573,8 @@ class PackageIndex(Environment):
def open_url(self, url): def open_url(self, url):
if url.startswith('file:'):
return local_open(url)
try: try:
return urllib2.urlopen(url) return urllib2.urlopen(url)
except urllib2.HTTPError, v: except urllib2.HTTPError, v:
...@@ -610,6 +612,7 @@ class PackageIndex(Environment): ...@@ -610,6 +612,7 @@ class PackageIndex(Environment):
else: else:
return filename return filename
def scan_url(self, url): def scan_url(self, url):
self.process_url(url, True) self.process_url(url, True)
...@@ -643,17 +646,6 @@ class PackageIndex(Environment): ...@@ -643,17 +646,6 @@ class PackageIndex(Environment):
def warn(self, msg, *args): def warn(self, msg, *args):
log.warn(msg, *args) log.warn(msg, *args)
def fix_sf_url(url): def fix_sf_url(url):
scheme, server, path, param, query, frag = urlparse.urlparse(url) scheme, server, path, param, query, frag = urlparse.urlparse(url)
if server!='prdownloads.sourceforge.net': if server!='prdownloads.sourceforge.net':
...@@ -674,22 +666,30 @@ def get_sf_ip(): ...@@ -674,22 +666,30 @@ def get_sf_ip():
return random.choice(_sf_mirrors) return random.choice(_sf_mirrors)
def local_open(url):
"""Read a local path, with special support for directories"""
scheme, server, path, param, query, frag = urlparse.urlparse(url)
filename = urllib2.url2pathname(path)
if os.path.isfile(filename):
return urllib2.urlopen(url)
elif path.endswith('/') and os.path.isdir(filename):
files = []
for f in os.listdir(filename):
if f=='index.html':
body = open(os.path.join(filename,f),'rb').read()
break
elif os.path.isdir(os.path.join(filename,f)):
f+='/'
files.append("<a href=%r>%s</a>" % (f,f))
else:
body = ("<html><head><title>%s</title>" % url) + \
"</head><body>%s</body></html>" % '\n'.join(files)
status, message = 200, "OK"
else:
status, message, body = 404, "Path not found", "Not found"
return urllib2.HTTPError(url, status, message,
{'content-type':'text/html'}, cStringIO.StringIO(body))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment