Skip to content

Commit 973f970

Browse files
committed
feat: support not oversyncing appwrite file
1 parent fc42d72 commit 973f970

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

appwrite_lab/test_suite/fixtures.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from pathlib import Path
22

3-
import pytest
43
from appwrite_lab.labs import Labs
54
from appwrite_lab.models import Lab
65

6+
import hashlib
7+
import pytest
8+
79

810
@pytest.fixture(scope="session")
911
def lab_svc():
@@ -42,19 +44,37 @@ def lab_config():
4244
def lab(lab_svc: Labs, appwrite_file: Path, lab_config: dict) -> Lab:
4345
"""Create or get existing lab with optional appwrite.json sync."""
4446
lab_name = lab_config["name"]
47+
hash_file_path = Path.home() / ".config" / "appwrite-lab" / "json_hashes"
48+
hash_file_path.touch()
4549

4650
if lab := lab_svc.get_lab(lab_name):
51+
# Check if the file has changed before unnecessary sync
4752
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)
5162
return lab
5263

5364
res = lab_svc.new(**lab_config)
5465

5566
if appwrite_file and appwrite_file.exists():
67+
hash_file_path.write_text(hash_file(appwrite_file))
5668
lab_svc.sync_with_appwrite_config(name=lab_name, appwrite_json=appwrite_file)
5769

5870
if not res.error:
5971
return lab_svc.get_lab(lab_name)
6072
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

Comments
 (0)