|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
5 |
|
"""This module is used to operate with 7Z archives.""" |
|
3
|
|
|
|
|
4
|
5 |
|
import os # filesystem read |
|
5
|
5 |
|
import subprocess # invocation of 7z |
|
6
|
|
|
|
|
7
|
5 |
|
from bbarchivist import decorators # timer |
|
8
|
5 |
|
from bbarchivist import utilities # platform determination |
|
9
|
|
|
|
|
10
|
5 |
|
__author__ = "Thurask" |
|
11
|
5 |
|
__license__ = "WTFPL v2" |
|
12
|
5 |
|
__copyright__ = "2018 Thurask" |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
5 |
|
def szcodes(): |
|
16
|
|
|
""" |
|
17
|
|
|
Return dictionary of 7-Zip error codes. |
|
18
|
|
|
""" |
|
19
|
5 |
|
szc = { |
|
20
|
|
|
0: "NO ERRORS", |
|
21
|
|
|
1: "COMPLETED WITH WARNINGS", |
|
22
|
|
|
2: "FATAL ERROR", |
|
23
|
|
|
7: "COMMAND LINE ERROR", |
|
24
|
|
|
8: "OUT OF MEMORY ERROR", |
|
25
|
|
|
255: "USER STOPPED PROCESS" |
|
26
|
|
|
} |
|
27
|
5 |
|
return szc |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
5 |
|
@decorators.timer |
|
31
|
5 |
|
def sz_compress(filepath, filename, szexe=None, strength=5, errors=False): |
|
32
|
|
|
""" |
|
33
|
|
|
Pack a file into a LZMA2 7z file. |
|
34
|
|
|
|
|
35
|
|
|
:param filepath: Basename of file, no extension. |
|
36
|
|
|
:type filepath: str |
|
37
|
|
|
|
|
38
|
|
|
:param filename: Name of file to pack. |
|
39
|
|
|
:type filename: str |
|
40
|
|
|
|
|
41
|
|
|
:param szexe: Path to 7z executable. |
|
42
|
|
|
:type szexe: str |
|
43
|
|
|
|
|
44
|
|
|
:param strength: Compression strength. 5 is normal, 9 is ultra. |
|
45
|
|
|
:type strength: int |
|
46
|
|
|
|
|
47
|
|
|
:param errors: Print completion status message. Default is false. |
|
48
|
|
|
:type errors: bool |
|
49
|
|
|
""" |
|
50
|
5 |
|
strength = str(strength) |
|
51
|
5 |
|
szc = szcodes() |
|
52
|
5 |
|
rawname = os.path.dirname(filepath) |
|
53
|
5 |
|
thr = str(utilities.get_core_count()) |
|
54
|
5 |
|
fold = os.path.join(rawname, filename) |
|
55
|
5 |
|
cmd = '{0} a -mx{1} -m0=lzma2 -mmt{2} "{3}.7z" "{4}"'.format(szexe, strength, thr, filepath, fold) |
|
56
|
5 |
|
excode = sz_subprocess(cmd) |
|
57
|
5 |
|
if errors: |
|
58
|
5 |
|
print(szc[excode]) |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
5 |
|
def sz_subprocess(cmd): |
|
62
|
|
|
""" |
|
63
|
|
|
Subprocess wrapper for 7-Zip commands. |
|
64
|
|
|
|
|
65
|
|
|
:param cmd: Command to pass to subprocess. |
|
66
|
|
|
:type cmd: str |
|
67
|
|
|
""" |
|
68
|
5 |
|
with open(os.devnull, 'wb') as dnull: |
|
69
|
5 |
|
output = subprocess.call(cmd, stdout=dnull, stderr=subprocess.STDOUT, shell=True) |
|
70
|
5 |
|
return output |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
5 |
|
def sz_verify(filepath, szexe=None): |
|
74
|
|
|
""" |
|
75
|
|
|
Verify that a .7z file is valid and working. |
|
76
|
|
|
|
|
77
|
|
|
:param filepath: Filename. |
|
78
|
|
|
:type filepath: str |
|
79
|
|
|
|
|
80
|
|
|
:param szexe: Path to 7z executable. |
|
81
|
|
|
:type szexe: str |
|
82
|
|
|
""" |
|
83
|
5 |
|
filepath = os.path.abspath(filepath) |
|
84
|
5 |
|
cmd = '{0} t "{1}"'.format(szexe, filepath) |
|
85
|
5 |
|
excode = sz_subprocess(cmd) |
|
86
|
5 |
|
return excode == 0 |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
5 |
|
def pack_tclloader_sz(dirname, filename, strength=5): |
|
90
|
|
|
""" |
|
91
|
|
|
Compress Android autoloader folder into a 7z file. |
|
92
|
|
|
|
|
93
|
|
|
:param dirname: Target folder. |
|
94
|
|
|
:type dirname: str |
|
95
|
|
|
|
|
96
|
|
|
:param filename: File title, without extension. |
|
97
|
|
|
:type filename: str |
|
98
|
|
|
|
|
99
|
|
|
:param strength: Compression strength. 5 is normal, 9 is ultra. |
|
100
|
|
|
:type strength: int |
|
101
|
|
|
""" |
|
102
|
5 |
|
szexe = utilities.get_seven_zip() |
|
103
|
5 |
|
thr = str(utilities.get_core_count()) |
|
104
|
5 |
|
cmd = '{0} a -mx{1} -m0=lzma2 -mmt{2} "{3}.7z" "./{4}/*"'.format(szexe, strength, thr, filename, dirname) |
|
105
|
|
|
sz_subprocess(cmd) |
|
106
|
|
|
|