7
7
import hashlib
8
8
import shutil
9
9
import re
10
+ from typing import Dict , Any , Optional , cast
11
+ from common import get_image_config , read_images
10
12
PRECENT_PROGRESS_SIZE = 5
11
13
12
14
class ChecksumFailException (Exception ):
13
15
pass
14
16
15
- IMAGES_CONFIG = os .path .join (os .path .dirname (__file__ ), "images.yml" )
16
17
RETRY = 3
17
18
18
19
def ensure_dir (d , chmod = 0o777 ):
@@ -26,13 +27,6 @@ def ensure_dir(d, chmod=0o777):
26
27
return False
27
28
return True
28
29
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
-
36
30
class DownloadProgress :
37
31
last_precent : float = 0
38
32
def show_progress (self , block_num , block_size , total_size ):
@@ -53,7 +47,7 @@ def get_sha256(filename):
53
47
return file_checksum
54
48
return
55
49
56
- def download_image_http (board : str , dest_folder : str , redownload : bool = False ):
50
+ def download_image_http (board : Dict [ str , Any ] , dest_folder : str , redownload : bool = False ):
57
51
url = board ["url" ]
58
52
checksum = board ["checksum" ]
59
53
@@ -118,14 +112,21 @@ def download_image_http(board: str, dest_folder: str, redownload: bool = False):
118
112
base_board = os .environ .get ("BASE_BOARD" , None )
119
113
base_image_path = os .environ .get ("BASE_IMAGE_PATH" , None )
120
114
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" :
115
+ if base_image_path is None :
116
+ print (f'Error: did not find image config file' )
117
+ exit (1 )
118
+ cast (str , base_image_path )
119
+
120
+ image_config = get_image_config ()
121
+ if image_config is not None :
122
+ if image_config ["type" ] == "http" :
123
+ download_image_http (image_config , base_image_path )
124
+ elif image_config ["type" ] == "torrent" :
125
125
print ("Error: Torrent not implemented" )
126
126
exit (1 )
127
127
else :
128
- print (" Error: Unsupported image download type" )
128
+ print (f' Error: Unsupported image download type: { image_config [ "type" ] } ' )
129
129
exit (1 )
130
+
130
131
131
132
print ("Done" )
0 commit comments