-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip_files.py
140 lines (115 loc) · 4.23 KB
/
zip_files.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import sys
import os
import subprocess
import shutil
import logging
import media_work_queue_enums as enums
import config_helper as config
##############################
# Logging
##############################
logger = logging.getLogger('media_work_queue.zip_files')
##############################
# Enums
##############################
VideoType = enums.VideoType
##############################
# Constants
##############################
archive_password = config.ConfigSectionMap("7ZIP")['archivepassword']
zip_exe_path = config.ConfigSectionMap("7ZIP")['exepath']
##############################
# Commands
##############################
zip_command = '\"{}\" a \"{}\" \"{}\" -p{}' # if you remove any variables, make sure to update zip method
unzip_command = '\"{}\" x \"{}\" -p{} -o\"{}\"' # if you remove any variables, make sure to update unzip method
unzip_rar = '\"{}\" x \"{}\" -o\"{}\"' # if you remove any variables, make sure to update unzip_rar method
##############################
# Zip directory
##############################
def zip(dir_path):
"""zip(dir_path)
Inputs:
dir_path: fully qualified path to the directory to zip
"""
if dir_path is None:
logger.error('path cannot be null')
raise ValueError('path cannot be null')
if os.path.isdir(dir_path):
output = dir_path + '.7z'
logger.info("==================================================")
logger.info("Zipping: " + dir_path + " To: " + output)
try:
command = zip_command.format(zip_exe_path, output, dir_path, archive_password)
logger.debug("Command: {}".format(command))
subprocess.call(command)
logger.info("Zip Complete!")
logger.info("Deleteing " + dir_path)
shutil.rmtree(dir_path)
logger.info("==================================================")
except:
logger.error('Potential Error On: ' + dir_path)
####################################################
# Unzip all Archive Files in specified directory
####################################################
def unzip(dir_path):
"""unzip(dir_path)
Inputs:
dir_path: fully qualified path to the location where your files are that
should be unzipped
"""
if dir_path is None:
logger.error('path cannot be null')
raise ValueError('path cannot be null')
for zipFile in os.listdir(dir_path):
input = dir_path + '\\' + zipFile
logger.info("==================================================")
logger.info("Unzipping: " + input)
logger.info("Destination: " + dir_path)
try:
command = unzip_command.format(zip_exe_path, input, archive_password, dir_path)
logger.debug("Command: {}".format(command))
subprocess.call(command)
logger.info("Unzip Complete!")
logger.info("==================================================")
except:
logger.error('Potential Error On: ' + input)
##############################
# Unzip Rar Files
##############################
def unzip_rar(video_type, dir_path):
"""unzip(dir_path)
Inputs:
video_type: valid types are - {}
dir_path: fully qualified path to the location where your files are that
should be unzipped
""".format(VideoType.tv)
if video_type is None:
logger.error('video_type cannot be null')
raise ValueError('video_type cannot be null')
if dir_path is None:
logger.error('path cannot be null')
raise ValueError('path cannot be null')
if video_type == VideoType.tv:
for season in os.listdir(dir_path):
# Season 01, 02, ect.
season_dir = dir_path + '\\' + season
logger.debug("Processing season: {}".format(season_dir))
# Rar files should be organized in directory per episode
for episode in os.listdir(season_dir):
episode_path = season_dir + '\\' + episode
for f in os.listdir(episode_path):
file_path = episode_path + '\\' + f
filename, file_extension = os.path.splitext(file_path)
if file_extension == '.rar':
input = file_path
logger.info("==================================================")
logger.info("Unzipping: " + input)
try:
command = unzip_rar.format(zip_exe_path, input, season_dir)
logger.debug("Command: {}".format(command))
subprocess.call(command)
logger.info("Unzip Complete!")
logger.info("==================================================")
except:
logger.info('Potential Error On: ' + input)