/
pyfileindex.py
322 lines (289 loc) · 10.7 KB
/
pyfileindex.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import os
import numpy as np
import pandas
try:
from os import scandir
except ImportError:
from scandir import scandir
class PyFileIndex(object):
"""
The PyFileIndex maintains a pandas Dataframe to track changes in the file system.
Args:
path (str): file system path
filter_function (function): function to filter for specific files (optional)
debug (bool): enable debug print statements (optional)
df (pandas.DataFrame): DataFrame of a previous PyFileIndex object (optional)
"""
def __init__(self, path=".", filter_function=None, debug=False, df=None):
abs_path = os.path.abspath(os.path.expanduser(path))
self._check_if_path_exists(path=abs_path)
self._debug = debug
self._filter_function = filter_function
self._path = abs_path
if df is None:
self._df = self._create_df_from_lst(
list([self._get_lst_entry_from_path(entry=self._path)])
+ list(self._scandir(path=self._path, df=None, recursive=True))
)
else:
self._df = df
@property
def df(self) -> pandas.DataFrame:
return self._df
@property
def dataframe(self) -> pandas.DataFrame:
return self.df
def open(self, path: str):
"""
Open PyFileIndex in the subdirectory path
Args:
path (str): subdirectory to open
Returns:
PyFileIndex: PyFileIndex for subdirectory
"""
abs_path = os.path.abspath(os.path.expanduser(os.path.join(self._path, path)))
self._check_if_path_exists(path=abs_path)
self.update()
if abs_path == self._path:
return self
elif (
os.path.commonpath([abs_path, self._path]) == self._path and os.name != "nt"
):
return PyFileIndex(
path=abs_path,
filter_function=self._filter_function,
debug=self._debug,
df=self._df[self._df["path"].str.contains(abs_path)],
)
elif (
os.path.commonpath([abs_path, self._path]) == self._path and os.name != "nt"
):
abs_path_unix = abs_path.replace("\\", "/")
return PyFileIndex(
path=abs_path,
filter_function=self._filter_function,
debug=self._debug,
df=self._df[
np.array(
[
p.replace("\\", "/").contains(abs_path_unix)
for p in self._df["path"].values
]
)
],
)
else:
return PyFileIndex(
path=abs_path,
filter_function=self._filter_function,
debug=self._debug,
)
def update(self):
"""
Update file index
"""
self._check_if_path_exists(path=self._path)
df_new, files_changed_lst, path_deleted_lst = self._get_changes_quick()
if self._debug:
print("Changes: ", df_new.path.values, files_changed_lst, path_deleted_lst)
if len(path_deleted_lst) != 0:
self._df = self._df[~self._df.path.isin(path_deleted_lst)]
if len(files_changed_lst) != 0:
df_updated = self._create_df_from_lst(
[self._get_lst_entry_from_path(entry=f) for f in files_changed_lst]
)
self._df = self._df[~self._df.path.isin(df_updated.path)]
self._df = (
pandas.concat([self._df, df_updated])
.drop_duplicates()
.reset_index(drop=True)
)
if len(df_new) != 0:
self._df = (
pandas.concat([self._df, df_new])
.drop_duplicates()
.reset_index(drop=True)
)
def _init_df_lst(
self, path_lst: list, df: pandas.DataFrame = None, include_root: bool = True
) -> pandas.DataFrame:
"""
Internal function to build the pandas file index from a list of directories
Args:
path_lst (list): list of directories to scan
df (pandas.DataFrame/ None): existing file index table
include_root (bool): include the root directory in file index
Returns:
pandas.DataFrame: pandas file index
"""
total_lst = []
for p in path_lst:
if include_root:
total_lst.append(self._get_lst_entry_from_path(entry=p))
for entry in list(self._scandir(path=p, df=df, recursive=True)):
total_lst.append(entry)
return self._create_df_from_lst(total_lst)
def _scandir(
self, path: str, df: pandas.DataFrame = None, recursive: bool = True
) -> list:
"""
Internal function to recursivley scan directories
Args:
path (str): file system path
df (pandas.DataFrame/ None): existing file index table
recursive (bool): recusivley iterate over sub directories
Returns:
list: list of file entries
"""
try:
if df is not None and len(df) > 0:
for entry in scandir(path):
if entry.path not in df.path.values:
if entry.is_dir(follow_symlinks=False) and recursive:
yield from self._scandir(
path=entry.path, df=df, recursive=recursive
)
yield self._get_lst_entry(entry=entry)
else:
yield self._get_lst_entry(entry=entry)
else:
for entry in scandir(path):
if entry.is_dir(follow_symlinks=False) and recursive:
yield from self._scandir(path=entry.path, recursive=recursive)
yield self._get_lst_entry(entry=entry)
else:
yield self._get_lst_entry(entry=entry)
except FileNotFoundError:
return []
def _get_changes_quick(self):
"""
Internal function to list the changes to the file system
Returns:
list: pandas.DataFrame with new entries, list of changed files and list of deleted paths
"""
path_exists_bool_lst = [os.path.exists(p) for p in self._df.path.values]
path_deleted_lst = self._df[~np.array(path_exists_bool_lst)].path.values
df_exists = self._df[path_exists_bool_lst]
stat_lst = [os.stat(p) for p in df_exists.path.values]
st_mtime = [s.st_mtime for s in stat_lst]
st_nlink = [s.st_nlink for s in stat_lst]
df_modified = df_exists[
~np.isclose(df_exists.mtime.values, st_mtime, rtol=1e-10, atol=1e-15)
| np.not_equal(df_exists.nlink.values, st_nlink)
]
if len(df_modified) > 0:
if sum(df_modified.is_directory.values) > 0:
dir_changed_lst = df_modified[df_modified.is_directory].path.values
else:
dir_changed_lst = []
files_changed_lst = df_modified.path.values
else:
files_changed_lst, dir_changed_lst = [], []
df_new = self._init_df_lst(
path_lst=dir_changed_lst, df=df_exists, include_root=False
)
return df_new, files_changed_lst, path_deleted_lst
def _get_lst_entry_from_path(self, entry: str) -> list:
"""
Internal function to generate file index entry from file system path
Args:
entry (str): file system path
Returns:
list: file index entry
"""
try:
stat = os.stat(entry)
isdir = os.path.isdir(entry)
if not isdir and self._filter_function is not None:
flag = self._filter_function(entry)
else:
flag = True
if flag:
return [
os.path.basename(entry),
entry,
os.path.dirname(entry),
isdir,
stat.st_mtime,
stat.st_nlink,
]
else:
return []
except FileNotFoundError:
return []
def _get_lst_entry(self, entry) -> list:
"""
Internal function to generate file index entry from scandir DirEntry
Args:
entry (DirEntry): scandir DirEntry
Returns:
list: file index entry
"""
try:
stat = entry.stat()
isdir = entry.is_dir()
if not isdir and self._filter_function is not None:
flag = self._filter_function(entry.path)
else:
flag = True
if flag:
return [
entry.name,
entry.path,
os.path.dirname(entry.path),
isdir,
stat.st_mtime,
stat.st_nlink,
]
else:
return []
except FileNotFoundError:
return []
def _repr_html_(self) -> str:
"""
Internal visualisation function for iPython notebooks
Returns:
HTML object: iPython notebook representation of the pandas.DataFrame
"""
return self._df._repr_html_()
@staticmethod
def _check_if_path_exists(path: str):
if not os.path.exists(path):
raise FileNotFoundError(
"The path " + path + " does not exist on your filesystem."
)
@staticmethod
def _create_df_from_lst(lst: list) -> pandas.DataFrame:
"""
Internal function to generate file index as pandas from a list of entries
Args:
lst (list): list of file index entries
Returns:
pandas.DataFrame: file index
"""
lst_clean = [l for l in lst if len(l) != 0]
if len(lst_clean) != 0:
name_lst, path_lst, dirname_lst, dir_lst, mtime_lst, nlink_lst = zip(
*lst_clean
)
else:
name_lst, path_lst, dirname_lst, dir_lst, mtime_lst, nlink_lst = (
[],
[],
[],
[],
[],
[],
)
return pandas.DataFrame(
{
"basename": name_lst,
"path": path_lst,
"dirname": dirname_lst,
"is_directory": dir_lst,
"mtime": mtime_lst,
"nlink": nlink_lst,
}
)
def __len__(self) -> int:
return len(self._df[~self._df.is_directory])