Skip to content

Commit 8b8e5d9

Browse files
committed
get_files_to_run shoudl handle extra files
Add option (to be used by #2006) to specify extra files particular tutorial depends on Make sure those files will not get deleted Add a bit more type annotation to the code
1 parent 57bb87c commit 8b8e5d9

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

.jenkins/get_files_to_run.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Any
1+
from typing import Any, Dict, List, Optional, Tuple
22
import json
33
import os
44
from pathlib import Path
@@ -14,17 +14,21 @@ def get_all_files() -> List[str]:
1414
return [str(x) for x in sources]
1515

1616

17-
def calculate_shards(all_files: List[str], num_shards: int = 20):
17+
def read_metadata() -> Dict[str, Any]:
1818
with (REPO_BASE_DIR / ".jenkins" / "metadata.json").open() as fp:
19-
metadata = json.load(fp)
20-
sharded_files = [(0.0, []) for _ in range(num_shards)]
19+
return json.load(fp)
2120

22-
def get_duration(file):
21+
22+
def calculate_shards(all_files: List[str], num_shards: int = 20) -> List[List[str]]:
23+
sharded_files: List[Tuple[float, List[str]]] = [(0.0, []) for _ in range(num_shards)]
24+
metadata = read_metadata()
25+
26+
def get_duration(file: str) -> int:
2327
# tutorials not listed in the metadata.json file usually take
2428
# <3min to run, so we'll default to 1min if it's not listed
2529
return metadata.get(file, {}).get("duration", 60)
2630

27-
def get_needs_machine(file):
31+
def get_needs_machine(file: str) -> Optional[str]:
2832
return metadata.get(file, {}).get("needs", None)
2933

3034
def add_to_shard(i, filename):
@@ -55,9 +59,19 @@ def add_to_shard(i, filename):
5559
return [x[1] for x in sharded_files]
5660

5761

58-
def remove_other_files(all_files, files_to_run) -> None:
62+
def compute_files_to_keep(files_to_run: List[str]) -> List[str]:
63+
metadata = read_metadata()
64+
files_to_keep = list(files_to_run)
65+
for file in files_to_run:
66+
extra_files = metadata.get(file, {}).get("extra_files", [])
67+
files_to_keep.extend(extra_files)
68+
return files_to_keep
69+
70+
71+
def remove_other_files(all_files, files_to_keep) -> None:
72+
5973
for file in all_files:
60-
if file not in files_to_run:
74+
if file not in files_to_keep:
6175
remove_runnable_code(file, file)
6276

6377

@@ -76,7 +90,7 @@ def main() -> None:
7690
all_files = get_all_files()
7791
files_to_run = calculate_shards(all_files, num_shards=args.num_shards)[args.shard_num]
7892
if not args.dry_run:
79-
remove_other_files(all_files, files_to_run)
93+
remove_other_files(all_files, compute_files_to_keep(files_to_run))
8094
stripped_file_names = [Path(x).stem for x in files_to_run]
8195
print(" ".join(stripped_file_names))
8296

0 commit comments

Comments
 (0)