Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save changes in DataLad if only the statefile was modified #121

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/tinuous/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,18 @@ def fetch(config_file: str, state_path: Optional[str], sanitize_secrets: bool) -
log.info("%d logs downloaded", logs_added)
log.info("%d artifacts downloaded", artifacts_added)
log.info("%d release assets downloaded", relassets_added)
if cfg.datalad.enabled and (logs_added or artifacts_added or relassets_added):
msg = f"[tinuous] {logs_added} logs added"
if artifacts_added:
msg += f", {artifacts_added} artifacts added"
if relassets_added:
msg += f", {relassets_added} release assets added"
msg += f"\n\nProduced by tinuous {__version__}"
ds.save(recursive=True, message=msg)
if cfg.datalad.enabled:
if logs_added or artifacts_added or relassets_added:
msg = f"[tinuous] {logs_added} logs added"
if artifacts_added:
msg += f", {artifacts_added} artifacts added"
if relassets_added:
msg += f", {relassets_added} release assets added"
msg += f"\n\nProduced by tinuous {__version__}"
ds.save(recursive=True, message=msg)
elif statefile.modified:
msg = f"[tinuous] Updated statefile\n\nProduced by tinuous {__version__}"
ds.save(recursive=True, message=msg)


@main.command("sanitize")
Expand Down
4 changes: 4 additions & 0 deletions src/tinuous/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StateFile(BaseModel):
path: Path
state: State
migrating: bool = False
modified: bool = False

@classmethod
def from_file(
Expand Down Expand Up @@ -60,6 +61,8 @@ def get_since(self, ciname: str) -> datetime:
return self.default_since

def set_since(self, ciname: str, since: datetime) -> None:
if getattr(self.state, ciname) == since:
return
setattr(self.state, ciname, since)
log.debug("%s timestamp floor updated to %s", ciname, since)
if self.migrating:
Expand All @@ -71,3 +74,4 @@ def set_since(self, ciname: str, since: datetime) -> None:
self.migrating = False
else:
self.path.write_text(self.state.json())
self.modified = True
17 changes: 17 additions & 0 deletions test/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ def test_migration(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
)
assert statefile.path == tmp_path / OLD_STATE_FILE
assert statefile.migrating
assert not statefile.modified

assert statefile.get_since("github") == datetime(
2021, 6, 11, 14, 44, 17, tzinfo=timezone.utc
)
assert os.listdir() == [OLD_STATE_FILE]
assert statefile.path == tmp_path / OLD_STATE_FILE
assert statefile.migrating
assert not statefile.modified

statefile.set_since(
"github", datetime(2021, 6, 11, 14, 44, 17, tzinfo=timezone.utc)
)
assert os.listdir() == [OLD_STATE_FILE]
assert statefile.path == tmp_path / OLD_STATE_FILE
assert statefile.migrating
assert not statefile.modified

newdt = datetime(2021, 6, 11, 14, 48, 39, tzinfo=timezone.utc)
statefile.set_since("github", newdt)
Expand All @@ -45,6 +55,7 @@ def test_migration(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
)
assert statefile.path == tmp_path / STATE_FILE
assert not statefile.migrating
assert statefile.modified
with open(STATE_FILE) as fp:
data = json.load(fp)
assert data == {
Expand All @@ -62,6 +73,7 @@ def test_defaulting(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
assert statefile.state == State(github=None, travis=None, appveyor=None)
assert statefile.path == tmp_path / STATE_FILE
assert not statefile.migrating
assert not statefile.modified

assert statefile.get_since("github") == dt
assert os.listdir() == []
Expand All @@ -70,6 +82,7 @@ def test_defaulting(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
statefile.set_since("github", newdt)
assert os.listdir() == [STATE_FILE]
assert statefile.state == State(github=newdt, travis=None, appveyor=None)
assert statefile.modified
with open(STATE_FILE) as fp:
data = json.load(fp)
assert data == {
Expand Down Expand Up @@ -98,6 +111,7 @@ def test_empty(contents: str, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -
assert statefile.state == State(github=None, travis=None, appveyor=None)
assert statefile.path == tmp_path / STATE_FILE
assert not statefile.migrating
assert not statefile.modified

assert statefile.get_since("github") == dt
assert os.listdir() == [STATE_FILE]
Expand All @@ -107,6 +121,7 @@ def test_empty(contents: str, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -
statefile.set_since("github", newdt)
assert os.listdir() == [STATE_FILE]
assert statefile.state == State(github=newdt, travis=None, appveyor=None)
assert statefile.modified
with f.open() as fp:
data = json.load(fp)
assert data == {
Expand Down Expand Up @@ -134,6 +149,7 @@ def test_populated_explicit_path(tmp_path: Path) -> None:
assert statefile.state == State(github=ghdt, travis=travdt, appveyor=None)
assert statefile.path == f
assert not statefile.migrating
assert not statefile.modified
assert statefile.get_since("github") == ghdt
assert statefile.get_since("travis") == travdt
assert statefile.get_since("appveyor") == dt
Expand All @@ -142,6 +158,7 @@ def test_populated_explicit_path(tmp_path: Path) -> None:
assert statefile.state == State(github=newdt, travis=travdt, appveyor=None)
assert statefile.path == f
assert not statefile.migrating
assert statefile.modified
with f.open() as fp:
data = json.load(fp)
assert data == {
Expand Down