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
24 from pysize
.core
.pysize_global_fs_cache
import get_dev_ino
, cache_add_dir
25 from pysize
.core
.deletion
import get_subtracted
27 def _extract_prefix_suffixes(paths
):
28 """['/prefix/suffix1', '/prefix/suffix2', '/prefix/suffix3'] =>
29 ('/prefix', ['suffix1', 'suffix2', 'suffix3'])"""
30 prefix_len
= os
.path
.commonprefix(paths
).rfind('/') + 1
31 prefix
= paths
[0][:prefix_len
- 1]
32 suffixes
= [p
[prefix_len
:] for p
in paths
]
33 return prefix
, suffixes
35 def _join_prefix_suffixes(prefix
, suffixes
):
36 if len(suffixes
) == 1:
39 suffix
= '{' + ','.join(suffixes
) + '}'
40 return os
.path
.join(prefix
, suffix
)
43 def _sort_nodes(nodes
):
45 return cmp(n2
.size
, n1
.size
) or cmp(n1
.get_name(), n2
.get_name())
46 nodes
.sort(cmp=cmp_fn
)
48 class _pysize_node(object):
49 """The parent class of all displayed nodes, as these nodes are displayed on
50 screen, there should be few instances."""
51 def __init__(self
, parent
, basename
, options
):
52 self
.rectangle
= 0, 0, 0, 0
55 self
.basename
= basename
57 self
.size
= compute_size
.slow(basename
, options
.cross_device
)
61 def compute_height(self
):
63 children_height
= max([c
.compute_height() for c
in self
.children
])
64 return children_height
+ 1
67 def compute_depth(self
):
75 def minimum_node_size(self
):
77 for child
in self
.children
:
78 child_min_node_size
= child
.minimum_node_size()
79 res
= min(res
, child_min_node_size
)
82 def get_dirname(self
):
83 return os
.path
.dirname(self
.get_fullpaths()[0])
89 """Does the node correspond to a path that actually exists?"""
92 def get_fullname(self
):
93 fullpaths
= self
.get_fullpaths()
94 prefix
, suffixes
= _extract_prefix_suffixes(fullpaths
)
95 fullname
= _join_prefix_suffixes(prefix
, suffixes
)
98 def get_fullpaths(self
):
100 fullpath
= self
.basename
102 fullpath
= os
.path
.join(parent
.basename
, fullpath
)
103 parent
= parent
.parent
107 """Return the name that should be displayed for this node."""
111 """The iterator is a depth first traversal."""
113 for child
in self
.children
:
117 def contains_point(self
, p
):
118 """Is the point p in the graphical representation of this node?"""
119 x0
, x1
, y0
, y1
= self
.rectangle
120 return x0
< p
.x
and p
.x
< x1
and y0
< p
.y
and p
.y
< y1
122 class _pysize_node_collection(_pysize_node
):
124 def __init__(self
, parent
, prefix
, children
, max_depth
, min_size
, options
):
125 super(_pysize_node_collection
, self
).__init
__(parent
, None, options
)
126 self
.basename
= prefix
127 fullpaths
= [os
.path
.join(prefix
, p
) for p
in children
]
129 self
.size
= compute_size
.slow_sum(fullpaths
, options
.cross_device
)
134 cookie
= chdir_browsing
.init(prefix
)
136 for child
in children
:
137 node
= create_node(self
, child
, max_depth
- 1, min_size
,
140 self
.children
.append(node
)
141 children_size
+= node
.size
144 remaining_nodes
.append(node
)
145 remaining_size
+= node
.size
147 _sort_nodes(self
.children
)
148 if remaining_size
> min_size
:
149 _sort_nodes(remaining_nodes
)
150 names
= [n
.__name
for n
in remaining_nodes
]
151 rem
= _pysize_node_remaining(self
, names
, options
)
152 self
.children
.append(rem
)
154 chdir_browsing
.finalize(cookie
)
155 self
.size
= children_size
+ remaining_size
158 """This node does not actually exists, it is an aggregate."""
161 class _pysize_node_forest(_pysize_node_collection
):
162 def __init__(self
, parent
, children
, max_depth
, min_size
, options
):
163 prefix
, suffixes
= _extract_prefix_suffixes(children
)
164 super(_pysize_node_forest
, self
).__init
__(parent
, prefix
, suffixes
,
165 max_depth
, min_size
, options
)
166 self
.basename
= prefix
167 self
.forest_paths
= suffixes
168 self
.forest_name
= _join_prefix_suffixes(prefix
, suffixes
)
171 return self
.forest_name
173 def get_dirname(self
):
176 def get_fullpaths(self
):
177 fullpaths
= self
.forest_paths
180 fullpaths
= [os
.path
.join(parent
.basename
, fp
) for fp
in fullpaths
]
181 parent
= parent
.parent
184 class _pysize_node_dir(_pysize_node_collection
):
186 def __init__(self
, parent
, basename
, max_depth
, min_size
, options
):
187 listing
= chdir_browsing
.listdir(basename
, options
.cross_device
)
188 super(_pysize_node_dir
, self
).__init
__(parent
, basename
,
191 self
.basename
= basename
192 self
.size
+= os
.lstat(basename
).st_blocks
* 512
193 # update size in cache, in case files were added/removed
194 dev_ino
= get_dev_ino(basename
)
195 cache_add_dir(dev_ino
, self
.size
+ get_subtracted(basename
))
209 class _pysize_node_remaining(_pysize_node_collection
):
210 """The sum of a directory's children that are too small to be drawn."""
211 def __init__(self
, parent
, elements
, options
):
212 _pysize_node
.__init
__(self
, parent
, None, options
)
213 # The parent constructor would visit the files
214 self
.size
= compute_size
.slow_sum(elements
, options
.cross_device
)
215 self
.remaining_elements
= elements
218 return '{' + ','.join(self
.remaining_elements
) + '}'
220 def get_fullname(self
):
221 if self
.remaining_elements
:
222 return _pysize_node_collection
.get_fullname(self
)
223 return '' # This is the initial node
225 def get_fullpaths(self
):
226 fullpaths
= self
.remaining_elements
229 fullpaths
= [os
.path
.join(parent
.basename
, fp
) for fp
in fullpaths
]
230 parent
= parent
.parent
233 class _pysize_node_file(_pysize_node
):
235 def __init__(self
, parent
, basename
, options
):
236 super(_pysize_node_file
, self
).__init
__(parent
, basename
, options
)
238 class _pysize_node_hardlink(_pysize_node_file
):
239 """A hardlink, the canonical one, or a link"""
240 def __init__(self
, parent
, basename
, options
):
241 super(_pysize_node_hardlink
, self
).__init
__(parent
, basename
, options
)
243 def create_node(parent
, what
, max_depth
, min_size
, options
):
244 """Return a pysize_node for parent/basename traversing up to max_depth
245 levels and only taking into account elements bigger than min_size."""
247 return _pysize_node_remaining(parent
, [], options
)
249 if isinstance(what
, list):
250 size
= compute_size
.slow_sum(what
, options
.cross_device
)
254 size
= compute_size
.slow(what
, options
.cross_device
)
257 if isinstance(what
, str):
259 node
= _pysize_node_remaining(parent
, what
, options
)
260 elif isinstance(what
, list):
261 node
= _pysize_node_forest(parent
, what
, max_depth
, min_size
, options
)
264 if stat
.S_ISDIR(st
.st_mode
):
265 node
= _pysize_node_dir(parent
, what
, max_depth
, min_size
, options
)
266 elif st
.st_nlink
> 1:
267 node
= _pysize_node_hardlink(parent
, what
, options
)
269 node
= _pysize_node_file(parent
, what
, options
)