-
Notifications
You must be signed in to change notification settings - Fork 0
/
zunzip
executable file
·100 lines (83 loc) · 3.24 KB
/
zunzip
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
import os
import sys
import argparse
import logging
import fnmatch
import shutil
import glob
import zipfile
import rarfile
from pathlib import Path
import contextlib
@contextlib.contextmanager
def ignored(*exceptions):
try:
yield
except exceptions:
pass
def process_container(scratch_path, archive_path, filter_pattern):
for path, _, files in os.walk(os.path.abspath(scratch_path)):
for archive in fnmatch.filter(files, filter_pattern):
source = os.path.join(path, archive)
sarch = Path(source)
processed = os.path.join(archive_path, archive)
print(f"Evaluate {source}")
# if there is a folder of the same name move to processed
target = Path(source.replace('.zip','').replace('.rar',''))
# else unzip and then move the file
# before doing so ensure the archive contains FLAC files
movearch = False
if target.exists() and target.is_dir():
# maybe check for flac files here too
movearch = True
else:
# define the container "class" methods
container = zipfile.ZipFile
if 'rar' in filter_pattern:
container = rarfile.RarFile
with container(source) as zip:
# available files in the container, inc. mp3 ughhhh!
for _ftest in zip.namelist():
if any(_test in _ftest for _test in ('flac','dsd','dsf','wav','mp3')):
zip.printdir()
print(f">>> Extract {source} to {target}")
target.mkdir(parents=True, exist_ok=True)
zip.extractall(target)
movearch = True
break
if movearch:
print(f">>> Move {source} to {archive_path}")
# use shutil so cross disk & NAS moves are supported
shutil.move(sarch,processed)
def main(args):
process_container(args.directory, args.processed, args.filter)
log_file = '/tmp/zunzip.log'
parser = argparse.ArgumentParser()
parser.add_argument('--directory', '-d',
help='Base Directory',
type=str,
default='/hdd/scratch/')
parser.add_argument('--processed', '-a',
help='Processed Directory',
type=str,
default='/data2/processed/')
parser.add_argument('--filter', '-f',
help='Archive (filter .zip or .rar string)',
type=str,
default='* - *.zip')
args = parser.parse_args()
if __name__ == "__main__":
log_format = '%(asctime)s %(levelname)-8s %(message)s'
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter(log_format)
console.setFormatter(formatter)
logging.basicConfig(level=logging.DEBUG,
format=log_format,
datefmt='%m-%d-%y %H:%M',
filename=log_file,
filemode='a')
logging.getLogger('').addHandler(console)
main(args)
sys.exit(0)