-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskReport.py
More file actions
executable file
·170 lines (128 loc) · 4.64 KB
/
diskReport.py
File metadata and controls
executable file
·170 lines (128 loc) · 4.64 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
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
#!/usr/bin/python3
import os
from re import search
from sys import argv
from Dirr import Dirr
from utils import roundsSize
from styling import S, T, Bg, stylizesStr
TAB = ' ' * 4
fullDataFolders = list()
FILE, FOLDER = "file", "folder"
def printsTree(name, type, idtPrev, pathPos, amtPrevDirrPaths, size=None):
idt = idtPrev
dots = stylizesStr(":", T.red, s1=S.strong)
if pathPos == amtPrevDirrPaths:
idt = "{}{}{}".format(idt, TAB, " ")
else:
idt = "{}{}{}".format(idt, TAB, dots)
arrowIdt = "{}{}".format(idtPrev, TAB)
if type == FOLDER:
if name[0] != '/':
name = "/{}".format(name)
name = stylizesStr(name, T.purple, s1=S.strong, s2=S.italic)
arrow = "`--> "
arrow = stylizesStr(arrow, T.red, s1=S.strong)
print("{}{}{}".format(arrowIdt, arrow, name))
elif type == FILE:
name = stylizesStr(name, T.purple)
size = roundsSize(size)
size = stylizesStr(size, T.cyan, s1=S.strong)
arrow = "`-> "
arrow = stylizesStr(arrow, T.red, s1=S.strong)
text = stylizesStr(" has usage ", s1=S.blur, s2=S.italic)
print("{}{}{}{}{}".format(arrowIdt, arrow, name, text, size))
if amtPrevDirrPaths == pathPos: print(arrowIdt)
return idt
def analyzesIgnoredDirr(entry, name, nv, amtIgnr, ignrSize):
newAmtIgnr, newIgnrS = 0, 0
if os.path.isdir(entry):
newAmtIgnr, newIgnrS, _, _, _, _ = analyzesMem(entry.path, nv+1)
elif os.path.isfile(entry):
newIgnrS = os.path.getsize(entry)
if name[0] == '.':
newAmtIgnr += 1
return amtIgnr + newAmtIgnr, ignrSize + newIgnrS
def analyzesMem(path, nv, amtPrevDirrPaths=1, idtPrev=" ", pathPos=1):
ignore = r"/\."
folderName = os.path.basename(path)
isIgnr = search(ignore, path)
amtIgnr, amtFolders, amtFiles = 0, 0, 0
for entry in os.scandir(path):
name = os.path.basename(entry)
if search(ignore, entry.path):
continue
if os.path.isdir(entry):
amtFolders += 1
elif os.path.isfile(entry):
amtFiles += 1
amtPrevDirrPaths = amtFolders + amtFiles
amtSubFolders, amtSubFiles = 0, 0
ignrSize, notIgnrSize = 0, 0
for entry in os.scandir(path):
name = os.path.basename(entry)
if search(ignore, entry.path):
amtIgnr, ignrSize = analyzesIgnoredDirr(entry, name, nv, amtIgnr, ignrSize)
continue
if os.path.isdir(entry):
idt = printsTree(name, FOLDER, idtPrev, pathPos, amtPrevDirrPaths)
# analyzesSubFolder()
amtIgnrF, ignrS, amtSubFo, amtSubFi, notIgnrS, _ = analyzesMem(entry.path, nv+1, amtPrevDirrPaths, idt)
amtIgnr += amtIgnrF
amtSubFolders += amtSubFo
amtSubFiles += amtSubFi
ignrSize += ignrS
notIgnrSize += notIgnrS
elif os.path.isfile(entry):
size = os.path.getsize(entry)
notIgnrSize += size
printsTree(name, FILE, idtPrev, pathPos, amtPrevDirrPaths, size)
pathPos += 1
if not isIgnr:
folderData = Dirr(nv, folderName, amtIgnr, ignrSize, amtFolders, amtSubFolders, amtSubFiles, amtFiles, notIgnrSize)
fullDataFolders.append(folderData)
return amtIgnr, ignrSize, amtSubFolders + amtFolders, amtSubFiles + amtFiles, notIgnrSize, amtFolders
def getsInputPath(path):
workingPath = os.getcwd()
dirr = "{}/{}".format(workingPath, path)
if not os.path.exists(dirr):
print("Bad inpuT. Try again with path to a existing directory.")
exit()
elif not os.path.isdir(dirr):
print("Bad inpuT. Try again with path to a directory.")
exit()
os.chdir(path)
dirr = os.getcwd()
return dirr
def validatesInput():
if len(argv) > 2:
print("Bad inpuT. Try again with:")
print(" \"./diskanalyzesMem <path>\" or only \"./diskanalyzesMem\" for analize current directory.")
exit()
dirr = "/home"
if len(argv) == 2:
dirr = getsInputPath(argv[1])
return dirr
def printHeaderTree(directory, terminalWidth):
name = "/{}".format(os.path.basename(directory))
name = stylizesStr(name, T.purple, s1=S.strong, s2=S.italic)
edge = ' - ' * (terminalWidth // 6 - 1)
title = "{}|TREE|{}".format(edge[1:], edge[:-1])
title = stylizesStr(title, T.cyan)
arrow = "`--> "
arrow = stylizesStr(arrow, T.red, s1=S.strong)
print("\n{}".format(title))
print("{}{}".format(arrow, name))
def printsDataTree(terminalWidth):
edge = ' - ' * (terminalWidth // 6 - 1)
title = "{}|DATA TREE|{}".format(edge[4:], edge[:-1])
title = stylizesStr(title, T.cyan, s1=S.strong)
print("\n\n{}".format(title))
for folder in fullDataFolders[::-1]: print(folder, end="")
def main():
directory = validatesInput()
terminalWidth = int(os.popen('stty size', 'r').read().split()[1])
printHeaderTree(directory, terminalWidth)
analyzesMem(directory, 1)
printsDataTree(terminalWidth)
if __name__ == "__main__":
main()