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