6464import subprocess
6565import re
6666import hglib
67+ import argparse
6768
6869# Be sure that the tools directory is in the search path
6970ROOT = abspath (join (dirname (__file__ ), ".." ))
7374
7475OFFICIAL_MBED_LIBRARY_BUILD = get_mbed_official_release ('2' )
7576
76- def log_message (str ):
77- """ Depending on the value of the global variable, dbg, writes a log message provided in
78- str to stdout.
79-
80- Args:
81- str - log string.
82-
83- """
84- global dbg
85- if dbg :
86- print str
87- sys .stdout .flush ()
88-
89-
9077def get_compilation_failure (messages ):
9178 """ Reads the json formatted 'messages' and checks for compilation errors.
9279 If there is a genuine compilation error then there should be a new message containing
@@ -124,25 +111,29 @@ def invoke_api(payload, url, auth, polls, begin="start/"):
124111 """
125112
126113 # send task to api
127- log_message (url + begin + "| data: " + str (payload ))
114+ logging . debug (url + begin + "| data: " + str (payload ))
128115 r = requests .post (url + begin , data = payload , auth = auth )
129- log_message (r .request .body )
130-
116+ logging . debug (r .request .body )
117+
131118 if r .status_code != 200 :
132119 raise Exception ("Error while talking to the mbed API" )
133120
134121 response = r .json ()
135- log_message (response )
122+ logging . debug (response )
136123 uuid = response ['result' ]['data' ]['task_id' ]
137- log_message ("Task accepted and given ID: %s" % uuid )
138- sys .stdout .write ("\t \t Compiling: " )
124+ logging .debug ("Task accepted and given ID: %s" , uuid )
139125 result = False
140126 fail_type = None
127+
128+ # It currently seems to take the onlide IDE API ~30s to process the compile request and
129+ # provide a response. Set the poll time to half that in case it does manage to compile
130+ # quicker.
131+ poll_delay = 15
132+ logging .debug ("Running with a poll for response delay of: %ss" , poll_delay )
141133
142134 # poll for output
143- for check in range (0 , polls ):
144- sys .stdout .write ('.' )
145- time .sleep (1 )
135+ for check in range (polls ):
136+ time .sleep (poll_delay )
146137 r = requests .get (url + "output/%s" % uuid , auth = auth )
147138 response = r .json ()
148139 if response ['result' ]['data' ]['task_complete' ]:
@@ -153,13 +144,15 @@ def invoke_api(payload, url, auth, polls, begin="start/"):
153144 # 3) Internal failure of the online compiler
154145 result = bool (response ['result' ]['data' ]['compilation_success' ])
155146 if result :
156- sys . stdout . write ( "SUCCESSFUL \n " )
147+ logging . info ( " \t \t Compilation SUCCESSFUL \n " )
157148 else :
158149 # Did this fail due to a genuine compilation error or a failue of the api itself ?
159- sys .stdout .write ("FAILURE \n " )
160- sys .stdout .flush ()
150+ logging .info ("\t \t Compilation FAILURE\n " )
161151 fail_type = get_compilation_failure (response ['result' ]['data' ]['new_messages' ])
162152 break
153+ else :
154+ logging .info ("\t \t Compilation FAILURE\n " )
155+
163156 if not result and fail_type == None :
164157 fail_type = "Internal"
165158
@@ -185,33 +178,30 @@ def build_repo(target, program, user, pw, polls=25, url="https://developer.mbed.
185178 auth = (user , pw )
186179 return invoke_api (payload , url , auth , polls )
187180
188- def run_cmd (command , print_warning_on_fail = True , exit_on_failure = False ):
181+ def run_cmd (command , exit_on_failure = False ):
189182 """ Passes a command to the system and returns a True/False result once the
190183 command has been executed, indicating success/failure. Commands are passed
191184 as a list of tokens.
192185 E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
193186
194187 Args:
195188 command - system command as a list of tokens
196- print_warning_on_fail - If True print any failure warning to the screen
197- (default = True)
198189 exit_on_failure - If True exit the program on failure (default = False)
199190
200191 Returns:
201192 result - True/False indicating the success/failure of the command
202193 """
203- print ('[Exec] %s' % ' ' .join (command ))
194+ logging . debug ('[Exec] %s' , ' ' .join (command ))
204195 return_code = subprocess .call (command , shell = True )
205196
206197 if return_code :
207- if print_warning_on_fail :
208- print ("The command '%s' failed with return code: %s" % (' ' .join (command ), return_code ))
198+ logging .warning ("The command '%s' failed with return code: %s" , (' ' .join (command ), return_code ))
209199 if exit_on_failure :
210200 sys .exit (1 )
211201
212202 return return_code
213203
214- def run_cmd_with_output (command , print_warning_on_fail = True , exit_on_failure = False ):
204+ def run_cmd_with_output (command , exit_on_failure = False ):
215205 """ Passes a command to the system and returns a True/False result once the
216206 command has been executed, indicating success/failure. If the command was
217207 successful then the output from the command is returned to the caller.
@@ -220,22 +210,19 @@ def run_cmd_with_output(command, print_warning_on_fail=True, exit_on_failure=Fal
220210
221211 Args:
222212 command - system command as a list of tokens
223- print_warning_on_fail - If True print any failure warning to the screen
224- (default = True)
225213 exit_on_failure - If True exit the program on failure (default = False)
226214
227215 Returns:
228216 result - True/False indicating the success/failure of the command
229217 output - The output of the command if it was successful, else empty string
230218 """
231- print ('[Exec] %s' % ' ' .join (command ))
219+ logging . debug ('[Exec] %s' , ' ' .join (command ))
232220 returncode = 0
233221 output = ""
234222 try :
235223 output = subprocess .check_output (command , shell = True )
236224 except subprocess .CalledProcessError as e :
237- if print_warning_on_fail :
238- print ("The command '%s' failed with return code: %s" % (' ' .join (command ), e .returncode ))
225+ logging .warning ("The command '%s' failed with return code: %s" , (' ' .join (command ), e .returncode ))
239226 returncode = e .returncode
240227 if exit_on_failure :
241228 sys .exit (1 )
@@ -255,15 +242,15 @@ def upgrade_test_repo(test, user, library, ref, repo_path):
255242 Returns:
256243 updated - True if library was updated, False otherwise
257244 """
258- print ( " \n Updating test repo: '%s' to SHA: %s" % ( test , ref ) )
245+ logging . info ( "Updating test repo: '%s' to SHA: %s", test , ref )
259246 cwd = os .getcwd ()
260247
261248 repo = "https://" + user + '@developer.mbed.org/users/' + user + '/code/' + test
262249
263250 # Clone the repo if it doesn't already exist
264251 path = abspath (repo_path + '/' + test )
265252 if not os .path .exists (path ):
266- print ("Test repo doesn't exist, cloning..." )
253+ logging . info ("Test repo doesn't exist, cloning..." )
267254 os .chdir (abspath (repo_path ))
268255 clone_cmd = ['hg' , 'clone' , repo ]
269256 run_cmd (clone_cmd , exit_on_failure = True )
@@ -282,7 +269,7 @@ def upgrade_test_repo(test, user, library, ref, repo_path):
282269
283270 os .rename (lib_file , bak_file )
284271 else :
285- print ("!! Error trying to backup lib file prior to updating." )
272+ logging . error ("!! Error trying to backup lib file prior to updating." )
286273 return False
287274
288275 # mbed 2 style lib file contains one line with the following format
@@ -318,7 +305,7 @@ def upgrade_test_repo(test, user, library, ref, repo_path):
318305 run_cmd (cmd , exit_on_failure = True )
319306
320307 except :
321- print ("Lib file already up to date and thus nothing to commit" )
308+ logging . info ("Lib file already up to date and thus nothing to commit" )
322309
323310 os .chdir (cwd )
324311 return updated
@@ -375,19 +362,27 @@ def get_latest_library_versions(repo_path):
375362 return mbed , mbed_dev
376363
377364if __name__ == '__main__' :
365+
366+ parser = argparse .ArgumentParser (description = __doc__ ,
367+ formatter_class = argparse .RawDescriptionHelpFormatter )
368+ parser .add_argument ('-l' , '--log-level' , help = "Level for providing logging output" , default = 'INFO' )
369+ args = parser .parse_args ()
370+
371+ default = getattr (logging , 'INFO' )
372+ level = getattr (logging , args .log_level .upper (), default )
373+
374+ # Set logging level
375+ logging .basicConfig (level = level )
378376
379377 # Read configuration data
380378 json_data = json .load (open (os .path .join (os .path .dirname (__file__ ), "check_release.json" )))
381379
382- # Debug output off by default
383- dbg = False
384380
385381 supported_targets = []
386382
387383 # Get a list of the officially supported mbed-os 2 targets
388384 for tgt in OFFICIAL_MBED_LIBRARY_BUILD :
389- if 'ARM' in tgt [1 ]:
390- supported_targets .append (tgt [0 ] )
385+ supported_targets .append (tgt [0 ])
391386
392387 config = json_data ["config" ]
393388 test_list = json_data ["test_list" ]
@@ -409,11 +404,11 @@ def get_latest_library_versions(repo_path):
409404 mbed , mbed_dev = get_latest_library_versions (repo_path )
410405
411406 if not mbed or not mbed_dev :
412- print ("Could not obtain latest versions of library files!!" )
407+ logging . error ("Could not obtain latest versions of library files!!" )
413408 exit (1 )
414409
415- print ("Latest mbed lib version = %s" % mbed )
416- print ("Latest mbed-dev lib version = %s" % mbed_dev )
410+ logging . info ("Latest mbed lib version = %s" , mbed )
411+ logging . info ("Latest mbed-dev lib version = %s" , mbed_dev )
417412
418413 # First update test repos to latest versions of their embedded libraries
419414 for test in test_list :
@@ -426,30 +421,27 @@ def get_latest_library_versions(repo_path):
426421
427422 # Compile each test for each supported target
428423 for test in tests :
429- print ("Test compiling program: %s\n " % test )
424+ logging . info ("Test compiling program: %s\n " , test )
430425 for target in supported_targets :
431- print ("\t Target: %s" % target )
432426 for retry in range (0 , retries ):
427+ logging .info ("\t Compiling target: %s , attempt %u\n " , target , retry )
433428 result , mesg = build_repo (target , test , user , password )
434429 if not result :
435430 if mesg == 'Internal' :
436431 # Internal compiler error thus retry
437- sys .stdout .write ("FAILURE \n " )
438- sys .stdout .flush ()
439432 continue
440433 else :
441434 # Genuine compilation error, thus print it out
442- print ("\t \t Error: %s\n " % mesg )
435+ logging . error ("\t \t Error: %s\n " , mesg )
443436
444437 passes += (int )(result )
445438 break
446439 else :
447- print ("\t \t Program/Target compilation failed due to internal errors. Removing from considered list!\n " )
440+ logging . error ("\t \t Program/Target compilation failed due to internal errors. Removing from considered list!\n " )
448441 total -= 1
449442
450443 # Output a % pass rate, indicate a failure if not 100% successful
451444 pass_rate = int (passes / total ) * 100
452- print ("Pass percentage = %d\n " % pass_rate )
445+ logging . info ("Pass percentage = %d\n " , pass_rate )
453446 sys .exit (not (pass_rate == 100 ))
454-
455-
447+
0 commit comments