77import hashlib
88import shutil
99import re
10+ from typing import Dict , Any , Optional , cast
11+ from common import get_image_config , read_images
1012PRECENT_PROGRESS_SIZE = 5
1113
1214class ChecksumFailException (Exception ):
1315 pass
1416
15- IMAGES_CONFIG = os .path .join (os .path .dirname (__file__ ), "images.yml" )
1617RETRY = 3
1718
1819def ensure_dir (d , chmod = 0o777 ):
@@ -26,13 +27,6 @@ def ensure_dir(d, chmod=0o777):
2627 return False
2728 return True
2829
29- def read_images ():
30- if not os .path .isfile (IMAGES_CONFIG ):
31- raise Exception (f"Error: Remotes config file not found: { IMAGES_CONFIG } " )
32- with open (IMAGES_CONFIG ,'r' ) as f :
33- output = yaml .safe_load (f )
34- return output
35-
3630class DownloadProgress :
3731 last_precent : float = 0
3832 def show_progress (self , block_num , block_size , total_size ):
@@ -41,8 +35,10 @@ def show_progress(self, block_num, block_size, total_size):
4135 print (f"{ new_precent } %" , end = "\r " )
4236 self .last_precent = new_precent
4337
44- def get_file_name (headers ):
45- return re .findall ("filename=(\S+)" , headers ["Content-Disposition" ])[0 ]
38+ def get_file_name (headers , url ):
39+ if "Content-Disposition" in headers .keys ():
40+ return re .findall ("filename=(\S+)" , headers ["Content-Disposition" ])[0 ]
41+ return url .split ('/' )[- 1 ]
4642
4743def get_sha256 (filename ):
4844 sha256_hash = hashlib .sha256 ()
@@ -53,7 +49,7 @@ def get_sha256(filename):
5349 return file_checksum
5450 return
5551
56- def download_image_http (board : str , dest_folder : str , redownload : bool = False ):
52+ def download_image_http (board : Dict [ str , Any ] , dest_folder : str , redownload : bool = False ):
5753 url = board ["url" ]
5854 checksum = board ["checksum" ]
5955
@@ -67,7 +63,7 @@ def download_image_http(board: str, dest_folder: str, redownload: bool = False):
6763 # Get sha and confirm its the right image
6864 download_progress = DownloadProgress ()
6965 _ , headers_checksum = urllib .request .urlretrieve (checksum , temp_file_checksum , download_progress .show_progress )
70- file_name_checksum = get_file_name (headers_checksum )
66+ file_name_checksum = get_file_name (headers_checksum , checksum )
7167
7268 checksum_data = None
7369 with open (temp_file_checksum , 'r' ) as f :
@@ -88,7 +84,7 @@ def download_image_http(board: str, dest_folder: str, redownload: bool = False):
8884 download_progress = DownloadProgress ()
8985 _ , headers = urllib .request .urlretrieve (url , temp_file_name , download_progress .show_progress )
9086
91- file_name = get_file_name (headers )
87+ file_name = get_file_name (headers , url )
9288 file_checksum = get_sha256 (temp_file_name )
9389 if file_checksum != online_checksum :
9490 print (f'Failed. Attempt # { r + 1 } , checksum missmatch: { file_checksum } expected: { online_checksum } ' )
@@ -102,6 +98,7 @@ def download_image_http(board: str, dest_folder: str, redownload: bool = False):
10298 else :
10399 print ('Error encoutered at {RETRY} attempt' )
104100 print (e )
101+ exit (1 )
105102 else :
106103 print (f"Success: { temp_file_name } " )
107104 break
@@ -118,14 +115,25 @@ def download_image_http(board: str, dest_folder: str, redownload: bool = False):
118115 base_board = os .environ .get ("BASE_BOARD" , None )
119116 base_image_path = os .environ .get ("BASE_IMAGE_PATH" , None )
120117
121- if base_board is not None and base_board in images ["images" ]:
122- if images ["images" ][base_board ]["type" ] == "http" :
123- download_image_http (images ["images" ][base_board ], base_image_path )
124- elif images ["images" ][base_board ]["type" ] == "torrent" :
118+ if base_image_path is None :
119+ print (f'Error: did not find image config file' )
120+ exit (1 )
121+ cast (str , base_image_path )
122+
123+ image_config = get_image_config ()
124+ if image_config is not None :
125+ if image_config ["type" ] == "http" :
126+ print (f"Downloading image for { base_board } " )
127+ download_image_http (image_config , base_image_path )
128+ elif image_config ["type" ] == "torrent" :
125129 print ("Error: Torrent not implemented" )
126130 exit (1 )
127131 else :
128- print (" Error: Unsupported image download type" )
132+ print (f' Error: Unsupported image download type: { image_config [ "type" ] } ' )
129133 exit (1 )
134+ else :
135+ print (f"Error: Image config not found for: { base_board } " )
136+ exit (1 )
137+
130138
131- print ("Done" )
139+ print ("Done" )
0 commit comments