-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyextract.py
executable file
·60 lines (42 loc) · 1.52 KB
/
pyextract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import argparse
import os
import subprocess
import logging
from pathlib import Path
def main():
logging.basicConfig(format='%(message)s')
parser = argparse.ArgumentParser()
parser.add_argument("input", help="File/Folder to extract")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable Verbosity")
parser.add_argument("-o", "--output", action='store', help="Output directory")
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
if not os.path.exists(args.input):
logging.error("ERROR: Requested file/folder does not exists")
exit(-1)
if os.path.isfile(args.input):
extract_file(args.input, args.output)
else:
extract_dir(args.input, args.output)
exit(0)
def extract_file(filename, output):
extract_cmd = ['7z']
extract_cmd.extend(['x', filename])
if filename.endswith(('.rar','.zip')):
if output:
extract_cmd.append('-o' + output)
else:
extract_cmd.append('-o' + os.path.dirname(filename))
subprocess.call(extract_cmd)
else:
logging.error("ERROR: Unsupported file format: " + filename)
def extract_dir(directory, output):
file_list = sorted(Path(directory).glob('**/*.rar'))
file_list += sorted(Path(directory).glob('**/*.zip'))
for x in file_list:
logging.debug("Attempting to extract: " + os.fspath(x))
extract_file(os.fspath(x), output)
if __name__ == '__main__':
main()