-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerge_mail.py
66 lines (58 loc) · 2.23 KB
/
merge_mail.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
#! /usr/bin/python3
# coding=utf-8
import os
import argparse
import hashlib
from shutil import copy2
def create_md5(dir):
'''Create a file to store all md5sum of a folder, need to give a
specific folder.
'''
with open('file.md5','w') as record:
for path, dirs, files in os.walk(dir):
for name in files:
full_path = os.path.join(path, name)
with open(full_path, 'rb') as f:
md5 = hashlib.md5(f.read()).hexdigest()
print(full_path+':'+md5, file=record)
def merge_dir(dir, merge_dir):
'''Merge two folder. For every file in import_dir, calc its md5sum.
If it is contained in the md5sum file, skip it. If not, copy it.
'''
try:
with open('file.md5','r') as md5file:
s = md5file.read()
for path, dirs, files in os.walk(merge_dir):
for name in files:
full_path = os.path.join(path, name)
with open(full_path, 'rb') as f:
md5 = hashlib.md5(f.read()).hexdigest()
if md5 not in s:
dst = full_path.replace(merge_dir, dir)
print(dst)
try:
copy2(full_path, dst)
except Error as e:
print("{}".format(e))
create_md5(dir)
except IOError as e:
print("({})".format(e))
def main():
parser = argparse.ArgumentParser(description='merge mail')
parser.add_argument('-c', '--create',
help='create a file to store all md5sum',
action="store_true")
parser.add_argument('-d', '--dir',
help='the target folder need to create md5sum')
parser.add_argument('-m', '--merge',
help='merge mail from another dir, check md5 of'
+ ' that file to avoid duplicate ')
args = parser.parse_args()
if args.dir:
print(args.dir)
if args.create:
create_md5(args.dir)
if args.merge:
merge_dir(args.dir, args.merge)
if __name__ == "__main__":
main()