def handle_(config, data): with Console(verbose=config.get("verbose", False), exit_on_error=True) as c: repo = init_repository(c, config["repository"]) pkg_mgr = PackageManager(repo, console=c, installer=None, is_python3=config.get("python3", False)) downloaded = pkg_mgr.download_by_name(data["obj"], data["dir"]) c.info("Downloaded: {0}".format(downloaded))
def test_upload_download(): pm = PackageManager(repo=Repository(), installer=Installer(), overwrite=False, console=Console(exit_on_error=False)) pkg = Package(name="test_package", version="1.0.0", type="SOURCE") wrong = Package(name="test_package", version="") filename = resource_filename(__name__, "data/test_package-1.0.0.zip") with pytest.raises(InvalidParameter): pm.upload(wrong, filename) pm.upload(pkg, filename) pm.list_items() == [Package.repo_name(pkg, filename)] #expecting cache to be updated pm.list_items(from_cache=True) == [Package.repo_name(pkg, filename)] #uploading again without overwrite mode with pytest.raises(InvalidState): pm.upload(pkg, filename) #downloading by name try: tdir = tempfile.mkdtemp() #non existing assert not pm.download_by_name("fake", tdir) assert not pm.download_by_name( Package.repo_name( Package(name="test_package", version="2.0.0", type="SOURCE"), filename), tdir) #valid dest = pm.download_by_name(Package.repo_name(pkg, filename), tdir) source_md5 = hashlib.md5() with open(filename, "rb") as f: source_md5.update(f.read()) dest_md5 = hashlib.md5() with open(dest, "rb") as f: dest_md5.update(f.read()) assert source_md5.hexdigest() == dest_md5.hexdigest() finally: shutil.rmtree(tdir) #downloading by package with version specified try: tdir = tempfile.mkdtemp() #non existing assert not pm.download(Package(name="fake", version="", type="SOURCE"), tdir, preferred_type="SOURCE") assert not pm.download(Package( name="test_package", version="2.0.0", type="SOURCE"), tdir, preferred_type="SOURCE") dest = pm.download(pkg, tdir, preferred_type="SOURCE") source_md5 = hashlib.md5() with open(filename, "rb") as f: source_md5.update(f.read()) dest_md5 = hashlib.md5() with open(dest, "rb") as f: dest_md5.update(f.read()) assert source_md5.hexdigest() == dest_md5.hexdigest() finally: shutil.rmtree(tdir) #downloading by package without version specified (gets last) try: tdir = tempfile.mkdtemp() dest = pm.download(Package(name="test_package", version="", type="SOURCE"), tdir, preferred_type="SOURCE") source_md5 = hashlib.md5() with open(filename, "rb") as f: source_md5.update(f.read()) dest_md5 = hashlib.md5() with open(dest, "rb") as f: dest_md5.update(f.read()) assert source_md5.hexdigest() == dest_md5.hexdigest() finally: shutil.rmtree(tdir)