Skip to content

Commit 2ae6319

Browse files
committed
extract_utils: move find_file() to utils
Change-Id: I962baf6f8aebfe5073ed75fee2f9e12941bbcfb3
1 parent 6394756 commit 2ae6319

2 files changed

Lines changed: 77 additions & 71 deletions

File tree

extract_utils/extract.py

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from __future__ import annotations
77

88
import os
9-
import re
109
import shutil
1110
import tarfile
1211
from os import path
@@ -24,7 +23,13 @@
2423
ota_extractor_path,
2524
sdat2img_path,
2625
)
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+
)
2833

2934
ALTERNATE_PARTITION_PATH_MAP = {
3035
'product': 'system/product',
@@ -102,75 +107,6 @@ def __init__(
102107
self.extract_all = extract_all
103108

104109

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-
174110
def find_alternate_partitions(
175111
extract_partitions: List[str],
176112
found_partitions: Iterable[str],

extract_utils/utils.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import hashlib
99
import importlib.util
1010
import os
11+
import re
1112
import shutil
1213
from contextlib import contextmanager
1314
from enum import Enum
@@ -305,3 +306,72 @@ def read_mmap_chunked(
305306
def write_zero(f: BinaryIO, size: int):
306307
f.seek(size - 1, SEEK_CUR)
307308
f.write(b'\x00')
309+
310+
311+
def file_name_to_partition(file_name: str):
312+
return file_name.split('.', 1)[0]
313+
314+
315+
def find_files(
316+
input_path: str,
317+
partition: Optional[str] = None,
318+
name: Optional[str] = None,
319+
regex: Optional[str] = None,
320+
magic: Optional[bytes] = None,
321+
position: int = 0,
322+
ext: Optional[str] = None,
323+
) -> List[str]:
324+
file_paths: List[str] = []
325+
for file in scan_tree(input_path):
326+
if not file.is_file():
327+
continue
328+
329+
file_partition_name = file_name_to_partition(file.name)
330+
if partition is not None and partition != file_partition_name:
331+
continue
332+
333+
if name is not None and name != file.name:
334+
continue
335+
336+
if regex is not None and re.match(regex, file.name) is None:
337+
continue
338+
339+
if ext is not None and not file.name.endswith(ext):
340+
continue
341+
342+
if magic is not None:
343+
with open(file, 'rb') as f:
344+
f.seek(position)
345+
file_magic = f.read(len(magic))
346+
if file_magic != magic:
347+
continue
348+
349+
file_paths.append(file.path)
350+
351+
return file_paths
352+
353+
354+
def find_file(
355+
input_path: str,
356+
partition: Optional[str] = None,
357+
name: Optional[str] = None,
358+
regex: Optional[str] = None,
359+
magic: Optional[bytes] = None,
360+
position: int = 0,
361+
ext: Optional[str] = None,
362+
):
363+
file_paths = find_files(
364+
input_path,
365+
partition=partition,
366+
name=name,
367+
regex=regex,
368+
magic=magic,
369+
position=position,
370+
ext=ext,
371+
)
372+
373+
assert len(file_paths) <= 1
374+
if file_paths:
375+
return file_paths[0]
376+
377+
return None

0 commit comments

Comments
 (0)