1 """PyPI and direct package downloading"""
2 import sys, os.path, re, urlparse, urllib2, shutil, random, socket, cStringIO
4 from pkg_resources import *
5 from distutils import log
6 from distutils.errors import DistutilsError
8 from hashlib import md5
11 from fnmatch import translate
12 EGG_FRAGMENT = re.compile(r'^egg=([-A-Za-z0-9_.]+)$')
13 HREF = re.compile("""href\\s*=\\s*['"]?([^'"> ]+)""", re.I)
14 # this is here to fix emacs' cruddy broken syntax highlighting
15 PYPI_MD5 = re.compile(
16 '<a href="([^"#]+)">([^<]+)</a>\n\s+\\(<a (?:title="MD5 hash"\n\s+)'
17 'href="[^?]+\?:action=show_md5&digest=([0-9a-f]{32})">md5</a>\\)'
19 URL_SCHEME = re.compile('([-+.a-z0-9]{2,}):',re.I).match
20 EXTENSIONS = ".tar.gz .tar.bz2 .tar .zip .tgz".split()
23 'PackageIndex', 'distros_for_url', 'parse_bdist_wininst',
24 'interpret_distro_name',
27 def parse_bdist_wininst(name):
28 """Return (base,pyversion) or (None,None) for possible .exe name"""
31 base, py_ver = None, None
33 if lower.endswith('.exe'):
34 if lower.endswith('.win32.exe'):
36 elif lower.startswith('.win32-py',-16):
42 def egg_info_for_url(url):
43 scheme, server, path, parameters, query, fragment = urlparse.urlparse(url)
44 base = urllib2.unquote(path.split('/')[-1])
45 if server=='sourceforge.net' and base=='download': # XXX Yuck
46 base = urllib2.unquote(path.split('/')[-2])
47 if '#' in base: base, fragment = base.split('#',1)
50 def distros_for_url(url, metadata=None):
51 """Yield egg or source distribution objects that might be found at a URL"""
52 base, fragment = egg_info_for_url(url)
53 for dist in distros_for_location(url, base, metadata): yield dist
55 match = EGG_FRAGMENT.match(fragment)
57 for dist in interpret_distro_name(
58 url, match.group(1), metadata, precedence = CHECKOUT_DIST
62 def distros_for_location(location, basename, metadata=None):
63 """Yield egg or source distribution objects based on basename"""
64 if basename.endswith('.egg.zip'):
65 basename = basename[:-4] # strip the .zip
66 if basename.endswith('.egg') and '-' in basename:
67 # only one, unambiguous interpretation
68 return [Distribution.from_location(location, basename, metadata)]
69 if basename.endswith('.exe'):
70 win_base, py_ver = parse_bdist_wininst(basename)
71 if win_base is not None:
72 return interpret_distro_name(
73 location, win_base, metadata, py_ver, BINARY_DIST, "win32"
75 # Try source distro extensions (.zip, .tgz, etc.)
77 for ext in EXTENSIONS:
78 if basename.endswith(ext):
79 basename = basename[:-len(ext)]
80 return interpret_distro_name(location, basename, metadata)
81 return [] # no extension matched
83 def distros_for_filename(filename, metadata=None):
84 """Yield possible egg or source distribution objects based on a filename"""
85 return distros_for_location(
86 normalize_path(filename), os.path.basename(filename), metadata
90 def interpret_distro_name(location, basename, metadata,
91 py_version=None, precedence=SOURCE_DIST, platform=None
93 """Generate alternative interpretations of a source distro name
95 Note: if `location` is a filesystem filename, you should call
96 ``pkg_resources.normalize_path()`` on it before passing it to this
99 # Generate alternative interpretations of a source distro name
100 # Because some packages are ambiguous as to name/versions split
101 # e.g. "adns-python-1.1.0", "egenix-mx-commercial", etc.
102 # So, we generate each possible interepretation (e.g. "adns, python-1.1.0"
103 # "adns-python, 1.1.0", and "adns-python-1.1.0, no version"). In practice,
104 # the spurious interpretations should be ignored, because in the event
105 # there's also an "adns" package, the spurious "python-1.1.0" version will
106 # compare lower than any numeric version number, and is therefore unlikely
107 # to match a request for it. It's still a potential problem, though, and
108 # in the long run PyPI and the distutils should go for "safe" names and
109 # versions in distribution archive names (sdist and bdist).
111 parts = basename.split('-')
113 for i,p in enumerate(parts[2:]):
114 if len(p)==5 and p.startswith('py2.'):
115 return # It's a bdist_dumb, not an sdist -- bail out
117 for p in range(1,len(parts)+1):
119 location, metadata, '-'.join(parts[:p]), '-'.join(parts[p:]),
120 py_version=py_version, precedence = precedence,
124 REL = re.compile("""<([^>]*\srel\s*=\s*['"]?([^'">]+)[^>]*)>""", re.I)
125 # this line is here to fix emacs' cruddy broken syntax highlighting
127 def find_external_links(url, page):
128 """Find rel="homepage" and rel="download" links in `page`, yielding URLs"""
130 for match in REL.finditer(page):
131 tag, rel = match.groups()
132 rels = map(str.strip, rel.lower().split(','))
133 if 'homepage' in rels or 'download' in rels:
134 for match in HREF.finditer(tag):
135 yield urlparse.urljoin(url, htmldecode(match.group(1)))
137 for tag in ("<th>Home Page", "<th>Download URL"):
140 match = HREF.search(page,pos)
142 yield urlparse.urljoin(url, htmldecode(match.group(1)))
144 user_agent = "Python-urllib/%s setuptools/%s" % (
145 urllib2.__version__, require('setuptools')[0].version
149 class PackageIndex(Environment):
150 """A distribution index that scans web pages for download URLs"""
152 def __init__(self, index_url="http://pypi.python.org/simple", hosts=('*',),
155 Environment.__init__(self,*args,**kw)
156 self.index_url = index_url + "/"[:not index_url.endswith('/')]
157 self.scanned_urls = {}
158 self.fetched_urls = {}
159 self.package_pages = {}
160 self.allows = re.compile('|'.join(map(translate,hosts))).match
165 def process_url(self, url, retrieve=False):
166 """Evaluate a URL as a possible download, and maybe retrieve it"""
167 if url in self.scanned_urls and not retrieve:
169 self.scanned_urls[url] = True
170 if not URL_SCHEME(url):
171 self.process_filename(url)
174 dists = list(distros_for_url(url))
176 if not self.url_ok(url):
178 self.debug("Found link: %s", url)
180 if dists or not retrieve or url in self.fetched_urls:
182 return # don't need the actual page
184 if not self.url_ok(url):
185 self.fetched_urls[url] = True
188 self.info("Reading %s", url)
189 self.fetched_urls[url] = True # prevent multiple fetch attempts
190 f = self.open_url(url, "Download error: %s -- Some packages may not be found!")
192 self.fetched_urls[f.url] = True
193 if 'html' not in f.headers.get('content-type', '').lower():
194 f.close() # not html, we can't process it
197 base = f.url # handle redirects
200 if url.startswith(self.index_url) and getattr(f,'code',None)!=404:
201 page = self.process_index(url, page)
202 for match in HREF.finditer(page):
203 link = urlparse.urljoin(base, htmldecode(match.group(1)))
204 self.process_url(link)
206 def process_filename(self, fn, nested=False):
207 # process filenames or directories
208 if not os.path.exists(fn):
209 self.warn("Not found: %s", fn)
212 if os.path.isdir(fn) and not nested:
213 path = os.path.realpath(fn)
214 for item in os.listdir(path):
215 self.process_filename(os.path.join(path,item), True)
217 dists = distros_for_filename(fn)
219 self.debug("Found: %s", fn)
222 def url_ok(self, url, fatal=False):
224 if (s and s.group(1).lower()=='file') or self.allows(urlparse.urlparse(url)[1]):
226 msg = "\nLink to % s ***BLOCKED*** by --allow-hosts\n"
228 raise DistutilsError(msg % url)
232 def scan_egg_links(self, search_path):
233 for item in search_path:
234 if os.path.isdir(item):
235 for entry in os.listdir(item):
236 if entry.endswith('.egg-link'):
237 self.scan_egg_link(item, entry)
239 def scan_egg_link(self, path, entry):
240 lines = filter(None, map(str.strip, file(os.path.join(path, entry))))
242 for dist in find_distributions(os.path.join(path, lines[0])):
243 dist.location = os.path.join(path, *lines)
244 dist.precedence = SOURCE_DIST
247 def process_index(self,url,page):
248 """Process the contents of a PyPI page"""
250 # Process a URL to see if it's for a package page
251 if link.startswith(self.index_url):
253 urllib2.unquote, link[len(self.index_url):].split('/')
255 if len(parts)==2 and '#' not in parts[1]:
256 # it's a package page, sanitize and index it
257 pkg = safe_name(parts[0])
258 ver = safe_version(parts[1])
259 self.package_pages.setdefault(pkg.lower(),{})[link] = True
260 return to_filename(pkg), to_filename(ver)
263 # process an index page into the package-page index
264 for match in HREF.finditer(page):
265 scan( urlparse.urljoin(url, htmldecode(match.group(1))) )
267 pkg, ver = scan(url) # ensure this page is in the page index
269 # process individual package page
270 for new_url in find_external_links(url, page):
271 # Process the found URL
272 base, frag = egg_info_for_url(new_url)
273 if base.endswith('.py') and not frag:
275 new_url+='#egg=%s-%s' % (pkg,ver)
277 self.need_version_info(url)
278 self.scan_url(new_url)
281 lambda m: '<a href="%s#md5=%s">%s</a>' % m.group(1,3,2), page
284 return "" # no sense double-scanning non-package pages
288 def need_version_info(self, url):
290 "Page at %s links to .py file(s) without version info; an index "
291 "scan is required.", url
294 def scan_all(self, msg=None, *args):
295 if self.index_url not in self.fetched_urls:
296 if msg: self.warn(msg,*args)
298 "Scanning index of all packages (this may take a while)"
300 self.scan_url(self.index_url)
302 def find_packages(self, requirement):
303 self.scan_url(self.index_url + requirement.unsafe_name+'/')
305 if not self.package_pages.get(requirement.key):
306 # Fall back to safe version of the name
307 self.scan_url(self.index_url + requirement.project_name+'/')
309 if not self.package_pages.get(requirement.key):
310 # We couldn't find the target package, so search the index page too
311 self.not_found_in_index(requirement)
313 for url in list(self.package_pages.get(requirement.key,())):
314 # scan each page that might be related to the desired package
317 def obtain(self, requirement, installer=None):
318 self.prescan(); self.find_packages(requirement)
319 for dist in self[requirement.key]:
320 if dist in requirement:
322 self.debug("%s does not match %s", requirement, dist)
323 return super(PackageIndex, self).obtain(requirement,installer)
329 def check_md5(self, cs, info, filename, tfp):
330 if re.match('md5=[0-9a-f]{32}$', info):
331 self.debug("Validating md5 checksum for %s", filename)
332 if cs.hexdigest()!=info[4:]:
335 raise DistutilsError(
336 "MD5 validation failed for "+os.path.basename(filename)+
337 "; possible download problem?"
340 def add_find_links(self, urls):
341 """Add `urls` to the list that will be prescanned for searches"""
344 self.to_scan is None # if we have already "gone online"
345 or not URL_SCHEME(url) # or it's a local file/directory
346 or url.startswith('file:')
347 or list(distros_for_url(url)) # or a direct package link
349 # then go ahead and process it now
352 # otherwise, defer retrieval till later
353 self.to_scan.append(url)
356 """Scan urls scheduled for prescanning (e.g. --find-links)"""
358 map(self.scan_url, self.to_scan)
359 self.to_scan = None # from now on, go ahead and process immediately
361 def not_found_in_index(self, requirement):
362 if self[requirement.key]: # we've seen at least one distro
363 meth, msg = self.info, "Couldn't retrieve index page for %r"
364 else: # no distros seen for this name, might be misspelled
365 meth, msg = (self.warn,
366 "Couldn't find index page for %r (maybe misspelled?)")
367 meth(msg, requirement.unsafe_name)
370 def download(self, spec, tmpdir):
371 """Locate and/or download `spec` to `tmpdir`, returning a local path
373 `spec` may be a ``Requirement`` object, or a string containing a URL,
374 an existing local filename, or a project/version requirement spec
375 (i.e. the string form of a ``Requirement`` object). If it is the URL
376 of a .py file with an unambiguous ``#egg=name-version`` tag (i.e., one
377 that escapes ``-`` as ``_`` throughout), a trivial ``setup.py`` is
378 automatically created alongside the downloaded file.
380 If `spec` is a ``Requirement`` object or a string containing a
381 project/version requirement spec, this method returns the location of
382 a matching distribution (possibly after downloading it to `tmpdir`).
383 If `spec` is a locally existing file or directory name, it is simply
384 returned unchanged. If `spec` is a URL, it is downloaded to a subpath
385 of `tmpdir`, and the local filename is returned. Various errors may be
386 raised if a problem occurs during downloading.
388 if not isinstance(spec,Requirement):
389 scheme = URL_SCHEME(spec)
391 # It's a url, download it to tmpdir
392 found = self._download_url(scheme.group(1), spec, tmpdir)
393 base, fragment = egg_info_for_url(spec)
394 if base.endswith('.py'):
395 found = self.gen_setup(found,fragment,tmpdir)
397 elif os.path.exists(spec):
398 # Existing file or directory, just return it
402 spec = Requirement.parse(spec)
404 raise DistutilsError(
405 "Not a URL, existing file, or requirement spec: %r" %
408 return getattr(self.fetch_distribution(spec, tmpdir),'location',None)
411 def fetch_distribution(self,
412 requirement, tmpdir, force_scan=False, source=False, develop_ok=False,
415 """Obtain a distribution suitable for fulfilling `requirement`
417 `requirement` must be a ``pkg_resources.Requirement`` instance.
418 If necessary, or if the `force_scan` flag is set, the requirement is
419 searched for in the (online) package index as well as the locally
420 installed packages. If a distribution matching `requirement` is found,
421 the returned distribution's ``location`` is the value you would have
422 gotten from calling the ``download()`` method with the matching
423 distribution's URL or filename. If no matching distribution is found,
424 ``None`` is returned.
426 If the `source` flag is set, only source distributions and source
427 checkout links will be considered. Unless the `develop_ok` flag is
428 set, development and system eggs (i.e., those using the ``.egg-info``
429 format) will be ignored.
431 # process a Requirement
432 self.info("Searching for %s", requirement)
437 # Find a matching distribution; may be called more than once
439 for dist in env[req.key]:
441 if dist.precedence==DEVELOP_DIST and not develop_ok:
442 if dist not in skipped:
443 self.warn("Skipping development or system egg: %s",dist)
447 if dist in req and (dist.precedence<=SOURCE_DIST or not source):
454 self.find_packages(requirement)
455 dist = find(self, requirement)
457 if local_index is not None:
458 dist = dist or find(local_index, requirement)
460 if dist is None and self.to_scan is not None:
462 dist = find(self, requirement)
464 if dist is None and not force_scan:
465 self.find_packages(requirement)
466 dist = find(self, requirement)
470 "No local packages or download links found for %s%s",
471 (source and "a source distribution of " or ""),
475 self.info("Best match: %s", dist)
476 return dist.clone(location=self.download(dist.location, tmpdir))
479 def fetch(self, requirement, tmpdir, force_scan=False, source=False):
480 """Obtain a file suitable for fulfilling `requirement`
482 DEPRECATED; use the ``fetch_distribution()`` method now instead. For
483 backward compatibility, this routine is identical but returns the
484 ``location`` of the downloaded distribution instead of a distribution
487 dist = self.fetch_distribution(requirement,tmpdir,force_scan,source)
493 def gen_setup(self, filename, fragment, tmpdir):
494 match = EGG_FRAGMENT.match(fragment)
495 dists = match and [d for d in
496 interpret_distro_name(filename, match.group(1), None) if d.version
499 if len(dists)==1: # unambiguous ``#egg`` fragment
500 basename = os.path.basename(filename)
502 # Make sure the file has been downloaded to the temp dir.
503 if os.path.dirname(filename) != tmpdir:
504 dst = os.path.join(tmpdir, basename)
505 from setuptools.command.easy_install import samefile
506 if not samefile(filename, dst):
507 shutil.copy2(filename, dst)
510 file = open(os.path.join(tmpdir, 'setup.py'), 'w')
512 "from setuptools import setup\n"
513 "setup(name=%r, version=%r, py_modules=[%r])\n"
515 dists[0].project_name, dists[0].version,
516 os.path.splitext(basename)[0]
523 raise DistutilsError(
524 "Can't unambiguously interpret project/version identifier %r; "
525 "any dashes in the name or version should be escaped using "
526 "underscores. %r" % (fragment,dists)
529 raise DistutilsError(
530 "Can't process plain .py files without an '#egg=name-version'"
531 " suffix to enable automatic setup script generation."
535 def _download_to(self, url, filename):
536 self.info("Downloading %s", url)
538 fp, tfp, info = None, None, None
541 url, info = url.split('#', 1)
542 fp = self.open_url(url)
543 if isinstance(fp, urllib2.HTTPError):
544 raise DistutilsError(
545 "Can't download %s: %s %s" % (url, fp.code,fp.msg)
550 bs = self.dl_blocksize
552 if "content-length" in headers:
553 size = int(headers["Content-Length"])
554 self.reporthook(url, filename, blocknum, bs, size)
555 tfp = open(filename,'wb')
562 self.reporthook(url, filename, blocknum, bs, size)
565 if info: self.check_md5(cs, info, filename, tfp)
571 def reporthook(self, url, filename, blocknum, blksize, size):
575 def open_url(self, url, warning=None):
576 if url.startswith('file:'): return local_open(url)
578 return open_with_auth(url)
579 except urllib2.HTTPError, v:
581 except urllib2.URLError, v:
583 except httplib.HTTPException, v:
584 reason = "%s: %s" % (v.__doc__ or v.__class__.__name__, v)
586 self.warn(warning, reason)
588 raise DistutilsError("Download error for %s: %s" % (url, reason))
590 def _download_url(self, scheme, url, tmpdir):
591 # Determine download filename
593 name, fragment = egg_info_for_url(url)
596 name = name.replace('..','.').replace('\\','_')
598 name = "__downloaded__" # default if URL has no path contents
600 if name.endswith('.egg.zip'):
601 name = name[:-4] # strip the extra .zip before download
603 filename = os.path.join(tmpdir,name)
607 if scheme=='svn' or scheme.startswith('svn+'):
608 return self._download_svn(url, filename)
610 return urllib2.url2pathname(urlparse.urlparse(url)[2])
612 self.url_ok(url, True) # raises error if not allowed
613 return self._attempt_download(url, filename)
616 def scan_url(self, url):
617 self.process_url(url, True)
620 def _attempt_download(self, url, filename):
621 headers = self._download_to(url, filename)
622 if 'html' in headers.get('content-type','').lower():
623 return self._download_html(url, headers, filename)
627 def _download_html(self, url, headers, filename):
628 file = open(filename)
631 # Check for a subversion index page
632 if re.search(r'<title>([^- ]+ - )?Revision \d+:', line):
633 # it's a subversion index page:
636 return self._download_svn(url, filename)
637 break # not an index page
640 raise DistutilsError("Unexpected HTML page found at "+url)
642 def _download_svn(self, url, filename):
643 url = url.split('#',1)[0] # remove any fragment for svn's sake
644 self.info("Doing subversion checkout from %s to %s", url, filename)
645 os.system("svn checkout -q %s %s" % (url, filename))
648 def debug(self, msg, *args):
649 log.debug(msg, *args)
651 def info(self, msg, *args):
654 def warn(self, msg, *args):
657 # This pattern matches a character entity reference (a decimal numeric
658 # references, a hexadecimal numeric reference, or a named reference).
659 entity_sub = re.compile(r'&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?').sub
662 if not isinstance(c, int):
664 if c>255: return unichr(c)
667 def decode_entity(match):
668 what = match.group(1)
669 if what.startswith('#x'):
670 what = int(what[2:], 16)
671 elif what.startswith('#'):
674 from htmlentitydefs import name2codepoint
675 what = name2codepoint.get(what, match.group(0))
678 def htmldecode(text):
679 """Decode HTML entities in the given text."""
680 return entity_sub(decode_entity, text)
698 def open_with_auth(url):
699 """Open a urllib2 request, handling HTTP authentication"""
701 scheme, netloc, path, params, query, frag = urlparse.urlparse(url)
703 if scheme in ('http', 'https'):
704 auth, host = urllib.splituser(netloc)
709 auth = "Basic " + urllib2.unquote(auth).encode('base64').strip()
710 new_url = urlparse.urlunparse((scheme,host,path,params,query,frag))
711 request = urllib2.Request(new_url)
712 request.add_header("Authorization", auth)
714 request = urllib2.Request(url)
716 request.add_header('User-Agent', user_agent)
717 fp = urllib2.urlopen(request)
720 # Put authentication info back into request URL if same host,
721 # so that links found on the page will work
722 s2, h2, path2, param2, query2, frag2 = urlparse.urlparse(fp.url)
723 if s2==scheme and h2==host:
724 fp.url = urlparse.urlunparse((s2,netloc,path2,param2,query2,frag2))
740 return url # backward compatibility
743 """Read a local path, with special support for directories"""
744 scheme, server, path, param, query, frag = urlparse.urlparse(url)
745 filename = urllib2.url2pathname(path)
746 if os.path.isfile(filename):
747 return urllib2.urlopen(url)
748 elif path.endswith('/') and os.path.isdir(filename):
750 for f in os.listdir(filename):
752 body = open(os.path.join(filename,f),'rb').read()
754 elif os.path.isdir(os.path.join(filename,f)):
756 files.append("<a href=%r>%s</a>" % (f,f))
758 body = ("<html><head><title>%s</title>" % url) + \
759 "</head><body>%s</body></html>" % '\n'.join(files)
760 status, message = 200, "OK"
762 status, message, body = 404, "Path not found", "Not found"
764 return urllib2.HTTPError(url, status, message,
765 {'content-type':'text/html'}, cStringIO.StringIO(body))
779 # this line is a kludge to keep the trailing blank lines for pje's editor