This repository was archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstructs.py
216 lines (158 loc) · 5.31 KB
/
structs.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from collections import namedtuple
from enum import IntEnum
from operator import attrgetter
from stat import S_IFMT
from threading import RLock
from .utils.fs import blkdevice, blksize
# from ssd import is_ssd
class SkipException(Exception):
"""
Skip Exception
"""
pass
class FilterType(IntEnum):
ID = 1
PATH = 2
NAME = 3
DIR = 4
MODE = 5
INODE = 6
DEV = 7
MTIME = 8
SIZE = 9
SIGNATURE = 10
RULE = 11
HASH = 12
BINARY = 13
_counter = 0 # NOTE: No multiprocessing proof.
# NOTE: blkdev is not a unique drive identifier...
_CacheInfo = namedtuple('CacheInfo', 'blkdev blksize')
_DupInfo = namedtuple('DupInfo', 'filter dups errors parent')
_FileInfo = namedtuple('FileInfo',
'index id path name dir mode inode dev mtime size')
_ResultInfo = namedtuple('ResultInfo',
'dups deldups duperrors scanerrors delerrors')
class Cache(object):
__slots__ = ['__dev', '__info', 'lock', 'maxlen']
DEFAULT_MAXLEN = 128
def __init__(self, maxlen=DEFAULT_MAXLEN):
self.__dev = {}
self.__info = {}
self.maxlen = int(maxlen)
self.lock = RLock()
def get(self, fileinfo):
blockdevice = self.__dev.setdefault(
fileinfo.dev,
blkdevice(fileinfo.path))
value = self.__info.setdefault(
blockdevice,
_CacheInfo(blockdevice, blksize(fileinfo.path)))
return value
def clear(self):
if self.lock.locked():
return False
self.__dev.clear()
self.__info.clear()
return True
def acquire(self):
self.lock.acquire()
def release(self):
self.lock.release()
if len(self.__dev) > self.maxlen:
self.clear()
class DupInfo(_DupInfo):
__slots__ = []
def __new__(cls, filtertype, dups, errors, parentobj=None, parentkey=None):
parent = (parentobj, parentkey) if parentobj and parentkey else None
new = super(DupInfo, cls).__new__
inst = new(cls, filtertype, dups, errors, parent)
if parent:
parentobj.dups[parentkey] = inst
return inst
def __init__(self, *args, **kwargs):
super(DupInfo, self).__init__(*args, **kwargs)
self._filter()
def _filter(self, delkey=None):
dupdict = self.dups
if delkey is None:
for key, value in dupdict.items():
if len(value) > 1:
continue
dupdict.pop(key)
else:
dupdict.pop(delkey, None)
if not dupdict and not self.errors and self.parent:
parentobj, parentkey = self.parent
parentobj._filter(parentkey)
class FileInfo(_FileInfo):
__slots__ = []
@classmethod
def __new(cls, name, path, st):
dirname, filename = os.path.split(name)
mode = st.st_mode
ifmt = S_IFMT(mode)
inode = st.st_ino
dev = st.st_dev
try:
mtime = st.st_mtime_ns
except AttributeError:
mtime = st.st_mtime
size = st.st_size
fileid = (ifmt, size)
global _counter
_counter += 1
new = super(FileInfo, cls).__new__
return new(cls, _counter, fileid, path, filename, dirname, mode, inode,
dev, mtime, size)
def __new__(cls, name, path=None, st=None):
if path is None:
path = os.path.abspath(name)
if st is None:
st = os.lstat(name)
return cls.__new(name, path, st)
class ResultInfo(_ResultInfo):
__slots__ = []
@staticmethod
def __iter_dups(dupinfo):
for key, value in dupinfo.dups.items():
if isinstance(value, DupInfo):
dups_it = ResultInfo.__iter_dups(value)
for subobj, subkey, subvalue in dups_it:
yield subobj, subkey, subvalue
else:
yield dupinfo, key, value
@staticmethod
def __iter_errors(dupinfo):
yield dupinfo.errors
for value in dupinfo.dups.values():
if not isinstance(value, DupInfo):
continue
for errlist in ResultInfo.__iter_errors(value):
yield errlist
@staticmethod
def __parse_dups(dupinfo):
sort_fn = attrgetter('index', 'path')
dups_it = ResultInfo.__iter_dups(dupinfo)
dups = [tuple(sorted(duplist, key=sort_fn))
for _, _, duplist in dups_it if duplist]
dups.sort(key=len, reverse=True)
return tuple(dups)
@staticmethod
def __parse_errors(dupinfo):
sort_fn = attrgetter('index', 'path')
errors_it = ResultInfo.__iter_errors(dupinfo)
errors = [tuple(sorted(errlist, key=sort_fn))
for errlist in errors_it if errlist]
errors.sort(key=len, reverse=True)
return tuple(errors)
def __new__(cls, dupinfo, delduplist, scnerrlist, delerrors):
dups = cls.__parse_dups(dupinfo)
deldups = tuple(delduplist)
duperrors = cls.__parse_errors(dupinfo)
scanerrors = tuple(scnerrlist)
delerrors = tuple(delerrors)
new = super(ResultInfo, cls).__new__
return new(cls, dups, deldups, duperrors, scanerrors, delerrors)