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