def test_simple(self): # Given session = self._create_file_session() source = __file__ target = os.path.join(self.prefix, "output.dat") # When resp = session.get("file://{0}".format(os.path.abspath(source))) with open(target, "wb") as fp: for chunk in _ResponseIterator(resp): fp.write(chunk) # Then self.assertEqual(compute_md5(target), compute_md5(source))
def test_simple(self): # Given session = self._create_file_session() source = __file__ target = os.path.join(self.prefix, "output.dat") # When resp = session.get("file://{0}".format(os.path.abspath(source))) with open(target, "wb") as fp: for chunk in _ResponseIterator(resp): fp.write(chunk) # Then self.assertEqual(compute_md5(target), compute_md5(source))
def test_simple(self): # Given source = os.path.join(self.tempdir, "source.data") self._write_content(source, b"data") # When target = os.path.join(self.tempdir, "target.data") with open(target, "wb") as _fp: fp = MD5File(_fp) fp.write(b"data") # Then self.assertEqual(fp.checksum, compute_md5(target)) self.assertEqual(compute_md5(target), compute_md5(source))
def fetch_dist(self, dist, fetch_dir, force=False, dry_run=False): """ Get a distribution, i.e. copy or download the distribution into fetch_dir. force: force download or copy if MD5 mismatches """ info = self.index[dist] repo, fn = dist_naming.split_dist(dist) path = join(fetch_dir, fn) # if force is used, make sure the md5 is the expected, otherwise # only see if the file exists if isfile(path) and (not force or compute_md5(path) == info.get('md5')): if self.verbose: print("Not forcing refetch, %r already matches MD5" % path) return if dry_run: return if self.verbose: print("Fetching: %r" % dist) print(" to: %r" % path) stream_to_file(self.repo_objs[repo].get_data(fn), path, info)
def index_section(zip_path): """ Returns a section corresponding to the zip-file, which can be appended to an index. """ return ('==> %s <==\n' % basename(zip_path) + 'size = %i\n' % getsize(zip_path) + 'md5 = %r\n' % compute_md5(zip_path) + 'mtime = %r\n' % getmtime(zip_path) + commit_from_dist(zip_path) + '\n' + rawspec_from_dist(zip_path) + '\n')
def index_section(zip_path): """ Returns a section corresponding to the zip-file, which can be appended to an index. """ return ('==> %s <==\n' % basename(zip_path) + 'size = %i\n' % getsize(zip_path) + 'md5 = %r\n' % compute_md5(zip_path) + 'mtime = %r\n' % getmtime(zip_path) + commit_from_dist(zip_path) + '\n' + rawspec_from_dist(zip_path) + '\n')
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 from_egg(cls, path, repository_info=None, python_version=PY_VER): """ Create an instance from an egg filename. """ with ZipFile(path) as zp: metadata = info_from_z(zp) repository_info = repository_info or \ FSRepositoryInfo(path_to_uri(os.path.dirname(path))) metadata["packages"] = metadata.get("packages", []) st = os.stat(path) metadata["size"] = st.st_size metadata["md5"] = compute_md5(path) metadata["mtime"] = st.st_mtime return cls.from_json_dict(os.path.basename(path), metadata, repository_info)
def from_egg(cls, path, store_location=""): """ Create an instance from an egg filename. """ with ZipFile(path) as zp: metadata = info_from_z(zp) if len(store_location) == 0: store_location = path_to_uri(os.path.dirname(path)) + "/" if not store_location.endswith("/"): msg = "Invalid uri for store location: {0!r} (expected an uri " \ "ending with '/')".format(store_location) raise ValueError(msg) metadata["packages"] = metadata.get("packages", []) st = os.stat(path) metadata["size"] = st.st_size metadata["md5"] = compute_md5(path) metadata["mtime"] = st.st_mtime metadata["store_location"] = store_location return cls.from_json_dict(os.path.basename(path), metadata)
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)