Skip to content

Commit 7ab9b3a

Browse files
committed
add optional s3 cache for sandag data
1 parent c8618cc commit 7ab9b3a

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎release/aws/prepare_data.py‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Download and verify the canonical full-scale MTC and SANDAG data."""
33

44
import argparse
5+
import subprocess
56
import sys
67
from pathlib import Path
78

@@ -25,13 +26,24 @@ def prepare_mtc(model: Path, cache: Path) -> None:
2526
)
2627

2728

28-
def prepare_sandag(model: Path) -> None:
29+
def fetch_s3_cache(source: str, destination: str) -> None:
30+
subprocess.run(
31+
["aws", "s3", "sync", source, destination, "--only-show-errors"], check=True
32+
)
33+
34+
35+
def prepare_sandag(model: Path, s3_cache_uri: str | None) -> None:
2936
scripts = model / "scripts"
3037
sys.path.insert(0, str(scripts))
3138
try:
3239
from fulldata import get_full_data
3340

34-
get_full_data(model / "data-full")
41+
full_data_dir = model / "data-full"
42+
full_data_dir.mkdir(parents=True, exist_ok=True)
43+
if s3_cache_uri:
44+
print(f"checking data cache {s3_cache_uri}")
45+
fetch_s3_cache(s3_cache_uri.rstrip("/") + "/", str(full_data_dir))
46+
get_full_data(full_data_dir)
3547
finally:
3648
sys.path.remove(str(scripts))
3749

@@ -41,13 +53,17 @@ def main(argv=None) -> int:
4153
parser.add_argument("model", choices=("mtc-extended", "sandag"))
4254
parser.add_argument("repository", type=Path)
4355
parser.add_argument("--cache", required=True, type=Path)
56+
parser.add_argument(
57+
"--s3-cache-uri",
58+
help="s3://bucket/prefix of a pre-populated, read-only cache of the extracted data",
59+
)
4460
args = parser.parse_args(argv)
4561
args.cache.mkdir(parents=True, exist_ok=True)
4662
repository = args.repository.resolve()
4763
if args.model == "mtc-extended":
4864
prepare_mtc(repository, args.cache.resolve())
4965
else:
50-
prepare_sandag(repository)
66+
prepare_sandag(repository, args.s3_cache_uri)
5167
return 0
5268

5369

‎release/aws/run-model.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,11 @@ fi
254254
"$work_dir/data-venv/bin/pip" freeze > "$metadata/data-download-pip-freeze.txt"
255255

256256
echo "Downloading and verifying full-scale data for $model_name"
257+
data_cache_bucket=${artifact_uri#s3://}
258+
data_cache_bucket=${data_cache_bucket%%/*}
257259
"$work_dir/data-venv/bin/python" "$script_dir/prepare_data.py" \
258-
"$model_name" "$model" --cache "$work_dir/data-cache"
260+
"$model_name" "$model" --cache "$work_dir/data-cache" \
261+
--s3-cache-uri "s3://$data_cache_bucket/data-cache/$model_name"
259262

260263
common=(
261264
--model-dir "$model"

‎release/aws/stack.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,17 @@ Resources:
117117
StringLike:
118118
s3:prefix:
119119
- !Sub '${ArtifactPrefix}/*'
120+
- 'data-cache/*'
120121
- Effect: Allow
121122
Action:
122123
- s3:GetObject
123124
- s3:PutObject
124125
- s3:AbortMultipartUpload
125126
- s3:ListMultipartUploadParts
126127
Resource: !Sub 'arn:${AWS::Partition}:s3:::${ArtifactBucket}/${ArtifactPrefix}/*'
128+
- Effect: Allow
129+
Action: s3:GetObject
130+
Resource: !Sub 'arn:${AWS::Partition}:s3:::${ArtifactBucket}/data-cache/*'
127131

128132
ReleaseInstanceProfile:
129133
Type: AWS::IAM::InstanceProfile

0 commit comments

Comments
 (0)