2024-04-23 18:22:53 -04:00
|
|
|
#!/usr/bin/env python3 -B
|
|
|
|
# coding=utf-8
|
2022-04-01 10:43:22 -04:00
|
|
|
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
2024-04-23 18:22:53 -04:00
|
|
|
Copyright (C) 2022-2024 Plato Mavropoulos
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
from common.externals import get_szip_path
|
|
|
|
from common.system import printer
|
2022-04-01 10:43:22 -04:00
|
|
|
|
|
|
|
|
2022-06-26 12:09:01 -04:00
|
|
|
def check_bad_exit_code(exit_code):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Check 7-Zip bad exit codes (0 OK, 1 Warning) """
|
|
|
|
|
|
|
|
if exit_code not in (0, 1):
|
|
|
|
raise ValueError(f'Bad exit code: {exit_code}')
|
|
|
|
|
2022-06-26 12:09:01 -04:00
|
|
|
|
2022-08-28 13:02:55 -04:00
|
|
|
def is_szip_supported(in_path, padding=0, args=None, check=False, silent=False):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Check if file is 7-Zip supported """
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
try:
|
2022-08-28 13:02:55 -04:00
|
|
|
if args is None:
|
|
|
|
args = []
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-08-28 13:02:55 -04:00
|
|
|
szip_c = [get_szip_path(), 't', in_path, *args, '-bso0', '-bse0', '-bsp0']
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 18:31:29 -04:00
|
|
|
szip_t = subprocess.run(szip_c, check=False)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if check:
|
|
|
|
check_bad_exit_code(szip_t.returncode)
|
2024-04-23 18:22:53 -04:00
|
|
|
except Exception as error: # pylint: disable=broad-except
|
2022-06-21 07:23:08 -04:00
|
|
|
if not silent:
|
2024-04-23 18:22:53 -04:00
|
|
|
printer(f'Error: 7-Zip could not check support for file {in_path}: {error}!', padding)
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
return False
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
return True
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-08-28 13:02:55 -04:00
|
|
|
def szip_decompress(in_path, out_path, in_name, padding=0, args=None, check=False, silent=False):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Archive decompression via 7-Zip """
|
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if not in_name:
|
|
|
|
in_name = 'archive'
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
try:
|
2022-08-28 13:02:55 -04:00
|
|
|
if args is None:
|
|
|
|
args = []
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-08-28 13:02:55 -04:00
|
|
|
szip_c = [get_szip_path(), 'x', *args, '-aou', '-bso0', '-bse0', '-bsp0', f'-o{out_path}', in_path]
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 18:31:29 -04:00
|
|
|
szip_x = subprocess.run(szip_c, check=False)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if check:
|
|
|
|
check_bad_exit_code(szip_x.returncode)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-07-06 10:54:17 -04:00
|
|
|
if not os.path.isdir(out_path):
|
2024-04-23 18:22:53 -04:00
|
|
|
raise OSError(f'Extraction directory not found: {out_path}')
|
|
|
|
except Exception as error: # pylint: disable=broad-except
|
2022-06-21 07:23:08 -04:00
|
|
|
if not silent:
|
2024-04-23 18:22:53 -04:00
|
|
|
printer(f'Error: 7-Zip could not extract {in_name} file {in_path}: {error}!', padding)
|
|
|
|
|
2022-04-01 10:43:22 -04:00
|
|
|
return 1
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-06-21 07:23:08 -04:00
|
|
|
if not silent:
|
|
|
|
printer(f'Succesfull {in_name} decompression via 7-Zip!', padding)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-15 11:17:58 -04:00
|
|
|
return 0
|