def test_list_local_url(self): dir_path = DATA_DIR / 'dir' contents = set(list(data_transfer.list_url(PhysicalKey.from_path(dir_path)))) assert contents == set([ ('foo.txt', 4), ('x/blah.txt', 6) ])
def test_list_local_url(self): dir_path = DATA_DIR / 'dir' contents = set(list(data_transfer.list_url(dir_path.as_uri()))) assert contents == set([ ('foo.txt', 4), ('x/blah.txt', 6) ])
def delete_package(self, pkg_name: str, top_hash: str = None): package_path = self.pointers_dir(pkg_name) paths = list(list_url(package_path)) if not paths: raise QuiltException( "No such package exists in the given directory.") if top_hash is not None: top_hash = self.resolve_top_hash(pkg_name, top_hash) deleted = [] remaining = [] for path, _ in paths: pkg_hash = get_bytes(self.pointer_pk(pkg_name, path)).decode() (deleted if pkg_hash == top_hash else remaining).append(path) if not deleted: raise QuiltException( "No such package version exists in the given directory.") for path in deleted: delete_url(self.pointer_pk(pkg_name, path)) if 'latest' in deleted and remaining: # Create a new "latest". Technically, we need to compare numerically, # but string comparisons will be fine till year 2286. new_latest = max(remaining) copy_file(self.pointer_pk(pkg_name, new_latest), self.pointer_latest_pk(pkg_name)) else: for path, _ in paths: delete_url(self.pointer_pk(pkg_name, path))
def list_packages(self): prev_pkg = None for path, _ in list_url(self.pointers_global_dir): pkg = path.rpartition('/')[0] # A package can have multiple versions, but we should only return the name once. if pkg != prev_pkg: prev_pkg = pkg yield pkg
def shorten_top_hash(self, pkg_name: str, top_hash: str) -> str: min_shorthash_len = 7 matches = [self._top_hash_from_path(h) for h, _ in list_url(self.manifests_package_dir(pkg_name)) if h.startswith(top_hash[:min_shorthash_len])] if len(matches) == 0: raise ValueError(f"Tophash {top_hash} was not found in registry {self.base}") for prefix_length in range(min_shorthash_len, 64): potential_shorthash = top_hash[:prefix_length] matches = [h for h in matches if h.startswith(potential_shorthash)] if len(matches) == 1: return potential_shorthash
def resolve_top_hash(self, pkg_name: str, hash_prefix: str) -> str: if len(hash_prefix) == 64: top_hash = hash_prefix elif 6 <= len(hash_prefix) < 64: matching_hashes = [self._top_hash_from_path(h) for h, _ in list_url(self.manifests_package_dir(pkg_name)) if h.startswith(hash_prefix)] if not matching_hashes: raise QuiltException("Found zero matches for %r" % hash_prefix) elif len(matching_hashes) > 1: raise QuiltException("Found multiple matches: %r" % hash_prefix) else: top_hash = matching_hashes[0] else: raise QuiltException("Invalid hash: %r" % hash_prefix) # TODO: verify that name is correct with respect to this top_hash return top_hash
def list_package_pointers(self, pkg_name: str): package_dir = self.pointers_dir(pkg_name) for path, _ in list_url(package_dir): pkg_hash = get_bytes(package_dir.join(path)) yield path, pkg_hash.decode().strip()