-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
335 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Ops utility for backfilling manifest.json files from db | ||
""" | ||
import argparse | ||
|
||
from opensafely._vendor.jobrunner.executors import local | ||
from opensafely._vendor.jobrunner.lib import database | ||
from opensafely._vendor.jobrunner.models import Job | ||
|
||
|
||
def main(workspaces=None): | ||
conn = database.get_connection() | ||
workspaces = [ | ||
w["workspace"] for w in conn.execute("SELECT DISTINCT(workspace) FROM job;") | ||
] | ||
|
||
n_workspaces = len(workspaces) | ||
for i, workspace in enumerate(workspaces): | ||
print(f"workspace {i+1}/{n_workspaces}: {workspace}") | ||
|
||
level4_dir = local.get_medium_privacy_workspace(workspace) | ||
|
||
sentinel = level4_dir / ".manifest-backfill" | ||
if sentinel.exists(): | ||
print(" - already done, skipping") | ||
continue | ||
|
||
write_manifest(workspace) | ||
|
||
sentinel.touch() | ||
|
||
|
||
def write_manifest(workspace): | ||
conn = database.get_connection() | ||
workspace_dir = local.get_high_privacy_workspace(workspace) | ||
level4_dir = local.get_medium_privacy_workspace(workspace) | ||
|
||
# ordering by most recent ensures we find the job that generated the | ||
# current version of the file. | ||
job_ids = conn.execute( | ||
""" | ||
SELECT id FROM job | ||
WHERE workspace = ? | ||
AND outputs != '' | ||
AND completed_at IS NOT NULL | ||
AND state = 'succeeded' | ||
ORDER BY completed_at DESC; | ||
""", | ||
(workspace,), | ||
) | ||
|
||
outputs = {} | ||
|
||
for row in job_ids: | ||
job_id = row["id"] | ||
job = database.find_one(Job, id=job_id) | ||
|
||
for output, level in job.outputs.items(): | ||
if output in outputs: | ||
# older version of the file, ignore | ||
continue | ||
|
||
abspath = workspace_dir / output | ||
|
||
# use presence of message file to detect excluded files | ||
message_file = level4_dir / (output + ".txt") | ||
excluded = message_file.exists() | ||
|
||
metadata = local.get_output_metadata( | ||
abspath, | ||
level, | ||
job_id=job_id, | ||
job_request=job.job_request_id, | ||
action=job.action, | ||
commit=job.commit, | ||
excluded=excluded, | ||
) | ||
|
||
outputs[output] = metadata | ||
|
||
manifest = local.read_manifest_file(level4_dir, workspace) | ||
manifest["outputs"] = outputs | ||
print(f" - writing manifest for {workspace} with {len(outputs)} outputs") | ||
local.write_manifest_file(level4_dir, manifest) | ||
|
||
|
||
def run(): | ||
parser = argparse.ArgumentParser(description=__doc__.partition("\n\n")[0]) | ||
args = parser.parse_args() | ||
main(**vars(args)) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.