def _get_cls(cls, client, spec): """ Get object class. Args: client (airfs.storage.github._api.ApiV3): Client. spec (dict): Object spec. Returns: _Model subclass: model. """ ref = spec["keys"][0] if ref == "HEAD": spec["keys"].popleft() return DefaultBranch for obj_cls in ((Commit, Branch, Tag) if len(ref) == 40 else (Branch, Tag, Commit)): obj_spec = spec.copy() obj_spec[obj_cls.KEY] = ref try: client.get(obj_cls.HEAD.format(**obj_spec)) except ObjectNotFoundError: continue return obj_cls raise ObjectNotFoundError(path=spec["full_path"])
def mkdir(path, mode=0o777, *, dir_fd=None): """ Create a directory named path with numeric mode mode. Equivalent to "os.mkdir". .. versionadded:: 1.1.0 Args: path (path-like object): Path or URL. mode (int): The mode parameter is passed to os.mkdir(); see the os.mkdir() description for how it is interpreted. Not supported on storage objects. dir_fd (int): directory descriptors; see the os.remove() description for how it is interpreted. Not supported on storage objects. Raises: FileExistsError : Directory already exists. FileNotFoundError: Parent directory not exists. """ raises_on_dir_fd(dir_fd) system = get_instance(path) relative = system.relpath(path) parent_dir = dirname(relative.rstrip("/")) if parent_dir: parent = "{}{}/".format(path.rsplit(relative, 1)[0], parent_dir) if not system.isdir(parent): raise ObjectNotFoundError(path=parent) if system.isdir(system.ensure_dir_path(path)): raise ObjectExistsError(path=path) system.make_dir(relative, relative=True)
def _list_objects(self, client_kwargs, path, max_results, first_level): """ Lists objects. args: client_kwargs (dict): Client arguments. path (str): Path to list. max_results (int): The maximum results that should return the method. first_level (bool): It True, may only first level objects. Yields: tuple: object path str, object header dict, has content bool """ prefix = self.split_locator(path)[1] index = len(prefix) client_kwargs = self._update_listing_client_kwargs( client_kwargs, max_results) blob = None with _handle_azure_exception(): for blob in self._client_block.list_blobs(prefix=prefix, **client_kwargs): yield blob.name[index:], self._model_to_dict(blob), False if blob is None: raise ObjectNotFoundError(path=path)
def _list(cls, client, spec, first_level=False): """ List tree using recursive then non recursive API. Yields raw results. Args: client (airfs.storage.github._api.ApiV3): Client. spec (dict): Item spec. first_level (bool): It True, returns only first level objects. Yields: tuple: Relative path, Absolute path, spec, headers, has content bool """ if "tree_sha" not in spec: parent = spec["parent"] if spec["object"] == cls else spec["object"] spec["tree_sha"] = parent.head(client, spec)["tree_sha"] cwd = spec.get("path", "").rstrip("/") cwd_index = len(cwd) cwd_seen = not cwd if cwd_index: # Include the ending "/" cwd_index += 1 response = client.get( cls.LIST.format(**spec), never_expire=True, params=dict(recursive=cwd or not first_level), )[0] for headers in response["tree"]: abspath = headers["path"] if cwd: if commonpath((abspath, cwd)) != cwd: continue relpath = abspath[cwd_index:] else: relpath = abspath if not relpath: cls._raise_if_not_dir(headers["type"] == "tree", spec) # Do not yield current working directory itself cwd_seen = True continue yield relpath, abspath, spec, headers, False if not cwd_seen: raise ObjectNotFoundError(path=spec["full_path"])
def _get_dict_model(cls, key, model, spec): """ Get submodel of a dict. Args: key (str): Model key. model (dict): Current model. spec (dict): Partial object spec. Returns: _Model subclass or dict: Next model. """ try: return model[key] except KeyError: raise ObjectNotFoundError(path=spec["full_path"])
def head_obj(cls, client, spec): """ Get asset headers. Args: client (airfs.storage.github._api.ApiV3): Client. spec (dict): Item spec. Returns: dict: Object headers. """ name = spec["asset"] for asset in cls._parent_release(client, spec)["assets"]: if asset["name"] == name: return cls.set_header(asset) raise ObjectNotFoundError(path=spec["full_path"])
def head_obj(cls, client, spec): """ Head the object of this Git tree matching the spec. Only return result directly from current object response as dict. Args: client (airfs.storage.github._api.ApiV3): Client. spec (dict): Item spec. Returns: dict: Object headers. """ path = spec["path"] parent_spec = spec.copy() parent_spec["path"] = dirname(path) for _, abspath, _, headers, _ in cls._list( client, parent_spec, "/" not in path ): if path == abspath: return cls.set_header(headers) raise ObjectNotFoundError(path=spec["full_path"])