Commit e01e634a authored by Julien Muchembled's avatar Julien Muchembled

download*: new 'offline' option to override ${buildout:offline}

parent c4362bb8
......@@ -342,6 +342,12 @@ In case of checksum mismatch::
Error: MD5 checksum mismatch downloading '...'
>>> ls('parts')
option: offline
---------------
Boolean option that can be specified to override `${buildout:offline}`.
option: alternate-url
---------------------
......
......@@ -145,8 +145,10 @@ class Script(EnvironMixin):
def download(self, *args, **kw):
if not (args or 'url' in kw):
args = self.options['url'], self.options.get('md5sum') or None
offline = kw.pop('offline', None)
path, is_temp = zc.buildout.download.Download(self.buildout['buildout'],
hash_name=True)(*args, **kw)
hash_name=True, **({} if offline is None else {'offline': offline})
)(*args, **kw)
if is_temp:
self.cleanup_list.append(path)
return path
......
import doctest
from zope.testing import renormalizing
import os
import re
import shutil
import tarfile
import tempfile
import unittest
import zc.buildout.testing
from zc.buildout.download import check_md5sum, ChecksumError
from zc.buildout.testing import buildoutTearDown
from contextlib import contextmanager
from copy import deepcopy
from functools import wraps
from subprocess import check_call, check_output, CalledProcessError, STDOUT
from .. import download, gitclone, make_read_only_recursively
import zc.buildout.testing
from zc.buildout.download import check_md5sum, ChecksumError
from zc.buildout.testing import buildoutTearDown
from zope.testing import renormalizing
from .. import download, downloadunpacked, gitclone, make_read_only_recursively
optionflags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE)
......@@ -99,6 +102,43 @@ class DownloadTests(TestCase):
self.assertRaises(ChecksumError,
download.Recipe(self.buildout, 'fail', options).install)
@with_buildout
def test_offline(self, write, start_server, **kw):
data = 'bar'
foo = os.path.join(self.dir, 'foo')
write(foo, data)
url = start_server(self.dir) + 'foo'
def check(Recipe, target):
def check(offline_error, buildout_offline=None, options_offline=None):
buildout = deepcopy(self.buildout)
if buildout_offline:
buildout['buildout']['offline'] = buildout_offline
options = {'url': url}
if options_offline:
options['offline'] = options_offline
try:
Recipe(buildout, 'offline', options).install()
except zc.buildout.UserError:
if not offline_error:
raise
else:
self.assertFalse(offline_error)
with open(target(options)) as f:
self.assertEqual(f.read(), data)
check(False)
check(False, 'false')
check(True, 'true')
check(False, None, 'false')
check(True, None, 'true')
check(False, 'true', 'false')
check(True, 'false', 'true')
check(download.Recipe, lambda options: options['target'])
with tarfile.open(os.path.join(foo + '.tar'), 'w') as tar:
tar.add(foo, 'foo')
url += '.tar'
check(downloadunpacked.Recipe, lambda options:
os.path.join(options['target'], 'foo'))
class GitCloneNonInformativeTests(TestCase):
......
......@@ -27,7 +27,7 @@
import errno
import os
from zc.buildout import download, UserError
from . import Shared
from . import is_true, Shared
class Recipe(object):
......@@ -38,6 +38,8 @@ class Recipe(object):
self._url = options['url']
self._alternate = options.get('alternate-url')
self._md5sum = options.get('md5sum') or None
offline = options.get('offline')
self._offline = is_true(offline) if offline else None
# 'mode' option was dropped; don't reimplement it
# an 'executable' boolean option may be acceptable
if 'mode' in options:
......@@ -75,10 +77,12 @@ class Recipe(object):
return [destination]
def _download(self):
offline = self._offline
kw = {} if offline is None else {'offline': offline}
dl = download.Download(self._buildout, hash_name=True, **kw)
alternate = self._alternate
path, is_temp = download.Download(self._buildout, hash_name=True)(
self._url, self._md5sum, self._destination,
**({'alternate_url': alternate} if alternate else {}))
kw = {'alternate_url': alternate} if alternate else {}
path, is_temp = dl(self._url, self._md5sum, self._destination, **kw)
assert path == self._destination and not is_temp, (
"SlapOS buildout >= 2.7.1+slapos014 needed")
......
......@@ -61,10 +61,12 @@ class Recipe(EnvironMixin):
# inside slapos.buildout (see networkcache support)
from zc.buildout import download
location = self._shared.location
offline = self.options.get('offline')
kw = {'offline': is_true(offline)} if offline else {}
dl = download.Download(self.buildout['buildout'], hash_name=True, **kw)
alternate = self.options.get('alternate-url')
path, is_temp = download.Download(self.buildout['buildout'],
hash_name=True)(self._url, self.options.get('md5sum') or None,
**({'alternate_url': alternate} if alternate else {}))
kw = {'alternate_url': alternate} if alternate else {}
path, is_temp = dl(self._url, self.options.get('md5sum') or None, **kw)
try:
unpack_archive(self, path, location)
finally:
......
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