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