@@ -147,7 +147,36 @@ def verify_files(filename, destination, rename_to):
147147    return  True 
148148
149149
150- def  unpack (filename , destination , force_extract ):  # noqa: C901 
150+ def  is_latest_version (destination , dirname , rename_to , cfile , checksum ):
151+     current_version  =  None 
152+     expected_version  =  None 
153+ 
154+     try :
155+         expected_version  =  checksum 
156+         with  open (os .path .join (destination , rename_to , ".package_checksum" ), "r" ) as  f :
157+             current_version  =  f .read ()
158+ 
159+         if  verbose :
160+             print (f"\n Tool: { rename_to }  )
161+             print (f"Current version: { current_version }  )
162+             print (f"Expected version: { expected_version }  )
163+ 
164+         if  current_version  and  current_version  ==  expected_version :
165+             if  verbose :
166+                 print ("Latest version already installed. Skipping extraction" )
167+             return  True 
168+ 
169+         if  verbose :
170+             print ("New version detected" )
171+ 
172+     except  Exception  as  e :
173+         if  verbose :
174+             print (f"Falied to verify version for { rename_to } { e }  )
175+ 
176+     return  False 
177+ 
178+ 
179+ def  unpack (filename , destination , force_extract , checksum ):  # noqa: C901 
151180    dirname  =  "" 
152181    cfile  =  None   # Compressed file 
153182    file_is_corrupted  =  False 
@@ -196,11 +225,11 @@ def unpack(filename, destination, force_extract):  # noqa: C901
196225        rename_to  =  "esp32-arduino-libs" 
197226
198227    if  not  force_extract :
199-         if  verify_files ( filename ,  destination , rename_to ):
200-             print ( " Files ok. Skipping Extraction" ) 
201-             return   True 
202-         else : 
203-              print (" Extracting archive..." )
228+         if  is_latest_version ( destination ,  dirname , rename_to ,  cfile ,  checksum ):
229+             if   verify_files ( filename ,  destination ,  rename_to ): 
230+                  print ( " Files ok. Skipping Extraction" ) 
231+                  return   True 
232+         print (" Extracting archive..." )
204233    else :
205234        print (" Forcing extraction" )
206235
@@ -225,6 +254,9 @@ def unpack(filename, destination, force_extract):  # noqa: C901
225254            shutil .rmtree (rename_to )
226255        shutil .move (dirname , rename_to )
227256
257+     with  open (os .path .join (destination , rename_to , ".package_checksum" ), "w" ) as  f :
258+         f .write (checksum )
259+ 
228260    if  verify_files (filename , destination , rename_to ):
229261        print (" Files extracted successfully." )
230262        return  True 
@@ -324,11 +356,11 @@ def get_tool(tool, force_download, force_extract):
324356        print ("Tool {0} already downloaded" .format (archive_name ))
325357        sys .stdout .flush ()
326358
327-     if  "esp32-arduino-libs"   not   in   archive_name   and   sha256sum (local_path ) !=  checksum :
359+     if  sha256sum (local_path ) !=  checksum :
328360        print ("Checksum mismatch for {0}" .format (archive_name ))
329361        return  False 
330362
331-     return  unpack (local_path , "." , force_extract )
363+     return  unpack (local_path , "." , force_extract ,  checksum )
332364
333365
334366def  load_tools_list (filename , platform ):
@@ -379,21 +411,17 @@ def identify_platform():
379411if  __name__  ==  "__main__" :
380412    parser  =  argparse .ArgumentParser (description = "Download and extract tools" )
381413
382-     parser .add_argument ("-v" , "--verbose" , type = bool ,  default = False , required = False , help = "Print verbose output" )
414+     parser .add_argument ("-v" , "--verbose" , action = "store_true" , required = False , help = "Print verbose output" )
383415
384-     parser .add_argument (
385-         "-d" , "--force_download" , type = bool , default = False , required = False , help = "Force download of tools" 
386-     )
416+     parser .add_argument ("-d" , "--force_download" , action = "store_true" , required = False , help = "Force download of tools" )
387417
388-     parser .add_argument (
389-         "-e" , "--force_extract" , type = bool , default = False , required = False , help = "Force extraction of tools" 
390-     )
418+     parser .add_argument ("-e" , "--force_extract" , action = "store_true" , required = False , help = "Force extraction of tools" )
391419
392420    parser .add_argument (
393-         "-f" , "--force_all" , type = bool ,  default = False , required = False , help = "Force download and extraction of tools" 
421+         "-f" , "--force_all" , action = "store_true" , required = False , help = "Force download and extraction of tools" 
394422    )
395423
396-     parser .add_argument ("-t" , "--test" , type = bool ,  default = False , required = False , help = argparse .SUPPRESS )
424+     parser .add_argument ("-t" , "--test" , action = "store_true" , required = False , help = argparse .SUPPRESS )
397425
398426    args  =  parser .parse_args ()
399427
0 commit comments