-
Notifications
You must be signed in to change notification settings - Fork 0
/
protect.py
174 lines (150 loc) · 5.56 KB
/
protect.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import platform
import shutil
from helpers.CustomCI import CustomInput, CustomPrint
# Detect OS
isWindows = False
isLinux = False
if platform.system() == 'Windows':
isWindows = True
if platform.system() == 'Linux':
isLinux = True
# Global command line helpers
extracted = 'extracted/'
bin = 'bin/'
if(isWindows):
sevenZip = 'bin\\7za.exe'
else:
sevenZip = '7z'
def main():
CustomPrint('This utility is for archiving your output folder with password to enchance it\'s security. Secure is a relative term. Choose longer password.')
isCompressing = CustomInput('Are you (C)ompressing or (D)ecompressing? : ')
while(True):
if(isCompressing.upper() == 'C'):
ListUserFolders()
print('\n')
userFolder = CustomInput(
'Enter a name of folder from above (case sensitive) : ')
Compress(userFolder)
break
elif(isCompressing.upper() == 'D'):
ListUserFiles()
print('\n')
userZip = CustomInput(
'Enter a name of file from above (case sensitive) : ')
Uncompress(userZip)
break
else:
isCompressing = CustomInput('Choose either \'c\' or \'d\' : ')
continue
def Compress(userFolder):
if(not os.path.isdir(extracted + userFolder)):
CustomPrint('Could not find directory ' + extracted + userFolder)
elif(len(os.listdir(extracted + userFolder)) == 0):
CustomPrint('User folder is empty.')
Exit()
else:
password = CustomInput('Choose a password for zip : ')
if(password):
password = ' -p' + password
os.system(sevenZip + ' a -t7z -mhe ' + extracted +
userFolder + ' ' + extracted + userFolder + '/* ' + password)
print('\n')
CustomPrint(
'If you see \'Everything is OK\' in above line then it is recommended to delete user folder.')
deleteUserFolder = CustomInput(
'Delete ' + userFolder + ' folder? (default y) : ') or 'y'
print('\n')
CustomPrint('\aYour \'' + userFolder + '.7z\' file is in extracted folder. Password is : ' +
password.replace(' -p', ''), 'yellow')
print('\n')
CustomInput('Hit Enter key to continue.')
if(deleteUserFolder.upper() == 'Y'):
DeleteUserFolder(userFolder)
else:
Exit()
def DeleteUserFolder(userFolder):
CustomPrint('Deleting...')
try:
shutil.rmtree(extracted + userFolder)
except Exception as e:
CustomPrint(e, 'red')
CustomPrint('Please manually delete it.', 'red')
Exit()
def DeleteUserZip(userZip):
CustomPrint('Deleting...')
try:
os.remove(extracted + userZip)
except Exception as e:
CustomPrint(e, 'red')
CustomPrint('Please manually delete it.', 'red')
Exit()
def Exit():
print('\n')
CustomPrint('Exiting...')
try: # Open in explorer.
if(isWindows):
os.startfile(os.path.realpath(extracted))
elif(isLinux):
os.system('xdg-open ' + os.path.realpath(extracted))
else:
os.system('open ' + os.path.realpath(extracted))
except:
pass
quit()
def ListUserFiles():
print('\n')
CustomPrint('Available user files in extracted directory.')
print('\n')
allFiles = next(os.walk(extracted))[2]
if(len(allFiles) == 1 and os.path.isfile(extracted + '.placeholder')):
CustomPrint('No user files found in ' + extracted + ' folder.', 'red')
Exit()
for file in allFiles:
if(file != '.placeholder'):
CustomPrint(file)
def ListUserFolders():
print('\n')
CustomPrint('Available user folders in extracted directory.')
print('\n')
allFolders = next(os.walk(extracted))[1]
if(len(allFolders) == 0):
CustomPrint('No folders found in ' + extracted + ' folder.', 'red')
Exit()
for folder in allFolders:
CustomPrint(folder)
def Uncompress(userZip):
if(not str(userZip).endswith('7z')):
userZip = userZip + '.7z'
if(not os.path.isfile(extracted + userZip)):
CustomPrint('Could not find ' + extracted + userZip)
elif(os.path.getsize(extracted + userZip) <= 0):
CustomPrint(extracted + userZip + ' is empty.')
Exit()
else:
password = CustomInput('Enter password, leave empty for none : ')
if(password):
password = ' -p' + password
os.system(sevenZip + ' e -aot ' + extracted + userZip +
' -o' + extracted + userZip.replace('.7z', '') + password)
print('\n')
CustomPrint(
'If you see \'Everything is OK\' in above line then you can delete user zip file.')
deleteUserZip = CustomInput(
'Delete ' + userZip + ' ? (default n) : ') or 'n'
print('\n')
CustomPrint('\aYour extracted \'' + userZip.replace('.7z',
'') + '\' folder is in extracted folder.', 'yellow')
print('\n')
CustomInput('Hit Enter key to continue.')
if(deleteUserZip.upper() == 'Y'):
DeleteUserZip(userZip)
else:
Exit()
if __name__ == "__main__":
main()
# For zipping and unzipping the extracted folder.
# .\bin\7za.exe a -t7z .\extracted\yuvraj .\extracted\yuvraj\* -p1234 -mhe
# (add) (type 7z) (name of ouput archive) (what to archive) (passowrd) (header ecnryption)
# check if already exists.
# .\bin\7za.exe e -aot .\extracted\yuvraj.7z -oextracted\yuvraj -p1234