def __init__(self, remote_repository, session, prefixes=[sys.prefix], progress_context=None, force=False, max_retries=_DEFAULT_MAX_RETRIES, runtime_info=None): self.prefixes = prefixes self.top_prefix = prefixes[0] self._runtime_info = ( runtime_info or _default_runtime_info(self.top_prefix) ) self._remote_repository = remote_repository self._installed_repository = Repository._from_prefixes(self.prefixes) self._top_installed_repository = \ Repository._from_prefixes([self.top_prefix]) self._session = session self._downloader = _DownloadManager(session, remote_repository) self._progress_context = progress_context or \ ProgressBarContext(dummy_progress_bar_factory) self._force = force self.max_retries = max_retries
def _downloader_factory(self, paths): repository = Repository() for path in paths: package = RepositoryPackageMetadata.from_egg(path) repository.add_package(package) return (_DownloadManager(Session(DummyAuthenticator(), self.tempdir), repository), repository)
def test_fetch_egg_refetch(self): # Given egg = "nose-1.3.0-1.egg" repository = self._create_store_and_repository([egg]) # When downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(egg) # Then target = os.path.join(self.tempdir, egg) self.assertTrue(os.path.exists(target))
def test_fetch_invalid_md5(self): # Given filename = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, filename) repository = Repository() package = RepositoryPackageMetadata.from_egg(path) package.md5 = "a" * 32 repository.add_package(package) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) with self.assertRaises(InvalidChecksum): downloader.fetch(filename)
def test_fetch_simple(self): # Given filename = "nose-1.3.0-1.egg" repository = self._create_store_and_repository([filename]) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(filename) # Then target = os.path.join(self.tempdir, filename) self.assertTrue(os.path.exists(target)) self.assertEqual(compute_md5(target), repository.find_package("nose", "1.3.0-1").md5)
def _retry_common_setup(self): store_location = "http://acme.com/" filename = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, filename) repository = Repository() package = RepositoryPackageMetadata.from_egg(path, store_location=store_location) repository.add_package(package) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) return path, downloader, repository
def _retry_common_setup(self): store_url = "http://acme.com" repository_info = OldstyleRepositoryInfo(store_url) filename = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, filename) repository = Repository() package = RemotePackageMetadata.from_egg(path, repository_info) repository.add_package(package) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) return path, downloader, repository
def test_fetch_abort(self): # Given filename = "nose-1.3.0-1.egg" repository = self._create_store_and_repository([filename]) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) target = os.path.join(self.tempdir, filename) # When context = downloader.iter_fetch(filename) for i, chunk in enumerate(context): if i == 1: context.cancel() break # Then self.assertFalse(os.path.exists(target))
def test_fetch_unauthorized(self): # Given filename = "nose-1.3.0-1.egg" url = "http://api.enthought.com/eggs/yoyo/" repository = Repository() path = os.path.join(_EGGINST_COMMON_DATA, filename) package = RepositoryPackageMetadata.from_egg(path, url) repository.add_package(package) responses.add(responses.GET, url + filename, body='{"error": "forbidden"}', status=403) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) # When/Then with self.assertRaises(requests.exceptions.HTTPError): downloader.fetch(filename)
def __init__(self, remote_repository, session, prefixes=[sys.prefix], progress_context=None, force=False, max_retries=_DEFAULT_MAX_RETRIES): self.prefixes = prefixes self.top_prefix = prefixes[0] self._remote_repository = remote_repository self._installed_repository = Repository._from_prefixes(self.prefixes) self._top_installed_repository = \ Repository._from_prefixes([self.top_prefix]) self._session = session self._downloader = _DownloadManager(session, remote_repository) self._progress_context = progress_context or \ ProgressBarContext(dummy_progress_bar_factory) self._force = force self.max_retries = max_retries
def test_fetch_egg_refetch_invalid_md5(self): # Given egg = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, egg) repository = self._create_store_and_repository([egg]) package = repository.find_package("nose", "1.3.0-1") def _corrupt_file(target): with open(target, "wb") as fo: fo.write(b"") # When downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(package) # Then target = os.path.join(self.tempdir, egg) self.assertEqual(compute_md5(target), compute_md5(path)) # When _corrupt_file(target) # Then self.assertNotEqual(compute_md5(target), compute_md5(path)) # When downloader.fetch(package, force=True) # Then self.assertEqual(compute_md5(target), compute_md5(path)) # When/Then # Ensure we deal correctly with force=False when the egg is already # there. downloader.fetch(package, force=False)
def test_fetch_egg_refetch_invalid_md5(self): # Given egg = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, egg) repository = self._create_store_and_repository([egg]) def _corrupt_file(target): with open(target, "wb") as fo: fo.write(b"") # When downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(egg) # Then target = os.path.join(self.tempdir, egg) self.assertEqual(compute_md5(target), compute_md5(path)) # When _corrupt_file(target) # Then self.assertNotEqual(compute_md5(target), compute_md5(path)) # When downloader.fetch(egg, force=True) # Then self.assertEqual(compute_md5(target), compute_md5(path)) # When/Then # Ensure we deal correctly with force=False when the egg is already # there. downloader.fetch(egg, force=False)
def inplace_update(session, repository, latest): downloader = _DownloadManager(session, repository) downloader.fetch(latest.key) target = downloader._path(latest.key) with zipfile.ZipFile(target) as zp: try: bootstrap_code = zp.read(BOOTSTRAP_ARCNAME) logger.debug("Using bootstrap code from egg") except KeyError: logger.debug("Using default bootstrap code") bootstrap_code = _DEFAULT_BOOTSTRAP_CODE d = os.path.dirname(target) script_path = os.path.join(d, "bootstrap.py") with open(script_path, "wt") as fp: fp.write(bootstrap_code) cmd = [sys.executable, script_path, target] ret = subprocess.call(cmd) if ret != 0: raise ValueError("Failed to run upgrade/bootstrap code.")