|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | import os |
9 | | -import re |
10 | 9 | import shutil |
11 | 10 | import tarfile |
12 | 11 | from os import path |
|
24 | 23 | ota_extractor_path, |
25 | 24 | sdat2img_path, |
26 | 25 | ) |
27 | | -from extract_utils.utils import Color, color_print, run_cmd, scan_tree |
| 26 | +from extract_utils.utils import ( |
| 27 | + Color, |
| 28 | + color_print, |
| 29 | + find_file, |
| 30 | + find_files, |
| 31 | + run_cmd, |
| 32 | +) |
28 | 33 |
|
29 | 34 | ALTERNATE_PARTITION_PATH_MAP = { |
30 | 35 | 'product': 'system/product', |
@@ -102,75 +107,6 @@ def __init__( |
102 | 107 | self.extract_all = extract_all |
103 | 108 |
|
104 | 109 |
|
105 | | -def file_name_to_partition(file_name: str): |
106 | | - return file_name.split('.', 1)[0] |
107 | | - |
108 | | - |
109 | | -def find_files( |
110 | | - input_path: str, |
111 | | - partition: Optional[str] = None, |
112 | | - name: Optional[str] = None, |
113 | | - regex: Optional[str] = None, |
114 | | - magic: Optional[bytes] = None, |
115 | | - position: int = 0, |
116 | | - ext: Optional[str] = None, |
117 | | -) -> List[str]: |
118 | | - file_paths: List[str] = [] |
119 | | - for file in scan_tree(input_path): |
120 | | - if not file.is_file(): |
121 | | - continue |
122 | | - |
123 | | - file_partition_name = file_name_to_partition(file.name) |
124 | | - if partition is not None and partition != file_partition_name: |
125 | | - continue |
126 | | - |
127 | | - if name is not None and name != file.name: |
128 | | - continue |
129 | | - |
130 | | - if regex is not None and re.match(regex, file.name) is None: |
131 | | - continue |
132 | | - |
133 | | - if ext is not None and not file.name.endswith(ext): |
134 | | - continue |
135 | | - |
136 | | - if magic is not None: |
137 | | - with open(file, 'rb') as f: |
138 | | - f.seek(position) |
139 | | - file_magic = f.read(len(magic)) |
140 | | - if file_magic != magic: |
141 | | - continue |
142 | | - |
143 | | - file_paths.append(file.path) |
144 | | - |
145 | | - return file_paths |
146 | | - |
147 | | - |
148 | | -def find_file( |
149 | | - input_path: str, |
150 | | - partition: Optional[str] = None, |
151 | | - name: Optional[str] = None, |
152 | | - regex: Optional[str] = None, |
153 | | - magic: Optional[bytes] = None, |
154 | | - position: int = 0, |
155 | | - ext: Optional[str] = None, |
156 | | -): |
157 | | - file_paths = find_files( |
158 | | - input_path, |
159 | | - partition=partition, |
160 | | - name=name, |
161 | | - regex=regex, |
162 | | - magic=magic, |
163 | | - position=position, |
164 | | - ext=ext, |
165 | | - ) |
166 | | - |
167 | | - assert len(file_paths) <= 1 |
168 | | - if file_paths: |
169 | | - return file_paths[0] |
170 | | - |
171 | | - return None |
172 | | - |
173 | | - |
174 | 110 | def find_alternate_partitions( |
175 | 111 | extract_partitions: List[str], |
176 | 112 | found_partitions: Iterable[str], |
|
0 commit comments