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

Set github-access-status in superdataset's .gitmodules #50

Merged
merged 1 commit into from
Jun 10, 2024
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
69 changes: 53 additions & 16 deletions src/backups2datalad/adataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@
!= ""
)

async def has_changes(
self, paths: Sequence[str | Path] = (), cached: bool = False
) -> bool:
args: list[str | Path] = ["diff", "--quiet"]

Check warning on line 189 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L189

Added line #L189 was not covered by tests
if cached:
args.append("--cached")

Check warning on line 191 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L191

Added line #L191 was not covered by tests
if paths:
args.append("--")
args.extend(paths)
try:
await self.call_git(*args, quiet_rcs=[1])
except subprocess.CalledProcessError as e:

Check warning on line 197 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L193-L197

Added lines #L193 - L197 were not covered by tests
if e.returncode == 1:
return True

Check warning on line 199 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L199

Added line #L199 was not covered by tests
else:
raise

Check warning on line 201 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L201

Added line #L201 was not covered by tests
else:
return False

Check warning on line 203 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L203

Added line #L203 was not covered by tests

async def get_repo_config(self, key: str, file: str | None = None) -> str | None:
args = ["--file", file] if file is not None else []
try:
Expand All @@ -193,6 +212,18 @@
else:
raise

async def set_repo_config(
self, key: str, value: str, file: str | None = None
) -> None:
args = ["--file", file] if file is not None else ["--local"]
await self.call_git(
"config",
*args,
"--replace-all",
key,
value,
)

async def get_datalad_id(self) -> str:
r = await self.get_repo_config("datalad.dataset.id", file=".datalad/config")
assert r is not None
Expand All @@ -206,13 +237,8 @@
return EmbargoStatus(value)

async def set_embargo_status(self, status: EmbargoStatus) -> None:
await self.call_git(
"config",
"--file",
".datalad/config",
"--replace-all",
EMBARGO_STATUS_KEY,
status.value,
await self.set_repo_config(
EMBARGO_STATUS_KEY, status.value, file=".datalad/config"
)

async def call_git(self, *args: str | Path, **kwargs: Any) -> None:
Expand Down Expand Up @@ -289,6 +315,22 @@
" Please check if all changes were staged."
)

async def commit_if_changed(
self,
message: str,
commit_date: datetime | None = None,
paths: Sequence[str | Path] = (),
check_dirty: bool = True,
) -> None:
await self.call_git("add", "-A", *paths)

Check warning on line 325 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L325

Added line #L325 was not covered by tests
if await self.has_changes(paths=paths, cached=True):
await self.commit(

Check warning on line 327 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L327

Added line #L327 was not covered by tests
message,
commit_date=commit_date,
paths=paths,
check_dirty=check_dirty,
)

async def push(self, to: str, jobs: int, data: str | None = None) -> None:
waits = exp_wait(attempts=6, base=2.1)
while True:
Expand Down Expand Up @@ -512,7 +554,7 @@
(f"branch.{DEFAULT_BRANCH}.remote", "github"),
(f"branch.{DEFAULT_BRANCH}.merge", f"refs/heads/{DEFAULT_BRANCH}"),
]:
await self.call_git("config", "--local", "--replace-all", key, value)
await self.set_repo_config(key, value)

Check warning on line 557 in src/backups2datalad/adataset.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/adataset.py#L557

Added line #L557 was not covered by tests
return True
else:
log.debug("GitHub remote already exists for %s", name)
Expand Down Expand Up @@ -658,13 +700,10 @@

async def add_submodule(self, path: str, url: str, datalad_id: str) -> None:
await self.call_git("submodule", "add", "--", url, path)
await self.call_git(
"config",
"--file",
".gitmodules",
"--replace-all",
await self.set_repo_config(
f"submodule.{path}.datalad-id",
datalad_id,
file=".gitmodules",
)
await self.add(".gitmodules")

Expand All @@ -684,9 +723,7 @@

async def update_populate_status(self) -> None:
head = await self.get_commit_hash()
await self.call_git(
"config", "--local", "--replace-all", "dandi.populated", head
)
await self.set_repo_config("dandi.populated", head)


class ObjectType(Enum):
Expand Down
19 changes: 19 additions & 0 deletions src/backups2datalad/datasetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,33 @@
workers=self.config.workers,
)
to_save: list[str] = []
access_status: dict[str, str] = {}
for d, changed in report.results:
if changed:
to_save.append(d.identifier)
if self.config.gh_org is not None:
access_status[d.identifier] = (

Check warning on line 88 in src/backups2datalad/datasetter.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/datasetter.py#L88

Added line #L88 was not covered by tests
"public"
if d.embargo_status is EmbargoStatus.OPEN
else "private"
)
if to_save:
log.debug("Committing superdataset")
superds.assert_no_duplicates_in_gitmodules()
msg = await self.get_superds_commit_message(superds, to_save)
await superds.save(message=msg, path=to_save)
if access_status:
for did, access in access_status.items():
await superds.set_repo_config(

Check warning on line 100 in src/backups2datalad/datasetter.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/datasetter.py#L100

Added line #L100 was not covered by tests
f"submodule.{did}.github-access-status",
access,
file=".gitmodules",
)
await superds.commit_if_changed(

Check warning on line 105 in src/backups2datalad/datasetter.py

View check run for this annotation

Codecov / codecov/patch

src/backups2datalad/datasetter.py#L105

Added line #L105 was not covered by tests
"[backups2datalad] Update github-access-status keys in .gitmodules",
paths=[".gitmodules"],
check_dirty=False,
)
superds.assert_no_duplicates_in_gitmodules()
log.debug("Superdataset committed")
if report.failed:
Expand Down