-
Notifications
You must be signed in to change notification settings - Fork 2
/
fortress-cli.py
executable file
·123 lines (95 loc) · 3.82 KB
/
fortress-cli.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
#!/usr/bin/python
import glob, os, sys
import time, getpass
class Util:
# Print iterations progress
@staticmethod
def progressBar(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
bar_length - Optional : character length of bar (Int)
"""
str_format = "{0:." + str(decimals) + "f}" #{0:.1f}
percents = str_format.format(100 * (iteration / float(total))) #{0:.1f}.format(100*(i/float(l))
filled_length = int(round(bar_length * iteration / float(total)))
bar = '*' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
class Fortress:
def __init__(self, action, dirPath, password):
# Local variables
self._files = []
self._action = action
self._dirPath = dirPath
self._password = password
# Invoke Methods
self.fetchFiles()
self.setupDir()
actionAttr = getattr(self, action)
actionAttr()
# print self._files
def encrypt(self):
# Progress bar
i = 0
l = len(self._files)
Util.progressBar(i, l, prefix = 'Progress:', suffix = 'Complete', bar_length = 50)
# Iterating over files list to decrypt
for file in self._files:
# Decrypting the pdf file
cmd = "pdftk '" + file + "' output '" + self._action + "/" + file + "' user_pw " + self._password #pdftk 'filename' output 'encrypt/filename' user_pwd password
os.system(cmd)
# Progress bar
i += 1
suffix = file
if i == l:
suffix = 'Completed '
Util.progressBar(i, l, prefix = 'Encrypting:', suffix = suffix, bar_length = 50)
def decrypt(self):
# Progress bar
i = 0
l = len(self._files)
Util.progressBar(i, l, prefix = 'Progress:', suffix = 'Complete', bar_length = 50)
# Iterating over files list to decrypt
for file in self._files:
# Decrypting the pdf file
cmd = "pdftk '" + file + "' input_pw " + self._password +" output '" + self._action + "/" + file +"'"
os.system(cmd)
# Progress bar
i += 1
suffix = file
if i == l:
suffix = 'Completed '
Util.progressBar(i, l, prefix = 'Decrypting:', suffix = suffix, bar_length = 50)
def fetchFiles(self):
# Changing the working directory
try:
os.chdir(self._dirPath) #changes directory to the specified path. if directory is not mentioned , this will throw an error
print "directory successfully changed to ", self._dirPath
except OSError:
print "Please enter only a valid directory with absolute or relative path"
exit()
# Iterating over *.pdf extension files using glob
for file in glob.glob("*.pdf"): #to match and collect all the .pdf files"
# Appending glob files into list
self._files.append(file) #the files with filename is added to the list file[]
def setupDir(self):
# Checks directory exists
if not os.path.isdir(self._action):
# Create directory
os.system("mkdir " + self._action)
# Need pdftk to be installed before using this code // sudo apt-get install pdftk
# Sample usage below
# Fortress("encrypt|decrypt", "path/to/folder", "password")
action = raw_input("Enter the action to be done\n'encrypt' for encryption\n'decrypt' for decryption:")
path = raw_input("Enter the absolute/relative path of directory in which the pdf files exists:")
password = getpass.getpass()
Fortress(action, path, password)
# Fortress("decrypt", "./sample", "12345")