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()
22 def is_local(url_or_fname):
23 """ Return True if url_or_fname is a "file:" url or if it is a schemaless thing (which is presumably a filename). """
24 mo = URL_SCHEME(url_or_fname)
25 return not (mo and mo.group(1).lower()!='file')
27 def url_or_fname_to_fname(url_or_fname):
28 """ Assert that is_local(url_or_fname) then if it is a "file:" url, parse it and run url2pathname on it, else just return it. """
29 assert is_local(url_or_fname)
31 mo = URL_SCHEME(url_or_fname)
33 return urllib2.url2pathname(urlparse.urlparse(url)[2])
38 'PackageIndex', 'distros_for_url', 'parse_bdist_wininst',
39 'interpret_distro_name',
42 def parse_bdist_wininst(name):
43 """Return (base,pyversion) or (None,None) for possible .exe name"""
46 base, py_ver = None, None
48 if lower.endswith('.exe'):
49 if lower.endswith('.win32.exe'):
51 elif lower.startswith('.win32-py',-16):
57 def egg_info_for_url(url):
58 scheme, server, path, parameters, query, fragment = urlparse.urlparse(url)
59 base = urllib2.unquote(path.split('/')[-1])
60 if server=='sourceforge.net' and base=='download': # XXX Yuck
61 base = urllib2.unquote(path.split('/')[-2])
62 if '#' in base: base, fragment = base.split('#',1)
65 def distros_for_url(url, metadata=None):
66 """Yield egg or source distribution objects that might be found at a URL"""
67 base, fragment = egg_info_for_url(url)
68 for dist in distros_for_location(url, base, metadata): yield dist
70 match = EGG_FRAGMENT.match(fragment)
72 for dist in interpret_distro_name(
73 url, match.group(1), metadata, precedence = CHECKOUT_DIST
77 def distros_for_location(location, basename, metadata=None):
78 """Yield egg or source distribution objects based on basename"""
79 if basename.endswith('.egg.zip'):
80 basename = basename[:-4] # strip the .zip
81 if basename.endswith('.egg') and '-' in basename:
82 # only one, unambiguous interpretation
83 return [Distribution.from_location(location, basename, metadata)]
84 if basename.endswith('.exe'):
85 win_base, py_ver = parse_bdist_wininst(basename)
86 if win_base is not None:
87 return interpret_distro_name(
88 location, win_base, metadata, py_ver, BINARY_DIST, "win32"
90 # Try source distro extensions (.zip, .tgz, etc.)
92 for ext in EXTENSIONS:
93 if basename.endswith(ext):
94 basename = basename[:-len(ext)]
95 return interpret_distro_name(location, basename, metadata)
96 return [] # no extension matched
98 def distros_for_filename(filename, metadata=None):
99 """Yield possible egg or source distribution objects based on a filename"""
100 return distros_for_location(
101 normalize_path(filename), os.path.basename(filename), metadata
105 def interpret_distro_name(location, basename, metadata,
106 py_version=None, precedence=SOURCE_DIST, platform=None
108 """Generate alternative interpretations of a source distro name
110 Note: if `location` is a filesystem filename, you should call
111 ``pkg_resources.normalize_path()`` on it before passing it to this
114 # Generate alternative interpretations of a source distro name
115 # Because some packages are ambiguous as to name/versions split
116 # e.g. "adns-python-1.1.0", "egenix-mx-commercial", etc.
117 # So, we generate each possible interepretation (e.g. "adns, python-1.1.0"
118 # "adns-python, 1.1.0", and "adns-python-1.1.0, no version"). In practice,
119 # the spurious interpretations should be ignored, because in the event
120 # there's also an "adns" package, the spurious "python-1.1.0" version will
121 # compare lower than any numeric version number, and is therefore unlikely
122 # to match a request for it. It's still a potential problem, though, and
123 # in the long run PyPI and the distutils should go for "safe" names and
124 # versions in distribution archive names (sdist and bdist).
126 parts = basename.split('-')
128 for i,p in enumerate(parts[2:]):
129 if len(p)==5 and p.startswith('py2.'):
130 return # It's a bdist_dumb, not an sdist -- bail out
132 for p in range(1,len(parts)+1):
134 location, metadata, '-'.join(parts[:p]), '-'.join(parts[p:]),
135 py_version=py_version, precedence = precedence,
139 REL = re.compile("""<([^>]*\srel\s*=\s*['"]?([^'">]+)[^>]*)>""", re.I)
140 # this line is here to fix emacs' cruddy broken syntax highlighting
142 def find_external_links(url, page):
143 """Find rel="homepage" and rel="download" links in `page`, yielding URLs"""
145 for match in REL.finditer(page):
146 tag, rel = match.groups()
147 rels = map(str.strip, rel.lower().split(','))
148 if 'homepage' in rels or 'download' in rels:
149 for match in HREF.finditer(tag):
150 yield urlparse.urljoin(url, htmldecode(match.group(1)))
152 for tag in ("<th>Home Page", "<th>Download URL"):
155 match = HREF.search(page,pos)
157 yield urlparse.urljoin(url, htmldecode(match.group(1)))
159 user_agent = "Python-urllib/%s setuptools/%s" % (
160 urllib2.__version__, require('setuptools')[0].version
164 class PackageIndex(Environment):
165 """A distribution index that scans web pages for download URLs"""
167 def __init__(self, index_url="http://pypi.python.org/simple", hosts=('*',),
170 Environment.__init__(self,*args,**kw)
171 self.index_url = index_url + "/"[:not index_url.endswith('/')]
172 self.scanned_urls = {}
173 self.fetched_urls = {}
174 self.package_pages = {}
175 self.allows = re.compile('|'.join(map(translate,hosts))).match
180 def process_url(self, url, retrieve=False):
181 """Evaluate a URL as a possible download, and maybe retrieve it"""
182 if url in self.scanned_urls and not retrieve:
184 self.scanned_urls[url] = True
185 if not URL_SCHEME(url):
186 self.process_filename(url)
189 dists = list(distros_for_url(url))
191 if not self.url_ok(url):
193 self.debug("Found link: %s", url)
195 if dists or not retrieve or url in self.fetched_urls:
197 return # don't need the actual page
199 if not self.url_ok(url):
200 self.fetched_urls[url] = True
203 self.info("Reading %s", url)
204 self.fetched_urls[url] = True # prevent multiple fetch attempts
205 f = self.open_url(url, "Download error: %s -- Some packages may not be found!")
207 self.fetched_urls[f.url] = True
208 if 'html' not in f.headers.get('content-type', '').lower():
209 f.close() # not html, we can't process it
212 base = f.url # handle redirects
215 if url.startswith(self.index_url) and getattr(f,'code',None)!=404:
216 page = self.process_index(url, page)
217 for match in HREF.finditer(page):
218 link = urlparse.urljoin(base, htmldecode(match.group(1)))
219 self.process_url(link)
221 def process_filename(self, fn, nested=False):
222 # process filenames or directories
223 if not os.path.exists(fn):
224 self.warn("Not found: %s", fn)
227 if os.path.isdir(fn) and not nested:
228 path = os.path.realpath(fn)
229 for item in os.listdir(path):
230 self.process_filename(os.path.join(path,item), True)
232 dists = distros_for_filename(fn)
234 self.debug("Found: %s", fn)
237 def url_ok(self, url, fatal=False):
239 if (s and s.group(1).lower()=='file') or self.allows(urlparse.urlparse(url)[1]):
241 msg = "\nLink to % s ***BLOCKED*** by --allow-hosts\n"
243 raise DistutilsError(msg % url)
247 def scan_egg_links(self, search_path):
248 for item in search_path:
249 if os.path.isdir(item):
250 for entry in os.listdir(item):
251 if entry.endswith('.egg-link'):
252 self.scan_egg_link(item, entry)
254 def scan_egg_link(self, path, entry):
255 lines = filter(None, map(str.strip, file(os.path.join(path, entry))))
257 for dist in find_distributions(os.path.join(path, lines[0])):
258 dist.location = os.path.join(path, *lines)
259 dist.precedence = SOURCE_DIST
262 def process_index(self,url,page):
263 """Process the contents of a PyPI page"""
265 # Process a URL to see if it's for a package page
266 if link.startswith(self.index_url):
268 urllib2.unquote, link[len(self.index_url):].split('/')
270 if len(parts)==2 and '#' not in parts[1]:
271 # it's a package page, sanitize and index it
272 pkg = safe_name(parts[0])
273 ver = safe_version(parts[1])
274 self.package_pages.setdefault(pkg.lower(),{})[link] = True
275 return to_filename(pkg), to_filename(ver)
278 # process an index page into the package-page index
279 for match in HREF.finditer(page):
280 scan( urlparse.urljoin(url, htmldecode(match.group(1))) )
282 pkg, ver = scan(url) # ensure this page is in the page index
284 # process individual package page
285 for new_url in find_external_links(url, page):
286 # Process the found URL
287 base, frag = egg_info_for_url(new_url)
288 if base.endswith('.py') and not frag:
290 new_url+='#egg=%s-%s' % (pkg,ver)
292 self.need_version_info(url)
293 self.scan_url(new_url)
296 lambda m: '<a href="%s#md5=%s">%s</a>' % m.group(1,3,2), page
299 return "" # no sense double-scanning non-package pages
303 def need_version_info(self, url):
305 "Page at %s links to .py file(s) without version info; an index "
306 "scan is required.", url
309 def scan_all(self, msg=None, *args):
310 if self.index_url not in self.fetched_urls:
311 if msg: self.warn(msg,*args)
313 "Scanning index of all packages (this may take a while)"
315 self.scan_url(self.index_url)
317 def find_packages(self, requirement):
318 self.scan_url(self.index_url + requirement.unsafe_name+'/')
320 if not self.package_pages.get(requirement.key):
321 # Fall back to safe version of the name
322 self.scan_url(self.index_url + requirement.project_name+'/')
324 if not self.package_pages.get(requirement.key):
325 # We couldn't find the target package, so search the index page too
326 self.not_found_in_index(requirement)
328 for url in list(self.package_pages.get(requirement.key,())):
329 # scan each page that might be related to the desired package
332 def obtain(self, requirement, installer=None):
333 self.prescan(); self.find_packages(requirement)
334 for dist in self[requirement.key]:
335 if dist in requirement:
337 self.debug("%s does not match %s", requirement, dist)
338 return super(PackageIndex, self).obtain(requirement,installer)
344 def check_md5(self, cs, info, filename, tfp):
345 if re.match('md5=[0-9a-f]{32}$', info):
346 self.debug("Validating md5 checksum for %s", filename)
347 if cs.hexdigest()!=info[4:]:
350 raise DistutilsError(
351 "MD5 validation failed for "+os.path.basename(filename)+
352 "; possible download problem?"
355 def add_find_links(self, urls):
356 """Add `urls` to the list that will be prescanned for searches"""
359 self.to_scan is None # if we have already "gone online"
360 or not URL_SCHEME(url) # or it's a local file/directory
361 or url.startswith('file:')
362 or list(distros_for_url(url)) # or a direct package link
364 # then go ahead and process it now
367 # otherwise, defer retrieval till later
368 self.to_scan.append(url)
371 """Scan urls scheduled for prescanning (e.g. --find-links)"""
373 map(self.scan_url, self.to_scan)
374 self.to_scan = None # from now on, go ahead and process immediately
376 def not_found_in_index(self, requirement):
377 if self[requirement.key]: # we've seen at least one distro
378 meth, msg = self.info, "Couldn't retrieve index page for %r"
379 else: # no distros seen for this name, might be misspelled
380 meth, msg = (self.warn,
381 "Couldn't find index page for %r (maybe misspelled?)")
382 meth(msg, requirement.unsafe_name)
385 def download(self, spec, tmpdir):
386 """Locate and/or download `spec` to `tmpdir`, returning a local path
388 `spec` may be a ``Requirement`` object, or a string containing a URL,
389 an existing local filename, or a project/version requirement spec
390 (i.e. the string form of a ``Requirement`` object). If it is the URL
391 of a .py file with an unambiguous ``#egg=name-version`` tag (i.e., one
392 that escapes ``-`` as ``_`` throughout), a trivial ``setup.py`` is
393 automatically created alongside the downloaded file.
395 If `spec` is a ``Requirement`` object or a string containing a
396 project/version requirement spec, this method returns the location of
397 a matching distribution (possibly after downloading it to `tmpdir`).
398 If `spec` is a locally existing file or directory name, it is simply
399 returned unchanged. If `spec` is a URL, it is downloaded to a subpath
400 of `tmpdir`, and the local filename is returned. Various errors may be
401 raised if a problem occurs during downloading.
403 if not isinstance(spec,Requirement):
404 scheme = URL_SCHEME(spec)
406 # It's a url, download it to tmpdir
407 found = self._download_url(scheme.group(1), spec, tmpdir)
408 base, fragment = egg_info_for_url(spec)
409 if base.endswith('.py'):
410 found = self.gen_setup(found,fragment,tmpdir)
412 elif os.path.exists(spec):
413 # Existing file or directory, just return it
417 spec = Requirement.parse(spec)
419 raise DistutilsError(
420 "Not a URL, existing file, or requirement spec: %r" %
423 return getattr(self.fetch_distribution(spec, tmpdir),'location',None)
426 def fetch_distribution(self,
427 requirement, tmpdir, force_scan=False, source=False, develop_ok=False,
430 """Obtain a distribution suitable for fulfilling `requirement`
432 `requirement` must be a ``pkg_resources.Requirement`` instance.
433 If necessary, or if the `force_scan` flag is set, the requirement is
434 searched for in the (online) package index as well as the locally
435 installed packages. If a distribution matching `requirement` is found,
436 the returned distribution's ``location`` is the value you would have
437 gotten from calling the ``download()`` method with the matching
438 distribution's URL or filename. If no matching distribution is found,
439 ``None`` is returned.
441 If the `source` flag is set, only source distributions and source
442 checkout links will be considered. Unless the `develop_ok` flag is
443 set, development and system eggs (i.e., those using the ``.egg-info``
444 format) will be ignored.
446 # process a Requirement
447 self.info("Searching for %s", requirement)
452 # Find a matching distribution; may be called more than once
454 # first try to find a local dist
455 for allow_remote in (False, True):
456 # then try to find a platform-dependent dist
457 for allow_platform_independent in (False, True):
458 for dist in env[req.key]:
459 if dist.precedence==DEVELOP_DIST and not develop_ok:
460 if dist not in skipped:
461 self.warn("Skipping development or system egg: %s",dist)
465 if ((is_local(dist.location) or allow_remote) and
467 ((allow_platform_independent or dist.platform is not None) and
468 (dist.precedence<=SOURCE_DIST or not source))):
473 self.find_packages(requirement)
474 dist = find(self, requirement)
476 if local_index is not None:
477 dist = dist or find(local_index, requirement)
479 if dist is None and self.to_scan is not None:
481 dist = find(self, requirement)
483 if dist is None and not force_scan:
484 self.find_packages(requirement)
485 dist = find(self, requirement)
489 "No local packages or download links found for %s%s",
490 (source and "a source distribution of " or ""),
494 self.info("Best match: %s", dist)
495 return dist.clone(location=self.download(dist.location, tmpdir))
498 def fetch(self, requirement, tmpdir, force_scan=False, source=False):
499 """Obtain a file suitable for fulfilling `requirement`
501 DEPRECATED; use the ``fetch_distribution()`` method now instead. For
502 backward compatibility, this routine is identical but returns the
503 ``location`` of the downloaded distribution instead of a distribution
506 dist = self.fetch_distribution(requirement,tmpdir,force_scan,source)
512 def gen_setup(self, filename, fragment, tmpdir):
513 match = EGG_FRAGMENT.match(fragment)
514 dists = match and [d for d in
515 interpret_distro_name(filename, match.group(1), None) if d.version
518 if len(dists)==1: # unambiguous ``#egg`` fragment
519 basename = os.path.basename(filename)
521 # Make sure the file has been downloaded to the temp dir.
522 if os.path.dirname(filename) != tmpdir:
523 dst = os.path.join(tmpdir, basename)
524 from setuptools.command.easy_install import samefile
525 if not samefile(filename, dst):
526 shutil.copy2(filename, dst)
529 file = open(os.path.join(tmpdir, 'setup.py'), 'w')
531 "from setuptools import setup\n"
532 "setup(name=%r, version=%r, py_modules=[%r])\n"
534 dists[0].project_name, dists[0].version,
535 os.path.splitext(basename)[0]
542 raise DistutilsError(
543 "Can't unambiguously interpret project/version identifier %r; "
544 "any dashes in the name or version should be escaped using "
545 "underscores. %r" % (fragment,dists)
548 raise DistutilsError(
549 "Can't process plain .py files without an '#egg=name-version'"
550 " suffix to enable automatic setup script generation."
554 def _download_to(self, url, filename):
555 self.info("Downloading %s", url)
557 fp, tfp, info = None, None, None
560 url, info = url.split('#', 1)
561 fp = self.open_url(url)
562 if isinstance(fp, urllib2.HTTPError):
563 raise DistutilsError(
564 "Can't download %s: %s %s" % (url, fp.code,fp.msg)
569 bs = self.dl_blocksize
571 if "content-length" in headers:
572 size = int(headers["Content-Length"])
573 self.reporthook(url, filename, blocknum, bs, size)
574 tfp = open(filename,'wb')
581 self.reporthook(url, filename, blocknum, bs, size)
584 if info: self.check_md5(cs, info, filename, tfp)
590 def reporthook(self, url, filename, blocknum, blksize, size):
594 def open_url(self, url, warning=None):
595 if url.startswith('file:'): return local_open(url)
597 return open_with_auth(url)
598 except urllib2.HTTPError, v:
600 except urllib2.URLError, v:
602 except httplib.HTTPException, v:
603 reason = "%s: %s" % (v.__doc__ or v.__class__.__name__, v)
605 self.warn(warning, reason)
607 raise DistutilsError("Download error for %s: %s" % (url, reason))
609 def _download_url(self, scheme, url, tmpdir):
610 # Determine download filename
612 name, fragment = egg_info_for_url(url)
615 name = name.replace('..','.').replace('\\','_')
617 name = "__downloaded__" # default if URL has no path contents
619 if name.endswith('.egg.zip'):
620 name = name[:-4] # strip the extra .zip before download
622 filename = os.path.join(tmpdir,name)
626 if scheme=='svn' or scheme.startswith('svn+'):
627 return self._download_svn(url, filename)
629 return urllib2.url2pathname(urlparse.urlparse(url)[2])
631 self.url_ok(url, True) # raises error if not allowed
632 return self._attempt_download(url, filename)
635 def scan_url(self, url):
636 self.process_url(url, True)
639 def _attempt_download(self, url, filename):
640 headers = self._download_to(url, filename)
641 if 'html' in headers.get('content-type','').lower():
642 return self._download_html(url, headers, filename)
646 def _download_html(self, url, headers, filename):
647 file = open(filename)
650 # Check for a subversion index page
651 if re.search(r'<title>([^- ]+ - )?Revision \d+:', line):
652 # it's a subversion index page:
655 return self._download_svn(url, filename)
656 break # not an index page
659 raise DistutilsError("Unexpected HTML page found at "+url)
661 def _download_svn(self, url, filename):
662 url = url.split('#',1)[0] # remove any fragment for svn's sake
663 self.info("Doing subversion checkout from %s to %s", url, filename)
664 os.system("svn checkout -q %s %s" % (url, filename))
667 def debug(self, msg, *args):
668 log.debug(msg, *args)
670 def info(self, msg, *args):
673 def warn(self, msg, *args):
676 # This pattern matches a character entity reference (a decimal numeric
677 # references, a hexadecimal numeric reference, or a named reference).
678 entity_sub = re.compile(r'&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?').sub
681 if not isinstance(c, int):
683 if c>255: return unichr(c)
686 def decode_entity(match):
687 what = match.group(1)
688 if what.startswith('#x'):
689 what = int(what[2:], 16)
690 elif what.startswith('#'):
693 from htmlentitydefs import name2codepoint
694 what = name2codepoint.get(what, match.group(0))
697 def htmldecode(text):
698 """Decode HTML entities in the given text."""
699 return entity_sub(decode_entity, text)
717 def open_with_auth(url):
718 """Open a urllib2 request, handling HTTP authentication"""
720 scheme, netloc, path, params, query, frag = urlparse.urlparse(url)
722 if scheme in ('http', 'https'):
723 auth, host = urllib.splituser(netloc)
728 auth = "Basic " + urllib2.unquote(auth).encode('base64').strip()
729 new_url = urlparse.urlunparse((scheme,host,path,params,query,frag))
730 request = urllib2.Request(new_url)
731 request.add_header("Authorization", auth)
733 request = urllib2.Request(url)
735 request.add_header('User-Agent', user_agent)
736 fp = urllib2.urlopen(request)
739 # Put authentication info back into request URL if same host,
740 # so that links found on the page will work
741 s2, h2, path2, param2, query2, frag2 = urlparse.urlparse(fp.url)
742 if s2==scheme and h2==host:
743 fp.url = urlparse.urlunparse((s2,netloc,path2,param2,query2,frag2))
759 return url # backward compatibility
762 """Read a local path, with special support for directories"""
763 scheme, server, path, param, query, frag = urlparse.urlparse(url)
764 filename = urllib2.url2pathname(path)
765 if os.path.isfile(filename):
766 return urllib2.urlopen(url)
767 elif path.endswith('/') and os.path.isdir(filename):
769 for f in os.listdir(filename):
771 body = open(os.path.join(filename,f),'rb').read()
773 elif os.path.isdir(os.path.join(filename,f)):
775 files.append("<a href=%r>%s</a>" % (f,f))
777 body = ("<html><head><title>%s</title>" % url) + \
778 "</head><body>%s</body></html>" % '\n'.join(files)
779 status, message = 200, "OK"
781 status, message, body = 404, "Path not found", "Not found"
783 return urllib2.HTTPError(url, status, message,
784 {'content-type':'text/html'}, cStringIO.StringIO(body))
798 # this line is a kludge to keep the trailing blank lines for pje's editor