Skip to content

Commit 7acb04e

Browse files
Darkhood148TiCoKH
authored andcommitted
DEVTOOLS: COMPANION: Add probe parser and --silent flag in ISO parser
Add new parser probe which detects the file system, extension and presence of japanese of a provided ISO; Add a silent flag that disables all print and log messages
1 parent e47fcad commit 7acb04e

File tree

1 file changed

+70
-12
lines changed

1 file changed

+70
-12
lines changed

devtools/dumper-companion.py

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,53 @@ def encode_string(args: argparse.Namespace) -> int:
291291
return 0
292292

293293

294+
def probe_iso(args):
295+
fs = check_fs(args.src)
296+
print('Detected file system:', fs)
297+
args.fs = fs
298+
args.dryrun = True
299+
args.dir = Path('testing')
300+
args.silent = True
301+
args.forcemacbinary = False
302+
args.addmacbinaryext = False
303+
args.log = 'INFO'
304+
args.nopunycode = False
305+
args.japanese = False
306+
if fs == 'hybrid' or fs == 'iso9660':
307+
args.extension = check_extension(args)
308+
print('Detected extension:', args.extension)
309+
print('Japanese detected:', check_japanese(args))
310+
311+
312+
def check_japanese(args):
313+
args.japanese = False
314+
if args.fs == 'hybrid':
315+
fn = extract_volume_hybrid
316+
elif args.fs == 'iso9660':
317+
fn = extract_volume_iso
318+
else:
319+
fn = extract_volume_hfs
320+
try:
321+
fn(args)
322+
except Exception as e:
323+
args.japanese = True
324+
try:
325+
fn(args)
326+
except Exception as e:
327+
raise Exception('Could not determine japanese')
328+
else:
329+
return True
330+
else:
331+
return False
332+
333+
294334
def check_extension(args):
295335
args_copy = copy.copy(args)
296336
args_copy.dryrun = True
297337
extensions = ['joliet', 'rr', 'udf']
298338
args_copy.dir = args.dir.joinpath('test')
299339
args_copy.fs = 'iso9660'
300-
args.silent = True
340+
args_copy.silent = True
301341
for extension in extensions:
302342
args_copy.extension = extension
303343
try:
@@ -371,13 +411,15 @@ def extract_volume_hfs(args: argparse.Namespace) -> int:
371411
"""Extract an HFS volume"""
372412
source_volume: Path = args.src
373413
loglevel: str = args.log
414+
silent: bool = args.silent
374415

375416
numeric_level = getattr(logging, loglevel.upper(), None)
376417
if not isinstance(numeric_level, int):
377418
raise ValueError("Invalid log level: %s" % loglevel)
378419
logging.basicConfig(format="%(levelname)s: %(message)s", level=numeric_level)
379420

380-
logging.info(f"Loading {source_volume} ...")
421+
if not silent:
422+
logging.info(f"Loading {source_volume} ...")
381423
vol = machfs.Volume()
382424
partitions = []
383425
with source_volume.open(mode="rb") as f:
@@ -425,13 +467,15 @@ def extract_volume_iso(args: argparse.Namespace):
425467
dopunycode: bool = not args.nopunycode
426468
dryrun: bool = args.dryrun
427469
japanese: bool = args.japanese
470+
silent: bool = args.silent
428471

429472
numeric_level = getattr(logging, loglevel.upper(), None)
430473
if not isinstance(numeric_level, int):
431474
raise ValueError("Invalid log level: %s" % loglevel)
432475
logging.basicConfig(format="%(levelname)s: %(message)s", level=numeric_level)
433476

434-
logging.info(f"Loading {source_volume} ...")
477+
if not silent:
478+
logging.info(f"Loading {source_volume} ...")
435479

436480
iso = pycdlib.PyCdlib()
437481
iso.open(source_volume)
@@ -458,7 +502,7 @@ def extract_volume_iso(args: argparse.Namespace):
458502
joined_path = os.path.join(pwd, dir)
459503
if not dryrun:
460504
os.makedirs(joined_path, exist_ok=True)
461-
elif not args.silent:
505+
elif not silent:
462506
print(joined_path)
463507

464508
if dryrun:
@@ -478,21 +522,25 @@ def extract_volume_iso(args: argparse.Namespace):
478522
def extract_volume_hybrid(args: argparse.Namespace):
479523
source_volume = args.src
480524
loglevel: str = args.log
525+
silent: bool = args.silent
481526

482527
numeric_level = getattr(logging, loglevel.upper(), None)
483528
if not isinstance(numeric_level, int):
484529
raise ValueError("Invalid log level: %s" % loglevel)
485530
logging.basicConfig(format="%(levelname)s: %(message)s", level=numeric_level)
486531

487-
logging.info(f"Loading {source_volume} ...")
532+
if not silent:
533+
logging.info(f"Loading {source_volume} ...")
488534

489535
main_dir = args.dir
490536

491-
args.dir = main_dir.joinpath('hfs')
492-
extract_volume_hfs(args)
537+
args_copy = copy.copy(args)
538+
539+
args_copy.dir = main_dir.joinpath('hfs')
540+
extract_volume_hfs(args_copy)
493541

494-
args.dir = main_dir.joinpath('iso9660')
495-
extract_volume_iso(args)
542+
args_copy.dir = main_dir.joinpath('iso9660')
543+
extract_volume_iso(args_copy)
496544

497545

498546
def extract_partition(args: argparse.Namespace, vol) -> int:
@@ -502,6 +550,7 @@ def extract_partition(args: argparse.Namespace, vol) -> int:
502550
dopunycode: bool = not args.nopunycode
503551
force_macbinary: bool = args.forcemacbinary
504552
add_macbinary_ext: bool = args.addmacbinaryext
553+
silent: bool = args.silent
505554

506555
if not dryrun:
507556
destination_dir.mkdir(parents=True, exist_ok=True)
@@ -529,14 +578,14 @@ def extract_partition(args: argparse.Namespace, vol) -> int:
529578

530579
upath /= el
531580

532-
if might_be_jp and not might_be_jp_warned:
581+
if might_be_jp and not might_be_jp_warned and not silent:
533582
logging.warning(
534583
"Possible Mac-Japanese string detected, did you mean to use --japanese?"
535584
)
536585
might_be_jp_warned = True
537586

538587
if dryrun:
539-
if not isinstance(obj, machfs.Folder):
588+
if not isinstance(obj, machfs.Folder) and not silent:
540589
print(upath)
541590
continue
542591

@@ -547,7 +596,7 @@ def extract_partition(args: argparse.Namespace, vol) -> int:
547596
folders.append((upath, obj.mddate - 2082844800))
548597
continue
549598

550-
print(upath)
599+
# print(upath)
551600
if obj.data and not obj.rsrc and not force_macbinary:
552601
upath.write_bytes(obj.data)
553602

@@ -906,6 +955,11 @@ def generate_parser() -> argparse.ArgumentParser:
906955
action="store_true",
907956
help="always encode using MacBinary, even for files with no resource fork",
908957
)
958+
parser_iso.add_argument(
959+
"--silent",
960+
action="store_true",
961+
help="do not print anything"
962+
)
909963
parser_iso.add_argument(
910964
"--addmacbinaryext",
911965
action="store_true",
@@ -926,6 +980,10 @@ def generate_parser() -> argparse.ArgumentParser:
926980
parser_dir.add_argument("directory", metavar="directory ", type=Path, help="Path")
927981
parser_dir.set_defaults(func=punyencode_arg)
928982

983+
parser_probe = subparsers.add_parser("probe", help="Detect file system and extension of the given ISO")
984+
parser_probe.add_argument("src", metavar="INPUT", type=Path, help="Disk image")
985+
parser_probe.set_defaults(func=probe_iso)
986+
929987
parser_str = subparsers.add_parser(
930988
"str", help="Convert strings or standard in to or from punycode"
931989
)

0 commit comments

Comments
 (0)