-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (115 loc) · 5.71 KB
/
main.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
import os
import subprocess
import getpass
import re
import sys
class FileCompression7z:
project_description = """
*****************************************************************
| Python 7z File Compression Utility Program |
*****************************************************************
| Compress or uncompress each top level directory(s) |
| and/or file(s) listed at a specific path location into |
| seperate compressed files without needing to use 7zip |
| commands for each individual item. You will only need to |
| provide one password for a collection of items. |
*****************************************************************
"""
compress = ""
dir_loc = ""
files = []
password_protect = ""
password = ""
def __init__(self):
self.print_description()
self.check7z()
self.compression_prompt()
self.parent_directory_prompt()
self.password_protection_prompts()
self.file_compression_actions()
def print_description(self):
print(self.project_description)
def check7z(self):
try:
subprocess.check_output('7z')
except OSError:
print('7z cli tools not installed')
input('Press enter to end program: ')
exit()
def compression_prompt(self):
while(self.compress == ""):
self.compress = str.lower(input('Compress or uncompress all (compress/uncompress): '))
if(self.compress == "compress" or self.compress == "uncompress"):
break
else:
print('Input must be either "compress" or "uncompress"')
self.compress = ""
def parent_directory_prompt(self):
while(self.dir_loc == ""):
self.dir_loc = input('Parent target directory of file(s)/folder(s):')
self.dir_loc = self.dir_loc.strip("\"")
if(self.dir_loc == ""):
print('Target directory cannot be found.')
else:
try:
self.files = os.listdir(self.dir_loc)
if(len(self.files) == 0):
print('Target directory does not contain any files or folders')
self.dir_loc = ""
else:
break
except FileNotFoundError:
self.dir_loc = ""
print('Taget directory does not exist')
def password_protection_prompts(self):
while(self.password_protect == ""):
self.password_protect = str.lower(input('Password protection for all files and folders (y/n)? '))
if(self.password_protect == "y" or self.password_protect == "n"):
if(self.password_protect == "y"):
while(self.password == ""):
if(self.compress == "compress"):
print("Password must be greater than 10 characters, contain at least one digit, and have at least one of the following characters: !@#$%^&*:<?")
self.password = getpass.getpass("Password (no echo): ")
if(self.compress == "compress"):
if(len(self.password) < 10):
print('Length of password must be greater than 10 characters')
self.password = ""
if(re.search(r"\d", self.password ) == None):
print('Password must contain at least one digit')
self.password = ""
if(re.search(r"[!@#$%^&*:<?]", self.password) == None):
print('Password must contain at least one of the following special characters !@#$%^&*:<?')
self.password = ""
if(self.password != ""):
password_verify = getpass.getpass("Verify Password (no echo): ")
if(self.password != password_verify):
print("Password does not match, try again.")
self.password = ""
else:
break
else:
print('Input must be either "y" or "n"')
self.password_protect = ""
def file_compression_actions(self):
os.chdir(self.dir_loc)
for file in self.files:
if(self.compress == "compress"):
file = file.replace(" ", "-")
if(re.search('[.]', file) == None):
if(self.password != ""):
os.system(f"7z a -p{self.password} {file}-encrypted.7z {file}")
else:
os.system(f"7z a {file}.7z {file}")
else:
file_no_extension = file.split(',')[0]
if(self.password != ""):
os.system(f"7z a -p{self.password} {file_no_extension}-encrypted.7z {file}")
else:
os.system(f"7z a {file_no_extension}.7z {file}")
else:
if ".7z" in file:
if(self.password != ""):
os.system(f"7z x -y -p{self.password} {file}")
else:
os.system(f"7z x -y {file}")
FileCompression7z()