Skip to content

Commit 407b7a0

Browse files
authored
Add files via upload
1 parent ee0591e commit 407b7a0

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

webArchiver.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/bin/python3
2+
import os
3+
import hashlib
4+
import csv
5+
import datetime
6+
import argparse
7+
import shutil
8+
import configparser
9+
#set vars
10+
archivedir1=os.path.expanduser("~") + "/archiver/files/"
11+
archivedir2=os.path.expanduser("~") + "/archiver/archive/"
12+
csv1=archivedir1 + "csv1"
13+
csv2=archivedir1 + "csv2"
14+
datetimeformat="%y.%m.%d-%H:%M"
15+
datetimes=datetime.datetime.now().strftime(datetimeformat)
16+
hashblocksize=65536
17+
hashertype="sha256"
18+
configfile=os.path.expanduser("~") + "/archiver/archiver.conf"
19+
#readtags
20+
piparser = argparse.ArgumentParser()
21+
piparser.add_argument("-fi", "--folderin", help="Input folder")
22+
piparser.add_argument("-fs", "--foldersave", help="Archive save directory location")
23+
piparser.add_argument("-fa", "--folderarchive", help="Archive archiving directory location")
24+
piparser.add_argument("-fl1", "--filelist1", help="Archive filelist1 location")
25+
piparser.add_argument("-fl2", "--filelist2", help="Archive filelist2 location")
26+
piparser.add_argument("-dtf", "--datetimeformat", help="Set datetime format")
27+
piparser.add_argument("-hb", "--hashblocksize", help="Set hash blocksize")
28+
piparser.add_argument("-ht", "--hashtype", help="Set hash type to one of:'blake2s', 'sha384', 'sha512', 'sha3_256', 'sha256', 'md5', 'sha3_512', 'sha3_224', 'shake_128', 'shake_256', 'sha1', 'blake2b', 'sha224', 'sha3_384'")
29+
piparser.add_argument("-c", "--configfile", help="Set configfile in init format (in [DEFAULT])")
30+
piargs = piparser.parse_args()
31+
if piargs.configfile is not None:
32+
configfile=piargs.configfile
33+
#to do:read config file
34+
config = configparser.ConfigParser()
35+
if os.path.isfile(configfile):
36+
config.read(configfile)
37+
if "DEFAULT" in config:
38+
if "folderarchive" in config["DEFAULT"]:
39+
archivedir2=config["DEFAULT"]["folderarchive"]
40+
if "filelist1" in config["DEFAULT"]:
41+
csv1=config["DEFAULT"]["filelist1"]
42+
if "filelist2" in config["DEFAULT"]:
43+
csv2=config["DEFAULT"]["filelist2"]
44+
if "datetimeformat" in config["DEFAULT"]:
45+
datetimeformat=config["DEFAULT"]["datetimeformat"]
46+
if "hashblocksize" in config["DEFAULT"]:
47+
hashblocksize=config["DEFAULT"]["hashblocksize"]
48+
if "hashtype" in config["DEFAULT"]:
49+
hashertype=config["DEFAULT"]["hashtype"]
50+
if "foldersave" in config["DEFAULT"]:
51+
archivedir1=config["DEFAULT"]["foldersave"]
52+
##
53+
if piargs.folderin is None:
54+
print("Give input folder")
55+
exit(1)
56+
if piargs.foldersave is not None:
57+
archivedir1=piargs.foldersave
58+
if piargs.folderarchive is not None:
59+
archivedir2=piargs.folderarchive
60+
if piargs.filelist1 is not None:
61+
csv1=piargs.filelist1
62+
if piargs.filelist2 is not None:
63+
csv2=piargs.filelist2
64+
if piargs.datetimeformat is not None:
65+
datetimeformat=piargs.datetimeformat
66+
if piargs.hashblocksize is not None:
67+
hashblocksize=piargs.hashblocksize
68+
if piargs.hashtype is not None:
69+
hashertype=piargs.hashtype
70+
#define functions
71+
def file_len(fname):
72+
if False == os.path.isfile(fname):
73+
return(0)
74+
with open(fname) as f:
75+
count=0
76+
for i, l in enumerate(f):
77+
count=i+1
78+
return(count)
79+
def forcedir(file_path):
80+
directory = os.path.dirname(file_path)
81+
if not os.path.exists(directory):
82+
os.makedirs(directory)
83+
def hasher(filein, hname, blocksize):
84+
if hname="blake2s":
85+
htype=hashlib.blake2s
86+
elif hname="sha384":
87+
htype=hashlib.sha384
88+
elif hname="sha512":
89+
htype=hashlib.sha512
90+
elif hname="sha3_256":
91+
htype=hashlib.sha3_256
92+
elif hname="sha256":
93+
htype=hashlib.sha256
94+
elif hname="md5":
95+
htype=hashlib.md5
96+
elif hname="sha3_512":
97+
htype=hashlib.sha3_512
98+
elif hname="sha3_224":
99+
htype=hashlib.sha3_224
100+
elif hname="shake_128":
101+
htype=hashlib.shake_128
102+
elif hname="shake_256":
103+
htype=hashlib.shake_256
104+
elif hname="sha1":
105+
htype=hashlib.sha1
106+
elif hname="blake2b":
107+
htype=hashlib.blake2b
108+
elif hname="sha224":
109+
htype=hashlib.sha224
110+
elif hname="sha3_384":
111+
htype=hashlib.sha3_384
112+
else:
113+
print("wrong hash type")
114+
exit(1)
115+
with open(filein, 'rb') as fi:
116+
while True:
117+
data = fi.read(blocksize)
118+
if not data:
119+
break
120+
htype.update(data)
121+
return(htype.hexdigest())
122+
def csver(filein, operation, data):
123+
if operation=="find":
124+
if False == os.path.isfile(filein):
125+
return("False")
126+
with open(filein, 'rt') as fi:
127+
reader = csv.reader(fi, delimiter=',')
128+
for row in reader:
129+
hashout=row[1] #location of hash
130+
if hashout == data:
131+
return(row[0])
132+
return("False")
133+
elif operation=="append":
134+
if False == os.path.isfile(filein):
135+
pass
136+
with open(filein,'a') as fd:
137+
writer = csv.writer(fd)
138+
writer.writerow(data)
139+
def wfile(filedef, filerel):
140+
hashin=hasher(filedef, hashertype, hashblocksize)
141+
csvid=csver(csv1, "find",hashin)
142+
if csvid=="False":
143+
csvid=file_len(csv1)+1
144+
csver(csv1, "append", [csvid, hashin])
145+
os.replace(filedef, archivedir1 + "/" + str(csvid))
146+
else:
147+
os.remove(filedef)
148+
csver(csv2, "append", [datetimes, csvid, filerel])
149+
os.symlink(archivedir1 + "/" + str(csvid), filedef)
150+
def wfolder(directory):
151+
for ff in os.listdir(directory):
152+
ffdef=directory + "/" + ff
153+
if os.path.isfile(ffdef):
154+
wfile(ffdef, ff)
155+
else:
156+
wfolder(ffdef)
157+
#to do:main program
158+
forcedir(archivedir1);forcedir(archivedir2)
159+
infolder=os.path.abspath(piargs.folderin)
160+
if not os.path.exists(infolder):
161+
print("Folder not found!")
162+
exit(1)
163+
wfolder(infolder)
164+
shutil.move(infolder, archivedir2 + "/")
165+
exit(0)

0 commit comments

Comments
 (0)