2828import atr .log as log
2929import atr .models .attestable as models
3030import atr .util as util
31+ from atr .models .attestable import AttestableChecksV1
3132
3233if TYPE_CHECKING :
3334 import pathlib
@@ -45,6 +46,10 @@ def github_tp_payload_path(project_name: str, version_name: str, revision_number
4546 return util .get_attestable_dir () / project_name / version_name / f"{ revision_number } .github-tp.json"
4647
4748
49+ def attestable_checks_path (project_name : str , version_name : str , revision_number : str ) -> pathlib .Path :
50+ return util .get_attestable_dir () / project_name / version_name / f"{ revision_number } .checks.json"
51+
52+
4853async def github_tp_payload_write (
4954 project_name : str , version_name : str , revision_number : str , github_payload : dict [str , Any ]
5055) -> None :
@@ -89,6 +94,22 @@ async def load_paths(
8994 return None
9095
9196
97+ async def load_checks (
98+ project_name : str ,
99+ version_name : str ,
100+ revision_number : str ,
101+ ) -> list [int ] | None :
102+ file_path = attestable_checks_path (project_name , version_name , revision_number )
103+ if await aiofiles .os .path .isfile (file_path ):
104+ try :
105+ async with aiofiles .open (file_path , encoding = "utf-8" ) as f :
106+ data = json .loads (await f .read ())
107+ return models .AttestableChecksV1 .model_validate (data ).checks
108+ except (json .JSONDecodeError , pydantic .ValidationError ) as e :
109+ log .warning (f"Could not parse { file_path } : { e } " )
110+ return []
111+
112+
92113def migrate_to_paths_files () -> int :
93114 attestable_dir = util .get_attestable_dir ()
94115 if not attestable_dir .is_dir ():
@@ -135,7 +156,7 @@ async def paths_to_hashes_and_sizes(directory: pathlib.Path) -> tuple[dict[str,
135156 return path_to_hash , path_to_size
136157
137158
138- async def write (
159+ async def write_files_data (
139160 project_name : str ,
140161 version_name : str ,
141162 revision_number : str ,
@@ -145,12 +166,37 @@ async def write(
145166 path_to_hash : dict [str , str ],
146167 path_to_size : dict [str , int ],
147168) -> None :
148- result = _generate (path_to_hash , path_to_size , revision_number , release_policy , uploader_uid , previous )
169+ result = _generate_files_data (path_to_hash , path_to_size , revision_number , release_policy , uploader_uid , previous )
149170 file_path = attestable_path (project_name , version_name , revision_number )
150171 await util .atomic_write_file (file_path , result .model_dump_json (indent = 2 ))
151172 paths_result = models .AttestablePathsV1 (paths = result .paths )
152173 paths_file_path = attestable_paths_path (project_name , version_name , revision_number )
153174 await util .atomic_write_file (paths_file_path , paths_result .model_dump_json (indent = 2 ))
175+ checks_file_path = attestable_checks_path (project_name , version_name , revision_number )
176+ if not checks_file_path .exists ():
177+ async with aiofiles .open (checks_file_path , "w" , encoding = "utf-8" ) as f :
178+ await f .write (models .AttestableChecksV1 ().model_dump_json (indent = 2 ))
179+
180+
181+ async def write_checks_data (
182+ project_name : str ,
183+ version_name : str ,
184+ revision_number : str ,
185+ checks : list [int ],
186+ ) -> None :
187+ log .info (f"Writing checks for { project_name } /{ version_name } /{ revision_number } : { checks } " )
188+
189+ def modify (content : str ) -> str :
190+ try :
191+ current = AttestableChecksV1 .model_validate_json (content ).checks
192+ except pydantic .ValidationError :
193+ current = []
194+ new_checks = set (current or [])
195+ new_checks .update (checks )
196+ result = models .AttestableChecksV1 (checks = sorted (new_checks ))
197+ return result .model_dump_json (indent = 2 )
198+
199+ await util .atomic_modify_file (attestable_checks_path (project_name , version_name , revision_number ), modify )
154200
155201
156202def _compute_hashes_with_attribution (
@@ -188,7 +234,7 @@ def _compute_hashes_with_attribution(
188234 return new_hashes
189235
190236
191- def _generate (
237+ def _generate_files_data (
192238 path_to_hash : dict [str , str ],
193239 path_to_size : dict [str , int ],
194240 revision_number : str ,
0 commit comments