Skip to content

Commit b965b83

Browse files
committed
Added wrapper functions for refreshing latest version, quota usage.
1 parent fec30a1 commit b965b83

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

src/pygobbler/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@
3535
from .clone_version import clone_version
3636
from .service_info import service_info
3737
from .version_path import version_path
38+
from .refresh_latest import refresh_latest
39+
from .refresh_usage import refresh_usage

src/pygobbler/refresh_latest.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import Optional
2+
from . import _utils as ut
3+
4+
5+
def refresh_latest(project: str, asset: str, staging: str, url:str) -> Optional[str]:
6+
"""
7+
Recompute the latest version of a project's asset. This is useful on rare
8+
occasions where multiple simultaneous uploads cause the latest version to
9+
be slightly out of sync.
10+
11+
Args:
12+
project:
13+
Name of the project.
14+
15+
asset:
16+
Name of the asset.
17+
18+
staging:
19+
Path to the staging directory.
20+
21+
url:
22+
URL of the gobbler REST API.
23+
24+
Returns:
25+
Latest version of the project, or None if there is no non-probational version.
26+
"""
27+
resp = ut.dump_request(staging, url, "refresh_latest", { "project": project, "asset": asset })
28+
if "version" in resp:
29+
return resp["version"]
30+
else:
31+
return None

src/pygobbler/refresh_usage.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from . import _utils as ut
2+
3+
4+
def refresh_usage(project: str, staging: str, url:str) -> int:
5+
"""
6+
Recompute the quota usage of a project. This is useful on rare occasions
7+
where multiple simultaneous uploads cause the usage calculations to be out
8+
of sync.
9+
10+
Args:
11+
project:
12+
Name of the project.
13+
14+
staging:
15+
Path to the staging directory.
16+
17+
url:
18+
URL of the gobbler REST API.
19+
20+
Returns:
21+
Total quota usage of this project, in bytes.
22+
"""
23+
resp = ut.dump_request(staging, url, "refresh_usage", { "project": project })
24+
return resp["total"]

tests/test_refresh_latest.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pygobbler as pyg
2+
import pytest
3+
import os
4+
import time
5+
import tempfile
6+
7+
8+
@pytest.fixture(scope="module")
9+
def setup():
10+
_, staging, registry, url = pyg.start_gobbler()
11+
12+
pyg.remove_project("test", staging=staging, url=url)
13+
pyg.create_project("test", staging=staging, url=url)
14+
15+
tmp = tempfile.mkdtemp()
16+
pyg.upload_directory(project="test", asset="latest", version="v1", directory=tmp, staging=staging, url=url)
17+
time.sleep(1.1)
18+
pyg.upload_directory(project="test", asset="latest", version="v2", directory=tmp, staging=staging, url=url)
19+
time.sleep(1.1)
20+
pyg.upload_directory(project="test", asset="latest", version="v3", directory=tmp, staging=staging, url=url)
21+
22+
23+
def test_refresh_latest(setup):
24+
_, staging, registry, url = pyg.start_gobbler()
25+
26+
assert pyg.fetch_latest("test", "latest", registry=registry, url=url) == "v3"
27+
28+
os.unlink(os.path.join(registry, "test", "latest", "..latest"))
29+
assert pyg.fetch_latest("test", "latest", registry=registry, url=url) is None
30+
31+
v = pyg.refresh_latest("test", "latest", staging=staging, url=url)
32+
assert v == "v3"
33+
assert pyg.fetch_latest("test", "latest", registry=registry, url=url) == "v3"

tests/test_refresh_usage.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pygobbler as pyg
2+
import pytest
3+
import os
4+
import tempfile
5+
6+
7+
@pytest.fixture(scope="module")
8+
def setup():
9+
_, staging, registry, url = pyg.start_gobbler()
10+
11+
pyg.remove_project("test-usage", staging=staging, url=url)
12+
pyg.create_project("test-usage", staging=staging, url=url)
13+
14+
tmp = tempfile.mkdtemp()
15+
with open(os.path.join(tmp, "blah.txt"), "w") as f:
16+
f.write("BAR")
17+
os.mkdir(os.path.join(tmp, "foo"))
18+
with open(os.path.join(tmp, "foo", "bar.txt"), "w") as f:
19+
f.write("1 2 3 4 5 6 7 8 9 10")
20+
21+
pyg.upload_directory(project="test-usage", asset="quota", version="v1", directory=tmp, staging=staging, url=url)
22+
23+
24+
def test_refresh_usage(setup):
25+
_, staging, registry, url = pyg.start_gobbler()
26+
27+
assert pyg.fetch_usage("test-usage", registry=registry, url=url) > 0
28+
29+
with open(os.path.join(registry, "test-usage", "..usage"), "w") as handle:
30+
handle.write('{ "total": 0 }')
31+
assert pyg.fetch_usage("test-usage", registry=registry, url=url) == 0
32+
33+
# Fixing the project usage.
34+
out = pyg.refresh_usage("test-usage", staging=staging, url=url)
35+
assert out > 0
36+
assert pyg.fetch_usage("test-usage", registry=registry, url=url) == out

0 commit comments

Comments
 (0)