1313See the License for the specific language governing permissions and
1414limitations under the License.
1515"""
16+
1617import proton
1718import proton .handlers
1819import threading
1920import logging
2021import json
2122import os
2223import asyncio
24+ import sys
25+ import time
2326from typing import List , Any , Tuple , Callable , Dict
2427from charon .config import get_config
2528from charon .constants import DEFAULT_SIGN_RESULT_LOC
2932
3033logger = logging .getLogger (__name__ )
3134
35+
3236class SignHandler :
3337 """
3438 Handle the sign result status management
3539 """
40+
3641 _is_processing : bool = True
3742 _downloaded_files : List [str ] = []
3843
@@ -52,6 +57,7 @@ def set_processing(cls, value: bool) -> None:
5257 def set_downloaded_files (cls , files : List [str ]) -> None :
5358 cls ._downloaded_files = files
5459
60+
5561class UmbListener (proton .handlers .MessagingHandler ):
5662 """
5763 UmbListener class (AMQP version), register this when setup UmbClient
@@ -74,10 +80,7 @@ def on_message(self, event: proton.Event) -> None:
7480 On message callback
7581 """
7682 # handle response from radas in a thread
77- thread = threading .Thread (
78- target = self ._process_message ,
79- args = [event .message .body ]
80- )
83+ thread = threading .Thread (target = self ._process_message , args = [event .message .body ])
8184 thread .start ()
8285
8386 def on_error (self , event : proton .Event ) -> None :
@@ -103,8 +106,8 @@ def _process_message(msg: Any) -> None:
103106 result_reference_url = msg_dict .get ("result_reference" )
104107
105108 if not result_reference_url :
106- logger .warning ("Not found result_reference in message,ignore." )
107- return
109+ logger .warning ("Not found result_reference in message,ignore." )
110+ return
108111
109112 conf = get_config ()
110113 if not conf :
@@ -117,20 +120,24 @@ def _process_message(msg: Any) -> None:
117120
118121 oras_client = OrasClient ()
119122 files = oras_client .pull (
120- result_reference_url = result_reference_url ,
121- sign_result_loc = sign_result_loc
123+ result_reference_url = result_reference_url , sign_result_loc = sign_result_loc
122124 )
123125 SignHandler .set_downloaded_files (files )
124126 finally :
125127 SignHandler .set_processing (False )
126128
129+
127130def generate_radas_sign (top_level : str ) -> Tuple [List [str ], List [str ]]:
128131 """
129132 Generate .asc files based on RADAS sign result json file
130133 """
131134 conf = get_config ()
132- timeout_count = conf .get_radas_sign_timeout_count () if conf else DEFAULT_RADAS_SIGN_TIMEOUT_COUNT
133- wait_interval_sec = conf .get_radas_sign_wait_interval_sec () if conf else DEFAULT_RADAS_SIGN_WAIT_INTERVAL_SEC
135+ timeout_count = (
136+ conf .get_radas_sign_timeout_count () if conf else DEFAULT_RADAS_SIGN_TIMEOUT_COUNT
137+ )
138+ wait_interval_sec = (
139+ conf .get_radas_sign_wait_interval_sec () if conf else DEFAULT_RADAS_SIGN_WAIT_INTERVAL_SEC
140+ )
134141 wait_count = 0
135142 while SignHandler .is_processing ():
136143 wait_count += 1
@@ -146,23 +153,25 @@ def generate_radas_sign(top_level: str) -> Tuple[List[str], List[str]]:
146153 # should only have the single sign result json file from the radas registry
147154 json_file_path = files [0 ]
148155 try :
149- with open (json_file_path , 'r' ) as f :
156+ with open (json_file_path , "r" ) as f :
150157 data = json .load (f )
151158 except Exception as e :
152159 logger .error (f"Failed to read or parse the JSON file: { e } " )
153160 raise
154161
155162 async def generate_single_sign_file (
156- file_path : str , signature : str , failed_paths : List [str ], generated_signs : List [str ],
157- sem : asyncio .BoundedSemaphore
163+ file_path : str ,
164+ signature : str ,
165+ failed_paths : List [str ],
166+ generated_signs : List [str ],
167+ sem : asyncio .BoundedSemaphore ,
158168 ):
159169 async with sem :
160170 if not file_path or not signature :
161- logger .error (f "Invalid JSON entry" )
171+ logger .error ("Invalid JSON entry" )
162172 return
163173 # remove the root path maven-repository
164174 filename = file_path .split ("/" , 1 )[1 ]
165- signature = item .get ("signature" )
166175
167176 artifact_path = os .path .join (top_level , filename )
168177 asc_filename = f"{ filename } .asc"
@@ -173,7 +182,7 @@ async def generate_single_sign_file(
173182 return
174183
175184 try :
176- with open (signature_path , 'w' ) as asc_file :
185+ with open (signature_path , "w" ) as asc_file :
177186 asc_file .write (signature )
178187 generated_signs .append (signature_path )
179188 logger .info (f"Generated .asc file: { signature_path } " )
@@ -182,14 +191,11 @@ async def generate_single_sign_file(
182191 logger .error (f"Failed to write .asc file for { artifact_path } : { e } " )
183192
184193 result = data .get ("result" , [])
185- return __do_path_cut_and (
186- path_handler = generate_single_sign_file ,
187- data = result
188- )
194+ return __do_path_cut_and (path_handler = generate_single_sign_file , data = result )
195+
189196
190197def __do_path_cut_and (
191- path_handler : Callable ,
192- data : List [Dict [str , str ]]
198+ path_handler : Callable , data : List [Dict [str , str ]]
193199) -> Tuple [List [str ], List [str ]]:
194200
195201 failed_paths : List [str ] = []
@@ -207,4 +213,4 @@ def __do_path_cut_and(
207213
208214 loop = asyncio .get_event_loop ()
209215 loop .run_until_complete (asyncio .gather (* tasks ))
210- return (failed_paths , generated_signs )
216+ return (failed_paths , generated_signs )
0 commit comments