|
1 | 1 | import base64
|
| 2 | +import re |
2 | 3 |
|
3 | 4 | import requests
|
4 | 5 |
|
@@ -265,3 +266,70 @@ def _open(
|
265 | 266 | cache_options=cache_options,
|
266 | 267 | **kwargs,
|
267 | 268 | )
|
| 269 | + |
| 270 | + def rm(self, path, recursive=False, maxdepth=None, message=None): |
| 271 | + path = self.expand_path(path, recursive=recursive, maxdepth=maxdepth) |
| 272 | + for p in reversed(path): |
| 273 | + self.rm_file(p, message=message) |
| 274 | + |
| 275 | + def rm_file(self, path, message=None, **kwargs): |
| 276 | + """ |
| 277 | + Remove a file from a specified branch using a given commit message. |
| 278 | +
|
| 279 | + Since Github DELETE operation requires a branch name, and we can't reliably |
| 280 | + determine whether the provided SHA refers to a branch, tag, or commit, we |
| 281 | + assume it's a branch. If it's not, the user will encounter an error when |
| 282 | + attempting to retrieve the file SHA or delete the file. |
| 283 | +
|
| 284 | + Parameters |
| 285 | + ---------- |
| 286 | + path: str |
| 287 | + The file's location relative to the repository root. |
| 288 | + message: str, optional |
| 289 | + The commit message for the deletion. |
| 290 | + """ |
| 291 | + |
| 292 | + if not self.username: |
| 293 | + raise ValueError("Authentication required") |
| 294 | + |
| 295 | + path = self._strip_protocol(path) |
| 296 | + |
| 297 | + # Attempt to get SHA from cache or Github API |
| 298 | + sha = self._get_sha_from_cache(path) |
| 299 | + if not sha: |
| 300 | + url = self.content_url.format( |
| 301 | + org=self.org, repo=self.repo, path=path.lstrip("/"), sha=self.root |
| 302 | + ) |
| 303 | + r = requests.get(url, timeout=self.timeout, **self.kw) |
| 304 | + if r.status_code == 404: |
| 305 | + raise FileNotFoundError(path) |
| 306 | + r.raise_for_status() |
| 307 | + sha = r.json()["sha"] |
| 308 | + |
| 309 | + # Delete the file |
| 310 | + delete_url = self.content_url.format( |
| 311 | + org=self.org, repo=self.repo, path=path, sha=self.root |
| 312 | + ) |
| 313 | + branch = self.root |
| 314 | + data = { |
| 315 | + "message": message or f"Delete {path}", |
| 316 | + "sha": sha, |
| 317 | + **({"branch": branch} if branch else {}), |
| 318 | + } |
| 319 | + |
| 320 | + r = requests.delete(delete_url, json=data, timeout=self.timeout, **self.kw) |
| 321 | + error_message = r.json().get("message", "") |
| 322 | + if re.search(r"Branch .+ not found", error_message): |
| 323 | + error = "Remove only works when the filesystem is initialised from a branch or default (None)" |
| 324 | + raise ValueError(error) |
| 325 | + r.raise_for_status() |
| 326 | + |
| 327 | + self.invalidate_cache(path) |
| 328 | + |
| 329 | + def _get_sha_from_cache(self, path): |
| 330 | + for entries in self.dircache.values(): |
| 331 | + for entry in entries: |
| 332 | + entry_path = entry.get("name") |
| 333 | + if entry_path and entry_path == path and "sha" in entry: |
| 334 | + return entry["sha"] |
| 335 | + return None |
0 commit comments