def _upload_package( self, package_uri: str, package_path: str, include_parent_dir: Optional[bool] = False, excludes: Optional[List[str]] = None, ) -> bool: logger.info(f"Uploading package {package_uri}.") with tempfile.TemporaryDirectory() as tmp_dir: protocol, package_name = uri_to_http_components(package_uri) package_file = Path(tmp_dir) / package_name create_package( package_path, package_file, include_parent_dir=include_parent_dir, excludes=excludes, ) try: r = self._do_request( "PUT", f"/api/packages/{protocol}/{package_name}", data=package_file.read_bytes(), ) if r.status_code != 200: self._raise_error(r) finally: package_file.unlink()
def _package_exists( self, package_uri: str, ) -> bool: protocol, package_name = uri_to_http_components(package_uri) r = self._do_request("GET", f"/api/packages/{protocol}/{package_name}") if r.status_code == 200: logger.debug(f"Package {package_uri} already exists.") return True elif r.status_code == 404: logger.debug(f"Package {package_uri} does not exist.") return False else: self._raise_error(r)
def test_uri_to_http_and_back(): assert uri_to_http_components("gcs://hello.zip") == ("gcs", "hello.zip") assert uri_to_http_components("gcs://hello.whl") == ("gcs", "hello.whl") with pytest.raises(ValueError, match="'blah' is not a valid Protocol"): uri_to_http_components("blah://halb.zip") with pytest.raises(ValueError, match="does not end in .zip or .whl"): assert uri_to_http_components("gcs://hello.not_zip") with pytest.raises(ValueError, match="does not end in .zip or .whl"): assert uri_to_http_components("gcs://hello") assert http_uri_components_to_uri("gcs", "hello.zip") == "gcs://hello.zip" assert http_uri_components_to_uri("blah", "halb.zip") == "blah://halb.zip" assert http_uri_components_to_uri("blah", "halb.whl") == "blah://halb.whl" for original_uri in ["gcs://hello.zip", "gcs://fasdf.whl"]: new_uri = http_uri_components_to_uri( *uri_to_http_components(original_uri)) assert new_uri == original_uri