Commit 3ed7c06b authored by Jim Fulton's avatar Jim Fulton

tests now pass on Python 2.6, 2.7 and 3.2

parent 44046ba2
...@@ -18,7 +18,7 @@ The script accepts buildout command-line options, so you can ...@@ -18,7 +18,7 @@ The script accepts buildout command-line options, so you can
use the -c option to specify an alternate configuration file. use the -c option to specify an alternate configuration file.
""" """
import os, shutil, sys, tempfile, urllib.request, urllib.error, urllib.parse import os, shutil, sys, tempfile
from optparse import OptionParser from optparse import OptionParser
tmpeggs = tempfile.mkdtemp() tmpeggs = tempfile.mkdtemp()
...@@ -80,9 +80,13 @@ try: ...@@ -80,9 +80,13 @@ try:
raise ImportError raise ImportError
except ImportError: except ImportError:
ez = {} ez = {}
exec(urllib.request.urlopen(
'http://python-distribute.org/distribute_setup.py' try:
).read(), ez) from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
exec(urlopen('http://python-distribute.org/distribute_setup.py').read(), ez)
setup_args = dict(to_dir=tmpeggs, download_delay=0, no_fake=True) setup_args = dict(to_dir=tmpeggs, download_delay=0, no_fake=True)
ez['use_setuptools'](**setup_args) ez['use_setuptools'](**setup_args)
......
...@@ -17,7 +17,7 @@ This is different from a normal boostrapping process because the ...@@ -17,7 +17,7 @@ This is different from a normal boostrapping process because the
buildout egg itself is installed as a develop egg. buildout egg itself is installed as a develop egg.
""" """
import os, shutil, sys, subprocess, urllib.request, urllib.error, urllib.parse import os, shutil, sys, subprocess
for d in 'eggs', 'develop-eggs', 'bin', 'parts': for d in 'eggs', 'develop-eggs', 'bin', 'parts':
if not os.path.exists(d): if not os.path.exists(d):
...@@ -49,16 +49,24 @@ try: ...@@ -49,16 +49,24 @@ try:
except ImportError: except ImportError:
pass pass
else: else:
raise SystemError( message = (
"Buildout development with a pre-installed setuptools or " "Buildout development with a pre-installed setuptools or "
"distribute is not supported.%s" "distribute is not supported."
% ('' if nosite else ' Try running with -S option to Python.')) )
if not nosite:
message += ' Try running with -S option to Python.'
raise SystemError(message)
###################################################################### ######################################################################
# Install distribute # Install distribute
ez = {} ez = {}
exec(urllib.request.urlopen(
'http://python-distribute.org/distribute_setup.py').read(), ez) try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
exec(urlopen('http://python-distribute.org/distribute_setup.py').read(), ez)
ez['use_setuptools'](to_dir='eggs', download_delay=0) ez['use_setuptools'](to_dir='eggs', download_delay=0)
import pkg_resources import pkg_resources
......
...@@ -79,11 +79,7 @@ Now let's try with `2.0.0`, which happens to exist:: ...@@ -79,11 +79,7 @@ Now let's try with `2.0.0`, which happens to exist::
... zc.buildout.easy_install._safe_arg(sys.executable)+' '+ ... zc.buildout.easy_install._safe_arg(sys.executable)+' '+
... 'bootstrap.py --version 2.0.0')); print_('X') ... 'bootstrap.py --version 2.0.0')); print_('X')
... # doctest: +ELLIPSIS ... # doctest: +ELLIPSIS
X X...Generated script '/sample/bin/buildout'...X
...
Generated script '/sample/bin/buildout'.
...
X
Let's make sure the generated `buildout` script uses it:: Let's make sure the generated `buildout` script uses it::
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
import zc.buildout.easy_install import zc.buildout.easy_install
no_site = zc.buildout.easy_install.no_site no_site = zc.buildout.easy_install.no_site
from .rmtree import rmtree from zc.buildout.rmtree import rmtree
from hashlib import md5 from hashlib import md5
try: try:
......
...@@ -17,6 +17,24 @@ try: ...@@ -17,6 +17,24 @@ try:
from hashlib import md5 from hashlib import md5
except ImportError: except ImportError:
from md5 import new as md5 from md5 import new as md5
try:
# Python 3
from urllib.request import FancyURLopener, URLopener, urlretrieve
from urllib.parse import urlparse
from urllib import request as urllib # for monkey patch below :(
except ImportError:
# Python 2
from urllib import FancyURLopener, URLopener, urlretrieve
from urlparse import urlparse
import urllib
class URLOpener(FancyURLopener):
http_error_default = URLopener.http_error_default
urllib._urlopener = URLOpener() # Ook! Monkey patch!
from zc.buildout.easy_install import realpath from zc.buildout.easy_install import realpath
import logging import logging
import os import os
...@@ -25,22 +43,11 @@ import re ...@@ -25,22 +43,11 @@ import re
import shutil import shutil
import sys import sys
import tempfile import tempfile
import urllib.request, urllib.parse, urllib.error
import urllib.parse
import zc.buildout import zc.buildout
class URLOpener(urllib.request.FancyURLopener):
http_error_default = urllib.request.URLopener.http_error_default
class ChecksumError(zc.buildout.UserError): class ChecksumError(zc.buildout.UserError):
pass pass
url_opener = URLOpener()
class Download(object): class Download(object):
"""Configurable download utility. """Configurable download utility.
...@@ -158,7 +165,7 @@ class Download(object): ...@@ -158,7 +165,7 @@ class Download(object):
if re.match(r"^[A-Za-z]:\\", url): if re.match(r"^[A-Za-z]:\\", url):
url = 'file:' + url url = 'file:' + url
parsed_url = urllib.parse.urlparse(url, 'file') parsed_url = urlparse(url, 'file')
url_scheme, _, url_path = parsed_url[:3] url_scheme, _, url_path = parsed_url[:3]
if url_scheme == 'file': if url_scheme == 'file':
self.logger.debug('Using local resource %s' % url) self.logger.debug('Using local resource %s' % url)
...@@ -173,10 +180,9 @@ class Download(object): ...@@ -173,10 +180,9 @@ class Download(object):
"Couldn't download %r in offline mode." % url) "Couldn't download %r in offline mode." % url)
self.logger.info('Downloading %s' % url) self.logger.info('Downloading %s' % url)
urllib.request._urlopener = url_opener
handle, tmp_path = tempfile.mkstemp(prefix='buildout-') handle, tmp_path = tempfile.mkstemp(prefix='buildout-')
try: try:
tmp_path, headers = urllib.request.urlretrieve(url, tmp_path) tmp_path, headers = urlretrieve(url, tmp_path)
if not check_md5sum(tmp_path, md5sum): if not check_md5sum(tmp_path, md5sum):
raise ChecksumError( raise ChecksumError(
'MD5 checksum mismatch downloading %r' % url) 'MD5 checksum mismatch downloading %r' % url)
...@@ -206,7 +212,7 @@ class Download(object): ...@@ -206,7 +212,7 @@ class Download(object):
else: else:
if re.match(r"^[A-Za-z]:\\", url): if re.match(r"^[A-Za-z]:\\", url):
url = 'file:' + url url = 'file:' + url
parsed = urllib.parse.urlparse(url, 'file') parsed = urlparse(url, 'file')
url_path = parsed[2] url_path = parsed[2]
if parsed[0] == 'file': if parsed[0] == 'file':
......
...@@ -256,11 +256,11 @@ ChecksumError: MD5 checksum mismatch downloading 'http://localhost/foo.txt' ...@@ -256,11 +256,11 @@ ChecksumError: MD5 checksum mismatch downloading 'http://localhost/foo.txt'
If the file is completely missing it should notify the user of the error: If the file is completely missing it should notify the user of the error:
>>> download(server_url+'bar.txt') # doctest: +NORMALIZE_WHITESPACE >>> download(server_url+'bar.txt') # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
... ...
UserError: Error downloading extends for URL http://localhost/bar.txt: UserError: Error downloading extends for URL http://localhost/bar.txt:
HTTP Error 404: Not Found ...404...
>>> ls(cache) >>> ls(cache)
Finally, let's see what happens if the download cache to be used doesn't exist Finally, let's see what happens if the download cache to be used doesn't exist
......
...@@ -25,7 +25,10 @@ import sys ...@@ -25,7 +25,10 @@ import sys
# handle -S # handle -S
def normpath(p): def normpath(p):
return p[:-1] if p.endswith(os.path.sep) else p if p.endswith(os.path.sep):
return p[:-1]
else:
return p
no_site = 'site' not in sys.modules no_site = 'site' not in sys.modules
if no_site: if no_site:
...@@ -1075,7 +1078,7 @@ def _create_script(contents, dest): ...@@ -1075,7 +1078,7 @@ def _create_script(contents, dest):
logger.info("Generated script %r.", script) logger.info("Generated script %r.", script)
try: try:
os.chmod(dest, 0o755) os.chmod(dest, 493) # 0755
except (AttributeError, os.error): except (AttributeError, os.error):
pass pass
...@@ -1143,7 +1146,7 @@ def _pyscript(path, dest, rsetup): ...@@ -1143,7 +1146,7 @@ def _pyscript(path, dest, rsetup):
if changed: if changed:
open(dest, 'w').write(contents) open(dest, 'w').write(contents)
try: try:
os.chmod(dest,0o755) os.chmod(dest, 493) # 0755
except (AttributeError, os.error): except (AttributeError, os.error):
pass pass
logger.info("Generated interpreter %r.", script) logger.info("Generated interpreter %r.", script)
......
...@@ -42,8 +42,7 @@ This document tests the 3rd option. ...@@ -42,8 +42,7 @@ This document tests the 3rd option.
... system("%s -S %s init demo" % (sys.executable, bootstrap_py)), ... system("%s -S %s init demo" % (sys.executable, bootstrap_py)),
... end='\n===') ... end='\n===')
... # doctest: +ELLIPSIS ... # doctest: +ELLIPSIS
X... X...Creating '/sample/buildout.cfg'.
Creating '/sample/buildout.cfg'.
Creating directory '/sample/bin'. Creating directory '/sample/bin'.
Creating directory '/sample/parts'. Creating directory '/sample/parts'.
Creating directory '/sample/eggs'. Creating directory '/sample/eggs'.
......
...@@ -54,7 +54,7 @@ def rmtree (path): ...@@ -54,7 +54,7 @@ def rmtree (path):
0 0
""" """
def retry_writeable (func, path, exc): def retry_writeable (func, path, exc):
os.chmod (path, 0o600) os.chmod (path, 384) # 0600
func (path) func (path)
shutil.rmtree (path, onerror = retry_writeable) shutil.rmtree (path, onerror = retry_writeable)
......
...@@ -14,7 +14,15 @@ ...@@ -14,7 +14,15 @@
"""Various test-support utility functions """Various test-support utility functions
""" """
import http.server try:
# Python 3
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.request import urlopen
except ImportError:
# Python 2
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from urllib2 import urlopen
import errno import errno
import logging import logging
import os import os
...@@ -28,7 +36,6 @@ import sys ...@@ -28,7 +36,6 @@ import sys
import tempfile import tempfile
import threading import threading
import time import time
import urllib.request, urllib.error, urllib.parse
import zc.buildout.buildout import zc.buildout.buildout
import zc.buildout.easy_install import zc.buildout.easy_install
...@@ -106,7 +113,7 @@ def system(command, input=''): ...@@ -106,7 +113,7 @@ def system(command, input=''):
return result.decode() return result.decode()
def get(url): def get(url):
return urllib.request.urlopen(url).read().decode() return str(urlopen(url).read().decode())
def _runsetup(setup, *args): def _runsetup(setup, *args):
if os.path.isdir(setup): if os.path.isdir(setup):
...@@ -265,10 +272,10 @@ def buildoutTearDown(test): ...@@ -265,10 +272,10 @@ def buildoutTearDown(test):
for f in test.globs['__tear_downs']: for f in test.globs['__tear_downs']:
f() f()
class Server(http.server.HTTPServer): class Server(HTTPServer):
def __init__(self, tree, *args): def __init__(self, tree, *args):
http.server.HTTPServer.__init__(self, *args) HTTPServer.__init__(self, *args)
self.tree = os.path.abspath(tree) self.tree = os.path.abspath(tree)
__run = True __run = True
...@@ -279,15 +286,14 @@ class Server(http.server.HTTPServer): ...@@ -279,15 +286,14 @@ class Server(http.server.HTTPServer):
def handle_error(self, *_): def handle_error(self, *_):
self.__run = False self.__run = False
class Handler(http.server.BaseHTTPRequestHandler): class Handler(BaseHTTPRequestHandler):
Server.__log = False Server.__log = False
def __init__(self, request, address, server): def __init__(self, request, address, server):
self.__server = server self.__server = server
self.tree = server.tree self.tree = server.tree
http.server.BaseHTTPRequestHandler.__init__( BaseHTTPRequestHandler.__init__(self, request, address, server)
self, request, address, server)
def do_GET(self): def do_GET(self):
if '__stop__' in self.path: if '__stop__' in self.path:
...@@ -382,7 +388,7 @@ def start_server(tree): ...@@ -382,7 +388,7 @@ def start_server(tree):
def stop_server(url, thread=None): def stop_server(url, thread=None):
try: try:
urllib.request.urlopen(url+'__stop__') urlopen(url+'__stop__')
except Exception: except Exception:
pass pass
if thread is not None: if thread is not None:
......
...@@ -1077,13 +1077,6 @@ because of the missing target file. ...@@ -1077,13 +1077,6 @@ because of the missing target file.
>>> write('recipe', 'some-file', '1') >>> write('recipe', 'some-file', '1')
>>> os.symlink(join('recipe', 'some-file'), >>> os.symlink(join('recipe', 'some-file'),
... join('recipe', 'another-file')) ... join('recipe', 'another-file'))
>>> ls('recipe')
l another-file
- foo.py
d recipe.egg-info
- setup.py
- some-file
>>> remove('recipe', 'some-file') >>> remove('recipe', 'some-file')
>>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='') >>> print_(system(join(sample_buildout, 'bin', 'buildout')), end='')
......
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