-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
168 lines (139 loc) · 4.91 KB
/
utils.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Roney'
import hashlib
import os
import logging
import shutil
import json
cache_dir = ""
ignore_name = ".FSignore"
backup_dir = ".backup"
config_name = ".FSconfig"
auth_name = ".auth"
default_ignore = [".FSconfig", ".backup", ".FSignore", ".auth", "*.git", "*.idea", "*__pycache__"]
def is_match(s, p):
scur, pcur, sstar, pstar = 0, 0, None, None
while scur < len(s):
if pcur < len(p) and p[pcur] in [s[scur], '?']:
scur, pcur = scur + 1, pcur + 1
elif pcur < len(p) and p[pcur] == '*':
pstar, pcur = pcur, pcur + 1
sstar = scur
elif pstar is not None:
pcur = pstar + 1
sstar += 1
scur = sstar
else:
return False
while pcur < len(p) and p[pcur] == '*':
pcur += 1
return pcur >= len(p)
class FileIgnore:
def __init__(self, ignore_file):
self.re_list = []
if os.path.exists(ignore_file):
with open(ignore_file, 'r') as f:
for line in f.readlines():
line = line.replace("\n", "").replace(" ", "")
_p = os.path.join(os.path.dirname(ignore_file), line)
self.re_list.append(_p)
def filter(self, txt):
for _re in self.re_list:
if is_match(txt, _re):
return True
return False
def get_file_md5(file_name):
m = hashlib.md5()
try:
with open(file_name, 'rb') as fobj:
while True:
data = fobj.read(4096)
if not data:
break
m.update(data)
return m.hexdigest()
except BaseException as e:
logging.warning(e)
return -1
def get_dir_tree(path):
path = os.path.abspath(path)
ignore_file = os.path.join(path, ignore_name)
dir_dict = dict()
dir_dict[path] = dict()
dir_list = [(path, dir_dict[path])]
_re = FileIgnore(ignore_file)
while len(dir_list) > 0:
_p, _d = dir_list.pop()
for file in os.listdir(_p):
now_path = os.path.join(_p, file)
if not _re.filter(now_path):
if os.path.islink(now_path):
# _d[file] = "link:"+os.readlink(now_path)
pass
elif os.path.isdir(now_path):
_d[file] = dict()
dir_list.append((now_path, _d[file]))
else:
_d[file] = get_file_md5(now_path)
return dir_dict
def move_file(old_file,new_file):
if os.path.exists(old_file) and not os.path.exists(new_file):
new_path = os.path.dirname(new_file)
if not os.path.exists(new_path):
os.makedirs(new_path)
os.rename(old_file, new_file)
return os.path.exists(new_file) and not os.path.exists(old_file)
def delete_file(file_or_dir):
if os.path.exists(file_or_dir):
if os.path.isdir(file_or_dir):
shutil.rmtree(file_or_dir)
else:
os.remove(file_or_dir)
def remove_file(root_path, relative_file):
abs_file = os.path.join(root_path, relative_file)
try:
if os.path.exists(abs_file):
if backup_dir:
backup_file = os.path.join(root_path, os.path.join(backup_dir, relative_file))
delete_file(backup_file)
if os.path.isdir(abs_file):
shutil.move(abs_file, backup_file)
else:
move_file(abs_file, backup_file)
else:
delete_file(abs_file)
except BaseException as e:
logging.warning("remove file/dir error: {}".format(e))
return not os.path.exists(abs_file)
def test_dir(path):
path = os.path.abspath(path)
ignore_file = os.path.join(path,ignore_name)
if not os.access(path, os.W_OK):
return False, path
else:
dir_list = [path]
_re = FileIgnore(ignore_file)
while len(dir_list) > 0:
_p = dir_list.pop()
for file in os.listdir(_p):
now_path = os.path.join(_p, file)
if not _re.filter(now_path):
if not os.access(now_path, os.W_OK) or not os.access(now_path, os.R_OK):
return False, now_path
if os.path.isdir(now_path):
dir_list.append(now_path)
return True,""
def creat_ignore(path):
ignore_file = os.path.join(path, ignore_name)
if not os.path.exists(ignore_file):
with open(ignore_file, "w") as f:
for line in default_ignore:
f.write(line + '\n')
def print_dict(d):
print(json.dumps(d, indent=4, sort_keys=True))
if __name__ == "__main__":
print(remove_file(".", "backup/1"))
# print(json.dumps(get_dir_tree("./"), indent=4, sort_keys=True))
# # print(os.access(r"C:\Users\zhangqiSX3552\Desktop\1", os.R_OK))
# print(creat_ignore("."))