File size: 1,194 Bytes
c786dae
adf68aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Puts the world's modules and the vendored legion library on sys.path; test helpers."""
from __future__ import annotations

import shutil
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]  # world/
sys.path[:0] = [str(ROOT), str(ROOT / "src")]


class FakeBucket:
    """A bucket in a local directory, with the bucket operations snapshot.py uses."""

    def __init__(self, root: Path):
        self.root = root
        self.ops: list[str] = []

    def upload(self, files):
        self.ops.append("upload")
        for local, rel in files:
            (self.root / rel).parent.mkdir(parents=True, exist_ok=True)
            shutil.copyfile(local, self.root / rel)

    def list(self, prefix):
        return [str(p.relative_to(self.root)) for p in self.root.rglob("*")
                if p.is_file() and str(p.relative_to(self.root)).startswith(prefix)]

    def download(self, files):
        for rel, local in files:
            local.parent.mkdir(parents=True, exist_ok=True)
            shutil.copyfile(self.root / rel, local)

    def delete(self, paths):
        self.ops.append("delete")
        for rel in paths:
            (self.root / rel).unlink()