1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@yahoo.fr>
22 from pysize
.core
import chdir_browsing
23 from pysize
.core
import compute_size
25 def _extract_prefix_suffixes(paths
):
26 """['/prefix/suffix1', '/prefix/suffix2', '/prefix/suffix3'] =>
27 ('/prefix', ['suffix1', 'suffix2', 'suffix3'])"""
28 prefix_len
= os
.path
.commonprefix(paths
).rfind('/') + 1
29 prefix
= paths
[0][:prefix_len
- 1]
30 suffixes
= [p
[prefix_len
:] for p
in paths
]
31 return prefix
, suffixes
33 def _join_prefix_suffixes(prefix
, suffixes
):
34 if len(suffixes
) == 1:
35 return prefix
+ '/' + suffixes
[0]
36 return prefix
+ '/{' + ','.join(suffixes
) + '}'
38 def _sort_nodes(nodes
):
40 return cmp(n2
.size
, n1
.size
) or cmp(n1
.get_name(), n2
.get_name())
41 nodes
.sort(cmp=cmp_fn
)
43 class _pysize_node(object):
44 """The parent class of all displayed nodes, as these nodes are displayed on
45 screen, there should be few instances."""
46 def __init__(self
, parent
, basename
):
47 self
.rectangle
= 0, 0, 0, 0
50 self
.basename
= basename
52 self
.size
= compute_size
.slow(basename
)
56 def compute_height(self
):
58 children_height
= max([c
.compute_height() for c
in self
.children
])
59 return children_height
+ 1
62 def compute_depth(self
):
70 def minimum_node_size(self
):
72 for child
in self
.children
:
73 child_min_node_size
= child
.minimum_node_size()
74 res
= min(res
, child_min_node_size
)
77 def get_dirname(self
):
78 return os
.path
.dirname(self
.get_fullpaths()[0])
84 """Does the node correspond to a path that actually exists?"""
87 def get_fullname(self
):
88 fullpaths
= self
.get_fullpaths()
89 prefix
, suffixes
= _extract_prefix_suffixes(fullpaths
)
90 fullname
= _join_prefix_suffixes(prefix
, suffixes
)
93 def get_fullpaths(self
):
95 fullpath
= self
.basename
97 fullpath
= parent
.basename
+ '/' + fullpath
98 parent
= parent
.parent
102 """Return the name that should be displayed for this node."""
106 """The iterator is a depth first traversal."""
108 for child
in self
.children
:
112 def contains_point(self
, p
):
113 """Is the point p in the graphical representation of this node?"""
114 x0
, x1
, y0
, y1
= self
.rectangle
115 return x0
< p
.x
and p
.x
< x1
and y0
< p
.y
and p
.y
< y1
117 class _pysize_node_collection(_pysize_node
):
119 def __init__(self
, parent
, prefix
, children
, max_depth
, min_size
):
120 super(_pysize_node_collection
, self
).__init
__(parent
, None)
121 self
.basename
= prefix
122 self
.size
= sum([compute_size
.slow(prefix
+ '/' + p
) for p
in children
])
127 cookie
= chdir_browsing
.init(prefix
)
129 for child
in children
:
130 node
= create_node(self
, child
, max_depth
- 1, min_size
)
132 self
.children
.append(node
)
133 children_size
+= node
.size
136 remaining_nodes
.append(node
)
137 remaining_size
+= node
.size
139 _sort_nodes(self
.children
)
140 if remaining_size
> min_size
:
141 _sort_nodes(remaining_nodes
)
142 names
= [n
.__name
for n
in remaining_nodes
]
143 rem
= _pysize_node_remaining(self
, names
)
144 self
.children
.append(rem
)
146 chdir_browsing
.finalize(cookie
)
147 self
.size
= max(children_size
+ remaining_size
, self
.size
)
150 """This node does not actually exists, it is an aggregate."""
153 class _pysize_node_forest(_pysize_node_collection
):
154 def __init__(self
, parent
, children
, max_depth
, min_size
):
155 prefix
, suffixes
= _extract_prefix_suffixes(children
)
156 super(_pysize_node_forest
, self
).__init
__(parent
, prefix
, suffixes
,
158 self
.basename
= prefix
159 self
.forest_paths
= suffixes
160 self
.forest_name
= _join_prefix_suffixes(prefix
, suffixes
)
163 return self
.forest_name
165 def get_dirname(self
):
168 def get_fullpaths(self
):
169 fullpaths
= self
.forest_paths
172 fullpaths
= [parent
.basename
+ '/' + fp
for fp
in fullpaths
]
173 parent
= parent
.parent
176 class _pysize_node_dir(_pysize_node_collection
):
178 def __init__(self
, parent
, basename
, max_depth
, min_size
):
179 super(_pysize_node_dir
, self
).__init
__(parent
, basename
,
180 os
.listdir(basename
), max_depth
,
182 self
.basename
= basename
183 self
.size
= max(self
.size
, compute_size
.slow(basename
))
192 return self
.basename
+ '/'
194 class _pysize_node_remaining(_pysize_node_collection
):
195 """The sum of a directory's children that are too small to be drawn."""
196 def __init__(self
, parent
, elements
):
197 _pysize_node
.__init
__(self
, parent
, None)
198 # The parent constructor would visit the files
199 self
.size
= sum(map(compute_size
.slow
, elements
))
200 self
.remaining_elements
= elements
203 return '{' + ','.join(self
.remaining_elements
) + '}'
205 def get_fullname(self
):
206 if self
.remaining_elements
:
207 return _pysize_node_collection
.get_fullname(self
)
208 return '' # This is the initial node
210 def get_fullpaths(self
):
211 fullpaths
= self
.remaining_elements
214 fullpaths
= [parent
.basename
+ '/' + fp
for fp
in fullpaths
]
215 parent
= parent
.parent
218 class _pysize_node_file(_pysize_node
):
220 def __init__(self
, parent
, basename
):
221 super(_pysize_node_file
, self
).__init
__(parent
, basename
)
223 class _pysize_node_hardlink(_pysize_node_file
):
224 """A hardlink, the canonical one, or a link"""
225 def __init__(self
, parent
, basename
):
226 super(_pysize_node_hardlink
, self
).__init
__(parent
, basename
)
228 def create_node(parent
, what
, max_depth
, min_size
):
229 """Return a pysize_node for parent/basename traversing up to max_depth
230 levels and only taking into account elements bigger than min_size."""
232 return _pysize_node_remaining(parent
, [])
234 if isinstance(what
, list):
235 size
= sum(map(compute_size
.slow
, what
))
239 size
= compute_size
.slow(what
)
242 if isinstance(what
, str):
244 node
= _pysize_node_remaining(parent
, what
)
245 elif isinstance(what
, list):
246 node
= _pysize_node_forest(parent
, what
, max_depth
, min_size
)
249 if stat
.S_ISDIR(st
.st_mode
):
250 node
= _pysize_node_dir(parent
, what
, max_depth
, min_size
)
251 elif st
.st_nlink
> 1:
252 node
= _pysize_node_hardlink(parent
, what
)
254 node
= _pysize_node_file(parent
, what
)