-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystem.py
More file actions
84 lines (68 loc) · 2.89 KB
/
Copy pathFilesystem.py
File metadata and controls
84 lines (68 loc) · 2.89 KB
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
import os
from Directory import Directory
from File import File
class Filesystem:
'''
File Manager
Class in charge to build the structure of the filesystem
localpath --> local path of the root directory
ignore --> ignore list of files/directories
'''
def __init__(self,
localpath,
ignore=('build',
'.DS_Store',
'.localized',
'builds',
'products',
'cache')):
name = os.path.basename(localpath)
timestamp = os.stat(localpath).st_mtime
root = Directory(name, timestamp, localpath)
# root directory
self.root = root
# files or directories to ignore
self.ignore = ignore
# build the filesystem
self.build_tree(localpath, self.root)
def build_tree(self, localpath, directory):
'''
Build the filesystem given a path to a local file/directory
localpath --> path to a local file/directory
directory --> Directory object
'''
print("Building tree.. (directory: " + directory.name + ")")
# loop over all the items in the local directory
for entry in os.scandir(localpath):
# if the item is a file and it is not in the ignore list
if (entry.is_file() and (entry.name not in self.ignore)):
# then store it in the directory's files list
directory.files.append(File(entry.name,
os.stat(entry.path).st_mtime,
entry.path))
# if the item is a directory and it is not in the ignore list
elif (entry.is_dir() and (entry.name not in self.ignore)):
# then store it in the directory's children list
directory.children.append(Directory(entry.name,
os.stat(entry.path).st_mtime,
entry.path))
# build the filesystem for the subdirectory
self.build_tree(entry.path, directory.children[-1])
def print_tree(self, directory):
'''
Print the filesystem
directory --> Directory object
'''
print("------- " + directory.name + " /", directory.timestamp, "/", directory.path, " -------")
# loop over all the files
for entry in directory.files:
print(entry.name, "\t", entry.timestamp)
# loop over all the subdirectories
for entry in directory.children:
print(entry.name)
for entry in directory.children:
# print subdirectory
self.print_tree(entry)
if __name__ == "__main__":
HOME = os.environ['HOME']
f = Filesystem(HOME + '/Desktop/test')