|
1 | 1 | from pathlib import Path
|
2 | 2 |
|
3 |
| -import pytest |
4 | 3 | from appwrite_lab.labs import Labs
|
5 | 4 | from appwrite_lab.models import Lab
|
6 | 5 |
|
| 6 | +import hashlib |
| 7 | +import pytest |
| 8 | + |
7 | 9 |
|
8 | 10 | @pytest.fixture(scope="session")
|
9 | 11 | def lab_svc():
|
@@ -42,19 +44,37 @@ def lab_config():
|
42 | 44 | def lab(lab_svc: Labs, appwrite_file: Path, lab_config: dict) -> Lab:
|
43 | 45 | """Create or get existing lab with optional appwrite.json sync."""
|
44 | 46 | lab_name = lab_config["name"]
|
| 47 | + hash_file_path = Path.home() / ".config" / "appwrite-lab" / "json_hashes" |
| 48 | + hash_file_path.touch() |
45 | 49 |
|
46 | 50 | if lab := lab_svc.get_lab(lab_name):
|
| 51 | + # Check if the file has changed before unnecessary sync |
47 | 52 | if appwrite_file and appwrite_file.exists():
|
48 |
| - lab_svc.sync_with_appwrite_config( |
49 |
| - name=lab_name, appwrite_json=appwrite_file |
50 |
| - ) |
| 53 | + hash = hash_file(appwrite_file) |
| 54 | + data = hash_file_path.read_text() |
| 55 | + if len(data) > 0 and data.strip() == hash: |
| 56 | + print("Skipping sync because the file has not changed") |
| 57 | + else: |
| 58 | + lab_svc.sync_with_appwrite_config( |
| 59 | + name=lab_name, appwrite_json=appwrite_file |
| 60 | + ) |
| 61 | + hash_file_path.write_text(hash) |
51 | 62 | return lab
|
52 | 63 |
|
53 | 64 | res = lab_svc.new(**lab_config)
|
54 | 65 |
|
55 | 66 | if appwrite_file and appwrite_file.exists():
|
| 67 | + hash_file_path.write_text(hash_file(appwrite_file)) |
56 | 68 | lab_svc.sync_with_appwrite_config(name=lab_name, appwrite_json=appwrite_file)
|
57 | 69 |
|
58 | 70 | if not res.error:
|
59 | 71 | return lab_svc.get_lab(lab_name)
|
60 | 72 | raise ValueError(res.message)
|
| 73 | + |
| 74 | + |
| 75 | +def hash_file(path, algo="sha256"): |
| 76 | + h = hashlib.new(algo) |
| 77 | + with open(path, "rb") as f: |
| 78 | + for chunk in iter(lambda: f.read(8192), b""): |
| 79 | + h.update(chunk) |
| 80 | + return h.hexdigest() |
0 commit comments