1
# cruft.py - base class for different kinds of cruft
2
# Copyright (C) 2008 Canonical, Ltd.
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, version 3 of the License.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
import computerjanitor
22
"""One piece of cruft to be cleaned out.
24
A piece of cruft can be a file, a package, a configuration tweak
25
that is missing, or something else.
27
This is a base class, which does nothing. Subclasses do the
33
"""Return the unique prefix used to group this type of cruft.
35
For example, the .deb package called 'foo' would have a prefix
36
of 'deb'. This way, the package foo is not confused with the
37
file foo, or the username foo.
39
Subclasses SHOULD define this. The default implementation
40
returns the name of the class, which is rarely useful to
45
return self.__class__.__name__
47
def get_prefix_description(self):
48
"""Return human-readable description of class of cruft."""
49
return self.get_description()
51
def get_shortname(self):
52
"""Return the name of this piece of cruft.
54
The name should be something that the user will understand.
55
For example, it might be the name of a package, or the full
58
The name should be unique within the unique prefix returned
59
by get_prefix. The prefix MUST NOT be included by this method,
60
the get_name() method does that instead. The intent is that
61
get_shortname() will be used by the user interface in contexts
62
where the prefix is shown separately from the short name,
63
and get_name() when a single string is used.
65
Subclasses MUST define this. The default implementation
70
raise computerjanitor.UnimplementedMethod(self.get_shortname)
73
"""Return prefix plus name.
75
See get_prefix() and get_shortname() for a discussion of
76
the prefix and the short name. This method will return
77
the prefix, a colon, and the short name.
79
The long name will used to store state/configuration data:
80
_this_ package should not be removed.
84
return "%s:%s" % (self.get_prefix(), self.get_shortname())
86
def get_description(self):
87
"""Return a description of this piece of cruft.
89
This may be arbitrarily long. The user interface will take
90
care of breaking it into lines or otherwise presenting it to
91
the user in a nice manner. The description should be plain
94
The default implementation returns the empty string. Subclasses
95
MAY override this as they wish.
101
def get_disk_usage(self):
102
"""Return amount of disk space reserved by this piece of cruft.
106
The disk space in question should be the amount that will be
107
freed if the cruft is cleaned up. The amount may be an estimate
108
(read: guess). It is intended to be shown to the user to help
109
them decide what to remove and what to keep.
111
This will also be used by the user interface to better
112
estimate how much remaining time there is when cleaning
115
For some types of cruft, this is not applicable and they should
116
return None. The base class implementation does that, so
117
subclasses MUST define this method if it is useful for them to
118
return something else.
120
The user interface will distinguish between None (not
121
applicable) and 0 (no disk space being used).
128
"""Clean up this piece of cruft.
130
Depending on the type of cruft, this may mean removing files,
131
packages, modifying configuration files, or something else.
133
The default implementation raises an exception. Subclasses
138
raise computerjanitor.UnimplementedMethod(self.cleanup)