Merge lp:~barry/computer-janitor/uicleanup into lp:computer-janitor
- uicleanup
- Merge into trunk
Proposed by
Barry Warsaw
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~barry/computer-janitor/uicleanup |
Merge into: | lp:computer-janitor |
Diff against target: |
5609 lines (+3346/-1851) 45 files modified
.bzrignore (+5/-0) computer-janitor (+12/-20) computer-janitor-gtk (+18/-9) computerjanitorapp/__init__.py (+20/-24) computerjanitorapp/app.py (+0/-192) computerjanitorapp/app_tests.py (+0/-178) computerjanitorapp/areyousure.py (+76/-0) computerjanitorapp/maincli.py (+290/-163) computerjanitorapp/maingtk.py (+32/-0) computerjanitorapp/state.py (+0/-56) computerjanitorapp/state_tests.py (+0/-77) computerjanitorapp/store.py (+147/-0) computerjanitorapp/terminalsize.py (+24/-29) computerjanitorapp/tests/test_all.py (+29/-0) computerjanitorapp/tests/test_terminalsize.py (+24/-9) computerjanitorapp/tests/test_utilities.py (+71/-0) computerjanitorapp/ui.py (+0/-39) computerjanitorapp/ui_cli_tests.py (+0/-246) computerjanitorapp/ui_gtk.py (+0/-714) computerjanitorapp/ui_tests.py (+0/-36) computerjanitorapp/uigtk.py (+515/-0) computerjanitorapp/utilities.py (+51/-0) computerjanitord/application.py (+111/-0) computerjanitord/authenticator.py (+85/-0) computerjanitord/collector.py (+149/-0) computerjanitord/data/com.ubuntu.ComputerJanitor.conf (+15/-0) computerjanitord/data/com.ubuntu.computerjanitor.policy (+19/-0) computerjanitord/errors.py (+57/-0) computerjanitord/main.py (+97/-0) computerjanitord/service.py (+260/-0) computerjanitord/state.py (+92/-0) computerjanitord/tests/data/etc/apt/sources.list (+1/-0) computerjanitord/tests/data/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_intrepid_restricted_binary-i386_Packages (+54/-0) computerjanitord/tests/data/var/lib/dpkg/status (+28/-0) computerjanitord/tests/test_all.py (+41/-0) computerjanitord/tests/test_application.py (+106/-0) computerjanitord/tests/test_authenticator.py (+97/-0) computerjanitord/tests/test_collector.py (+187/-0) computerjanitord/tests/test_state.py (+141/-0) computerjanitord/tests/test_whitelist.py (+119/-0) computerjanitord/whitelist.py (+86/-0) data/ComputerJanitor.ui (+10/-56) janitord (+40/-0) po/computerjanitor.pot (+233/-0) setup.py (+4/-3) |
To merge this branch: | bzr merge lp:~barry/computer-janitor/uicleanup |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ubuntu Release Team | Pending | ||
computer-janitor-hackers | Pending | ||
Review via email:
|
Commit message
Description of the change
This removes the "Recommended" column, since it is never filled. It also does a few minor other ui tweaks such as move the progress bar and shrink the default size of the window.
To post a comment you must log in.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Barry Warsaw (barry) wrote : | # |
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file '.bzrignore' | |||
2 | --- .bzrignore 1970-01-01 00:00:00 +0000 | |||
3 | +++ .bzrignore 2010-03-10 21:40:30 +0000 | |||
4 | @@ -0,0 +1,5 @@ | |||
5 | 1 | build | ||
6 | 2 | computer_janitor.egg-info | ||
7 | 3 | computerjanitord/tests/data/var/cache | ||
8 | 4 | computerjanitord/tests/data/var/lib/dpkg | ||
9 | 5 | computerjanitord/tests/data/var/lib/apt/lists/partial | ||
10 | 0 | 6 | ||
11 | === modified file 'computer-janitor' | |||
12 | --- computer-janitor 2009-08-18 13:48:50 +0000 | |||
13 | +++ computer-janitor 2010-03-10 21:40:30 +0000 | |||
14 | @@ -1,7 +1,6 @@ | |||
15 | 1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
16 | 2 | # | 2 | # |
19 | 3 | # computer-janitor - clean up a Unix-like operating system | 3 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. |
18 | 4 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
20 | 5 | # | 4 | # |
21 | 6 | # This program is free software: you can redistribute it and/or modify | 5 | # This program is free software: you can redistribute it and/or modify |
22 | 7 | # it under the terms of the GNU General Public License as published by | 6 | # it under the terms of the GNU General Public License as published by |
23 | @@ -15,21 +14,14 @@ | |||
24 | 15 | # You should have received a copy of the GNU General Public License | 14 | # You should have received a copy of the GNU General Public License |
25 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
26 | 17 | 16 | ||
45 | 18 | 17 | """Start the command-line computer janitor.""" | |
46 | 19 | import logging | 18 | |
47 | 20 | import os | 19 | from __future__ import absolute_import, unicode_literals |
48 | 21 | import sys | 20 | |
49 | 22 | import traceback | 21 | __metaclass__ = type |
50 | 23 | 22 | __all__ = [ | |
51 | 24 | import computerjanitor | 23 | ] |
52 | 25 | import computerjanitorapp | 24 | |
53 | 26 | 25 | ||
54 | 27 | 26 | from computerjanitorapp.maincli import main | |
55 | 28 | try: | 27 | main() |
38 | 29 | app = computerjanitorapp.Application() | ||
39 | 30 | app.run(computerjanitorapp.CommandLineUserInterface) | ||
40 | 31 | except computerjanitor.Exception, e: | ||
41 | 32 | logging.debug(unicode(traceback.format_exc())) | ||
42 | 33 | logging.error(e._str) | ||
43 | 34 | sys.exit(1) | ||
44 | 35 | |||
56 | 36 | 28 | ||
57 | === modified file 'computer-janitor-gtk' | |||
58 | --- computer-janitor-gtk 2009-02-11 17:21:24 +0000 | |||
59 | +++ computer-janitor-gtk 2010-03-10 21:40:30 +0000 | |||
60 | @@ -1,7 +1,7 @@ | |||
61 | 1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
62 | 2 | # | 2 | # |
63 | 3 | # computer-janitor-gtk - clean up a Unix-like operating system (GTK version) | 3 | # computer-janitor-gtk - clean up a Unix-like operating system (GTK version) |
65 | 4 | # Copyright (C) 2008, 2009 Canonical, Ltd. | 4 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. |
66 | 5 | # | 5 | # |
67 | 6 | # This program is free software: you can redistribute it and/or modify | 6 | # This program is free software: you can redistribute it and/or modify |
68 | 7 | # it under the terms of the GNU General Public License as published by | 7 | # it under the terms of the GNU General Public License as published by |
69 | @@ -15,20 +15,29 @@ | |||
70 | 15 | # You should have received a copy of the GNU General Public License | 15 | # You should have received a copy of the GNU General Public License |
71 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
72 | 17 | 17 | ||
75 | 18 | 18 | """Start the gtk computer janitor.""" | |
76 | 19 | import logging | 19 | |
77 | 20 | from __future__ import absolute_import, unicode_literals | ||
78 | 21 | |||
79 | 22 | __metaclass__ = type | ||
80 | 23 | __all__ = [ | ||
81 | 24 | ] | ||
82 | 25 | |||
83 | 26 | |||
84 | 20 | import os | 27 | import os |
85 | 21 | import sys | 28 | import sys |
86 | 29 | import logging | ||
87 | 22 | import traceback | 30 | import traceback |
88 | 23 | |||
89 | 24 | import computerjanitor | 31 | import computerjanitor |
91 | 25 | import computerjanitorapp | 32 | |
92 | 33 | from computerjanitorapp.maingtk import main | ||
93 | 26 | 34 | ||
94 | 27 | 35 | ||
95 | 28 | try: | 36 | try: |
99 | 29 | app = computerjanitorapp.Application() | 37 | main() |
100 | 30 | app.run(computerjanitorapp.GtkUserInterface) | 38 | except KeyboardInterrupt: |
101 | 31 | except computerjanitor.Exception, e: | 39 | pass |
102 | 40 | except computerjanitor.Exception as error: | ||
103 | 32 | logging.debug(unicode(traceback.format_exc())) | 41 | logging.debug(unicode(traceback.format_exc())) |
105 | 33 | logging.error(unicode(e)) | 42 | logging.error(unicode(error)) |
106 | 34 | sys.exit(1) | 43 | sys.exit(1) |
107 | 35 | 44 | ||
108 | === modified file 'computerjanitorapp/__init__.py' | |||
109 | --- computerjanitorapp/__init__.py 2009-09-09 14:36:30 +0000 | |||
110 | +++ computerjanitorapp/__init__.py 2010-03-10 21:40:30 +0000 | |||
111 | @@ -1,5 +1,4 @@ | |||
114 | 1 | # __init__.py for computerjanitorapp | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. |
113 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
115 | 3 | # | 2 | # |
116 | 4 | # This program is free software: you can redistribute it and/or modify | 3 | # This program is free software: you can redistribute it and/or modify |
117 | 5 | # it under the terms of the GNU General Public License as published by | 4 | # it under the terms of the GNU General Public License as published by |
118 | @@ -13,24 +12,29 @@ | |||
119 | 13 | # You should have received a copy of the GNU General Public License | 12 | # You should have received a copy of the GNU General Public License |
120 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
121 | 15 | 14 | ||
130 | 16 | 15 | """computerjanitorapp module.""" | |
131 | 17 | VERSION = "1.13.3" | 16 | |
132 | 18 | 17 | from __future__ import absolute_import, unicode_literals | |
133 | 19 | 18 | ||
134 | 20 | # Set up gettext. This needs to be before the import statements below | 19 | __metaclass__ = type |
135 | 21 | # so that if any modules call it right after importing, they find | 20 | __all__ = [ |
136 | 22 | # setup_gettext. | 21 | '__version__', |
137 | 23 | 22 | 'setup_gettext', | |
138 | 23 | ] | ||
139 | 24 | |||
140 | 25 | |||
141 | 26 | __version__ = '2.0' | ||
142 | 27 | |||
143 | 28 | |||
144 | 29 | # Set up gettext. | ||
145 | 24 | def setup_gettext(): | 30 | def setup_gettext(): |
146 | 25 | """Set up gettext for a module. | 31 | """Set up gettext for a module. |
147 | 26 | 32 | ||
153 | 27 | Return a method to be used for looking up translations. Usage: | 33 | Return a method to be used for looking up translations. Usage: |
154 | 28 | 34 | ||
155 | 29 | import computerjanitorapp | 35 | >>> import computerjanitorapp |
156 | 30 | _ = computerjanitorapp.setup_gettext() | 36 | >>> _ = computerjanitorapp.setup_gettext() |
152 | 31 | |||
157 | 32 | """ | 37 | """ |
158 | 33 | |||
159 | 34 | import gettext | 38 | import gettext |
160 | 35 | import os | 39 | import os |
161 | 36 | 40 | ||
162 | @@ -38,11 +42,3 @@ | |||
163 | 38 | localedir = os.environ.get('LOCPATH', None) | 42 | localedir = os.environ.get('LOCPATH', None) |
164 | 39 | t = gettext.translation(domain, localedir=localedir, fallback=True) | 43 | t = gettext.translation(domain, localedir=localedir, fallback=True) |
165 | 40 | return t.ugettext | 44 | return t.ugettext |
166 | 41 | |||
167 | 42 | |||
168 | 43 | from app import Application | ||
169 | 44 | from ui import UserInterface | ||
170 | 45 | from ui_cli import CommandLineUserInterface | ||
171 | 46 | from ui_gtk import GtkUserInterface | ||
172 | 47 | from state import State | ||
173 | 48 | from terminalsize import get_terminal_size | ||
174 | 49 | 45 | ||
175 | === removed file 'computerjanitorapp/app.py' | |||
176 | --- computerjanitorapp/app.py 2009-11-10 20:01:34 +0000 | |||
177 | +++ computerjanitorapp/app.py 1970-01-01 00:00:00 +0000 | |||
178 | @@ -1,192 +0,0 @@ | |||
179 | 1 | # app.py - the application main program of Computer Janitor | ||
180 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
181 | 3 | # | ||
182 | 4 | # This program is free software: you can redistribute it and/or modify | ||
183 | 5 | # it under the terms of the GNU General Public License as published by | ||
184 | 6 | # the Free Software Foundation, version 3 of the License. | ||
185 | 7 | # | ||
186 | 8 | # This program is distributed in the hope that it will be useful, | ||
187 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
188 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
189 | 11 | # GNU General Public License for more details. | ||
190 | 12 | # | ||
191 | 13 | # You should have received a copy of the GNU General Public License | ||
192 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
193 | 15 | |||
194 | 16 | |||
195 | 17 | import ConfigParser | ||
196 | 18 | import logging | ||
197 | 19 | import logging.handlers | ||
198 | 20 | import optparse | ||
199 | 21 | import os | ||
200 | 22 | import sys | ||
201 | 23 | |||
202 | 24 | import computerjanitor | ||
203 | 25 | import computerjanitorapp | ||
204 | 26 | _ = computerjanitorapp.setup_gettext() | ||
205 | 27 | |||
206 | 28 | |||
207 | 29 | DEFAULT_PLUGINS_DIRS = "/usr/share/computerjanitor/plugins" | ||
208 | 30 | DEFAULT_STATE_FILE = "/var/lib/computer-janitor/state.dat" | ||
209 | 31 | |||
210 | 32 | |||
211 | 33 | class SourcesListProblem(computerjanitor.Exception): | ||
212 | 34 | |||
213 | 35 | def __init__(self, missing_package): | ||
214 | 36 | self._str = _("Essential package %s is missing. There may be " | ||
215 | 37 | "problems with apt sources.list or Packages files may " | ||
216 | 38 | "be missing?") % missing_package | ||
217 | 39 | |||
218 | 40 | |||
219 | 41 | class AsciiFormatter(logging.Formatter): | ||
220 | 42 | |||
221 | 43 | def format(self, record): # pragma: no cover | ||
222 | 44 | msg = logging.Formatter.format(self, record) | ||
223 | 45 | return msg.encode('ascii', 'replace') | ||
224 | 46 | |||
225 | 47 | |||
226 | 48 | def setup_logging(): # pragma: no cover | ||
227 | 49 | if os.environ.get("COMPUTER_JANITOR_DEBUG", None) == "yes": | ||
228 | 50 | level = logging.DEBUG | ||
229 | 51 | elif os.environ.get("COMPUTER_JANITOR_UNITTEST", None) == "yes": | ||
230 | 52 | level = logging.CRITICAL | ||
231 | 53 | else: | ||
232 | 54 | level = logging.INFO | ||
233 | 55 | logging.basicConfig(level=level, format="%(levelname)s: %(message)s") | ||
234 | 56 | |||
235 | 57 | try: | ||
236 | 58 | formatter = AsciiFormatter( | ||
237 | 59 | "computer-janitor %(levelname)s: %(message)s") | ||
238 | 60 | handler = logging.handlers.SysLogHandler("/dev/log") | ||
239 | 61 | handler.setLevel(level) | ||
240 | 62 | handler.setFormatter(formatter) | ||
241 | 63 | logging.getLogger().addHandler(handler) | ||
242 | 64 | except: | ||
243 | 65 | # We're OK with something going wrong. | ||
244 | 66 | logging.debug(_("Logging to syslog cannot be set up.")) | ||
245 | 67 | |||
246 | 68 | |||
247 | 69 | class Application(object): | ||
248 | 70 | |||
249 | 71 | """The main class for the program.""" | ||
250 | 72 | |||
251 | 73 | whitelist_dirs = ["/etc/computer-janitor.d"] | ||
252 | 74 | |||
253 | 75 | def __init__(self, apt=None): | ||
254 | 76 | self.state = computerjanitorapp.State() | ||
255 | 77 | self.parser = self.create_option_parser() | ||
256 | 78 | self.apt = computerjanitor.apt | ||
257 | 79 | if apt is not None: | ||
258 | 80 | self.apt = apt | ||
259 | 81 | self.refresh_apt_cache() | ||
260 | 82 | |||
261 | 83 | # The Plugin API requires that we have a config that is a | ||
262 | 84 | # ConfigParser. We don't use it for anything, for now. | ||
263 | 85 | self.config = ConfigParser.ConfigParser() | ||
264 | 86 | |||
265 | 87 | def run(self, ui_class, | ||
266 | 88 | plugin_manager_class=computerjanitor.PluginManager): | ||
267 | 89 | setup_logging() | ||
268 | 90 | logging.debug(_("Running application, with:")) | ||
269 | 91 | logging.debug(" plugin_manager_class=%s" % plugin_manager_class) | ||
270 | 92 | |||
271 | 93 | options, args = self.parse_options() | ||
272 | 94 | |||
273 | 95 | pluginpath = os.environ.get("COMPUTER_JANITOR_PLUGINS", | ||
274 | 96 | DEFAULT_PLUGINS_DIRS) | ||
275 | 97 | plugindirs = pluginpath.split(":") | ||
276 | 98 | pm = plugin_manager_class(self, plugindirs) | ||
277 | 99 | |||
278 | 100 | ui = ui_class(self, pm) | ||
279 | 101 | ui.run(options, args) | ||
280 | 102 | |||
281 | 103 | def create_option_parser(self): # pragma: no cover | ||
282 | 104 | parser = optparse.OptionParser(version="%%prog %s" % | ||
283 | 105 | computerjanitorapp.VERSION) | ||
284 | 106 | |||
285 | 107 | parser.usage = _(""" | ||
286 | 108 | %prog [options] find | ||
287 | 109 | %prog [options] cleanup [CRUFT]... | ||
288 | 110 | %prog [options] ignore [CRUFT]... | ||
289 | 111 | %prog [options] unignore [CRUFT]... | ||
290 | 112 | |||
291 | 113 | %prog finds and removes cruft from your system. | ||
292 | 114 | |||
293 | 115 | Cruft is anything that shouldn't be on the system, but is. Stretching | ||
294 | 116 | the definition, it is also things that should be on the system, but | ||
295 | 117 | aren't.""") | ||
296 | 118 | |||
297 | 119 | parser.add_option("--all", action="store_true", | ||
298 | 120 | help=_("Make the 'cleanup' command remove all " | ||
299 | 121 | "packages, if none are given on the " | ||
300 | 122 | "command line.")) | ||
301 | 123 | |||
302 | 124 | parser.add_option("--state-file", metavar="FILE", | ||
303 | 125 | default=DEFAULT_STATE_FILE, | ||
304 | 126 | help=_("Store state of each piece of cruft in " | ||
305 | 127 | "FILE. (Default is %default).")) | ||
306 | 128 | |||
307 | 129 | parser.add_option("--no-act", action="store_true", | ||
308 | 130 | help=_("Don't actually remove anything, just " | ||
309 | 131 | "pretend to do so. This is useful for " | ||
310 | 132 | "testing stuff.")) | ||
311 | 133 | |||
312 | 134 | parser.add_option("--verbose", action="store_true", | ||
313 | 135 | help=_("Verbose operation: make find show an " | ||
314 | 136 | "explanation for each piece of cruft " | ||
315 | 137 | "found.")) | ||
316 | 138 | |||
317 | 139 | return parser | ||
318 | 140 | |||
319 | 141 | def parse_options(self, args=None): | ||
320 | 142 | return self.parser.parse_args(args=args) | ||
321 | 143 | |||
322 | 144 | def refresh_apt_cache(self): | ||
323 | 145 | self.apt_cache = self.apt.Cache() | ||
324 | 146 | self.apt_cache._depcache.ReadPinFile("/var/lib/synaptic/preferences") | ||
325 | 147 | |||
326 | 148 | def verify_apt_cache(self): | ||
327 | 149 | for name in ["dash", "gzip"]: | ||
328 | 150 | if name not in self.apt_cache: | ||
329 | 151 | raise SourcesListProblem(name) | ||
330 | 152 | if not any(v.downloadable for v in self.apt_cache[name].versions): | ||
331 | 153 | raise SourcesListProblem(name) | ||
332 | 154 | |||
333 | 155 | def whitelist_files(self, dirnames): | ||
334 | 156 | """Find files with whitelists in a list of directories. | ||
335 | 157 | |||
336 | 158 | The directory is scanned for files ending in ".whitelist". | ||
337 | 159 | Subdirectories are not scanned. | ||
338 | 160 | |||
339 | 161 | """ | ||
340 | 162 | |||
341 | 163 | list = [] | ||
342 | 164 | for dirname in dirnames: | ||
343 | 165 | try: | ||
344 | 166 | basenames = os.listdir(dirname) | ||
345 | 167 | whitelists = [x for x in basenames if x.endswith(".whitelist")] | ||
346 | 168 | list += [os.path.join(dirname, x) for x in whitelists] | ||
347 | 169 | except os.error: | ||
348 | 170 | pass | ||
349 | 171 | |||
350 | 172 | return list | ||
351 | 173 | |||
352 | 174 | def whitelisted_cruft(self, dirnames=None): | ||
353 | 175 | """Return list of cruft that is whitelisted.""" | ||
354 | 176 | |||
355 | 177 | dirnames = dirnames or self.whitelist_dirs | ||
356 | 178 | |||
357 | 179 | list = [] | ||
358 | 180 | for filename in self.whitelist_files(dirnames): | ||
359 | 181 | f = file(filename, "r") | ||
360 | 182 | for line in f.readlines(): | ||
361 | 183 | line = line.strip() | ||
362 | 184 | if line and not line.startswith("#"): | ||
363 | 185 | list.append(line) | ||
364 | 186 | f.close() | ||
365 | 187 | |||
366 | 188 | return list | ||
367 | 189 | |||
368 | 190 | def remove_whitelisted(self, crufts, dirnames=None): | ||
369 | 191 | whitelisted = self.whitelisted_cruft(dirnames=dirnames) | ||
370 | 192 | return [c for c in crufts if c.get_name() not in whitelisted] | ||
371 | 193 | 0 | ||
372 | === removed file 'computerjanitorapp/app_tests.py' | |||
373 | --- computerjanitorapp/app_tests.py 2009-08-14 12:37:25 +0000 | |||
374 | +++ computerjanitorapp/app_tests.py 1970-01-01 00:00:00 +0000 | |||
375 | @@ -1,178 +0,0 @@ | |||
376 | 1 | # app_tests.py - unit tests for app.py | ||
377 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
378 | 3 | # | ||
379 | 4 | # This program is free software: you can redistribute it and/or modify | ||
380 | 5 | # it under the terms of the GNU General Public License as published by | ||
381 | 6 | # the Free Software Foundation, version 3 of the License. | ||
382 | 7 | # | ||
383 | 8 | # This program is distributed in the hope that it will be useful, | ||
384 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
385 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
386 | 11 | # GNU General Public License for more details. | ||
387 | 12 | # | ||
388 | 13 | # You should have received a copy of the GNU General Public License | ||
389 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
390 | 15 | |||
391 | 16 | |||
392 | 17 | import os | ||
393 | 18 | import shutil | ||
394 | 19 | import tempfile | ||
395 | 20 | import unittest | ||
396 | 21 | |||
397 | 22 | import computerjanitor | ||
398 | 23 | import computerjanitorapp | ||
399 | 24 | |||
400 | 25 | |||
401 | 26 | class MockUI(object): | ||
402 | 27 | |||
403 | 28 | def __init__(self, testcase, app, pm): | ||
404 | 29 | self.testcase = testcase | ||
405 | 30 | |||
406 | 31 | def run(self, options, args): | ||
407 | 32 | self.testcase.ui_ran = True | ||
408 | 33 | |||
409 | 34 | |||
410 | 35 | class MockPackage(object): | ||
411 | 36 | |||
412 | 37 | def __init__(self, downloadable=False): | ||
413 | 38 | self.downloadable = downloadable | ||
414 | 39 | self.candidate = self | ||
415 | 40 | |||
416 | 41 | |||
417 | 42 | class MockApt(dict): | ||
418 | 43 | |||
419 | 44 | def __init__(self): | ||
420 | 45 | self._depcache = self | ||
421 | 46 | |||
422 | 47 | def ReadPinFile(self, filename): | ||
423 | 48 | pass | ||
424 | 49 | |||
425 | 50 | def Cache(self): | ||
426 | 51 | return self | ||
427 | 52 | |||
428 | 53 | |||
429 | 54 | class MockCruft(object): | ||
430 | 55 | |||
431 | 56 | def __init__(self, name): | ||
432 | 57 | self.name = name | ||
433 | 58 | |||
434 | 59 | def get_name(self): | ||
435 | 60 | return self.name | ||
436 | 61 | |||
437 | 62 | |||
438 | 63 | class ApplicationTests(unittest.TestCase): | ||
439 | 64 | |||
440 | 65 | def setUp(self): | ||
441 | 66 | self.mock_apt = MockApt() | ||
442 | 67 | self.app = computerjanitorapp.Application(apt=self.mock_apt) | ||
443 | 68 | self.app.apt_cache["dash"] = MockPackage(downloadable=True) | ||
444 | 69 | self.app.apt_cache["gzip"] = MockPackage(downloadable=True) | ||
445 | 70 | |||
446 | 71 | def testSetsUpAptAttributeCorrectly(self): | ||
447 | 72 | self.assertEqual(self.app.apt, self.mock_apt) | ||
448 | 73 | |||
449 | 74 | def testSetsUpAndReturnsState(self): | ||
450 | 75 | self.assert_(self.app.state) | ||
451 | 76 | |||
452 | 77 | def testSetsUpAptCacheWhenRequested(self): | ||
453 | 78 | self.assertNotEqual(self.app.apt_cache, None) | ||
454 | 79 | |||
455 | 80 | def testSetsOptionDefaultsCorrectly(self): | ||
456 | 81 | options, args = self.app.parse_options(args=[]) | ||
457 | 82 | self.assertEqual(args, []) | ||
458 | 83 | self.assertEqual(options.all, None) | ||
459 | 84 | self.assertEqual(options.state_file, | ||
460 | 85 | "/var/lib/computer-janitor/state.dat") | ||
461 | 86 | self.assertEqual(options.no_act, None) | ||
462 | 87 | |||
463 | 88 | def testAcceptsDashDashAllOption(self): | ||
464 | 89 | options, args = self.app.parse_options(args=["--all"]) | ||
465 | 90 | self.assertEqual(options.all, True) | ||
466 | 91 | |||
467 | 92 | def testAcceptsDashDashStateFileOption(self): | ||
468 | 93 | options, args = self.app.parse_options(args=["--state-file=foo"]) | ||
469 | 94 | self.assertEqual(options.state_file, "foo") | ||
470 | 95 | |||
471 | 96 | def testAcceptsDashDashNoActOption(self): | ||
472 | 97 | options, args = self.app.parse_options(args=["--no-act"]) | ||
473 | 98 | self.assertEqual(options.no_act, True) | ||
474 | 99 | |||
475 | 100 | def testRunsUserInterface(self): | ||
476 | 101 | |||
477 | 102 | def pm_class(app, plugin_dirs): | ||
478 | 103 | self.pm_ran = True | ||
479 | 104 | |||
480 | 105 | def ui_class(app, pm): | ||
481 | 106 | return MockUI(self, app, pm) | ||
482 | 107 | |||
483 | 108 | self.pm_ran = False | ||
484 | 109 | self.ui_ran = False | ||
485 | 110 | self.app.run(ui_class=ui_class, plugin_manager_class=pm_class) | ||
486 | 111 | self.assert_(self.ui_ran) | ||
487 | 112 | |||
488 | 113 | def testAcceptsAptCacheWhenEssentialPackagesAreThere(self): | ||
489 | 114 | self.assertEqual(self.app.verify_apt_cache(), None) | ||
490 | 115 | |||
491 | 116 | def testRejectsAptCacheWhenDashIsMissing(self): | ||
492 | 117 | del self.app.apt_cache["dash"] | ||
493 | 118 | self.assertRaises(computerjanitor.Exception, | ||
494 | 119 | self.app.verify_apt_cache) | ||
495 | 120 | |||
496 | 121 | def testRejectsAptCacheWhenGzipIsMissing(self): | ||
497 | 122 | del self.app.apt_cache["gzip"] | ||
498 | 123 | self.assertRaises(computerjanitor.Exception, | ||
499 | 124 | self.app.verify_apt_cache) | ||
500 | 125 | |||
501 | 126 | def testRejectsAptCacheWhenDashIsNotDownloadable(self): | ||
502 | 127 | self.app.apt_cache["dash"].candidate.downloadable = False | ||
503 | 128 | self.assertRaises(computerjanitor.Exception, | ||
504 | 129 | self.app.verify_apt_cache) | ||
505 | 130 | |||
506 | 131 | def testRejectsAptCacheWhenGzipIsNotDownloadable(self): | ||
507 | 132 | self.app.apt_cache["dash"].candidate.downloadable = False | ||
508 | 133 | self.assertRaises(computerjanitor.Exception, | ||
509 | 134 | self.app.verify_apt_cache) | ||
510 | 135 | |||
511 | 136 | def testSetsDefaultListOfWhitelistDirectoriesCorrectly(self): | ||
512 | 137 | self.assert_("/etc/computer-janitor.d" in self.app.whitelist_dirs) | ||
513 | 138 | |||
514 | 139 | def testReturnsEmptyWhitelistByDefault(self): | ||
515 | 140 | dirname = tempfile.mkdtemp() | ||
516 | 141 | whitelist = self.app.whitelisted_cruft(dirnames=[dirname]) | ||
517 | 142 | shutil.rmtree(dirname) | ||
518 | 143 | self.assertEqual(whitelist, []) | ||
519 | 144 | |||
520 | 145 | def testDoesNotMindNonExistentWhitelistDirectory(self): | ||
521 | 146 | dirname = tempfile.mkdtemp() | ||
522 | 147 | subdir = os.path.join(dirname, "foo") | ||
523 | 148 | whitelist = self.app.whitelisted_cruft(dirnames=[subdir]) | ||
524 | 149 | shutil.rmtree(dirname) | ||
525 | 150 | self.assertEqual(whitelist, []) | ||
526 | 151 | |||
527 | 152 | def testReadsWhitelistFilesCorrectly(self): | ||
528 | 153 | dirname = tempfile.mkdtemp() | ||
529 | 154 | temp1 = os.path.join(dirname, "foo.whitelist") | ||
530 | 155 | temp2 = os.path.join(dirname, "foo.whitelist~") | ||
531 | 156 | |||
532 | 157 | file(temp1, "w").write("deb:foo\n") | ||
533 | 158 | file(temp2, "w").write("deb:bar\n") | ||
534 | 159 | |||
535 | 160 | whitelist = self.app.whitelisted_cruft(dirnames=[dirname]) | ||
536 | 161 | shutil.rmtree(dirname) | ||
537 | 162 | self.assertEqual(whitelist, ["deb:foo"]) | ||
538 | 163 | |||
539 | 164 | def testFindsCorrectWhitelistFilesInDotDDirectory(self): | ||
540 | 165 | dirname = tempfile.mkdtemp() | ||
541 | 166 | file(os.path.join(dirname, "foo.whitelist"), "w").write("deb:foo\n") | ||
542 | 167 | file(os.path.join(dirname, "foo.whitelist~"), "w").write("deb:bar\n") | ||
543 | 168 | list = self.app.whitelist_files([dirname]) | ||
544 | 169 | shutil.rmtree(dirname) | ||
545 | 170 | self.assertEqual(list, [os.path.join(dirname, "foo.whitelist")]) | ||
546 | 171 | |||
547 | 172 | def testRemovesWhitelistedCruftCorrectly(self): | ||
548 | 173 | crufts = [MockCruft("deb:foo"), MockCruft("deb:bar")] | ||
549 | 174 | dirname = tempfile.mkdtemp() | ||
550 | 175 | file(os.path.join(dirname, "foo.whitelist"), "w").write("deb:foo\n") | ||
551 | 176 | crufts2 = self.app.remove_whitelisted(crufts, dirnames=[dirname]) | ||
552 | 177 | shutil.rmtree(dirname) | ||
553 | 178 | self.assertEqual(crufts2, crufts[1:]) | ||
554 | 179 | 0 | ||
555 | === added file 'computerjanitorapp/areyousure.py' | |||
556 | --- computerjanitorapp/areyousure.py 1970-01-01 00:00:00 +0000 | |||
557 | +++ computerjanitorapp/areyousure.py 2010-03-10 21:40:30 +0000 | |||
558 | @@ -0,0 +1,76 @@ | |||
559 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
560 | 2 | # | ||
561 | 3 | # This program is free software: you can redistribute it and/or modify | ||
562 | 4 | # it under the terms of the GNU General Public License as published by | ||
563 | 5 | # the Free Software Foundation, version 3 of the License. | ||
564 | 6 | # | ||
565 | 7 | # This program is distributed in the hope that it will be useful, | ||
566 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
567 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
568 | 10 | # GNU General Public License for more details. | ||
569 | 11 | # | ||
570 | 12 | # You should have received a copy of the GNU General Public License | ||
571 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
572 | 14 | |||
573 | 15 | """Confirm some actions.""" | ||
574 | 16 | |||
575 | 17 | from __future__ import absolute_import, unicode_literals | ||
576 | 18 | |||
577 | 19 | __metaclass__ = type | ||
578 | 20 | __all__ = [ | ||
579 | 21 | 'AreYouSure', | ||
580 | 22 | ] | ||
581 | 23 | |||
582 | 24 | |||
583 | 25 | import gtk | ||
584 | 26 | |||
585 | 27 | from computerjanitorapp import setup_gettext | ||
586 | 28 | from computerjanitorapp.store import ListStoreColumns | ||
587 | 29 | |||
588 | 30 | _ = setup_gettext() | ||
589 | 31 | NL = '\n' | ||
590 | 32 | |||
591 | 33 | |||
592 | 34 | class AreYouSure: | ||
593 | 35 | """Confirmation of destructive actions.""" | ||
594 | 36 | |||
595 | 37 | def __init__(self, ui): | ||
596 | 38 | self._ui = ui | ||
597 | 39 | |||
598 | 40 | def verify(self): | ||
599 | 41 | """Confirm package removal.""" | ||
600 | 42 | # Start by getting all the active, non-ignored cruft. These are the | ||
601 | 43 | # candidates for removal. | ||
602 | 44 | cleanable_cruft = self._ui.get_cleanable_cruft() | ||
603 | 45 | # It would be nice if we could produce better messages than these, but | ||
604 | 46 | # that would require a richer interface to the dbus service, and | ||
605 | 47 | # probably to the cruft plugin architecture underneath that. | ||
606 | 48 | message = _('Are you sure you want to clean your system?') | ||
607 | 49 | dialog = gtk.MessageDialog( | ||
608 | 50 | parent=self._ui.widgets['window'], | ||
609 | 51 | type=gtk.MESSAGE_WARNING, | ||
610 | 52 | buttons=gtk.BUTTONS_NONE, | ||
611 | 53 | message_format=message) | ||
612 | 54 | dialog.set_title(_('Clean up')) | ||
613 | 55 | package_cruft_count = sum(1 for cruft_name, is_pkg in cleanable_cruft | ||
614 | 56 | if is_pkg) | ||
615 | 57 | other_cruft_count = len(cleanable_cruft) - package_cruft_count | ||
616 | 58 | messages = [] | ||
617 | 59 | if package_cruft_count > 0: | ||
618 | 60 | messages = [_('<b>Software packages to remove: {packages}</b>.'), | ||
619 | 61 | _('\nRemoving packages that are still in use can ' | ||
620 | 62 | 'cause errors.')] | ||
621 | 63 | ok_button = _('Remove packages') | ||
622 | 64 | if other_cruft_count > 0: | ||
623 | 65 | messages.insert(1, _('Non-package items to remove: {others}.')) | ||
624 | 66 | ok_button = _('Clean up') | ||
625 | 67 | message = NL.join(messages).format(packages=package_cruft_count, | ||
626 | 68 | others=other_cruft_count) | ||
627 | 69 | dialog.format_secondary_markup(message) | ||
628 | 70 | dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CLOSE) | ||
629 | 71 | dialog.add_button(ok_button, gtk.RESPONSE_YES) | ||
630 | 72 | # Show the dialog and get the user's response. | ||
631 | 73 | dialog.show_all() | ||
632 | 74 | response = dialog.run() | ||
633 | 75 | dialog.hide() | ||
634 | 76 | return response == gtk.RESPONSE_YES | ||
635 | 0 | 77 | ||
636 | === renamed file 'computerjanitorapp/ui_cli.py' => 'computerjanitorapp/maincli.py' | |||
637 | --- computerjanitorapp/ui_cli.py 2009-08-18 14:10:42 +0000 | |||
638 | +++ computerjanitorapp/maincli.py 2010-03-10 21:40:30 +0000 | |||
639 | @@ -1,5 +1,4 @@ | |||
642 | 1 | # ui_cli.py - command line user interface | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. |
641 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
643 | 3 | # | 2 | # |
644 | 4 | # This program is free software: you can redistribute it and/or modify | 3 | # This program is free software: you can redistribute it and/or modify |
645 | 5 | # it under the terms of the GNU General Public License as published by | 4 | # it under the terms of the GNU General Public License as published by |
646 | @@ -13,168 +12,296 @@ | |||
647 | 13 | # You should have received a copy of the GNU General Public License | 12 | # You should have received a copy of the GNU General Public License |
648 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
649 | 15 | 14 | ||
653 | 16 | 15 | """Command line user interface.""" | |
654 | 17 | import os | 16 | |
655 | 18 | import logging | 17 | from __future__ import absolute_import, unicode_literals |
656 | 18 | |||
657 | 19 | __metaclass__ = type | ||
658 | 20 | __all__ = [ | ||
659 | 21 | 'main', | ||
660 | 22 | ] | ||
661 | 23 | |||
662 | 24 | |||
663 | 19 | import sys | 25 | import sys |
664 | 26 | import dbus | ||
665 | 27 | import gobject | ||
666 | 28 | import argparse | ||
667 | 20 | import textwrap | 29 | import textwrap |
688 | 21 | import traceback | 30 | |
689 | 22 | 31 | from dbus.mainloop.glib import DBusGMainLoop | |
690 | 23 | import computerjanitor | 32 | |
691 | 24 | import computerjanitorapp | 33 | from computerjanitorapp import __version__, setup_gettext |
692 | 25 | _ = computerjanitorapp.setup_gettext() | 34 | from computerjanitorapp.terminalsize import get_terminal_size |
693 | 26 | 35 | from computerjanitorapp.utilities import format_size | |
694 | 27 | 36 | from computerjanitord.service import DBUS_INTERFACE_NAME | |
695 | 28 | class UnknownCommand(computerjanitor.Exception): | 37 | _ = setup_gettext() |
696 | 29 | 38 | ||
697 | 30 | def __init__(self, name): | 39 | |
698 | 31 | self._str = _("Unknown command: %s") % name | 40 | class Options: |
699 | 32 | 41 | """Command line option parser.""" | |
700 | 33 | 42 | def __init__(self, runner): | |
701 | 34 | class UnknownCruft(computerjanitor.Exception): | 43 | """Parse the command line options. |
702 | 35 | 44 | ||
703 | 36 | def __init__(self, name): | 45 | :param runner: The class implementing the sub-commands. |
704 | 37 | self._str = _("Unknown cruft: %s") % name | 46 | :type runner: `Runner` |
705 | 38 | 47 | """ | |
706 | 39 | 48 | self.runner = runner | |
707 | 40 | class MustBeRoot(computerjanitor.Exception): | 49 | # Parse the arguments. |
708 | 50 | self.parser = argparse.ArgumentParser( | ||
709 | 51 | description=_("""\ | ||
710 | 52 | Find and remove cruft from your system. | ||
711 | 53 | |||
712 | 54 | Cruft is anything that shouldn't be on your system, but is. | ||
713 | 55 | Stretching the definition, cruft is also things that should be on | ||
714 | 56 | your system but aren't."""), | ||
715 | 57 | version=__version__) | ||
716 | 58 | subparser = self.parser.add_subparsers(title='Commands') | ||
717 | 59 | # The 'find' subcommand. | ||
718 | 60 | command = subparser.add_parser( | ||
719 | 61 | 'find', help=_('Find and display all cruft found on your system.')) | ||
720 | 62 | command.add_argument( | ||
721 | 63 | '-v', '--verbose', action='store_true', | ||
722 | 64 | help=_("""\ | ||
723 | 65 | Display a detailed explanation for each piece of cruft found.""")) | ||
724 | 66 | command.add_argument( | ||
725 | 67 | '-i', '--ignored', action='store_true', | ||
726 | 68 | help=_('Find and display only the ignored cruft.')) | ||
727 | 69 | command.add_argument( | ||
728 | 70 | '-r', '--removable', action='store_true', | ||
729 | 71 | help=_('Find and display only the removable cruft.')) | ||
730 | 72 | command.add_argument( | ||
731 | 73 | '-s', '--short', action='store_true', | ||
732 | 74 | help=_('Display only the package names. Do not use with -v.')) | ||
733 | 75 | command.set_defaults(func=self.runner.find) | ||
734 | 76 | # The 'ignore' subcommand. | ||
735 | 77 | command = subparser.add_parser( | ||
736 | 78 | 'ignore', help=_("""\ | ||
737 | 79 | Ignore a piece of cruft so that it is not cleaned up.""")) | ||
738 | 80 | command.add_argument( | ||
739 | 81 | 'cruft', nargs=1, | ||
740 | 82 | help=_('The name of the cruft to ignore.')) | ||
741 | 83 | command.set_defaults(func=self.runner.ignore) | ||
742 | 84 | # The 'unignore' subcommand. | ||
743 | 85 | command = subparser.add_parser( | ||
744 | 86 | 'unignore', help=_("""\ | ||
745 | 87 | Unignore a piece of cruft so that it will be cleaned up.""")) | ||
746 | 88 | command.add_argument( | ||
747 | 89 | 'cruft', nargs=1, | ||
748 | 90 | help=_('The name of the cruft to unignore.')) | ||
749 | 91 | command.set_defaults(func=self.runner.unignore) | ||
750 | 92 | # The 'clean' subcommand. | ||
751 | 93 | command = subparser.add_parser( | ||
752 | 94 | 'clean', | ||
753 | 95 | help=_('Remove the selected cruft from the system.')) | ||
754 | 96 | command.add_argument( | ||
755 | 97 | '-a', '--all', action='store_true', | ||
756 | 98 | help=_('Clean up all unignored cruft.')) | ||
757 | 99 | command.add_argument( | ||
758 | 100 | '-v', '--verbose', action='store_true', | ||
759 | 101 | help=_("Provide more details on what's being cleaned.")) | ||
760 | 102 | command.add_argument( | ||
761 | 103 | 'cruft', nargs='?', | ||
762 | 104 | help=_("""\ | ||
763 | 105 | The name of the cruft to clean up. Do not use if specifying | ||
764 | 106 | --all.""")) | ||
765 | 107 | command.set_defaults(func=self.runner.clean) | ||
766 | 108 | # Parse the arguments and execute the subcommand. | ||
767 | 109 | self.arguments = self.parser.parse_args() | ||
768 | 110 | |||
769 | 111 | |||
770 | 112 | class Runner: | ||
771 | 113 | """Implementations of subcommands.""" | ||
772 | 41 | 114 | ||
773 | 42 | def __init__(self): | 115 | def __init__(self): |
912 | 43 | self._str = _("computer-janitor must be run as root, sorry.") | 116 | # Connect to the dbus service. |
913 | 44 | 117 | system_bus = dbus.SystemBus() | |
914 | 45 | 118 | proxy = system_bus.get_object(DBUS_INTERFACE_NAME, '/') | |
915 | 46 | class CommandLineUserInterface(computerjanitorapp.UserInterface): | 119 | self.janitord = dbus.Interface( |
916 | 47 | 120 | proxy, dbus_interface=DBUS_INTERFACE_NAME) | |
917 | 48 | def run(self, options, args): | 121 | # Connect to the signal the server will emit when cleaning up. |
918 | 49 | if self.mustberoot and os.getuid() != 0: | 122 | self.janitord.connect_to_signal('cleanup_status', self._clean_working) |
919 | 50 | raise MustBeRoot() | 123 | # This will get backpatched by __main__. We need it to produce error |
920 | 51 | 124 | # messages from the argparser. | |
921 | 52 | self.app.verify_apt_cache() | 125 | self.options = None |
922 | 53 | 126 | # The main loop for asynchronous calls and signal reception. | |
923 | 54 | dict = { | 127 | self.loop = gobject.MainLoop() |
924 | 55 | "find": self.show_cruft, | 128 | |
925 | 56 | "cleanup": self.cleanup, | 129 | def _error(self, message): |
926 | 57 | "ignore": self.ignore, | 130 | """Generate a parser error and exit. |
927 | 58 | "unignore": self.unignore, | 131 | |
928 | 59 | "help": self.help, | 132 | :param message: The error message. |
929 | 60 | } | 133 | :type message: string |
930 | 61 | 134 | """ | |
931 | 62 | if args: | 135 | self.options.parser.error(message) |
932 | 63 | cmd = args[0] | 136 | # No return. |
933 | 64 | args = args[1:] | 137 | |
934 | 65 | else: | 138 | def find(self, arguments): |
935 | 66 | cmd = "help" | 139 | """Find and display all cruft. |
936 | 67 | args = [] | 140 | |
937 | 68 | 141 | :param arguments: Command line options. | |
938 | 69 | if cmd in dict: | 142 | """ |
939 | 70 | app = self.app | 143 | # Cruft will be prefixed by 'removable' if it is not being ignored. |
940 | 71 | app.state.load(options.state_file) | 144 | ignored = set(self.janitord.ignored()) |
941 | 72 | try: | 145 | cruft_names = set(self.janitord.find()) |
942 | 73 | dict[cmd](options, args) | 146 | # Filter names based on option flags. |
943 | 74 | except Exception, e: # pragma: no cover | 147 | if arguments.ignored: |
944 | 75 | logging.debug(unicode(traceback.format_exc())) | 148 | cruft = sorted(cruft_names & ignored) |
945 | 76 | logging.critical(unicode(e)) | 149 | elif arguments.removable: |
946 | 77 | sys.exit(1) | 150 | cruft = sorted(cruft_names - ignored) |
947 | 78 | else: | 151 | else: |
948 | 79 | raise UnknownCommand(cmd) | 152 | cruft = sorted(cruft_names) |
949 | 80 | 153 | # The prefix will either be 'ignored' or 'removable' however this | |
950 | 81 | def find_cruft(self): | 154 | # string will be translated, so calculate the prefix size in the |
951 | 82 | list = [] | 155 | # native language, then add two columns of separator, followed by the |
952 | 83 | for plugin in self.pm.get_plugins(): | 156 | # cruft name. |
953 | 84 | for cruft in plugin.get_cruft(): | 157 | prefixi = _('ignored') |
954 | 85 | list.append(cruft) | 158 | prefixr = _('removable') |
955 | 86 | return self.app.remove_whitelisted(list) | 159 | prefix_width = max(len(prefixi), len(prefixr)) |
956 | 87 | 160 | # Long, short, shorter display. | |
957 | 88 | def show_one_cruft(self, name, desc, s, width): #pragma: no cover | 161 | if arguments.verbose and arguments.short: |
958 | 89 | if width is None: | 162 | self._error('Use either -s or -v but not both.') |
959 | 90 | max = len(name) + len(desc or '') | 163 | if arguments.short: |
960 | 91 | else: | 164 | # This is the shorter output. |
961 | 92 | max = width | 165 | for name in cruft: |
962 | 93 | max -= 9 # state column | 166 | print name |
963 | 94 | max -= 2 # two spaces | 167 | elif not arguments.verbose: |
964 | 95 | max -= 1 # avoid the last column, some terminals word wrap there | 168 | # This is the short output. |
965 | 96 | 169 | for name in cruft: | |
966 | 97 | msg = ["%-9s %.*s" % (s, max, name)] | 170 | prefix = (prefixi if name in ignored else prefixr) |
967 | 98 | if desc: | 171 | # 10 spaces for the prefix |
968 | 99 | paras = desc.split("\n\n") | 172 | print '{0:{1}} {2}'.format(prefix, prefix_width, name) |
969 | 100 | for para in paras: | 173 | else: |
970 | 101 | for line in textwrap.wrap(para, max): | 174 | # This is the verbose output. Start by getting the terminal's |
971 | 102 | msg.append("%9s %s" % ("", line)) | 175 | # size, though all we care about is the width. |
972 | 103 | msg.append("") | 176 | rows, columns = get_terminal_size() |
973 | 104 | print ("\n".join(msg)).encode('utf-8') # FIXME: horrible kludge | 177 | width = ((80 if columns in (None, 0) else columns) |
974 | 105 | # the above makes it possible to write out stuff even when | 178 | - prefix_width # space for prefix |
975 | 106 | # it's going to somewhere like a pipe, because we explicitly | 179 | - 2 # separator |
976 | 107 | # encode it as utf-8 first. This ignores the user's desired | 180 | - 1 # avoid last column, some terminals wrap |
977 | 108 | # charset, which is bad, bad, bad, but I can't figure out a | 181 | ) |
978 | 109 | # way to make Python behave in a sensible way. *sigh* | 182 | margin = ' ' * (prefix_width + 2) |
979 | 110 | # --liw, 2009-08-18 | 183 | for name in cruft: |
980 | 111 | 184 | prefix = (prefixi if name in ignored else prefixr) | |
981 | 112 | def show_cruft(self, options, args): | 185 | # 10 spaces for the prefix |
982 | 113 | list = [] | 186 | print '{0:{1}} {2}'.format(prefix, prefix_width, name) |
983 | 114 | maxname = "" | 187 | # Print some details about the cruft. |
984 | 115 | state = self.app.state | 188 | cruft_type, disk_usage = self.janitord.get_details(name) |
985 | 116 | for cruft in self.find_cruft(): | 189 | print '{0}{1} of type {2}'.format( |
986 | 117 | name = cruft.get_name() | 190 | margin, format_size(disk_usage), cruft_type) |
987 | 118 | if options.verbose: | 191 | # Get the description, wrap it to the available columns, and |
988 | 119 | desc = cruft.get_description() # pragma: no cover | 192 | # display it line-by-line with the proper amount of leading |
989 | 120 | else: | 193 | # spaces (prefix_width + 2). |
990 | 121 | desc = None | 194 | description = self.janitord.get_description(name) |
991 | 122 | if state.is_enabled(name): | 195 | if len(description) == 0: |
992 | 123 | s = _("removable") | 196 | |
993 | 124 | else: | 197 | continue |
994 | 125 | s = _("ignored") | 198 | paragraphs = description.split('\n\n') |
995 | 126 | list.append((name, desc, s)) | 199 | for paragraph in paragraphs: |
996 | 127 | if len(name) > len(maxname): | 200 | for line in textwrap.wrap(paragraph, width): |
997 | 128 | maxname = name | 201 | # 2010-02-09 barry: The original code forced the |
998 | 129 | 202 | # output to utf-8, claiming that was necessary to | |
999 | 130 | rows, cols = computerjanitorapp.get_terminal_size() | 203 | # "write out stuff even when it's going to somewhere |
1000 | 131 | for name, desc, s in sorted(list): | 204 | # like a pipe [...] ignor[ing] the user's desired |
1001 | 132 | self.show_one_cruft(name, desc, s, cols) | 205 | # charset, which is bad, bad, bad...". This makes me |
1002 | 133 | 206 | # pretty uncomfortable, so I'd like to see a bug | |
1003 | 134 | def cleanup(self, options, args): | 207 | # report before I copy that from the previous |
1004 | 135 | crufts = {} | 208 | # implementation. |
1005 | 136 | for cruft in self.find_cruft(): | 209 | print '{0}{1}'.format(margin, line) |
1006 | 137 | crufts[cruft.get_name()] = cruft | 210 | # Paragraph separator. |
1007 | 138 | 211 | ||
1008 | 139 | if args: | 212 | |
1009 | 140 | for arg in args: | 213 | def ignore(self, arguments): |
1010 | 141 | if arg not in crufts: | 214 | """Ignore some cruft. |
1011 | 142 | raise UnknownCruft(arg) | 215 | |
1012 | 143 | elif options.all: | 216 | :param arguments: Command line options. |
1013 | 144 | state = self.app.state | 217 | """ |
1014 | 145 | args = [] | 218 | assert len(arguments.cruft) == 1, 'Unexpected arguments' |
1015 | 146 | for name in crufts.keys(): | 219 | self.janitord.ignore(arguments.cruft[0]) |
1016 | 147 | if state.is_enabled(name): | 220 | self.janitord.save() |
1017 | 148 | args.append(name) | 221 | |
1018 | 149 | else: | 222 | def unignore(self, arguments): |
1019 | 150 | logging.info(_("Ignored: %s") % name) | 223 | """Unignore some cruft. |
1020 | 151 | 224 | ||
1021 | 152 | for arg in args: | 225 | :param arguments: Command line options. |
1022 | 153 | if options.no_act: | 226 | """ |
1023 | 154 | logging.info(_("Pretending to remove cruft: %s") % arg) | 227 | assert len(arguments.cruft) == 1, 'Unexpected arguments' |
1024 | 155 | else: | 228 | self.janitord.unignore(arguments.cruft[0]) |
1025 | 156 | logging.info(_("Removing cruft: %s") % arg) | 229 | self.janitord.save() |
1026 | 157 | crufts[arg].cleanup() | 230 | |
1027 | 158 | for plugin in self.pm.get_plugins(): | 231 | def _clean_reply(self): |
1028 | 159 | if options.no_act: | 232 | """The 'clean' operation has completed successfully.""" |
1029 | 160 | logging.info(_("Pretending to post-cleanup: %s") % plugin) | 233 | self.loop.quit() |
1030 | 161 | else: | 234 | print 'done.' |
1031 | 162 | logging.info(_("Post-cleanup: %s") % plugin) | 235 | |
1032 | 163 | plugin.post_cleanup() | 236 | def _clean_error(self, exception): |
1033 | 164 | 237 | """The 'clean' operation has failed.""" | |
1034 | 165 | def ignore(self, options, cruft_names): | 238 | self.loop.quit() |
1035 | 166 | state = self.app.state | 239 | print 'dbus service error:', exception |
1036 | 167 | for cruft_name in cruft_names: | 240 | |
1037 | 168 | state.disable(cruft_name) | 241 | def _clean_working(self, cruft): |
1038 | 169 | if not options.no_act: | 242 | """The 'clean' operation is in progress. |
1039 | 170 | state.save(options.state_file) | 243 | |
1040 | 171 | 244 | :param cruft: The cruft that is being cleaned up. | |
1041 | 172 | def unignore(self, options, cruft_names): | 245 | :type cruft: string |
1042 | 173 | state = self.app.state | 246 | """ |
1043 | 174 | for cruft_name in cruft_names: | 247 | verbose = self.options.arguments.verbose |
1044 | 175 | state.enable(cruft_name) | 248 | if cruft == '': |
1045 | 176 | if not options.no_act: | 249 | # We're done. |
1046 | 177 | state.save(options.state_file) | 250 | if not verbose: |
1047 | 178 | 251 | sys.stdout.write(' ') | |
1048 | 179 | def help(self, options, args): | 252 | sys.stdout.flush() |
1049 | 180 | self.app.parser.print_help() | 253 | else: |
1050 | 254 | if verbose: | ||
1051 | 255 | print 'Working on', cruft | ||
1052 | 256 | else: | ||
1053 | 257 | sys.stdout.write('.') | ||
1054 | 258 | sys.stdout.flush() | ||
1055 | 259 | |||
1056 | 260 | def clean(self, arguments): | ||
1057 | 261 | """Clean up the cruft. | ||
1058 | 262 | |||
1059 | 263 | :param arguments: Command line options. | ||
1060 | 264 | """ | ||
1061 | 265 | if arguments.all: | ||
1062 | 266 | # You can't specify both --all and a cruft name. | ||
1063 | 267 | if arguments.cruft is not None: | ||
1064 | 268 | self._error('Specify a cruft name or --all, but not both') | ||
1065 | 269 | # No return. | ||
1066 | 270 | all_cruft = set(self.janitord.find()) | ||
1067 | 271 | ignored = set(self.janitord.ignored()) | ||
1068 | 272 | cleanable_cruft = all_cruft - ignored | ||
1069 | 273 | else: | ||
1070 | 274 | if arguments.cruft is None: | ||
1071 | 275 | self._error('You must specify a cruft name, or use --all') | ||
1072 | 276 | # No return. | ||
1073 | 277 | cleanable_cruft = (arguments.cruft,) | ||
1074 | 278 | # Make the asynchronous call because this can take a long time. We'll | ||
1075 | 279 | # get status updates periodically. Note however that even though this | ||
1076 | 280 | # is asynchronous, dbus still expects a response within a certain | ||
1077 | 281 | # amount of time. We have no idea how long it will take to clean up | ||
1078 | 282 | # the cruft though, so just crank the timeout up to some insanely huge | ||
1079 | 283 | # number (of seconds). | ||
1080 | 284 | self.janitord.clean(cleanable_cruft, | ||
1081 | 285 | reply_handler=self._clean_reply, | ||
1082 | 286 | error_handler=self._clean_error, | ||
1083 | 287 | # If it takes longer than an hour, we're screwed. | ||
1084 | 288 | timeout=3600) | ||
1085 | 289 | # Start the main loop. This will exit when the remote operation is | ||
1086 | 290 | # complete. | ||
1087 | 291 | if not arguments.verbose: | ||
1088 | 292 | sys.stdout.write('processing') | ||
1089 | 293 | sys.stdout.flush() | ||
1090 | 294 | self.loop.run() | ||
1091 | 295 | |||
1092 | 296 | |||
1093 | 297 | def main(): | ||
1094 | 298 | # We'll need a main loop to receive status signals from the dbus service. | ||
1095 | 299 | # Don't start the main loop yet though, since we only need it for 'clean' | ||
1096 | 300 | # commands. | ||
1097 | 301 | DBusGMainLoop(set_as_default=True) | ||
1098 | 302 | runner = Runner() | ||
1099 | 303 | options = Options(runner) | ||
1100 | 304 | # Backpatch runner because of circular references. | ||
1101 | 305 | runner.options = options | ||
1102 | 306 | # Execute the subcommand. | ||
1103 | 307 | options.arguments.func(options.arguments) | ||
1104 | 181 | 308 | ||
1105 | === added file 'computerjanitorapp/maingtk.py' | |||
1106 | --- computerjanitorapp/maingtk.py 1970-01-01 00:00:00 +0000 | |||
1107 | +++ computerjanitorapp/maingtk.py 2010-03-10 21:40:30 +0000 | |||
1108 | @@ -0,0 +1,32 @@ | |||
1109 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
1110 | 2 | # | ||
1111 | 3 | # This program is free software: you can redistribute it and/or modify | ||
1112 | 4 | # it under the terms of the GNU General Public License as published by | ||
1113 | 5 | # the Free Software Foundation, version 3 of the License. | ||
1114 | 6 | # | ||
1115 | 7 | # This program is distributed in the hope that it will be useful, | ||
1116 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1117 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1118 | 10 | # GNU General Public License for more details. | ||
1119 | 11 | # | ||
1120 | 12 | # You should have received a copy of the GNU General Public License | ||
1121 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1122 | 14 | |||
1123 | 15 | """Command line user interface.""" | ||
1124 | 16 | |||
1125 | 17 | from __future__ import absolute_import, unicode_literals | ||
1126 | 18 | |||
1127 | 19 | __metaclass__ = type | ||
1128 | 20 | __all__ = [ | ||
1129 | 21 | 'main', | ||
1130 | 22 | ] | ||
1131 | 23 | |||
1132 | 24 | |||
1133 | 25 | from computerjanitorapp.uigtk import UserInterface | ||
1134 | 26 | from dbus.mainloop.glib import DBusGMainLoop | ||
1135 | 27 | |||
1136 | 28 | |||
1137 | 29 | def main(): | ||
1138 | 30 | DBusGMainLoop(set_as_default=True) | ||
1139 | 31 | ui = UserInterface() | ||
1140 | 32 | ui.run() | ||
1141 | 0 | 33 | ||
1142 | === removed file 'computerjanitorapp/state.py' | |||
1143 | --- computerjanitorapp/state.py 2009-07-13 09:48:06 +0000 | |||
1144 | +++ computerjanitorapp/state.py 1970-01-01 00:00:00 +0000 | |||
1145 | @@ -1,56 +0,0 @@ | |||
1146 | 1 | # state.py - store persistent state of crufts | ||
1147 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
1148 | 3 | # | ||
1149 | 4 | # This program is free software: you can redistribute it and/or modify | ||
1150 | 5 | # it under the terms of the GNU General Public License as published by | ||
1151 | 6 | # the Free Software Foundation, version 3 of the License. | ||
1152 | 7 | # | ||
1153 | 8 | # This program is distributed in the hope that it will be useful, | ||
1154 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1155 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1156 | 11 | # GNU General Public License for more details. | ||
1157 | 12 | # | ||
1158 | 13 | # You should have received a copy of the GNU General Public License | ||
1159 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1160 | 15 | |||
1161 | 16 | |||
1162 | 17 | import ConfigParser | ||
1163 | 18 | |||
1164 | 19 | |||
1165 | 20 | class State(object): | ||
1166 | 21 | |||
1167 | 22 | def __init__(self): | ||
1168 | 23 | self._cp = ConfigParser.ConfigParser() | ||
1169 | 24 | self._previously_ignored = set() | ||
1170 | 25 | |||
1171 | 26 | def load(self, filename): | ||
1172 | 27 | self._cp.read(filename) | ||
1173 | 28 | self._previously_ignored = set() | ||
1174 | 29 | for cruft_name in self._cp.sections(): | ||
1175 | 30 | if not self.is_enabled(cruft_name): | ||
1176 | 31 | self._previously_ignored.add(cruft_name) | ||
1177 | 32 | |||
1178 | 33 | def save(self, filename): | ||
1179 | 34 | f = file(filename, "w") | ||
1180 | 35 | self._cp.write(f) | ||
1181 | 36 | f.close() | ||
1182 | 37 | |||
1183 | 38 | def is_enabled(self, cruft_name): | ||
1184 | 39 | if self._cp.has_section(cruft_name): | ||
1185 | 40 | return self._cp.getboolean(cruft_name, "enabled") | ||
1186 | 41 | else: | ||
1187 | 42 | return True | ||
1188 | 43 | |||
1189 | 44 | def enable(self, cruft_name): | ||
1190 | 45 | if not self._cp.has_section(cruft_name): | ||
1191 | 46 | self._cp.add_section(cruft_name) | ||
1192 | 47 | self._cp.set(cruft_name, "enabled", "true") | ||
1193 | 48 | |||
1194 | 49 | def disable(self, cruft_name): | ||
1195 | 50 | if not self._cp.has_section(cruft_name): | ||
1196 | 51 | self._cp.add_section(cruft_name) | ||
1197 | 52 | self._cp.set(cruft_name, "enabled", "false") | ||
1198 | 53 | |||
1199 | 54 | def was_previously_ignored(self, cruft_name): | ||
1200 | 55 | return cruft_name in self._previously_ignored | ||
1201 | 56 | |||
1202 | 57 | 0 | ||
1203 | === removed file 'computerjanitorapp/state_tests.py' | |||
1204 | --- computerjanitorapp/state_tests.py 2009-07-13 09:48:06 +0000 | |||
1205 | +++ computerjanitorapp/state_tests.py 1970-01-01 00:00:00 +0000 | |||
1206 | @@ -1,77 +0,0 @@ | |||
1207 | 1 | # state_tests.py - unit tests for store.py | ||
1208 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
1209 | 3 | # | ||
1210 | 4 | # This program is free software: you can redistribute it and/or modify | ||
1211 | 5 | # it under the terms of the GNU General Public License as published by | ||
1212 | 6 | # the Free Software Foundation, version 3 of the License. | ||
1213 | 7 | # | ||
1214 | 8 | # This program is distributed in the hope that it will be useful, | ||
1215 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1216 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1217 | 11 | # GNU General Public License for more details. | ||
1218 | 12 | # | ||
1219 | 13 | # You should have received a copy of the GNU General Public License | ||
1220 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1221 | 15 | |||
1222 | 16 | |||
1223 | 17 | import os | ||
1224 | 18 | import tempfile | ||
1225 | 19 | import unittest | ||
1226 | 20 | |||
1227 | 21 | import computerjanitorapp | ||
1228 | 22 | |||
1229 | 23 | |||
1230 | 24 | class StateTests(unittest.TestCase): | ||
1231 | 25 | |||
1232 | 26 | def setUp(self): | ||
1233 | 27 | self.state = computerjanitorapp.State() | ||
1234 | 28 | |||
1235 | 29 | def testInitiallyEverythingIsEnabled(self): | ||
1236 | 30 | self.assert_(self.state.is_enabled("foo")) | ||
1237 | 31 | |||
1238 | 32 | def testDisablesWhenAsked(self): | ||
1239 | 33 | self.state.disable("foo") | ||
1240 | 34 | self.assertFalse(self.state.is_enabled("foo")) | ||
1241 | 35 | |||
1242 | 36 | def testEnablesDisabledCruft(self): | ||
1243 | 37 | self.state.disable("foo") | ||
1244 | 38 | self.state.enable("foo") | ||
1245 | 39 | self.assert_(self.state.is_enabled("foo")) | ||
1246 | 40 | |||
1247 | 41 | def testEnablesEnabledCruft(self): | ||
1248 | 42 | self.state.enable("foo") | ||
1249 | 43 | self.assert_(self.state.is_enabled("foo")) | ||
1250 | 44 | |||
1251 | 45 | def testSavesAndLoadsFiles(self): | ||
1252 | 46 | fd, filename = tempfile.mkstemp() | ||
1253 | 47 | os.close(fd) | ||
1254 | 48 | self.state.enable("foo") | ||
1255 | 49 | self.state.disable("bar") | ||
1256 | 50 | self.state.save(filename) | ||
1257 | 51 | self.state.disable("foo") | ||
1258 | 52 | self.state.enable("bar") | ||
1259 | 53 | self.state.load(filename) | ||
1260 | 54 | self.assert_(self.state.is_enabled("foo")) | ||
1261 | 55 | self.assertFalse(self.state.is_enabled("bar")) | ||
1262 | 56 | os.remove(filename) | ||
1263 | 57 | |||
1264 | 58 | def testInitallyNothingIsPreviouslyIgnored(self): | ||
1265 | 59 | self.assertFalse(self.state.was_previously_ignored("foo")) | ||
1266 | 60 | |||
1267 | 61 | def testRemembersWhatWasPreviouslyIgnored(self): | ||
1268 | 62 | fd, filename = tempfile.mkstemp() | ||
1269 | 63 | os.close(fd) | ||
1270 | 64 | |||
1271 | 65 | # Set a state to disabled. | ||
1272 | 66 | state1 = computerjanitorapp.State() | ||
1273 | 67 | state1.disable("foo") | ||
1274 | 68 | state1.save(filename) | ||
1275 | 69 | |||
1276 | 70 | # Load new state. | ||
1277 | 71 | state2 = computerjanitorapp.State() | ||
1278 | 72 | state2.load(filename) | ||
1279 | 73 | |||
1280 | 74 | self.assert_(state2.was_previously_ignored("foo")) | ||
1281 | 75 | |||
1282 | 76 | os.remove(filename) | ||
1283 | 77 | |||
1284 | 78 | 0 | ||
1285 | === added file 'computerjanitorapp/store.py' | |||
1286 | --- computerjanitorapp/store.py 1970-01-01 00:00:00 +0000 | |||
1287 | +++ computerjanitorapp/store.py 2010-03-10 21:40:30 +0000 | |||
1288 | @@ -0,0 +1,147 @@ | |||
1289 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
1290 | 2 | # | ||
1291 | 3 | # This program is free software: you can redistribute it and/or modify | ||
1292 | 4 | # it under the terms of the GNU General Public License as published by | ||
1293 | 5 | # the Free Software Foundation, version 3 of the License. | ||
1294 | 6 | # | ||
1295 | 7 | # This program is distributed in the hope that it will be useful, | ||
1296 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1297 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1298 | 10 | # GNU General Public License for more details. | ||
1299 | 11 | # | ||
1300 | 12 | # You should have received a copy of the GNU General Public License | ||
1301 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1302 | 14 | |||
1303 | 15 | """Gtk ListStore model for backing the TreeViews of cruft.""" | ||
1304 | 16 | |||
1305 | 17 | from __future__ import absolute_import, unicode_literals | ||
1306 | 18 | |||
1307 | 19 | __metaclass__ = type | ||
1308 | 20 | __all__ = [ | ||
1309 | 21 | 'Store', | ||
1310 | 22 | 'optimize', | ||
1311 | 23 | 'unused', | ||
1312 | 24 | ] | ||
1313 | 25 | |||
1314 | 26 | |||
1315 | 27 | import gtk | ||
1316 | 28 | import gobject | ||
1317 | 29 | |||
1318 | 30 | |||
1319 | 31 | # XXX 2010-03-04 barry: Use a munepy enum. | ||
1320 | 32 | class ListStoreColumns: | ||
1321 | 33 | # The cruft name as known by the dbus service. | ||
1322 | 34 | name = 0 | ||
1323 | 35 | # The short name of the cruft. | ||
1324 | 36 | short_name = 1 | ||
1325 | 37 | # The text displayed in the cell. | ||
1326 | 38 | text = 2 | ||
1327 | 39 | # Should the cruft be shown? | ||
1328 | 40 | show = 3 | ||
1329 | 41 | # Is the cruft active locally (i.e. toggle button is set)? | ||
1330 | 42 | active = 4 | ||
1331 | 43 | # Is the cruft being ignored in the dbus service? | ||
1332 | 44 | server_ignored = 5 | ||
1333 | 45 | # Is the cruft's description expanded? | ||
1334 | 46 | expanded = 6 | ||
1335 | 47 | # Is this cruft package cruft or some other kind of cruft? | ||
1336 | 48 | is_package_cruft = 7 | ||
1337 | 49 | |||
1338 | 50 | |||
1339 | 51 | class Store: | ||
1340 | 52 | """The higher level wrapper around the gtk.ListStore.""" | ||
1341 | 53 | |||
1342 | 54 | def __init__(self, janitord): | ||
1343 | 55 | """Create the Store. | ||
1344 | 56 | |||
1345 | 57 | :param janitord: The `dbus.Interface` for talking to the Computer | ||
1346 | 58 | Janitor dbus service. | ||
1347 | 59 | """ | ||
1348 | 60 | self.janitord = janitord | ||
1349 | 61 | # This ListStore is the backing data structure for the TreeViews in | ||
1350 | 62 | # the main window. It holds information about the individual pieces | ||
1351 | 63 | # of cruft. See ListStoreColumns for details. | ||
1352 | 64 | # | ||
1353 | 65 | # XXX 2010-03-04 barry: pygtk does not like 'unicode'. | ||
1354 | 66 | self.store = gtk.ListStore(str, str, str, bool, bool, bool, bool, bool) | ||
1355 | 67 | |||
1356 | 68 | def find_cruft(self): | ||
1357 | 69 | """Find cruft and populate the backing store.""" | ||
1358 | 70 | ignored_cruft = set(self.janitord.ignored()) | ||
1359 | 71 | all_cruft_names = set(self.janitord.find()) | ||
1360 | 72 | pkg_cruft_names = set( | ||
1361 | 73 | cruft_name for cruft_name in all_cruft_names | ||
1362 | 74 | if self.janitord.get_details(cruft_name)[0].lower() | ||
1363 | 75 | == 'packagecruft') | ||
1364 | 76 | self.store.clear() | ||
1365 | 77 | # Cruft always starts out in the active, not-expanded state. Package | ||
1366 | 78 | # cruft goes in the 'unused' column while non-package cruft goes in | ||
1367 | 79 | # the 'optimize' column. | ||
1368 | 80 | for cruft_name in all_cruft_names: | ||
1369 | 81 | cruft_shortname = self.janitord.get_shortname(cruft_name) | ||
1370 | 82 | self.store.append(( | ||
1371 | 83 | cruft_name, cruft_shortname, | ||
1372 | 84 | # What is being displayed in the cell. | ||
1373 | 85 | gobject.markup_escape_text(cruft_shortname), | ||
1374 | 86 | # Show the cruft be shown? | ||
1375 | 87 | cruft_name not in ignored_cruft, | ||
1376 | 88 | # Is the cruft active locally? | ||
1377 | 89 | cruft_name not in ignored_cruft, | ||
1378 | 90 | # Is the cruft ignored in the dbus service? | ||
1379 | 91 | cruft_name in ignored_cruft, | ||
1380 | 92 | # Is the cruft's description expanded? | ||
1381 | 93 | False, | ||
1382 | 94 | # Is this package cruft? | ||
1383 | 95 | cruft_name in pkg_cruft_names, | ||
1384 | 96 | )) | ||
1385 | 97 | |||
1386 | 98 | # Forwards to the underlying store, for convenience. Really, these should | ||
1387 | 99 | # use generic delegates, or clients should just use self.store.store. | ||
1388 | 100 | |||
1389 | 101 | def get_value(self, *args, **kws): | ||
1390 | 102 | return self.store.get_value(*args, **kws) | ||
1391 | 103 | |||
1392 | 104 | def set_value(self, *args, **kws): | ||
1393 | 105 | return self.store.set_value(*args, **kws) | ||
1394 | 106 | |||
1395 | 107 | def filter_new(self, *args, **kws): | ||
1396 | 108 | return self.store.filter_new(*args, **kws) | ||
1397 | 109 | |||
1398 | 110 | def foreach(self, *args, **kws): | ||
1399 | 111 | return self.store.foreach(*args, **kws) | ||
1400 | 112 | |||
1401 | 113 | def reorder(self, *args, **kws): | ||
1402 | 114 | return self.store.reorder(*args, **kws) | ||
1403 | 115 | |||
1404 | 116 | def get_iter_first(self, *args, **kws): | ||
1405 | 117 | return self.store.get_iter_first(*args, **kws) | ||
1406 | 118 | |||
1407 | 119 | def iter_next(self, *args, **kws): | ||
1408 | 120 | return self.store.iter_next(*args, **kws) | ||
1409 | 121 | |||
1410 | 122 | def clear(self, *args, **kws): | ||
1411 | 123 | return self.store.clear(*args, **kws) | ||
1412 | 124 | |||
1413 | 125 | |||
1414 | 126 | # Filter functions for use with TreeView column display. | ||
1415 | 127 | |||
1416 | 128 | def unused(store, iter): | ||
1417 | 129 | """True if the cruft is not being ignored and is package cruft. | ||
1418 | 130 | |||
1419 | 131 | :param store: The ListStore instance. | ||
1420 | 132 | :param iter: The TreeIter instance. | ||
1421 | 133 | """ | ||
1422 | 134 | show = store.get_value(iter, ListStoreColumns.show) | ||
1423 | 135 | is_package_cruft = store.get_value(iter, ListStoreColumns.is_package_cruft) | ||
1424 | 136 | return show and is_package_cruft | ||
1425 | 137 | |||
1426 | 138 | |||
1427 | 139 | def optimize(store, iter): | ||
1428 | 140 | """True if the cruft is not package cruft. | ||
1429 | 141 | |||
1430 | 142 | :param store: The ListStore instance. | ||
1431 | 143 | :param iter: The TreeIter instance. | ||
1432 | 144 | """ | ||
1433 | 145 | show = store.get_value(iter, ListStoreColumns.show) | ||
1434 | 146 | is_package_cruft = store.get_value(iter, ListStoreColumns.is_package_cruft) | ||
1435 | 147 | return show and not is_package_cruft | ||
1436 | 0 | 148 | ||
1437 | === modified file 'computerjanitorapp/terminalsize.py' | |||
1438 | --- computerjanitorapp/terminalsize.py 2008-09-22 21:39:57 +0000 | |||
1439 | +++ computerjanitorapp/terminalsize.py 2010-03-10 21:40:30 +0000 | |||
1440 | @@ -26,44 +26,39 @@ | |||
1441 | 26 | 26 | ||
1442 | 27 | def get_terminal_size(fd=1): | 27 | def get_terminal_size(fd=1): |
1443 | 28 | """Return size of terminal attached to the standard output. | 28 | """Return size of terminal attached to the standard output. |
1452 | 29 | 29 | ||
1453 | 30 | Use ioctl(2) to query a terminal for its size, given a file | 30 | Use ioctl(2) to query a terminal for its size, given a file descriptor |
1454 | 31 | descriptor attached to the terminal. Return (None, None) if this | 31 | attached to the terminal. |
1455 | 32 | fails, otherwise a tuple (columns, rows). | 32 | |
1456 | 33 | 33 | :param fd: Use the given file descriptor. | |
1457 | 34 | (The optional 'fd' argument can be set to whatever file | 34 | :type fd: int |
1458 | 35 | descriptor you want to use. This is useful for unit tests.) | 35 | :return: The columns and rows representing the size of the terminal. If |
1459 | 36 | 36 | this cannot be determined, None is returned for both values. | |
1460 | 37 | :rtype: 2-tuple | ||
1461 | 37 | """ | 38 | """ |
1462 | 38 | 39 | ||
1463 | 39 | try: | 40 | try: |
1469 | 40 | # Do the ioctl call. termios.TIOCGWINSZ is the code to query | 41 | # Do the ioctl call. termios.TIOCGWINSZ is the code to query terminal |
1470 | 41 | # terminal size (see tty_ioctl(4), at least on Linux). We need | 42 | # size (see tty_ioctl(4), at least on Linux). We need to give it a |
1471 | 42 | # to give it a string of suitable size to use as the input | 43 | # string of suitable size to use as the input buffer for ioctl. |
1472 | 43 | # buffer for ioctl. Ioctl modifies the buffer and returns the | 44 | # ioctl() modifies the buffer and returns the modified buffer as its |
1473 | 44 | # modified buffer as its return value. | 45 | # return value. |
1474 | 45 | # | 46 | # |
1475 | 46 | # The manual page specifies a struct winsize to be used, which | 47 | # The manual page specifies a struct winsize to be used, which |
1477 | 47 | # consists of four unsigned shorts. We use struct.calcsize to | 48 | # consists of four unsigned shorts. We use struct.calcsize() to |
1478 | 48 | # compute the size of that. | 49 | # compute the size of that. |
1488 | 49 | # | 50 | # |
1489 | 50 | # Note that Blake's original code assumes only the first two | 51 | # Note that Blake's original code assumes only the first two shorts in |
1490 | 51 | # shorts in the struct are used, and that two shorts fit into | 52 | # the struct are used, and that two shorts fit into four bytes, which |
1491 | 52 | # four bytes, which is probably true for all the relevant | 53 | # is probably true for all the relevant platforms, but is cramped |
1492 | 53 | # platforms, but is cramped enough that it makes me feel icky. | 54 | # enough that it makes me feel icky. Thus, I assume less. This will |
1493 | 54 | # Thus, I assume less. This will still break if the contents | 55 | # still break if the contents of the struct change, but since that |
1494 | 55 | # of the struct change, but since that would change the system | 56 | # would change the system call API, that's unlikely. |
1486 | 56 | # call API, that's unlikely. | ||
1487 | 57 | |||
1495 | 58 | buflen = struct.calcsize('hhhh') | 57 | buflen = struct.calcsize('hhhh') |
1496 | 59 | buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * buflen) | 58 | buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * buflen) |
1497 | 60 | |||
1498 | 61 | # ioctl returns a binary buffer that represents the struct | 59 | # ioctl returns a binary buffer that represents the struct |
1499 | 62 | # at the C level. We unpack it with struct.unpack. | 60 | # at the C level. We unpack it with struct.unpack. |
1503 | 63 | 61 | return tuple(struct.unpack('hhhh', buf)[:2]) | |
1504 | 64 | tuple = struct.unpack('hhhh', buf) | 62 | except Exception: |
1502 | 65 | except: | ||
1505 | 66 | # If anything went wrong, we give up and claim we don't know. | 63 | # If anything went wrong, we give up and claim we don't know. |
1506 | 67 | return None, None | 64 | return None, None |
1507 | 68 | |||
1508 | 69 | return tuple[0], tuple[1] | ||
1509 | 70 | 65 | ||
1510 | === added directory 'computerjanitorapp/tests' | |||
1511 | === added file 'computerjanitorapp/tests/__init__.py' | |||
1512 | === added file 'computerjanitorapp/tests/test_all.py' | |||
1513 | --- computerjanitorapp/tests/test_all.py 1970-01-01 00:00:00 +0000 | |||
1514 | +++ computerjanitorapp/tests/test_all.py 2010-03-10 21:40:30 +0000 | |||
1515 | @@ -0,0 +1,29 @@ | |||
1516 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
1517 | 2 | # | ||
1518 | 3 | # This program is free software: you can redistribute it and/or modify | ||
1519 | 4 | # it under the terms of the GNU General Public License as published by | ||
1520 | 5 | # the Free Software Foundation, version 3 of the License. | ||
1521 | 6 | # | ||
1522 | 7 | # This program is distributed in the hope that it will be useful, | ||
1523 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1524 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1525 | 10 | # GNU General Public License for more details. | ||
1526 | 11 | # | ||
1527 | 12 | # You should have received a copy of the GNU General Public License | ||
1528 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1529 | 14 | |||
1530 | 15 | """Test suite for Computer Janitor.""" | ||
1531 | 16 | |||
1532 | 17 | import unittest | ||
1533 | 18 | |||
1534 | 19 | from computerjanitorapp.tests import test_terminalsize | ||
1535 | 20 | from computerjanitorapp.tests import test_utilities | ||
1536 | 21 | from computerjanitord.tests.test_all import test_suite as cjd_suite | ||
1537 | 22 | |||
1538 | 23 | |||
1539 | 24 | def test_suite(): | ||
1540 | 25 | suite = unittest.TestSuite() | ||
1541 | 26 | suite.addTests(test_terminalsize.test_suite()) | ||
1542 | 27 | suite.addTests(test_utilities.test_suite()) | ||
1543 | 28 | suite.addTests(cjd_suite()) | ||
1544 | 29 | return suite | ||
1545 | 0 | 30 | ||
1546 | === renamed file 'computerjanitorapp/terminalsize_tests.py' => 'computerjanitorapp/tests/test_terminalsize.py' | |||
1547 | --- computerjanitorapp/terminalsize_tests.py 2008-09-22 21:39:57 +0000 | |||
1548 | +++ computerjanitorapp/tests/test_terminalsize.py 2010-03-10 21:40:30 +0000 | |||
1549 | @@ -1,5 +1,4 @@ | |||
1552 | 1 | # terminalsize_tests.py - unit tests for terminalsize.py | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. |
1551 | 2 | # Copyright (C) 2008 Canonical, Ltd. | ||
1553 | 3 | # | 2 | # |
1554 | 4 | # This program is free software: you can redistribute it and/or modify | 3 | # This program is free software: you can redistribute it and/or modify |
1555 | 5 | # it under the terms of the GNU General Public License as published by | 4 | # it under the terms of the GNU General Public License as published by |
1556 | @@ -13,23 +12,33 @@ | |||
1557 | 13 | # You should have received a copy of the GNU General Public License | 12 | # You should have received a copy of the GNU General Public License |
1558 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
1559 | 15 | 14 | ||
1560 | 15 | """Test calculation of terminal sizes.""" | ||
1561 | 16 | |||
1562 | 17 | from __future__ import absolute_import, unicode_literals | ||
1563 | 18 | |||
1564 | 19 | __metaclass__ = type | ||
1565 | 20 | __all__ = [ | ||
1566 | 21 | 'test_suite', | ||
1567 | 22 | ] | ||
1568 | 23 | |||
1569 | 16 | 24 | ||
1570 | 17 | import os | 25 | import os |
1571 | 18 | import unittest | 26 | import unittest |
1572 | 19 | 27 | ||
1579 | 20 | import terminalsize | 28 | from computerjanitorapp import terminalsize |
1580 | 21 | 29 | ||
1581 | 22 | 30 | ||
1582 | 23 | class GetTerminalSizeTests(unittest.TestCase): | 31 | class TestTerminalSize(unittest.TestCase): |
1583 | 24 | 32 | """Test calculation of terminal sizes.""" | |
1584 | 25 | def testReturnsUnknownWhenQueryingDevNull(self): | 33 | |
1585 | 34 | def test_returns_unknown_when_querying_dev_null(self): | ||
1586 | 26 | fd = os.open("/dev/null", os.O_RDONLY) | 35 | fd = os.open("/dev/null", os.O_RDONLY) |
1587 | 27 | rows, cols = terminalsize.get_terminal_size(fd) | 36 | rows, cols = terminalsize.get_terminal_size(fd) |
1588 | 28 | os.close(fd) | 37 | os.close(fd) |
1589 | 29 | self.assertEqual(rows, None) | 38 | self.assertEqual(rows, None) |
1590 | 30 | self.assertEqual(cols, None) | 39 | self.assertEqual(cols, None) |
1591 | 31 | 40 | ||
1593 | 32 | def testReturnsTwoIntegersWhenStdoutIsATerminal(self): | 41 | def test_returns_two_integers_when_stdout_is_a_terminal(self): |
1594 | 33 | # We only run this check if stdout is a terminal. | 42 | # We only run this check if stdout is a terminal. |
1595 | 34 | # Unfortunately, there is no sensible way of checking the values. | 43 | # Unfortunately, there is no sensible way of checking the values. |
1596 | 35 | # But that's OK, they're lumberjacks. | 44 | # But that's OK, they're lumberjacks. |
1597 | @@ -37,3 +46,9 @@ | |||
1598 | 37 | rows, cols = terminalsize.get_terminal_size(1) | 46 | rows, cols = terminalsize.get_terminal_size(1) |
1599 | 38 | self.assertEqual(type(rows), int) | 47 | self.assertEqual(type(rows), int) |
1600 | 39 | self.assertEqual(type(cols), int) | 48 | self.assertEqual(type(cols), int) |
1601 | 49 | |||
1602 | 50 | |||
1603 | 51 | def test_suite(): | ||
1604 | 52 | suite = unittest.TestSuite() | ||
1605 | 53 | suite.addTests(unittest.makeSuite(TestTerminalSize)) | ||
1606 | 54 | return suite | ||
1607 | 40 | 55 | ||
1608 | === added file 'computerjanitorapp/tests/test_utilities.py' | |||
1609 | --- computerjanitorapp/tests/test_utilities.py 1970-01-01 00:00:00 +0000 | |||
1610 | +++ computerjanitorapp/tests/test_utilities.py 2010-03-10 21:40:30 +0000 | |||
1611 | @@ -0,0 +1,71 @@ | |||
1612 | 1 | # Copyright (C) 2010 Canonical, Ltd. | ||
1613 | 2 | # | ||
1614 | 3 | # This program is free software: you can redistribute it and/or modify | ||
1615 | 4 | # it under the terms of the GNU General Public License as published by | ||
1616 | 5 | # the Free Software Foundation, version 3 of the License. | ||
1617 | 6 | # | ||
1618 | 7 | # This program is distributed in the hope that it will be useful, | ||
1619 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1620 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1621 | 10 | # GNU General Public License for more details. | ||
1622 | 11 | # | ||
1623 | 12 | # You should have received a copy of the GNU General Public License | ||
1624 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1625 | 14 | |||
1626 | 15 | """Test common utilities.""" | ||
1627 | 16 | |||
1628 | 17 | from __future__ import absolute_import, unicode_literals | ||
1629 | 18 | |||
1630 | 19 | __metaclass__ = type | ||
1631 | 20 | __all__ = [ | ||
1632 | 21 | 'test_suite', | ||
1633 | 22 | ] | ||
1634 | 23 | |||
1635 | 24 | |||
1636 | 25 | import os | ||
1637 | 26 | import unittest | ||
1638 | 27 | |||
1639 | 28 | from computerjanitorapp.utilities import format_size | ||
1640 | 29 | |||
1641 | 30 | |||
1642 | 31 | class TestUtilities(unittest.TestCase): | ||
1643 | 32 | """Test common utilities.""" | ||
1644 | 33 | |||
1645 | 34 | def test_format_negative(self): | ||
1646 | 35 | self.assertRaises(AssertionError, format_size, -1) | ||
1647 | 36 | |||
1648 | 37 | def test_format_zero(self): | ||
1649 | 38 | self.assertEqual(format_size(0), '0B') | ||
1650 | 39 | |||
1651 | 40 | def test_format_small(self): | ||
1652 | 41 | self.assertEqual(format_size(500), '500B') | ||
1653 | 42 | |||
1654 | 43 | def test_format_1k(self): | ||
1655 | 44 | self.assertEqual(format_size(1000), '1kB') | ||
1656 | 45 | |||
1657 | 46 | def test_format_smallish(self): | ||
1658 | 47 | self.assertEqual(format_size(500000), '500kB') | ||
1659 | 48 | |||
1660 | 49 | def test_format_1M(self): | ||
1661 | 50 | self.assertEqual(format_size(1000000), '1MB') | ||
1662 | 51 | |||
1663 | 52 | def test_format_mediumish(self): | ||
1664 | 53 | self.assertEqual(format_size(500000000), '500MB') | ||
1665 | 54 | |||
1666 | 55 | def test_format_1G(self): | ||
1667 | 56 | self.assertEqual(format_size(1000000000), '1GB') | ||
1668 | 57 | |||
1669 | 58 | def test_format_bigish(self): | ||
1670 | 59 | self.assertEqual(format_size(500000000000), '500GB') | ||
1671 | 60 | |||
1672 | 61 | def test_format_1T(self): | ||
1673 | 62 | self.assertEqual(format_size(1000000000000), '1TB') | ||
1674 | 63 | |||
1675 | 64 | def test_format_hugish(self): | ||
1676 | 65 | self.assertEqual(format_size(500000000000000), '>1TB') | ||
1677 | 66 | |||
1678 | 67 | |||
1679 | 68 | def test_suite(): | ||
1680 | 69 | suite = unittest.TestSuite() | ||
1681 | 70 | suite.addTests(unittest.makeSuite(TestUtilities)) | ||
1682 | 71 | return suite | ||
1683 | 0 | 72 | ||
1684 | === removed file 'computerjanitorapp/ui.py' | |||
1685 | --- computerjanitorapp/ui.py 2009-02-11 16:25:03 +0000 | |||
1686 | +++ computerjanitorapp/ui.py 1970-01-01 00:00:00 +0000 | |||
1687 | @@ -1,39 +0,0 @@ | |||
1688 | 1 | # ui.py - user interface interface class | ||
1689 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
1690 | 3 | # | ||
1691 | 4 | # This program is free software: you can redistribute it and/or modify | ||
1692 | 5 | # it under the terms of the GNU General Public License as published by | ||
1693 | 6 | # the Free Software Foundation, version 3 of the License. | ||
1694 | 7 | # | ||
1695 | 8 | # This program is distributed in the hope that it will be useful, | ||
1696 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1697 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1698 | 11 | # GNU General Public License for more details. | ||
1699 | 12 | # | ||
1700 | 13 | # You should have received a copy of the GNU General Public License | ||
1701 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1702 | 15 | |||
1703 | 16 | |||
1704 | 17 | import computerjanitor | ||
1705 | 18 | |||
1706 | 19 | |||
1707 | 20 | class UserInterface(object): | ||
1708 | 21 | |||
1709 | 22 | """This is the base class for user interfaces. | ||
1710 | 23 | |||
1711 | 24 | The user interface is in charge of obeying command line arguments, | ||
1712 | 25 | and interacting with the user. | ||
1713 | 26 | |||
1714 | 27 | The app and pm given to the constructor are stored in the app and | ||
1715 | 28 | pm attributes. | ||
1716 | 29 | |||
1717 | 30 | """ | ||
1718 | 31 | |||
1719 | 32 | def __init__(self, app, pm, mustberoot=True): | ||
1720 | 33 | self.pm = pm | ||
1721 | 34 | self.app = app | ||
1722 | 35 | self.mustberoot = mustberoot | ||
1723 | 36 | |||
1724 | 37 | def run(self, options, args): | ||
1725 | 38 | """Obey command line arguments in ARGS, and options in OPTIONS.""" | ||
1726 | 39 | raise computerjanitor.UnimplementedMethod(self.run) | ||
1727 | 40 | 0 | ||
1728 | === removed file 'computerjanitorapp/ui_cli_tests.py' | |||
1729 | --- computerjanitorapp/ui_cli_tests.py 2009-02-11 16:25:03 +0000 | |||
1730 | +++ computerjanitorapp/ui_cli_tests.py 1970-01-01 00:00:00 +0000 | |||
1731 | @@ -1,246 +0,0 @@ | |||
1732 | 1 | # ui_cli_tests.py - unit tests for ui_cli.py | ||
1733 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
1734 | 3 | # | ||
1735 | 4 | # This program is free software: you can redistribute it and/or modify | ||
1736 | 5 | # it under the terms of the GNU General Public License as published by | ||
1737 | 6 | # the Free Software Foundation, version 3 of the License. | ||
1738 | 7 | # | ||
1739 | 8 | # This program is distributed in the hope that it will be useful, | ||
1740 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1741 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1742 | 11 | # GNU General Public License for more details. | ||
1743 | 12 | # | ||
1744 | 13 | # You should have received a copy of the GNU General Public License | ||
1745 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1746 | 15 | |||
1747 | 16 | |||
1748 | 17 | import unittest | ||
1749 | 18 | |||
1750 | 19 | import computerjanitor | ||
1751 | 20 | import computerjanitorapp | ||
1752 | 21 | |||
1753 | 22 | |||
1754 | 23 | class MockCruft(object): | ||
1755 | 24 | |||
1756 | 25 | def __init__(self, name): | ||
1757 | 26 | self._name = name | ||
1758 | 27 | self.cleaned = False | ||
1759 | 28 | self.post_cleaned = False | ||
1760 | 29 | |||
1761 | 30 | def get_name(self): | ||
1762 | 31 | return self._name | ||
1763 | 32 | |||
1764 | 33 | def cleanup(self): | ||
1765 | 34 | self.cleaned = True | ||
1766 | 35 | |||
1767 | 36 | |||
1768 | 37 | class MockPlugin(object): | ||
1769 | 38 | |||
1770 | 39 | def __init__(self, crufts): | ||
1771 | 40 | self._crufts = crufts | ||
1772 | 41 | self.post_cleaned = False | ||
1773 | 42 | |||
1774 | 43 | def get_cruft(self): | ||
1775 | 44 | for cruft in self._crufts: | ||
1776 | 45 | yield cruft | ||
1777 | 46 | |||
1778 | 47 | def post_cleanup(self): | ||
1779 | 48 | self.post_cleaned = True | ||
1780 | 49 | |||
1781 | 50 | |||
1782 | 51 | class MockPluginManager(object): | ||
1783 | 52 | |||
1784 | 53 | def __init__(self, plugins): | ||
1785 | 54 | self._plugins = plugins | ||
1786 | 55 | |||
1787 | 56 | def get_plugins(self): | ||
1788 | 57 | return self._plugins | ||
1789 | 58 | |||
1790 | 59 | |||
1791 | 60 | class MockState(object): | ||
1792 | 61 | |||
1793 | 62 | def __init__(self, enabled): | ||
1794 | 63 | self._enabled = set(enabled) | ||
1795 | 64 | |||
1796 | 65 | def is_enabled(self, name): | ||
1797 | 66 | return name in self._enabled | ||
1798 | 67 | |||
1799 | 68 | def enable(self, name): | ||
1800 | 69 | if name not in self._enabled: | ||
1801 | 70 | self._enabled.add(name) | ||
1802 | 71 | |||
1803 | 72 | def disable(self, name): | ||
1804 | 73 | if name in self._enabled: | ||
1805 | 74 | self._enabled.remove(name) | ||
1806 | 75 | |||
1807 | 76 | def load(self, filename): | ||
1808 | 77 | self.load_filename = filename | ||
1809 | 78 | |||
1810 | 79 | def save(self, filename): | ||
1811 | 80 | self.save_filename = filename | ||
1812 | 81 | |||
1813 | 82 | |||
1814 | 83 | class MockOptionParser(object): | ||
1815 | 84 | |||
1816 | 85 | def __init__(self): | ||
1817 | 86 | self.help_printed = False | ||
1818 | 87 | |||
1819 | 88 | def print_help(self): | ||
1820 | 89 | self.help_printed = True | ||
1821 | 90 | |||
1822 | 91 | |||
1823 | 92 | class MockApplication(object): | ||
1824 | 93 | |||
1825 | 94 | def __init__(self, enabled): | ||
1826 | 95 | self.state = MockState(enabled) | ||
1827 | 96 | self.parser = MockOptionParser() | ||
1828 | 97 | |||
1829 | 98 | def verify_apt_cache(self): | ||
1830 | 99 | pass | ||
1831 | 100 | |||
1832 | 101 | def remove_whitelisted(self, crufts): | ||
1833 | 102 | return crufts | ||
1834 | 103 | |||
1835 | 104 | |||
1836 | 105 | class MockOptions(object): | ||
1837 | 106 | |||
1838 | 107 | def __init__(self): | ||
1839 | 108 | self.all = None | ||
1840 | 109 | self.state_file = "foo" | ||
1841 | 110 | self.no_act = None | ||
1842 | 111 | self.verbose = None | ||
1843 | 112 | |||
1844 | 113 | |||
1845 | 114 | class CommandLineUserInterfaceTests(unittest.TestCase): | ||
1846 | 115 | |||
1847 | 116 | def setUp(self): | ||
1848 | 117 | self.app = MockApplication(["foo"]) | ||
1849 | 118 | self.cruft_names = ["foo", "bar"] | ||
1850 | 119 | self.crufts = [MockCruft(name) for name in self.cruft_names] | ||
1851 | 120 | self.cruftdict = dict((c.get_name(), c) for c in self.crufts) | ||
1852 | 121 | self.plugin = MockPlugin(self.crufts) | ||
1853 | 122 | self.pm = MockPluginManager([self.plugin]) | ||
1854 | 123 | self.ui = computerjanitorapp.CommandLineUserInterface(self.app, | ||
1855 | 124 | self.pm, mustberoot=False) | ||
1856 | 125 | |||
1857 | 126 | self.options = MockOptions() | ||
1858 | 127 | |||
1859 | 128 | def testInsistsOnBeingRoot(self): | ||
1860 | 129 | self.ui.mustberoot = True | ||
1861 | 130 | self.assertRaises(computerjanitor.Exception, self.ui.run, None, | ||
1862 | 131 | None) | ||
1863 | 132 | |||
1864 | 133 | def testFindsTheRightCruft(self): | ||
1865 | 134 | self.assertEqual(self.ui.find_cruft(), self.crufts) | ||
1866 | 135 | |||
1867 | 136 | def testShowsTheRightCruftTheRightWay(self): | ||
1868 | 137 | |||
1869 | 138 | def mock_find_cruft(): | ||
1870 | 139 | return self.crufts | ||
1871 | 140 | |||
1872 | 141 | def mock_show_one_cruft(name, desc, state, width): | ||
1873 | 142 | output.append((name, state)) | ||
1874 | 143 | |||
1875 | 144 | output = [] | ||
1876 | 145 | self.ui.find_cruft = mock_find_cruft | ||
1877 | 146 | self.ui.show_one_cruft = mock_show_one_cruft | ||
1878 | 147 | self.ui.show_cruft(MockOptions(), None) | ||
1879 | 148 | self.assertEqual(output, | ||
1880 | 149 | sorted([("foo", "removable"), ("bar", "ignored")])) | ||
1881 | 150 | |||
1882 | 151 | def testIgnoresCruftCorrectly(self): | ||
1883 | 152 | self.ui.ignore(self.options, ["foo"]) | ||
1884 | 153 | self.assertFalse(self.app.state.is_enabled("foo")) | ||
1885 | 154 | |||
1886 | 155 | def testUnignoresCruftCorrectly(self): | ||
1887 | 156 | self.ui.unignore(self.options, ["bar"]) | ||
1888 | 157 | self.assert_(self.app.state.is_enabled("bar")) | ||
1889 | 158 | |||
1890 | 159 | def testCleansUpEnabledCruftWithDashDashAll(self): | ||
1891 | 160 | self.options.all = True | ||
1892 | 161 | self.ui.cleanup(self.options, []) | ||
1893 | 162 | self.assert_(self.cruftdict["foo"].cleaned) | ||
1894 | 163 | |||
1895 | 164 | def testDoesNotCleanUpEnabledCruftWithDashDashAllWhenNoActIsSet(self): | ||
1896 | 165 | self.options.all = True | ||
1897 | 166 | self.options.no_act = True | ||
1898 | 167 | self.ui.cleanup(self.options, []) | ||
1899 | 168 | self.assertFalse(self.cruftdict["foo"].cleaned) | ||
1900 | 169 | |||
1901 | 170 | def testCleansUpRequestedEnabledCruft(self): | ||
1902 | 171 | self.ui.cleanup(self.options, ["foo"]) | ||
1903 | 172 | self.assert_(self.cruftdict["foo"].cleaned) | ||
1904 | 173 | |||
1905 | 174 | def testDoesNotCleanUpDisaabledCruftWithDashDashAll(self): | ||
1906 | 175 | self.options.all = True | ||
1907 | 176 | self.ui.cleanup(self.options, []) | ||
1908 | 177 | self.assertFalse(self.cruftdict["bar"].cleaned) | ||
1909 | 178 | |||
1910 | 179 | def testCleansUpRequestedDisabledCruft(self): | ||
1911 | 180 | self.ui.cleanup(self.options, ["bar"]) | ||
1912 | 181 | self.assert_(self.cruftdict["bar"].cleaned) | ||
1913 | 182 | |||
1914 | 183 | def testRunsPostCleanup(self): | ||
1915 | 184 | self.ui.cleanup(self.options, []) | ||
1916 | 185 | self.assert_(self.plugin.post_cleaned) | ||
1917 | 186 | |||
1918 | 187 | def testDoesNotRunPostCleanupWhenNoActIsSet(self): | ||
1919 | 188 | self.options.no_act = True | ||
1920 | 189 | self.ui.cleanup(self.options, []) | ||
1921 | 190 | self.assertFalse(self.plugin.post_cleaned) | ||
1922 | 191 | |||
1923 | 192 | def testRaisesExceptionForUnknownCruft(self): | ||
1924 | 193 | self.assertRaises(computerjanitor.Exception, self.ui.cleanup, | ||
1925 | 194 | self.options, ["unknown"]) | ||
1926 | 195 | |||
1927 | 196 | def testHelpCallsParserPrintHelp(self): | ||
1928 | 197 | self.ui.help(None, None) | ||
1929 | 198 | self.assert_(self.app.parser.help_printed) | ||
1930 | 199 | |||
1931 | 200 | def setup_run(self): | ||
1932 | 201 | names = ["show_cruft", "cleanup", "ignore", "unignore", "help"] | ||
1933 | 202 | for name in names: | ||
1934 | 203 | method = getattr(self.ui, name) | ||
1935 | 204 | wrapper = lambda options, args, name=name: \ | ||
1936 | 205 | setattr(self, "operation", name) | ||
1937 | 206 | setattr(self.ui, name, wrapper) | ||
1938 | 207 | |||
1939 | 208 | def testRunCallsShowCruft(self): | ||
1940 | 209 | self.setup_run() | ||
1941 | 210 | self.ui.run(self.options, ["find"]) | ||
1942 | 211 | self.assertEqual(self.operation, "show_cruft") | ||
1943 | 212 | |||
1944 | 213 | def testRunCallsCleanup(self): | ||
1945 | 214 | self.setup_run() | ||
1946 | 215 | self.ui.run(self.options, ["cleanup"]) | ||
1947 | 216 | self.assertEqual(self.operation, "cleanup") | ||
1948 | 217 | |||
1949 | 218 | def testRunCallsIgnore(self): | ||
1950 | 219 | self.setup_run() | ||
1951 | 220 | self.ui.run(self.options, ["ignore"]) | ||
1952 | 221 | self.assertEqual(self.operation, "ignore") | ||
1953 | 222 | |||
1954 | 223 | def testRunCallsUnignore(self): | ||
1955 | 224 | self.setup_run() | ||
1956 | 225 | self.ui.run(self.options, ["unignore"]) | ||
1957 | 226 | self.assertEqual(self.operation, "unignore") | ||
1958 | 227 | |||
1959 | 228 | def testRunCallsHelp(self): | ||
1960 | 229 | self.setup_run() | ||
1961 | 230 | self.ui.run(self.options, ["help"]) | ||
1962 | 231 | self.assertEqual(self.operation, "help") | ||
1963 | 232 | |||
1964 | 233 | def testRunCallsHelpWhenThereAreNoArguments(self): | ||
1965 | 234 | self.setup_run() | ||
1966 | 235 | self.ui.run(self.options, []) | ||
1967 | 236 | self.assertEqual(self.operation, "help") | ||
1968 | 237 | |||
1969 | 238 | def testRaisesExceptionForUnknownCommand(self): | ||
1970 | 239 | self.assertRaises(computerjanitor.Exception, self.ui.run, | ||
1971 | 240 | self.options, ["yikes"]) | ||
1972 | 241 | |||
1973 | 242 | def testRunLoadsStateFromRightFile(self): | ||
1974 | 243 | self.setup_run() | ||
1975 | 244 | self.ui.run(self.options, ["ignore"]) | ||
1976 | 245 | self.assertEqual(self.app.state.load_filename, | ||
1977 | 246 | self.options.state_file) | ||
1978 | 247 | 0 | ||
1979 | === removed file 'computerjanitorapp/ui_gtk.py' | |||
1980 | --- computerjanitorapp/ui_gtk.py 2010-01-21 13:54:34 +0000 | |||
1981 | +++ computerjanitorapp/ui_gtk.py 1970-01-01 00:00:00 +0000 | |||
1982 | @@ -1,714 +0,0 @@ | |||
1983 | 1 | # ui_gtk.py - graphical user interface implemented using GTK+ | ||
1984 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
1985 | 3 | # | ||
1986 | 4 | # This program is free software: you can redistribute it and/or modify | ||
1987 | 5 | # it under the terms of the GNU General Public License as published by | ||
1988 | 6 | # the Free Software Foundation, version 3 of the License. | ||
1989 | 7 | # | ||
1990 | 8 | # This program is distributed in the hope that it will be useful, | ||
1991 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1992 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1993 | 11 | # GNU General Public License for more details. | ||
1994 | 12 | # | ||
1995 | 13 | # You should have received a copy of the GNU General Public License | ||
1996 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1997 | 15 | |||
1998 | 16 | |||
1999 | 17 | import logging | ||
2000 | 18 | import os | ||
2001 | 19 | import sys | ||
2002 | 20 | import threading | ||
2003 | 21 | import time | ||
2004 | 22 | import traceback | ||
2005 | 23 | |||
2006 | 24 | import computerjanitor | ||
2007 | 25 | import computerjanitorapp | ||
2008 | 26 | _ = computerjanitorapp.setup_gettext() | ||
2009 | 27 | |||
2010 | 28 | # We can't import gtk here, since that would mean it gets imported | ||
2011 | 29 | # even when we run in command line mode. Thus, we do the imports in | ||
2012 | 30 | # the class, when we know we need them. | ||
2013 | 31 | |||
2014 | 32 | |||
2015 | 33 | import os | ||
2016 | 34 | GLADE = os.environ.get('COMPUTER_JANITOR_GLADE', | ||
2017 | 35 | '/usr/share/computer-janitor/ComputerJanitor.ui') | ||
2018 | 36 | |||
2019 | 37 | |||
2020 | 38 | STATE_COL = 0 | ||
2021 | 39 | NAME_COL = 1 | ||
2022 | 40 | CRUFT_COL = 2 | ||
2023 | 41 | EXPANDED_COL = 3 | ||
2024 | 42 | SHOW_COL = 4 | ||
2025 | 43 | |||
2026 | 44 | |||
2027 | 45 | def ui(method): | ||
2028 | 46 | """Decorator function for UI methods. | ||
2029 | 47 | |||
2030 | 48 | Use @ui to decorate all methods in the controller class that | ||
2031 | 49 | call GTK stuff, to ensure correct locking. | ||
2032 | 50 | |||
2033 | 51 | """ | ||
2034 | 52 | |||
2035 | 53 | def new(self, *args, **kwargs): | ||
2036 | 54 | self.gtk.gdk.threads_enter() | ||
2037 | 55 | ret = method(self, *args, **kwargs) | ||
2038 | 56 | self.gtk.gdk.threads_leave() | ||
2039 | 57 | return ret | ||
2040 | 58 | |||
2041 | 59 | return new | ||
2042 | 60 | |||
2043 | 61 | |||
2044 | 62 | class ListUpdater(threading.Thread): | ||
2045 | 63 | |||
2046 | 64 | """Find cruft in the background, update user interface when done.""" | ||
2047 | 65 | |||
2048 | 66 | def __init__(self, ui): | ||
2049 | 67 | threading.Thread.__init__(self) | ||
2050 | 68 | self.ui = ui | ||
2051 | 69 | |||
2052 | 70 | def run(self): | ||
2053 | 71 | self.ui.finding = True | ||
2054 | 72 | self.ui.set_sensitive() | ||
2055 | 73 | status = self.ui.widgets['statusbar'] | ||
2056 | 74 | context_id = status.get_context_id('ListUpdater') | ||
2057 | 75 | status.push(context_id, _('Analyzing system...')) | ||
2058 | 76 | for plugin in self.ui.pm.get_plugins(): | ||
2059 | 77 | for cruft in self.ui.app.remove_whitelisted(plugin.get_cruft()): | ||
2060 | 78 | self.ui.add_cruft(cruft) | ||
2061 | 79 | status.pop(context_id) | ||
2062 | 80 | self.ui.finding = False | ||
2063 | 81 | self.ui.set_sensitive() | ||
2064 | 82 | self.ui.done_updating_list() | ||
2065 | 83 | |||
2066 | 84 | |||
2067 | 85 | class Cleaner(threading.Thread): | ||
2068 | 86 | |||
2069 | 87 | """Actually clean up the cruft.""" | ||
2070 | 88 | |||
2071 | 89 | def __init__(self, ui): | ||
2072 | 90 | threading.Thread.__init__(self) | ||
2073 | 91 | self.ui = ui | ||
2074 | 92 | self.gtk = self.ui.gtk | ||
2075 | 93 | self.crufts = ui.get_crufts() | ||
2076 | 94 | self.plugins = ui.pm.get_plugins() | ||
2077 | 95 | # progress reporting dialog/bar | ||
2078 | 96 | # FIXME: the progress bar is a bit silly, it takes | ||
2079 | 97 | # items like fix-fstab (0.1s) and deb_package (5min) | ||
2080 | 98 | # and assigns them the same slice - use a pulsing one | ||
2081 | 99 | # instead? | ||
2082 | 100 | self.dialog = ui.widgets['cleanup_dialog'] | ||
2083 | 101 | self.pbar = ui.widgets['cleanup_progressbar'] | ||
2084 | 102 | # add status bar context | ||
2085 | 103 | self.status = self.ui.widgets['statusbar'] | ||
2086 | 104 | self.context_id = self.status.get_context_id('ListUpdater') | ||
2087 | 105 | self.status.push(self.context_id, _('Cleaning up...')) | ||
2088 | 106 | # cancel button | ||
2089 | 107 | button = ui.widgets['cleanup_cancel'] | ||
2090 | 108 | button.connect('clicked', self.cancel) | ||
2091 | 109 | self.cancel_event = threading.Event() | ||
2092 | 110 | |||
2093 | 111 | def cancel(self, *args): | ||
2094 | 112 | self.cancel_event.set() | ||
2095 | 113 | |||
2096 | 114 | @ui | ||
2097 | 115 | def inc_done(self): | ||
2098 | 116 | self.done_work += 1.0 | ||
2099 | 117 | self.pbar.set_fraction(self.done_work / self.total_work) | ||
2100 | 118 | |||
2101 | 119 | @ui | ||
2102 | 120 | def start_reporting(self): | ||
2103 | 121 | self.total_work = len(self.crufts) + len(self.plugins) | ||
2104 | 122 | self.done_work = 0.0 | ||
2105 | 123 | self.pbar.set_fraction(0.0) | ||
2106 | 124 | |||
2107 | 125 | self.dialog.show() | ||
2108 | 126 | |||
2109 | 127 | @ui | ||
2110 | 128 | def end_reporting(self): | ||
2111 | 129 | self.pbar.set_fraction(1.0) | ||
2112 | 130 | self.dialog.hide() | ||
2113 | 131 | self.status.pop(self.context_id) | ||
2114 | 132 | |||
2115 | 133 | def run(self): | ||
2116 | 134 | self.ui.cleaning = True | ||
2117 | 135 | self.ui.set_sensitive() | ||
2118 | 136 | |||
2119 | 137 | self.start_reporting() | ||
2120 | 138 | |||
2121 | 139 | for cruft in self.crufts: | ||
2122 | 140 | if self.cancel_event.isSet(): | ||
2123 | 141 | break | ||
2124 | 142 | name = cruft.get_name() | ||
2125 | 143 | if self.ui.app.state.is_enabled(name): | ||
2126 | 144 | if self.ui.options.no_act: | ||
2127 | 145 | time.sleep(0.1) | ||
2128 | 146 | logging.info(_("Pretending to remove cruft: %s") % name) | ||
2129 | 147 | else: | ||
2130 | 148 | logging.info(_("Removing cruft: %s") % name) | ||
2131 | 149 | cruft.cleanup() | ||
2132 | 150 | self.inc_done() | ||
2133 | 151 | |||
2134 | 152 | for plugin in self.plugins: | ||
2135 | 153 | if self.cancel_event.isSet(): | ||
2136 | 154 | break | ||
2137 | 155 | if self.ui.options.no_act: | ||
2138 | 156 | logging.info(_("Pretending to post-cleanup: %s") % plugin) | ||
2139 | 157 | else: | ||
2140 | 158 | logging.info(_("Post-cleanup: %s") % plugin) | ||
2141 | 159 | error = None | ||
2142 | 160 | try: | ||
2143 | 161 | plugin.post_cleanup() | ||
2144 | 162 | except Exception, e: | ||
2145 | 163 | logging.debug(unicode(traceback.format_exc())) | ||
2146 | 164 | self.ui.show_error(_("Could not clean up properly"), | ||
2147 | 165 | unicode(e)) | ||
2148 | 166 | break | ||
2149 | 167 | self.inc_done() | ||
2150 | 168 | |||
2151 | 169 | # do not run it directly (thread deadlocks), register for running | ||
2152 | 170 | # in the gtk main thread instead | ||
2153 | 171 | self.ui.glib.timeout_add(100, self.ui.show_cruft) | ||
2154 | 172 | self.ui.cleaning = False | ||
2155 | 173 | |||
2156 | 174 | self.end_reporting() | ||
2157 | 175 | |||
2158 | 176 | |||
2159 | 177 | class GtkUserInterface(computerjanitorapp.UserInterface): | ||
2160 | 178 | |||
2161 | 179 | """The GTK+ user interface of Computer Janitor.""" | ||
2162 | 180 | |||
2163 | 181 | # This acts as the controller in MVC. To simplify binding of callbacks | ||
2164 | 182 | # to GTK signals, we follow a strict convention: any method named | ||
2165 | 183 | # on_WIDGETNAME_SIGNALNAME is a callback and will be bound automatically. | ||
2166 | 184 | # This way, we don't need do add each binding by hand, either in the | ||
2167 | 185 | # code or in the .glade file. See the find_and_bind_widgets method | ||
2168 | 186 | # for details. | ||
2169 | 187 | |||
2170 | 188 | def run(self, options, args): | ||
2171 | 189 | # This is where UI execution starts. | ||
2172 | 190 | |||
2173 | 191 | import gtk | ||
2174 | 192 | self.gtk = gtk | ||
2175 | 193 | |||
2176 | 194 | import gobject | ||
2177 | 195 | self.gobject = gobject | ||
2178 | 196 | |||
2179 | 197 | import glib | ||
2180 | 198 | self.glib = glib | ||
2181 | 199 | |||
2182 | 200 | self.options = options | ||
2183 | 201 | self.app.state.load(options.state_file) | ||
2184 | 202 | |||
2185 | 203 | builder = gtk.Builder() | ||
2186 | 204 | builder.set_translation_domain('computerjanitor') | ||
2187 | 205 | builder.add_from_file(GLADE) | ||
2188 | 206 | self.find_and_bind_widgets(builder) | ||
2189 | 207 | |||
2190 | 208 | self.store = self.create_cruft_store() | ||
2191 | 209 | self.name_cols = set() | ||
2192 | 210 | self.popup_menus = dict() | ||
2193 | 211 | self.create_column('unused_treeview', self.unused_filter) | ||
2194 | 212 | self.create_column('recommended_treeview', self.recommended_filter) | ||
2195 | 213 | self.create_column('optimize_treeview', self.optimize_filter) | ||
2196 | 214 | |||
2197 | 215 | self.show_previously_ignored = False | ||
2198 | 216 | |||
2199 | 217 | self.first_map = True | ||
2200 | 218 | |||
2201 | 219 | self.set_default_window_size() | ||
2202 | 220 | self.widgets['window'].show() | ||
2203 | 221 | |||
2204 | 222 | self.sort_crufts_by_current_order = self.sort_crufts_by_name | ||
2205 | 223 | |||
2206 | 224 | self.finding = False | ||
2207 | 225 | self.cleaning = False | ||
2208 | 226 | |||
2209 | 227 | # set thread switches interval to make it more UI friendly | ||
2210 | 228 | sys.setcheckinterval(0) | ||
2211 | 229 | gtk.gdk.threads_init() | ||
2212 | 230 | gtk.main() | ||
2213 | 231 | |||
2214 | 232 | def find_and_bind_widgets(self, builder): | ||
2215 | 233 | """Bind widgets and callbacks.""" | ||
2216 | 234 | import gtk | ||
2217 | 235 | self.widgets = {} | ||
2218 | 236 | for o in builder.get_objects(): | ||
2219 | 237 | if issubclass(type(o), gtk.Buildable): | ||
2220 | 238 | name = gtk.Buildable.get_name(o) | ||
2221 | 239 | self.widgets[name] = o | ||
2222 | 240 | for attr in dir(self): | ||
2223 | 241 | prefix = 'on_%s_' % name | ||
2224 | 242 | if attr.startswith(prefix): | ||
2225 | 243 | signal_name = attr[len(prefix):] | ||
2226 | 244 | method = getattr(self, attr) | ||
2227 | 245 | o.connect(signal_name, method) | ||
2228 | 246 | |||
2229 | 247 | |||
2230 | 248 | def set_default_window_size(self): | ||
2231 | 249 | w = self.widgets['window'] | ||
2232 | 250 | width = 900 | ||
2233 | 251 | height = 700 | ||
2234 | 252 | self.widgets['window'].set_default_size(width, height) | ||
2235 | 253 | |||
2236 | 254 | def create_cruft_store(self): | ||
2237 | 255 | """Create a gtk.ListStore for holding all the cruft.""" | ||
2238 | 256 | pairs = ((NAME_COL, self.gobject.TYPE_STRING), | ||
2239 | 257 | (STATE_COL, self.gobject.TYPE_BOOLEAN), | ||
2240 | 258 | (CRUFT_COL, self.gobject.TYPE_PYOBJECT), | ||
2241 | 259 | (EXPANDED_COL, self.gobject.TYPE_BOOLEAN), | ||
2242 | 260 | (SHOW_COL, self.gobject.TYPE_BOOLEAN)) | ||
2243 | 261 | column_types = [pair[1] for pair in sorted(pairs)] | ||
2244 | 262 | store = self.gtk.ListStore(*column_types) | ||
2245 | 263 | |||
2246 | 264 | return store | ||
2247 | 265 | |||
2248 | 266 | def get_crufts(self): | ||
2249 | 267 | def get(model, path, iter, crufts): | ||
2250 | 268 | cruft = model.get_value(iter, CRUFT_COL) | ||
2251 | 269 | crufts.append(cruft) | ||
2252 | 270 | crufts = [] | ||
2253 | 271 | self.store.foreach(get, crufts) | ||
2254 | 272 | return crufts | ||
2255 | 273 | |||
2256 | 274 | def sort_crufts(self, get_key): | ||
2257 | 275 | crufts = self.get_crufts() | ||
2258 | 276 | crufts = [(get_key(c), i, c) for i, c in enumerate(crufts)] | ||
2259 | 277 | crufts.sort() | ||
2260 | 278 | crufts = [i for key, i, c in crufts] | ||
2261 | 279 | self.store.reorder(crufts) | ||
2262 | 280 | |||
2263 | 281 | def sort_crufts_by_name(self): | ||
2264 | 282 | def get_key(cruft): | ||
2265 | 283 | return cruft.get_name() | ||
2266 | 284 | self.sort_crufts(get_key) | ||
2267 | 285 | |||
2268 | 286 | def sort_crufts_by_size(self): | ||
2269 | 287 | def get_key(cruft): | ||
2270 | 288 | return -cruft.get_disk_usage() | ||
2271 | 289 | self.sort_crufts(get_key) | ||
2272 | 290 | |||
2273 | 291 | def create_column(self, widget_name, filterfunc): | ||
2274 | 292 | """Add gtk.TreeViewColumn to the desired widget.""" | ||
2275 | 293 | treeview = self.widgets[widget_name] | ||
2276 | 294 | treeview.set_rules_hint(True) | ||
2277 | 295 | |||
2278 | 296 | toggle_cr = self.gtk.CellRendererToggle() | ||
2279 | 297 | toggle_cr.connect('toggled', self.toggled, treeview) | ||
2280 | 298 | toggle_cr.set_property("yalign", 0) | ||
2281 | 299 | toggle_col = self.gtk.TreeViewColumn() | ||
2282 | 300 | toggle_col.pack_start(toggle_cr) | ||
2283 | 301 | toggle_col.add_attribute(toggle_cr, 'active', STATE_COL) | ||
2284 | 302 | treeview.append_column(toggle_col) | ||
2285 | 303 | |||
2286 | 304 | name_cr = self.gtk.CellRendererText() | ||
2287 | 305 | name_cr.set_property("yalign", 0) | ||
2288 | 306 | import pango | ||
2289 | 307 | name_cr.set_property("wrap-mode", pango.WRAP_WORD) | ||
2290 | 308 | name_col = self.gtk.TreeViewColumn() | ||
2291 | 309 | name_col.pack_start(name_cr) | ||
2292 | 310 | name_col.add_attribute(name_cr, 'markup', NAME_COL) | ||
2293 | 311 | treeview.append_column(name_col) | ||
2294 | 312 | self.name_cols.add(name_col) | ||
2295 | 313 | |||
2296 | 314 | filter_store = self.store.filter_new() | ||
2297 | 315 | filter_store.set_visible_func(filterfunc) | ||
2298 | 316 | treeview.set_model(filter_store) | ||
2299 | 317 | |||
2300 | 318 | self.create_popup_menu_for_treeview(treeview) | ||
2301 | 319 | |||
2302 | 320 | def create_popup_menu_for_treeview(self, treeview): | ||
2303 | 321 | select_all = self.gtk.MenuItem(label='Select all') | ||
2304 | 322 | select_all.connect('activate', self.popup_menu_select_all, treeview) | ||
2305 | 323 | |||
2306 | 324 | unselect_all = self.gtk.MenuItem(label='Unselect all') | ||
2307 | 325 | unselect_all.connect('activate', self.popup_menu_unselect_all, treeview) | ||
2308 | 326 | |||
2309 | 327 | menu = self.gtk.Menu() | ||
2310 | 328 | menu.append(select_all) | ||
2311 | 329 | menu.append(unselect_all) | ||
2312 | 330 | menu.show_all() | ||
2313 | 331 | |||
2314 | 332 | self.popup_menus[treeview] = menu | ||
2315 | 333 | |||
2316 | 334 | def unused_filter(self, store, iter): | ||
2317 | 335 | cruft = store.get_value(iter, CRUFT_COL) | ||
2318 | 336 | shown = store.get_value(iter, SHOW_COL) | ||
2319 | 337 | return shown and isinstance(cruft, computerjanitor.PackageCruft) | ||
2320 | 338 | |||
2321 | 339 | def recommended_filter(self, store, iter): | ||
2322 | 340 | return False | ||
2323 | 341 | |||
2324 | 342 | def optimize_filter(self, store, iter): | ||
2325 | 343 | shown = store.get_value(iter, SHOW_COL) | ||
2326 | 344 | return (shown and | ||
2327 | 345 | not self.unused_filter(store, iter) and | ||
2328 | 346 | not self.recommended_filter(store, iter)) | ||
2329 | 347 | |||
2330 | 348 | def error_dialog(self, msg, secondary_msg=None): | ||
2331 | 349 | dialog = self.gtk.MessageDialog(parent=self.widgets["window"], | ||
2332 | 350 | type=self.gtk.MESSAGE_ERROR, | ||
2333 | 351 | buttons=self.gtk.BUTTONS_OK, | ||
2334 | 352 | message_format=msg) | ||
2335 | 353 | if secondary_msg: | ||
2336 | 354 | dialog.format_secondary_text(secondary_msg) | ||
2337 | 355 | |||
2338 | 356 | return dialog | ||
2339 | 357 | |||
2340 | 358 | @ui | ||
2341 | 359 | def show_error(self, msg, secondary_msg=None): | ||
2342 | 360 | dialog = self.error_dialog(msg, secondary_msg) | ||
2343 | 361 | dialog.show() | ||
2344 | 362 | dialog.run() | ||
2345 | 363 | dialog.hide() | ||
2346 | 364 | |||
2347 | 365 | def require_root(self): | ||
2348 | 366 | if os.getuid() != 0: | ||
2349 | 367 | dialog = self.error_dialog(_("Root access required."), | ||
2350 | 368 | _("You must run computer-janitor-gtk " | ||
2351 | 369 | "as root. Sorry.")) | ||
2352 | 370 | dialog.show() | ||
2353 | 371 | dialog.run() | ||
2354 | 372 | sys.exit(1) | ||
2355 | 373 | |||
2356 | 374 | def require_working_apt_cache(self): | ||
2357 | 375 | """ensure that the apt cache is in good state and error/exit | ||
2358 | 376 | otherwise | ||
2359 | 377 | """ | ||
2360 | 378 | try: | ||
2361 | 379 | self.app.verify_apt_cache() | ||
2362 | 380 | except computerjanitor.Exception, e: | ||
2363 | 381 | logging.error(unicode(traceback.format_exc())) | ||
2364 | 382 | dialog = self.error_dialog(str(e)) | ||
2365 | 383 | dialog.show() | ||
2366 | 384 | dialog.run() | ||
2367 | 385 | sys.exit(1) | ||
2368 | 386 | |||
2369 | 387 | def pulse(self): | ||
2370 | 388 | """pulse callback that shows a progress pulse until finding is False""" | ||
2371 | 389 | progress = self.widgets['progressbar_status'] | ||
2372 | 390 | if self.finding or self.cleaning: | ||
2373 | 391 | progress.show() | ||
2374 | 392 | progress.pulse() | ||
2375 | 393 | return True | ||
2376 | 394 | else: | ||
2377 | 395 | progress.hide() | ||
2378 | 396 | return False | ||
2379 | 397 | |||
2380 | 398 | def show_cruft(self): | ||
2381 | 399 | """clear the cruft store and update it again via a thread """ | ||
2382 | 400 | self.store.clear() | ||
2383 | 401 | # run as "daemon" thread to ensure that the main app exist | ||
2384 | 402 | # if the user presses "quit" before the ListUpdater thread | ||
2385 | 403 | # has finished | ||
2386 | 404 | t = ListUpdater(self) | ||
2387 | 405 | t.daemon = True | ||
2388 | 406 | t.start() | ||
2389 | 407 | # run a glib handler to shows a pulse progress | ||
2390 | 408 | self.glib.timeout_add(150, self.pulse) | ||
2391 | 409 | |||
2392 | 410 | @ui | ||
2393 | 411 | def add_cruft(self, cruft): | ||
2394 | 412 | state = self.app.state.is_enabled(cruft.get_name()) | ||
2395 | 413 | shown = (self.show_previously_ignored or | ||
2396 | 414 | not self.app.state.was_previously_ignored(cruft.get_name())) | ||
2397 | 415 | sort_index = 0 | ||
2398 | 416 | values = ((CRUFT_COL, cruft), | ||
2399 | 417 | (NAME_COL, cruft.get_shortname()), | ||
2400 | 418 | (STATE_COL, state), | ||
2401 | 419 | (EXPANDED_COL, False), | ||
2402 | 420 | (SHOW_COL, shown)) | ||
2403 | 421 | values = [pair[1] for pair in sorted(values)] | ||
2404 | 422 | self.store.append(values) | ||
2405 | 423 | self.sort_crufts_by_current_order() | ||
2406 | 424 | |||
2407 | 425 | @ui | ||
2408 | 426 | def done_updating_list(self): | ||
2409 | 427 | if not self.find_visible_cruft(): | ||
2410 | 428 | dialog = self.widgets['borednow_messagedialog'] | ||
2411 | 429 | dialog.show() | ||
2412 | 430 | dialog.run() | ||
2413 | 431 | dialog.hide() | ||
2414 | 432 | |||
2415 | 433 | def foreach_set_state(self, treeview, enabled): | ||
2416 | 434 | def set_state(model, path, iter, user_data): | ||
2417 | 435 | iter2 = model.convert_iter_to_child_iter(iter) | ||
2418 | 436 | cruft = self.store.get_value(iter2, CRUFT_COL) | ||
2419 | 437 | cruft_name = cruft.get_name() | ||
2420 | 438 | if enabled: | ||
2421 | 439 | self.app.state.enable(cruft_name) | ||
2422 | 440 | else: | ||
2423 | 441 | self.app.state.disable(cruft_name) | ||
2424 | 442 | self.store.set_value(iter2, STATE_COL, enabled) | ||
2425 | 443 | treeview.get_model().foreach(set_state, None) | ||
2426 | 444 | self.app.state.save(self.options.state_file) | ||
2427 | 445 | self.set_sensitive_unlocked() | ||
2428 | 446 | |||
2429 | 447 | def format_name(self, cruft): | ||
2430 | 448 | return self.gobject.markup_escape_text(cruft.get_shortname()) | ||
2431 | 449 | |||
2432 | 450 | def format_size(self, bytes): | ||
2433 | 451 | table = ((1000**3, "GB"), | ||
2434 | 452 | (1000**2, "MB"), | ||
2435 | 453 | (1000**1, "kB"), | ||
2436 | 454 | ( 1, "B")) | ||
2437 | 455 | for factor, unit in table: | ||
2438 | 456 | if bytes >= factor or factor == 1: | ||
2439 | 457 | return '%d %s' % (bytes / factor, unit) | ||
2440 | 458 | |||
2441 | 459 | def format_description(self, cruft): | ||
2442 | 460 | esc = self.gobject.markup_escape_text | ||
2443 | 461 | |||
2444 | 462 | lines = [esc(cruft.get_shortname())] | ||
2445 | 463 | |||
2446 | 464 | # FIXME: The action verbs should come from the crufts themselves. | ||
2447 | 465 | action_verbs = { | ||
2448 | 466 | computerjanitor.PackageCruft: 'uninstall', | ||
2449 | 467 | computerjanitor.FileCruft: 'remove', | ||
2450 | 468 | computerjanitor.MissingPackageCruft: 'install', | ||
2451 | 469 | } | ||
2452 | 470 | action_descriptions = { | ||
2453 | 471 | 'uninstall': _('Package will be <b>removed</b>.'), | ||
2454 | 472 | 'install': _('Package will be <b>installed</b>.'), | ||
2455 | 473 | 'remove': _('File will be <b>removed</b>.'), | ||
2456 | 474 | } | ||
2457 | 475 | action_verb = action_verbs.get(type(cruft)) | ||
2458 | 476 | if action_verb: | ||
2459 | 477 | lines += [action_descriptions[action_verb]] | ||
2460 | 478 | |||
2461 | 479 | size = cruft.get_disk_usage() | ||
2462 | 480 | if size is not None: | ||
2463 | 481 | lines += [_('Size: %s.') % self.format_size(size)] | ||
2464 | 482 | |||
2465 | 483 | desc = cruft.get_description() | ||
2466 | 484 | if desc: | ||
2467 | 485 | lines += ['', esc(desc)] | ||
2468 | 486 | |||
2469 | 487 | return '\n'.join(lines) | ||
2470 | 488 | |||
2471 | 489 | def toggle_long_description(self, treeview): | ||
2472 | 490 | """Toggle the showing of the long description of some cruft.""" | ||
2473 | 491 | |||
2474 | 492 | selection = treeview.get_selection() | ||
2475 | 493 | filtermodel, selected = selection.get_selected() | ||
2476 | 494 | if not selected: | ||
2477 | 495 | return | ||
2478 | 496 | model = filtermodel.get_model() | ||
2479 | 497 | iter = filtermodel.convert_iter_to_child_iter(selected) | ||
2480 | 498 | cruft = model.get_value(iter, CRUFT_COL) | ||
2481 | 499 | expanded = model.get_value(iter, EXPANDED_COL) | ||
2482 | 500 | expanded = not expanded | ||
2483 | 501 | model.set_value(iter, EXPANDED_COL, expanded) | ||
2484 | 502 | if expanded: | ||
2485 | 503 | value = self.format_description(cruft) | ||
2486 | 504 | else: | ||
2487 | 505 | value = self.format_name(cruft) | ||
2488 | 506 | model.set_value(iter, NAME_COL, value) | ||
2489 | 507 | |||
2490 | 508 | @ui | ||
2491 | 509 | def set_sensitive(self): | ||
2492 | 510 | self.set_sensitive_unlocked() | ||
2493 | 511 | |||
2494 | 512 | def set_sensitive_unlocked(self): | ||
2495 | 513 | do = self.widgets['do_button'] | ||
2496 | 514 | |||
2497 | 515 | names = ['unused_treeview', 'recommended_treeview', | ||
2498 | 516 | 'optimize_treeview'] | ||
2499 | 517 | cleanable_cruft = self.find_visible_cruft() | ||
2500 | 518 | do.set_sensitive(not self.finding and | ||
2501 | 519 | not self.cleaning and | ||
2502 | 520 | len(cleanable_cruft) > 0) | ||
2503 | 521 | |||
2504 | 522 | def find_visible_cruft(self): | ||
2505 | 523 | names = ['unused_treeview', 'recommended_treeview', | ||
2506 | 524 | 'optimize_treeview'] | ||
2507 | 525 | cleanable_cruft = [] | ||
2508 | 526 | for name in names: | ||
2509 | 527 | w = self.widgets[name] | ||
2510 | 528 | model = w.get_model() | ||
2511 | 529 | it = model.get_iter_first() | ||
2512 | 530 | while it is not None: | ||
2513 | 531 | cruft = model.get_value(it, CRUFT_COL) | ||
2514 | 532 | if self.app.state.is_enabled(cruft.get_name()): | ||
2515 | 533 | cleanable_cruft.append(cruft) | ||
2516 | 534 | it = model.iter_next(it) | ||
2517 | 535 | return cleanable_cruft | ||
2518 | 536 | |||
2519 | 537 | def really_cleanup(self): | ||
2520 | 538 | """Ask user if they really mean to clean up. | ||
2521 | 539 | |||
2522 | 540 | Be especially insistent (in wording) if they are removing | ||
2523 | 541 | packages. | ||
2524 | 542 | |||
2525 | 543 | """ | ||
2526 | 544 | |||
2527 | 545 | crufts = self.get_crufts() | ||
2528 | 546 | crufts = [c | ||
2529 | 547 | for c in crufts | ||
2530 | 548 | if self.app.state.is_enabled(c.get_name())] | ||
2531 | 549 | packages = [c | ||
2532 | 550 | for c in crufts | ||
2533 | 551 | if isinstance(c, computerjanitor.PackageCruft)] | ||
2534 | 552 | others = [c for c in crufts if c not in packages] | ||
2535 | 553 | |||
2536 | 554 | # The following messages are a bit vague, since we need to handle | ||
2537 | 555 | # cases where we remove packages, and don't remove packages, and | ||
2538 | 556 | # so on, given that "clean up cruft" is such a general concept. | ||
2539 | 557 | # My apologies to anyone who thinks this is confusing. Please | ||
2540 | 558 | # provide a patch that a) works b) is not specific to removing | ||
2541 | 559 | # packages. | ||
2542 | 560 | |||
2543 | 561 | msg = _('Are you sure you want to clean up?') | ||
2544 | 562 | dialog = self.gtk.MessageDialog(parent=self.widgets['window'], | ||
2545 | 563 | type=self.gtk.MESSAGE_WARNING, | ||
2546 | 564 | buttons=self.gtk.BUTTONS_NONE, | ||
2547 | 565 | message_format=msg) | ||
2548 | 566 | dialog.set_title(_('Clean up')) | ||
2549 | 567 | |||
2550 | 568 | if packages: | ||
2551 | 569 | msg = (_('You have chosen to <b>remove %d software packages.</b> ' | ||
2552 | 570 | 'Removing packages that are still needed can cause ' | ||
2553 | 571 | 'errors.') % | ||
2554 | 572 | len(packages)) | ||
2555 | 573 | else: | ||
2556 | 574 | msg = _('Do you want to continue?') | ||
2557 | 575 | dialog.format_secondary_markup(msg) | ||
2558 | 576 | |||
2559 | 577 | dialog.add_button(self.gtk.STOCK_CANCEL, self.gtk.RESPONSE_CLOSE) | ||
2560 | 578 | if others: | ||
2561 | 579 | dialog.add_button(_('Clean up'), self.gtk.RESPONSE_YES) | ||
2562 | 580 | else: | ||
2563 | 581 | dialog.add_button(_('Remove packages'), self.gtk.RESPONSE_YES) | ||
2564 | 582 | |||
2565 | 583 | dialog.show_all() | ||
2566 | 584 | response = dialog.run() | ||
2567 | 585 | dialog.hide() | ||
2568 | 586 | |||
2569 | 587 | return response == self.gtk.RESPONSE_YES | ||
2570 | 588 | |||
2571 | 589 | # The rest of this class is callbacks for GTK signals. | ||
2572 | 590 | |||
2573 | 591 | def on_about_menuitem_activate(self, *args): | ||
2574 | 592 | w = self.widgets['about_dialog'] | ||
2575 | 593 | w.set_name(_('Computer Janitor')) | ||
2576 | 594 | w.set_version(computerjanitorapp.VERSION) | ||
2577 | 595 | w.show() | ||
2578 | 596 | w.run() | ||
2579 | 597 | w.hide() | ||
2580 | 598 | |||
2581 | 599 | def on_do_button_clicked(self, *args): | ||
2582 | 600 | if self.really_cleanup(): | ||
2583 | 601 | Cleaner(self).start() | ||
2584 | 602 | self.glib.timeout_add(150, self.pulse) | ||
2585 | 603 | |||
2586 | 604 | def on_show_previously_ignored_toggled(self, menuitem): | ||
2587 | 605 | self.show_previously_ignored = menuitem.get_active() | ||
2588 | 606 | iter = self.store.get_iter_first() | ||
2589 | 607 | while iter: | ||
2590 | 608 | cruft = self.store.get_value(iter, CRUFT_COL) | ||
2591 | 609 | name = cruft.get_name() | ||
2592 | 610 | shown = (self.show_previously_ignored or | ||
2593 | 611 | not self.app.state.was_previously_ignored(name)) | ||
2594 | 612 | self.store.set_value(iter, SHOW_COL, shown) | ||
2595 | 613 | iter = self.store.iter_next(iter) | ||
2596 | 614 | |||
2597 | 615 | def treeview_size_allocate(self, treeview, *args): | ||
2598 | 616 | column = treeview.get_column(NAME_COL) | ||
2599 | 617 | name_cr = column.get_cell_renderers()[0] | ||
2600 | 618 | x, y, width, height = name_cr.get_size(treeview, None) | ||
2601 | 619 | width = column.get_width() | ||
2602 | 620 | name_cr.set_property("wrap-width", width) | ||
2603 | 621 | |||
2604 | 622 | on_unused_treeview_size_allocate = treeview_size_allocate | ||
2605 | 623 | on_recommended_treeview_size_allocate = treeview_size_allocate | ||
2606 | 624 | on_optimize_treeview_size_allocate = treeview_size_allocate | ||
2607 | 625 | |||
2608 | 626 | def on_quit_menuitem_activate(self, *args): | ||
2609 | 627 | self.gtk.main_quit() | ||
2610 | 628 | |||
2611 | 629 | on_window_delete_event = on_quit_menuitem_activate | ||
2612 | 630 | |||
2613 | 631 | def on_window_map_event(self, *args): | ||
2614 | 632 | if self.first_map: | ||
2615 | 633 | self.first_map = False | ||
2616 | 634 | self.require_root() | ||
2617 | 635 | self.require_working_apt_cache() | ||
2618 | 636 | self.show_cruft() | ||
2619 | 637 | |||
2620 | 638 | def treeview_button_press_event(self, treeview, event): | ||
2621 | 639 | # We handle mouse button presses ourselves so that we can either | ||
2622 | 640 | # toggle the long description (button 1, typically left) or | ||
2623 | 641 | # pop up a menu (button 3, typically right). | ||
2624 | 642 | # | ||
2625 | 643 | # This is slightly tricky and probably a source of bugs. | ||
2626 | 644 | # Oh well. | ||
2627 | 645 | |||
2628 | 646 | if event.button == 1: | ||
2629 | 647 | # Select row being clicked on. Also show/hide its long | ||
2630 | 648 | # description. But only if click is on the name | ||
2631 | 649 | # portion of the column, not the toggle button. | ||
2632 | 650 | x = int(event.x) | ||
2633 | 651 | y = int(event.y) | ||
2634 | 652 | time = event.time | ||
2635 | 653 | pathinfo = treeview.get_path_at_pos(x, y) | ||
2636 | 654 | if pathinfo: | ||
2637 | 655 | path, col, cellx, celly = pathinfo | ||
2638 | 656 | if col in self.name_cols: | ||
2639 | 657 | treeview.set_cursor(path, col, False) | ||
2640 | 658 | self.toggle_long_description(treeview) | ||
2641 | 659 | else: | ||
2642 | 660 | return False | ||
2643 | 661 | return True | ||
2644 | 662 | if event.button == 3: | ||
2645 | 663 | # Popup a menu | ||
2646 | 664 | x = int(event.x) | ||
2647 | 665 | y = int(event.y) | ||
2648 | 666 | time = event.time | ||
2649 | 667 | pathinfo = treeview.get_path_at_pos(x, y) | ||
2650 | 668 | if pathinfo: | ||
2651 | 669 | path, col, cellx, celly = pathinfo | ||
2652 | 670 | treeview.grab_focus() | ||
2653 | 671 | treeview.set_cursor(path, col, False) | ||
2654 | 672 | menu = self.popup_menus[treeview] | ||
2655 | 673 | menu.popup(None, None, None, event.button, time) | ||
2656 | 674 | return True | ||
2657 | 675 | |||
2658 | 676 | on_unused_treeview_button_press_event = treeview_button_press_event | ||
2659 | 677 | on_recommended_treeview_button_press_event = treeview_button_press_event | ||
2660 | 678 | on_optimize_treeview_button_press_event = treeview_button_press_event | ||
2661 | 679 | |||
2662 | 680 | def popup_menu_select_all(self, menuitem, treeview): | ||
2663 | 681 | self.foreach_set_state(treeview, True) | ||
2664 | 682 | |||
2665 | 683 | def popup_menu_unselect_all(self, menuitem, treeview): | ||
2666 | 684 | self.foreach_set_state(treeview, False) | ||
2667 | 685 | |||
2668 | 686 | def toggled(self, cr, path, treeview): | ||
2669 | 687 | model = treeview.get_model() | ||
2670 | 688 | filter_iter = model.get_iter(path) | ||
2671 | 689 | iter = model.convert_iter_to_child_iter(filter_iter) | ||
2672 | 690 | cruft = self.store.get_value(iter, CRUFT_COL) | ||
2673 | 691 | cruft_name = cruft.get_name() | ||
2674 | 692 | enabled = self.app.state.is_enabled(cruft.get_name()) | ||
2675 | 693 | enabled = not enabled | ||
2676 | 694 | if enabled: | ||
2677 | 695 | self.app.state.enable(cruft_name) | ||
2678 | 696 | else: | ||
2679 | 697 | self.app.state.disable(cruft_name) | ||
2680 | 698 | self.app.state.save(self.options.state_file) | ||
2681 | 699 | self.store.set_value(iter, STATE_COL, enabled) | ||
2682 | 700 | self.set_sensitive_unlocked() | ||
2683 | 701 | |||
2684 | 702 | def on_sort_by_name_toggled(self, menuitem): | ||
2685 | 703 | if menuitem.get_active(): | ||
2686 | 704 | self.sort_crufts_by_current_order = self.sort_crufts_by_name | ||
2687 | 705 | else: | ||
2688 | 706 | self.sort_crufts_by_current_order = self.sort_crufts_by_size | ||
2689 | 707 | self.sort_crufts_by_current_order() | ||
2690 | 708 | |||
2691 | 709 | def on_borednow_messagedialog_close(self, dialog): | ||
2692 | 710 | dialog.hide() | ||
2693 | 711 | |||
2694 | 712 | def on_borednow_messagedialog_response(self, dialog, response): | ||
2695 | 713 | dialog.hide() | ||
2696 | 714 | |||
2697 | 715 | 0 | ||
2698 | === removed file 'computerjanitorapp/ui_tests.py' | |||
2699 | --- computerjanitorapp/ui_tests.py 2009-02-11 16:25:03 +0000 | |||
2700 | +++ computerjanitorapp/ui_tests.py 1970-01-01 00:00:00 +0000 | |||
2701 | @@ -1,36 +0,0 @@ | |||
2702 | 1 | # ui_tests.py - unit tests for ui.py | ||
2703 | 2 | # Copyright (C) 2008, 2009 Canonical, Ltd. | ||
2704 | 3 | # | ||
2705 | 4 | # This program is free software: you can redistribute it and/or modify | ||
2706 | 5 | # it under the terms of the GNU General Public License as published by | ||
2707 | 6 | # the Free Software Foundation, version 3 of the License. | ||
2708 | 7 | # | ||
2709 | 8 | # This program is distributed in the hope that it will be useful, | ||
2710 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2711 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2712 | 11 | # GNU General Public License for more details. | ||
2713 | 12 | # | ||
2714 | 13 | # You should have received a copy of the GNU General Public License | ||
2715 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2716 | 15 | |||
2717 | 16 | |||
2718 | 17 | import unittest | ||
2719 | 18 | |||
2720 | 19 | import computerjanitor | ||
2721 | 20 | import computerjanitorapp | ||
2722 | 21 | |||
2723 | 22 | |||
2724 | 23 | class UserInterfaceTests(unittest.TestCase): | ||
2725 | 24 | |||
2726 | 25 | def setUp(self): | ||
2727 | 26 | self.ui = computerjanitorapp.UserInterface("app", "pm") | ||
2728 | 27 | |||
2729 | 28 | def testReturnsCorrectApp(self): | ||
2730 | 29 | self.assertEqual(self.ui.app, "app") | ||
2731 | 30 | |||
2732 | 31 | def testReturnsCorrectPluginManager(self): | ||
2733 | 32 | self.assertEqual(self.ui.pm, "pm") | ||
2734 | 33 | |||
2735 | 34 | def testRunRaisesUnimplemented(self): | ||
2736 | 35 | self.assertRaises(computerjanitor.UnimplementedMethod, | ||
2737 | 36 | self.ui.run, None, None) | ||
2738 | 37 | 0 | ||
2739 | === added file 'computerjanitorapp/uigtk.py' | |||
2740 | --- computerjanitorapp/uigtk.py 1970-01-01 00:00:00 +0000 | |||
2741 | +++ computerjanitorapp/uigtk.py 2010-03-10 21:40:30 +0000 | |||
2742 | @@ -0,0 +1,515 @@ | |||
2743 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
2744 | 2 | # | ||
2745 | 3 | # This program is free software: you can redistribute it and/or modify | ||
2746 | 4 | # it under the terms of the GNU General Public License as published by | ||
2747 | 5 | # the Free Software Foundation, version 3 of the License. | ||
2748 | 6 | # | ||
2749 | 7 | # This program is distributed in the hope that it will be useful, | ||
2750 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2751 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2752 | 10 | # GNU General Public License for more details. | ||
2753 | 11 | # | ||
2754 | 12 | # You should have received a copy of the GNU General Public License | ||
2755 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2756 | 14 | |||
2757 | 15 | """Gtk user interface for computer janitor.""" | ||
2758 | 16 | |||
2759 | 17 | from __future__ import absolute_import, unicode_literals | ||
2760 | 18 | |||
2761 | 19 | __metaclass__ = type | ||
2762 | 20 | __all__ = [ | ||
2763 | 21 | 'UserInterface', | ||
2764 | 22 | ] | ||
2765 | 23 | |||
2766 | 24 | |||
2767 | 25 | import os | ||
2768 | 26 | import gtk | ||
2769 | 27 | import dbus | ||
2770 | 28 | import glib | ||
2771 | 29 | import pango | ||
2772 | 30 | import gobject | ||
2773 | 31 | |||
2774 | 32 | from computerjanitorapp import __version__, setup_gettext | ||
2775 | 33 | from computerjanitorapp.areyousure import AreYouSure | ||
2776 | 34 | from computerjanitorapp.store import ( | ||
2777 | 35 | ListStoreColumns, Store, optimize, unused) | ||
2778 | 36 | from computerjanitorapp.utilities import format_size | ||
2779 | 37 | from computerjanitord.service import DBUS_INTERFACE_NAME | ||
2780 | 38 | |||
2781 | 39 | _ = setup_gettext() | ||
2782 | 40 | |||
2783 | 41 | GLADE = '/usr/share/computer-janitor/ComputerJanitor.ui' | ||
2784 | 42 | ROOT_WIDTH = 900 | ||
2785 | 43 | ROOT_HEIGHT = 500 | ||
2786 | 44 | NL = '\n' | ||
2787 | 45 | |||
2788 | 46 | # Keys are lower-cased cruft types, i.e. class name of cruft instances. | ||
2789 | 47 | ACTIONS = dict( | ||
2790 | 48 | packagecruft=_('Package will be <b>removed</b>.'), | ||
2791 | 49 | filecruft=_('Package will be <b>installed</b>.'), | ||
2792 | 50 | missingpackagecruft=_('File will be <b>removed</b>.'), | ||
2793 | 51 | ) | ||
2794 | 52 | |||
2795 | 53 | SENSITIVE_WIDGETS = ( | ||
2796 | 54 | 'do_button', | ||
2797 | 55 | 'optimize_treeview', | ||
2798 | 56 | 'quit_menuitem', | ||
2799 | 57 | 'show_previously_ignored', | ||
2800 | 58 | 'sort_by_name', | ||
2801 | 59 | 'sort_by_size', | ||
2802 | 60 | 'unused_treeview', | ||
2803 | 61 | ) | ||
2804 | 62 | |||
2805 | 63 | |||
2806 | 64 | class UserInterface: | ||
2807 | 65 | """Implementation of the Gtk user interface.""" | ||
2808 | 66 | |||
2809 | 67 | def __init__(self): | ||
2810 | 68 | # Connect to the dbus service. | ||
2811 | 69 | system_bus = dbus.SystemBus() | ||
2812 | 70 | proxy = system_bus.get_object(DBUS_INTERFACE_NAME, '/') | ||
2813 | 71 | self.janitord = dbus.Interface( | ||
2814 | 72 | proxy, dbus_interface=DBUS_INTERFACE_NAME) | ||
2815 | 73 | # Create the model on which the TreeViews will be based. | ||
2816 | 74 | self.store = Store(self.janitord) | ||
2817 | 75 | self.popup_menus = {} | ||
2818 | 76 | self.cruft_name_columns = set() | ||
2819 | 77 | # Sort by name by default. | ||
2820 | 78 | self._sort_key = self._by_name | ||
2821 | 79 | # Work is happening asynchronously on the dbus service. | ||
2822 | 80 | self.working = False | ||
2823 | 81 | # Connect to the signal the server will emit when cleaning up. | ||
2824 | 82 | self.janitord.connect_to_signal('cleanup_status', self._clean_working) | ||
2825 | 83 | |||
2826 | 84 | def run(self): | ||
2827 | 85 | """Set up the widgets and run the main loop.""" | ||
2828 | 86 | builder = gtk.Builder() | ||
2829 | 87 | builder.set_translation_domain('computerjanitor') | ||
2830 | 88 | # Load the glade ui file, which can be overridden from the environment | ||
2831 | 89 | # for testing purposes. | ||
2832 | 90 | glade_file = os.environ.get('COMPUTER_JANITOR_GLADE', GLADE) | ||
2833 | 91 | builder.add_from_file(glade_file) | ||
2834 | 92 | # Bind widgets to callbacks. | ||
2835 | 93 | self.find_and_bind_widgets(builder) | ||
2836 | 94 | # Do the initial search for cruft and set up the TreeView model. | ||
2837 | 95 | self.store.find_cruft() | ||
2838 | 96 | self.sort_cruft() | ||
2839 | 97 | # Now hook the TreeViews up to there view of the model. | ||
2840 | 98 | self.create_column('unused_treeview', unused) | ||
2841 | 99 | self.create_column('optimize_treeview', optimize) | ||
2842 | 100 | # Set the dimensions of the root window. | ||
2843 | 101 | root = self.widgets['window'] | ||
2844 | 102 | root.set_default_size(ROOT_WIDTH, ROOT_HEIGHT) | ||
2845 | 103 | # Map the root window and go! | ||
2846 | 104 | root.show() | ||
2847 | 105 | gtk.main() | ||
2848 | 106 | |||
2849 | 107 | def find_and_bind_widgets(self, builder): | ||
2850 | 108 | """Bind widgets and callbacks.""" | ||
2851 | 109 | # Start by extracting all the bindable widgets from the ui builder, | ||
2852 | 110 | # keeping track of them as mapped to their name. | ||
2853 | 111 | self.widgets = {} | ||
2854 | 112 | for ui_object in builder.get_objects(): | ||
2855 | 113 | if issubclass(type(ui_object), gtk.Buildable): | ||
2856 | 114 | widget_name = gtk.Buildable.get_name(ui_object) | ||
2857 | 115 | self.widgets[widget_name] = ui_object | ||
2858 | 116 | # Search through the attributes of this instance looking for | ||
2859 | 117 | # callbacks for this widget. We use the naming convention | ||
2860 | 118 | # 'on_<widget>_<signal>' for such methods. Both the widget | ||
2861 | 119 | # name and signal (or event) can contain underscores. Connect | ||
2862 | 120 | # the widget to the callback. | ||
2863 | 121 | prefix = 'on_{0}_'.format(widget_name) | ||
2864 | 122 | for method_name in dir(self): | ||
2865 | 123 | if method_name.startswith(prefix): | ||
2866 | 124 | signal_name = method_name[len(prefix):] | ||
2867 | 125 | ui_object.connect( | ||
2868 | 126 | signal_name, getattr(self, method_name)) | ||
2869 | 127 | |||
2870 | 128 | def create_column(self, widget_name, filter_func): | ||
2871 | 129 | """Set up a column in the named TreeView.""" | ||
2872 | 130 | treeview = self.widgets[widget_name] | ||
2873 | 131 | # XXX 2010-03-04 barry: This is a bit of an abuse because it's | ||
2874 | 132 | # supposed to specify whether users are to read across the rows. As a | ||
2875 | 133 | # side effect it renders the columns with alternating row colors, but | ||
2876 | 134 | # that's not it's primary function. | ||
2877 | 135 | treeview.set_rules_hint(True) | ||
2878 | 136 | # Each TreeView contains two columns. The leftmost one is a toggle | ||
2879 | 137 | # that when select tells c-j to act on that cruft. Deselecting the | ||
2880 | 138 | # toggle ignores the package for next time. | ||
2881 | 139 | toggle_cr = gtk.CellRendererToggle() | ||
2882 | 140 | toggle_cr.connect('toggled', self._toggled, treeview) | ||
2883 | 141 | toggle_cr.set_property('yalign', 0) | ||
2884 | 142 | toggle_col = gtk.TreeViewColumn() | ||
2885 | 143 | toggle_col.pack_start(toggle_cr) | ||
2886 | 144 | toggle_col.add_attribute(toggle_cr, 'active', ListStoreColumns.active) | ||
2887 | 145 | treeview.append_column(toggle_col) | ||
2888 | 146 | # The rightmost column contains the details of the cruft. It will | ||
2889 | 147 | # always contain the cruft name and can be expanded to display cruft | ||
2890 | 148 | # details. Tell the column to get its toggle's active state from the | ||
2891 | 149 | # model. | ||
2892 | 150 | name_cr = gtk.CellRendererText() | ||
2893 | 151 | name_cr.set_property('yalign', 0) | ||
2894 | 152 | name_cr.set_property('wrap-mode', pango.WRAP_WORD) | ||
2895 | 153 | name_col = gtk.TreeViewColumn() | ||
2896 | 154 | name_col.pack_start(name_cr) | ||
2897 | 155 | name_col.add_attribute(name_cr, 'markup', ListStoreColumns.text) | ||
2898 | 156 | treeview.append_column(name_col) | ||
2899 | 157 | self.cruft_name_columns.add(name_col) | ||
2900 | 158 | # The individual crufts may or may not be visible in this TreeView. | ||
2901 | 159 | # It's the filter function that controls this, so set that now. | ||
2902 | 160 | filter_store = self.store.filter_new() | ||
2903 | 161 | filter_store.set_visible_func(filter_func) | ||
2904 | 162 | treeview.set_model(filter_store) | ||
2905 | 163 | # Each TreeView has a popup menu for select or deselecting all visible | ||
2906 | 164 | # cruft. | ||
2907 | 165 | self.create_popup_menu_for_treeview(treeview) | ||
2908 | 166 | |||
2909 | 167 | def create_popup_menu_for_treeview(self, treeview): | ||
2910 | 168 | """The tree views have a popup menu to select/deselect everything. | ||
2911 | 169 | |||
2912 | 170 | :param treeview: The `TreeView` to attach the menu to. | ||
2913 | 171 | """ | ||
2914 | 172 | select_all = gtk.MenuItem(label='Select all') | ||
2915 | 173 | select_all.connect('activate', self.popup_menu_select_all, treeview) | ||
2916 | 174 | unselect_all = gtk.MenuItem(label='Unselect all') | ||
2917 | 175 | unselect_all.connect('activate', | ||
2918 | 176 | self.popup_menu_unselect_all, treeview) | ||
2919 | 177 | menu = gtk.Menu() | ||
2920 | 178 | menu.append(select_all) | ||
2921 | 179 | menu.append(unselect_all) | ||
2922 | 180 | menu.show_all() | ||
2923 | 181 | self.popup_menus[treeview] = menu | ||
2924 | 182 | |||
2925 | 183 | def _by_name(self, cruft_name): | ||
2926 | 184 | """Sort by cruft name.""" | ||
2927 | 185 | return cruft_name | ||
2928 | 186 | |||
2929 | 187 | def _by_size(self, cruft_name): | ||
2930 | 188 | """Sort by cruft size, from largest to smallest.""" | ||
2931 | 189 | # Return negative size to sort from largest to smallest. | ||
2932 | 190 | return -self.janitord.get_details(cruft_name)[1] | ||
2933 | 191 | |||
2934 | 192 | def sort_cruft(self): | ||
2935 | 193 | """Sort the cruft displays, either by name or size.""" | ||
2936 | 194 | # The way reordering (not technically 'sorting') works in gtk is that | ||
2937 | 195 | # you give a list of integer indexes to the the ListStore. These | ||
2938 | 196 | # indexes are in sorted order, and refer to the pre-sort indexes of | ||
2939 | 197 | # the items in the store. IOW, the ListStore knows that if the first | ||
2940 | 198 | # integer in the list is 7, it will move the 7th item to the top. | ||
2941 | 199 | # | ||
2942 | 200 | # Start by getting the indexes and names of the currenly sorted cruft. | ||
2943 | 201 | # We'll fill this list with 2-tuples of the format: | ||
2944 | 202 | # (sort-key, current-index). | ||
2945 | 203 | cruft_data = [] | ||
2946 | 204 | def get(model, path, iter, crufts): | ||
2947 | 205 | cruft_name = model.get_value(iter, ListStoreColumns.name) | ||
2948 | 206 | crufts.append((self._sort_key(cruft_name), len(crufts))) | ||
2949 | 207 | # Continue iterating. | ||
2950 | 208 | return False | ||
2951 | 209 | self.store.foreach(get, cruft_data) | ||
2952 | 210 | cruft_data.sort() | ||
2953 | 211 | cruft_indexes = [index for key, index in cruft_data] | ||
2954 | 212 | self.store.reorder(cruft_indexes) | ||
2955 | 213 | |||
2956 | 214 | def get_cleanable_cruft(self): | ||
2957 | 215 | """Return the list of cleanable cruft candidates. | ||
2958 | 216 | |||
2959 | 217 | :return: List of cleanable cruft. | ||
2960 | 218 | :rtype: list of 2-tuples of (cruft_name, is_package_cruft) | ||
2961 | 219 | """ | ||
2962 | 220 | cleanable_cruft = [] | ||
2963 | 221 | def collect(model, path, iter, crufts): | ||
2964 | 222 | # Only clean up active cruft, i.e. those that are specifically | ||
2965 | 223 | # checked as ready for cleaning. | ||
2966 | 224 | cruft_active = model.get_value(iter, ListStoreColumns.active) | ||
2967 | 225 | if cruft_active: | ||
2968 | 226 | cruft_name = model.get_value(iter, ListStoreColumns.name) | ||
2969 | 227 | cruft_is_package_cruft = model.get_value( | ||
2970 | 228 | iter, ListStoreColumns.is_package_cruft) | ||
2971 | 229 | crufts.append((cruft_name, cruft_is_package_cruft)) | ||
2972 | 230 | # Continue iterating. | ||
2973 | 231 | return False | ||
2974 | 232 | self.store.foreach(collect, cleanable_cruft) | ||
2975 | 233 | return cleanable_cruft | ||
2976 | 234 | |||
2977 | 235 | def toggle_long_description(self, treeview): | ||
2978 | 236 | """Toggle the currently selected cruft's long description. | ||
2979 | 237 | |||
2980 | 238 | :param treeview: The TreeView | ||
2981 | 239 | """ | ||
2982 | 240 | selection = treeview.get_selection() | ||
2983 | 241 | filter_model, selected = selection.get_selected() | ||
2984 | 242 | if not selected: | ||
2985 | 243 | return | ||
2986 | 244 | model = filter_model.get_model() | ||
2987 | 245 | iter = filter_model.convert_iter_to_child_iter(selected) | ||
2988 | 246 | cruft_name = model.get_value(iter, ListStoreColumns.name) | ||
2989 | 247 | expanded = model.get_value(iter, ListStoreColumns.expanded) | ||
2990 | 248 | shortname = model.get_value(iter, ListStoreColumns.short_name) | ||
2991 | 249 | if expanded: | ||
2992 | 250 | # Collapse it. | ||
2993 | 251 | value = gobject.markup_escape_text(shortname) | ||
2994 | 252 | else: | ||
2995 | 253 | cruft_type, size = self.janitord.get_details(cruft_name) | ||
2996 | 254 | lines = [gobject.markup_escape_text(shortname)] | ||
2997 | 255 | action = ACTIONS.get(cruft_type.lower()) | ||
2998 | 256 | if action is not None: | ||
2999 | 257 | lines.append(action) | ||
3000 | 258 | lines.append('Size: {0}'.format(format_size(size))) | ||
3001 | 259 | lines.append('') | ||
3002 | 260 | description = self.janitord.get_description(cruft_name) | ||
3003 | 261 | lines.append(gobject.markup_escape_text(description)) | ||
3004 | 262 | value = NL.join(lines) | ||
3005 | 263 | model.set_value(iter, ListStoreColumns.text, value) | ||
3006 | 264 | model.set_value(iter, ListStoreColumns.expanded, not expanded) | ||
3007 | 265 | |||
3008 | 266 | def desensitize(self): | ||
3009 | 267 | """Make certain ui elements insensitive during work.""" | ||
3010 | 268 | for widget in SENSITIVE_WIDGETS: | ||
3011 | 269 | self.widgets[widget].set_sensitive(False) | ||
3012 | 270 | |||
3013 | 271 | def sensitize(self): | ||
3014 | 272 | """Make certain ui elements sensitive after work.""" | ||
3015 | 273 | for widget in SENSITIVE_WIDGETS: | ||
3016 | 274 | self.widgets[widget].set_sensitive(True) | ||
3017 | 275 | |||
3018 | 276 | # Popup menu support. | ||
3019 | 277 | |||
3020 | 278 | def popup_menu_foreach_set_state(self, treeview, enabled): | ||
3021 | 279 | """Set the state of the cruft 'active' flag for all cruft. | ||
3022 | 280 | |||
3023 | 281 | :param treeview: The `TreeView` to set cruft state on. | ||
3024 | 282 | :param enabled: The new state flag for all cruft. True means enabled. | ||
3025 | 283 | :type enabled: bool | ||
3026 | 284 | """ | ||
3027 | 285 | def set_state(model, path, iter, user_data): | ||
3028 | 286 | # Set the state on an individual piece of cruft. Start by | ||
3029 | 287 | # changing the state of the cruft on the dbus service. | ||
3030 | 288 | child_iter = model.convert_iter_to_child_iter(iter) | ||
3031 | 289 | cruft_name = self.store.get_value( | ||
3032 | 290 | child_iter, ListStoreColumns.name) | ||
3033 | 291 | if enabled: | ||
3034 | 292 | self.janitord.unignore(cruft_name) | ||
3035 | 293 | else: | ||
3036 | 294 | self.janitord.ignore(cruft_name) | ||
3037 | 295 | # Now set the active state in the model. | ||
3038 | 296 | self.store.set_value(child_iter, ListStoreColumns.active, enabled) | ||
3039 | 297 | treeview.get_model().foreach(set_state, None) | ||
3040 | 298 | # Save the updated state on the dbus service. | ||
3041 | 299 | self.janitord.save() | ||
3042 | 300 | |||
3043 | 301 | def popup_menu_select_all(self, menuitem, treeview): | ||
3044 | 302 | self.popup_menu_foreach_set_state(treeview, True) | ||
3045 | 303 | |||
3046 | 304 | def popup_menu_unselect_all(self, menuitem, treeview): | ||
3047 | 305 | self.popup_menu_foreach_set_state(treeview, False) | ||
3048 | 306 | |||
3049 | 307 | # Progress bar | ||
3050 | 308 | |||
3051 | 309 | def pulse(self): | ||
3052 | 310 | """Progress bar callback, showing that something is happening.""" | ||
3053 | 311 | progress = self.widgets['progressbar_status'] | ||
3054 | 312 | if self.working: | ||
3055 | 313 | progress.show() | ||
3056 | 314 | progress.pulse() | ||
3057 | 315 | return True | ||
3058 | 316 | else: | ||
3059 | 317 | # All done. Hide the progress bar, make the ui elements sensitive | ||
3060 | 318 | # again, update the store, and kill the timer. | ||
3061 | 319 | progress.hide() | ||
3062 | 320 | self.store.clear() | ||
3063 | 321 | self.store.find_cruft() | ||
3064 | 322 | self.sensitize() | ||
3065 | 323 | return False | ||
3066 | 324 | |||
3067 | 325 | def _clean_working(self, cruft): | ||
3068 | 326 | """dbus signal handler; the 'clean' operation is in progress. | ||
3069 | 327 | |||
3070 | 328 | :param done: The cruft that is being cleaned up. | ||
3071 | 329 | :type done: string | ||
3072 | 330 | """ | ||
3073 | 331 | # Just mark the status here. The progress bar pulsar will handle | ||
3074 | 332 | # doing the actual work. | ||
3075 | 333 | self.working = (cruft != '') | ||
3076 | 334 | if self.working: | ||
3077 | 335 | self.widgets['progressbar_status'].set_text( | ||
3078 | 336 | _('Processing {0}').format(cruft)) | ||
3079 | 337 | |||
3080 | 338 | # Callbacks | ||
3081 | 339 | |||
3082 | 340 | def _toggled(self, widget, path, treeview): | ||
3083 | 341 | """Handle the toggle button in a TreeView cell. | ||
3084 | 342 | |||
3085 | 343 | :param widget: The CellRendererToggle | ||
3086 | 344 | :param path: The cell's path. | ||
3087 | 345 | :param treeview: The TreeView | ||
3088 | 346 | """ | ||
3089 | 347 | # Find out which cruft's toggle was clicked. | ||
3090 | 348 | model = treeview.get_model() | ||
3091 | 349 | filter_iter = model.get_iter(path) | ||
3092 | 350 | child_iter = model.convert_iter_to_child_iter(filter_iter) | ||
3093 | 351 | cruft_name = self.store.get_value(child_iter, ListStoreColumns.name) | ||
3094 | 352 | state = self.store.get_value(child_iter, ListStoreColumns.active) | ||
3095 | 353 | # Toggle the current state. | ||
3096 | 354 | new_state = not state | ||
3097 | 355 | if new_state: | ||
3098 | 356 | self.janitord.unignore(cruft_name) | ||
3099 | 357 | else: | ||
3100 | 358 | self.janitord.ignore(cruft_name) | ||
3101 | 359 | self.store.set_value(child_iter, ListStoreColumns.active, new_state) | ||
3102 | 360 | self.store.set_value( | ||
3103 | 361 | child_iter, ListStoreColumns.server_ignored, not new_state) | ||
3104 | 362 | # Save the new ignored state on the dbus service. | ||
3105 | 363 | self.janitord.save() | ||
3106 | 364 | |||
3107 | 365 | # Signal and event handlers. | ||
3108 | 366 | |||
3109 | 367 | def on_quit_menuitem_activate(self, *args): | ||
3110 | 368 | """Signal and event handlers for quitting. | ||
3111 | 369 | |||
3112 | 370 | Since we just want things to go away, we don't really care about the | ||
3113 | 371 | arguments. Just tell the main loop to exit. | ||
3114 | 372 | """ | ||
3115 | 373 | # Don't quit while we're working. | ||
3116 | 374 | if self.working: | ||
3117 | 375 | return True | ||
3118 | 376 | gtk.main_quit() | ||
3119 | 377 | |||
3120 | 378 | on_window_delete_event = on_quit_menuitem_activate | ||
3121 | 379 | |||
3122 | 380 | def treeview_button_press_event(self, treeview, event): | ||
3123 | 381 | """Handle mouse button press events on the TreeView ourselves. | ||
3124 | 382 | |||
3125 | 383 | We handle mouse button presses ourselves so that we can either | ||
3126 | 384 | toggle the long description (button 1, typically left) or | ||
3127 | 385 | pop up a menu (button 3, typically right). | ||
3128 | 386 | """ | ||
3129 | 387 | # Original comment: This is slightly tricky and probably a source of | ||
3130 | 388 | # bugs. Oh well. | ||
3131 | 389 | if event.button == 1: | ||
3132 | 390 | # Left button event. Select the row being clicked on. If the | ||
3133 | 391 | # click is on the cruft name, show or hide its long description. | ||
3134 | 392 | # If the click the click is elsewhere do not handle it. This | ||
3135 | 393 | # allows the toggle button event to be handled separately. | ||
3136 | 394 | x = int(event.x) | ||
3137 | 395 | y = int(event.y) | ||
3138 | 396 | time = event.time | ||
3139 | 397 | pathinfo = treeview.get_path_at_pos(x, y) | ||
3140 | 398 | if pathinfo is None: | ||
3141 | 399 | # The click was not in a cell, but we've handled it anyway. | ||
3142 | 400 | return True | ||
3143 | 401 | path, column, cell_x, cell_y = pathinfo | ||
3144 | 402 | if column in self.cruft_name_columns: | ||
3145 | 403 | treeview.set_cursor(path, column, False) | ||
3146 | 404 | self.toggle_long_description(treeview) | ||
3147 | 405 | return True | ||
3148 | 406 | else: | ||
3149 | 407 | # We are not handling this event so that the toggle button | ||
3150 | 408 | # handling can occur. | ||
3151 | 409 | return False | ||
3152 | 410 | elif event.button == 3: | ||
3153 | 411 | # Right button event. Pop up the select/deselect all menu. | ||
3154 | 412 | treeview.grab_focus() | ||
3155 | 413 | x = int(event.x) | ||
3156 | 414 | y = int(event.y) | ||
3157 | 415 | time = event.time | ||
3158 | 416 | pathinfo = treeview.get_path_at_pos(x, y) | ||
3159 | 417 | if pathinfo is not None: | ||
3160 | 418 | path, column, cell_x, cell_y = pathinfo | ||
3161 | 419 | treeview.set_cursor(path, column, False) | ||
3162 | 420 | menu = self.popup_menus[treeview] | ||
3163 | 421 | menu.popup(None, None, None, event.button, time) | ||
3164 | 422 | return True | ||
3165 | 423 | else: | ||
3166 | 424 | # No other events are handled by us. | ||
3167 | 425 | return False | ||
3168 | 426 | |||
3169 | 427 | # The actual event handler is totally generic. Alias it to names | ||
3170 | 428 | # recognized by the automatic event binding scheme. | ||
3171 | 429 | on_unused_treeview_button_press_event = treeview_button_press_event | ||
3172 | 430 | on_optimize_treeview_button_press_event = treeview_button_press_event | ||
3173 | 431 | |||
3174 | 432 | def treeview_size_allocate(self, treeview, *args): | ||
3175 | 433 | """Allocate space for the tree view and set wrap width. | ||
3176 | 434 | |||
3177 | 435 | :param treeview: The TreeView | ||
3178 | 436 | :param args: Additional ignored positional arguments | ||
3179 | 437 | """ | ||
3180 | 438 | # Get the rightmost of the two columns in the TreeView, i.e. the one | ||
3181 | 439 | # containing the text. | ||
3182 | 440 | column = treeview.get_column(1) | ||
3183 | 441 | name_cr = column.get_cell_renderers()[0] | ||
3184 | 442 | # Wrap to the entire width of the column. | ||
3185 | 443 | width = column.get_width() | ||
3186 | 444 | name_cr.set_property('wrap-width', width) | ||
3187 | 445 | |||
3188 | 446 | on_unused_treeview_size_allocate = treeview_size_allocate | ||
3189 | 447 | on_optimize_treeview_size_allocate = treeview_size_allocate | ||
3190 | 448 | |||
3191 | 449 | def on_sort_by_name_toggled(self, menuitem): | ||
3192 | 450 | """Reorder the crufts to be sorted by name or size.""" | ||
3193 | 451 | if menuitem.get_active(): | ||
3194 | 452 | self._sort_key = self._by_name | ||
3195 | 453 | else: | ||
3196 | 454 | self._sort_key = self._by_size | ||
3197 | 455 | self.sort_cruft() | ||
3198 | 456 | |||
3199 | 457 | def on_about_menuitem_activate(self, *args): | ||
3200 | 458 | dialog = self.widgets['about_dialog'] | ||
3201 | 459 | dialog.set_name(_('Computer Janitor')) | ||
3202 | 460 | dialog.set_version(__version__) | ||
3203 | 461 | dialog.show() | ||
3204 | 462 | dialog.run() | ||
3205 | 463 | dialog.hide() | ||
3206 | 464 | |||
3207 | 465 | def on_show_previously_ignored_toggled(self, menuitem): | ||
3208 | 466 | """Show all cruft, even those being ignored. | ||
3209 | 467 | |||
3210 | 468 | Normally, we only show cruft that wasn't explicitly ignored. By | ||
3211 | 469 | toggling this menu item, the janitor can also display cruft that is | ||
3212 | 470 | marked as ignored on the dbus service. | ||
3213 | 471 | """ | ||
3214 | 472 | show_ignored_cruft = menuitem.get_active() | ||
3215 | 473 | iter = self.store.get_iter_first() | ||
3216 | 474 | while iter: | ||
3217 | 475 | cruft_name = self.store.get_value(iter, ListStoreColumns.name) | ||
3218 | 476 | server_ignored = self.store.get_value( | ||
3219 | 477 | iter, ListStoreColumns.server_ignored) | ||
3220 | 478 | show = (show_ignored_cruft or not server_ignored) | ||
3221 | 479 | self.store.set_value(iter, ListStoreColumns.show, show) | ||
3222 | 480 | iter = self.store.iter_next(iter) | ||
3223 | 481 | |||
3224 | 482 | def on_do_button_clicked(self, *args): | ||
3225 | 483 | """JFDI, well almost.""" | ||
3226 | 484 | self.count = 0 | ||
3227 | 485 | response = AreYouSure(self).verify() | ||
3228 | 486 | if not response: | ||
3229 | 487 | return | ||
3230 | 488 | # This can take a long time. Make an asynchronous call to the dbus | ||
3231 | 489 | # service and arrange for it to occasionally provide us with status. | ||
3232 | 490 | # This isn't great ui, but OTOH, the package cruft cleaners themselves | ||
3233 | 491 | # don't provide much granularity, so there's little we can do anyway | ||
3234 | 492 | # without a major rewrite of the plugin architecture. | ||
3235 | 493 | self.working = True | ||
3236 | 494 | glib.timeout_add(150, self.pulse) | ||
3237 | 495 | # Make various ui elements insensitive. | ||
3238 | 496 | self.desensitize() | ||
3239 | 497 | cleanable = [cruft for cruft, ispkg in self.get_cleanable_cruft()] | ||
3240 | 498 | # XXX 2010-03-08 barry: Do better than this. | ||
3241 | 499 | def error(exception): | ||
3242 | 500 | print exception | ||
3243 | 501 | self.working = False | ||
3244 | 502 | def reply(): | ||
3245 | 503 | pass | ||
3246 | 504 | # Make the asynchronous call because this can take a long time. We'll | ||
3247 | 505 | # get status updates periodically. Note however that even though this | ||
3248 | 506 | # is asynchronous, dbus still expects a response within a certain | ||
3249 | 507 | # amount of time. We have no idea how long it will take to clean up | ||
3250 | 508 | # the cruft though, so just crank the timeout up to some insanely huge | ||
3251 | 509 | # number (of seconds). | ||
3252 | 510 | self.widgets['progressbar_status'].set_text('Authenticating...') | ||
3253 | 511 | self.janitord.clean(cleanable, | ||
3254 | 512 | reply_handler=reply, | ||
3255 | 513 | error_handler=error, | ||
3256 | 514 | # If it takes longer than an hour, we're screwed. | ||
3257 | 515 | timeout=3600) | ||
3258 | 0 | 516 | ||
3259 | === added file 'computerjanitorapp/utilities.py' | |||
3260 | --- computerjanitorapp/utilities.py 1970-01-01 00:00:00 +0000 | |||
3261 | +++ computerjanitorapp/utilities.py 2010-03-10 21:40:30 +0000 | |||
3262 | @@ -0,0 +1,51 @@ | |||
3263 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3264 | 2 | # | ||
3265 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3266 | 4 | # it under the terms of the GNU General Public License as published by | ||
3267 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3268 | 6 | # | ||
3269 | 7 | # This program is distributed in the hope that it will be useful, | ||
3270 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3271 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3272 | 10 | # GNU General Public License for more details. | ||
3273 | 11 | # | ||
3274 | 12 | # You should have received a copy of the GNU General Public License | ||
3275 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3276 | 14 | |||
3277 | 15 | """Common utilities.""" | ||
3278 | 16 | |||
3279 | 17 | from __future__ import absolute_import, unicode_literals | ||
3280 | 18 | |||
3281 | 19 | __metaclass__ = type | ||
3282 | 20 | __all__ = [ | ||
3283 | 21 | 'format_size', | ||
3284 | 22 | ] | ||
3285 | 23 | |||
3286 | 24 | |||
3287 | 25 | from math import log10 | ||
3288 | 26 | |||
3289 | 27 | |||
3290 | 28 | TABLE = ( | ||
3291 | 29 | 'B', | ||
3292 | 30 | 'kB', | ||
3293 | 31 | 'MB', | ||
3294 | 32 | 'GB', | ||
3295 | 33 | 'TB', | ||
3296 | 34 | ) | ||
3297 | 35 | |||
3298 | 36 | |||
3299 | 37 | def format_size(bytes): | ||
3300 | 38 | """Format size in bytes. | ||
3301 | 39 | |||
3302 | 40 | :param bytes: Integer size in bytes. | ||
3303 | 41 | :type bytes: integer | ||
3304 | 42 | :return: Formatted size | ||
3305 | 43 | :rtype: string | ||
3306 | 44 | """ | ||
3307 | 45 | assert bytes >= 0, 'Cannot have negative sizes' | ||
3308 | 46 | if bytes == 0: | ||
3309 | 47 | return '0B' | ||
3310 | 48 | if bytes > 10**12: | ||
3311 | 49 | return '>1TB' | ||
3312 | 50 | key = divmod(int(log10(bytes)), 3)[0] | ||
3313 | 51 | return '{0}{1}'.format(bytes // 10**(key * 3), TABLE[key]) | ||
3314 | 0 | 52 | ||
3315 | === added directory 'computerjanitord' | |||
3316 | === added file 'computerjanitord/__init__.py' | |||
3317 | === added file 'computerjanitord/application.py' | |||
3318 | --- computerjanitord/application.py 1970-01-01 00:00:00 +0000 | |||
3319 | +++ computerjanitord/application.py 2010-03-10 21:40:30 +0000 | |||
3320 | @@ -0,0 +1,111 @@ | |||
3321 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3322 | 2 | # | ||
3323 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3324 | 4 | # it under the terms of the GNU General Public License as published by | ||
3325 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3326 | 6 | # | ||
3327 | 7 | # This program is distributed in the hope that it will be useful, | ||
3328 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3329 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3330 | 10 | # GNU General Public License for more details. | ||
3331 | 11 | # | ||
3332 | 12 | # You should have received a copy of the GNU General Public License | ||
3333 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3334 | 14 | |||
3335 | 15 | """Application interface for use by plugins. | ||
3336 | 16 | |||
3337 | 17 | This primarily makes certain apt functionality available to the plugin. | ||
3338 | 18 | """ | ||
3339 | 19 | |||
3340 | 20 | from __future__ import absolute_import, unicode_literals | ||
3341 | 21 | |||
3342 | 22 | __metaclass__ = type | ||
3343 | 23 | __all__ = [ | ||
3344 | 24 | 'Application', | ||
3345 | 25 | 'SourcesListError', | ||
3346 | 26 | ] | ||
3347 | 27 | |||
3348 | 28 | |||
3349 | 29 | import apt | ||
3350 | 30 | |||
3351 | 31 | import computerjanitor | ||
3352 | 32 | import computerjanitorapp | ||
3353 | 33 | |||
3354 | 34 | _ = computerjanitorapp.setup_gettext() | ||
3355 | 35 | |||
3356 | 36 | SYNTAPTIC_PREFERENCES_FILE = '/var/lib/synaptic/preferences' | ||
3357 | 37 | # There really isn't anything special about these packages. These really just | ||
3358 | 38 | # represent landmarks in the package namespace that we look for to try to | ||
3359 | 39 | # judge the sanity of the apt cache. If these are missing, things are really | ||
3360 | 40 | # messed up and we can't actually figure out how to continue. | ||
3361 | 41 | LANDMARK_PACKAGES = [ | ||
3362 | 42 | 'dash', | ||
3363 | 43 | 'gzip', | ||
3364 | 44 | ] | ||
3365 | 45 | |||
3366 | 46 | |||
3367 | 47 | class LandmarkPackageError(computerjanitor.Exception): | ||
3368 | 48 | """Base class for problems with the landmark packages.""" | ||
3369 | 49 | |||
3370 | 50 | _errmsg = None | ||
3371 | 51 | |||
3372 | 52 | def __init__(self, package): | ||
3373 | 53 | self.package = package | ||
3374 | 54 | |||
3375 | 55 | def __str__(self): | ||
3376 | 56 | # gettext translation needs to be called at run time. | ||
3377 | 57 | return _(self._errmsg).format(self) | ||
3378 | 58 | |||
3379 | 59 | |||
3380 | 60 | class MissingLandmarkError(LandmarkPackageError): | ||
3381 | 61 | """A landmark package could not be found.""" | ||
3382 | 62 | |||
3383 | 63 | _errmsg = _('Landmark package {0.package} is missing') | ||
3384 | 64 | |||
3385 | 65 | |||
3386 | 66 | class NonDownloadableError(LandmarkPackageError): | ||
3387 | 67 | """A landmark package is not downloadable.""" | ||
3388 | 68 | |||
3389 | 69 | _errmsg = _('Landmark package {0.package} is not downloadable') | ||
3390 | 70 | |||
3391 | 71 | |||
3392 | 72 | class Application: | ||
3393 | 73 | """Interface for plugins requesting apt actions.""" | ||
3394 | 74 | |||
3395 | 75 | def __init__(self, apt_cache=None): | ||
3396 | 76 | """Create the application interface. | ||
3397 | 77 | |||
3398 | 78 | :param apt_cache: Alternative apt cache for testing purposes. When | ||
3399 | 79 | `None` use the default apt cache. | ||
3400 | 80 | """ | ||
3401 | 81 | if apt_cache is None: | ||
3402 | 82 | # Use the real apt cache. | ||
3403 | 83 | self.apt_cache = apt.Cache() | ||
3404 | 84 | else: | ||
3405 | 85 | self.apt_cache = apt_cache | ||
3406 | 86 | self.refresh_apt_cache() | ||
3407 | 87 | |||
3408 | 88 | def refresh_apt_cache(self): | ||
3409 | 89 | """Refresh the apt cache. | ||
3410 | 90 | |||
3411 | 91 | This API is used by plugins. | ||
3412 | 92 | """ | ||
3413 | 93 | self.apt_cache.open() | ||
3414 | 94 | # For historical purposes, Synaptic has a different way of pinning | ||
3415 | 95 | # packages than apt, so we have to load its preferences file in order | ||
3416 | 96 | # to know what it's pinning. | ||
3417 | 97 | self.apt_cache._depcache.ReadPinFile(SYNTAPTIC_PREFERENCES_FILE) | ||
3418 | 98 | |||
3419 | 99 | def verify_apt_cache(self): | ||
3420 | 100 | """Verify that essential packages are available in the apt cache. | ||
3421 | 101 | |||
3422 | 102 | This API is used by plugins. | ||
3423 | 103 | |||
3424 | 104 | :raises SourcesListProblem: when an essential package is not | ||
3425 | 105 | available. | ||
3426 | 106 | """ | ||
3427 | 107 | for name in LANDMARK_PACKAGES: | ||
3428 | 108 | if name not in self.apt_cache: | ||
3429 | 109 | raise MissingLandmarkError(name) | ||
3430 | 110 | if not any(v.downloadable for v in self.apt_cache[name].versions): | ||
3431 | 111 | raise NonDownloadableError(name) | ||
3432 | 0 | 112 | ||
3433 | === added file 'computerjanitord/authenticator.py' | |||
3434 | --- computerjanitord/authenticator.py 1970-01-01 00:00:00 +0000 | |||
3435 | +++ computerjanitord/authenticator.py 2010-03-10 21:40:30 +0000 | |||
3436 | @@ -0,0 +1,85 @@ | |||
3437 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3438 | 2 | # | ||
3439 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3440 | 4 | # it under the terms of the GNU General Public License as published by | ||
3441 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3442 | 6 | # | ||
3443 | 7 | # This program is distributed in the hope that it will be useful, | ||
3444 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3445 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3446 | 10 | # GNU General Public License for more details. | ||
3447 | 11 | # | ||
3448 | 12 | # You should have received a copy of the GNU General Public License | ||
3449 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3450 | 14 | |||
3451 | 15 | """Authentication for Computer Janitor backend services.""" | ||
3452 | 16 | |||
3453 | 17 | from __future__ import absolute_import, unicode_literals | ||
3454 | 18 | |||
3455 | 19 | __metaclass__ = type | ||
3456 | 20 | __all__ = [ | ||
3457 | 21 | 'Authenticator', | ||
3458 | 22 | ] | ||
3459 | 23 | |||
3460 | 24 | |||
3461 | 25 | import dbus | ||
3462 | 26 | |||
3463 | 27 | |||
3464 | 28 | PK_AUTHORITY_BUS_NAME = 'org.freedesktop.PolicyKit1' | ||
3465 | 29 | PK_AUTHORITY_OBJECT_PATH = '/org/freedesktop/PolicyKit1/Authority' | ||
3466 | 30 | PK_AUTHORITY_INTERFACE = 'org.freedesktop.PolicyKit1.Authority' | ||
3467 | 31 | # From the PolicyKit API. | ||
3468 | 32 | # http://hal.freedesktop.org/docs/polkit/ | ||
3469 | 33 | # eggdbus-interface-org.freedesktop.PolicyKit1.Authority.html | ||
3470 | 34 | AllowUserInteraction = 0x00000001 | ||
3471 | 35 | |||
3472 | 36 | |||
3473 | 37 | class Authenticator: | ||
3474 | 38 | """PolicyKit authenticator.""" | ||
3475 | 39 | |||
3476 | 40 | def authenticate(self, sender, connection, privilege): | ||
3477 | 41 | """Authenticate with PolicyKit. | ||
3478 | 42 | |||
3479 | 43 | :param sender: The initiator of the action. | ||
3480 | 44 | :param connection: The dbus connection that initiated the action. | ||
3481 | 45 | :param privilege: The privilege being requested. | ||
3482 | 46 | :return: Whether the subject is authorized or not. | ||
3483 | 47 | :rtype: bool | ||
3484 | 48 | """ | ||
3485 | 49 | policykit = self._get_policykit_proxy() | ||
3486 | 50 | sender_pid = self._get_sender_pid(connection, sender) | ||
3487 | 51 | # This is the CheckAuthorization() 'subject' structure. | ||
3488 | 52 | subject = ( | ||
3489 | 53 | 'unix-process', { | ||
3490 | 54 | 'pid': sender_pid, | ||
3491 | 55 | 'start-time': 0, | ||
3492 | 56 | }) | ||
3493 | 57 | # No details or cancellation_id needed. | ||
3494 | 58 | details = {'': ''} | ||
3495 | 59 | cancellation_id = '' | ||
3496 | 60 | flags = AllowUserInteraction | ||
3497 | 61 | # CheckAuthorization returns an AuthorizationResult structure, modeled | ||
3498 | 62 | # as a 3-tuple. The only thing we care about though is the boolean | ||
3499 | 63 | # describing whether we got authorized or not. | ||
3500 | 64 | is_authorized, is_challenge, details = policykit.CheckAuthorization( | ||
3501 | 65 | subject, privilege, details, flags, cancellation_id) | ||
3502 | 66 | return is_authorized | ||
3503 | 67 | |||
3504 | 68 | def _get_policykit_proxy(self): | ||
3505 | 69 | """Contact the system bus to get a PolicyKit proxy.""" | ||
3506 | 70 | system_bus = dbus.SystemBus() | ||
3507 | 71 | pk_proxy = system_bus.get_object( | ||
3508 | 72 | PK_AUTHORITY_BUS_NAME, PK_AUTHORITY_OBJECT_PATH) | ||
3509 | 73 | return dbus.Interface(pk_proxy, PK_AUTHORITY_INTERFACE) | ||
3510 | 74 | |||
3511 | 75 | def _get_sender_pid(self, connection, sender): | ||
3512 | 76 | """Contact the system bus to get the sender connection PID.""" | ||
3513 | 77 | # Since we're going to authorize a Unix process, we need to get the | ||
3514 | 78 | # sender's process id. This is available on the dbus. The | ||
3515 | 79 | # CheckAuthorization() method also requires us to have a start-time, | ||
3516 | 80 | # but it's not clear what the semantics are for that, so we'll just | ||
3517 | 81 | # put a zero there. | ||
3518 | 82 | db_proxy = connection.get_object( | ||
3519 | 83 | dbus.BUS_DAEMON_NAME, dbus.BUS_DAEMON_PATH, introspect=False) | ||
3520 | 84 | info = dbus.Interface(db_proxy, dbus.BUS_DAEMON_IFACE) | ||
3521 | 85 | return info.GetConnectionUnixProcessID(sender) | ||
3522 | 0 | 86 | ||
3523 | === added file 'computerjanitord/collector.py' | |||
3524 | --- computerjanitord/collector.py 1970-01-01 00:00:00 +0000 | |||
3525 | +++ computerjanitord/collector.py 2010-03-10 21:40:30 +0000 | |||
3526 | @@ -0,0 +1,149 @@ | |||
3527 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3528 | 2 | # | ||
3529 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3530 | 4 | # it under the terms of the GNU General Public License as published by | ||
3531 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3532 | 6 | # | ||
3533 | 7 | # This program is distributed in the hope that it will be useful, | ||
3534 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3535 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3536 | 10 | # GNU General Public License for more details. | ||
3537 | 11 | # | ||
3538 | 12 | # You should have received a copy of the GNU General Public License | ||
3539 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3540 | 14 | |||
3541 | 15 | """A cruft collector.""" | ||
3542 | 16 | |||
3543 | 17 | from __future__ import absolute_import, unicode_literals | ||
3544 | 18 | |||
3545 | 19 | __metaclass__ = type | ||
3546 | 20 | __all__ = [ | ||
3547 | 21 | 'Collector', | ||
3548 | 22 | ] | ||
3549 | 23 | |||
3550 | 24 | |||
3551 | 25 | import os | ||
3552 | 26 | import time | ||
3553 | 27 | import logging | ||
3554 | 28 | |||
3555 | 29 | from computerjanitor import PluginManager | ||
3556 | 30 | from computerjanitord.errors import DuplicateCruftError | ||
3557 | 31 | from computerjanitord.whitelist import Whitelist | ||
3558 | 32 | |||
3559 | 33 | |||
3560 | 34 | log = logging.getLogger('computerjanitor') | ||
3561 | 35 | MISSING = object() | ||
3562 | 36 | DEFAULT_PLUGINS_DIRS = "/usr/share/computerjanitor/plugins" | ||
3563 | 37 | |||
3564 | 38 | # For testing purposes. | ||
3565 | 39 | SLEEPY_TIME = float(os.environ.get('COMPUTER_JANITOR_SLEEPY_TIME', '1.0')) | ||
3566 | 40 | |||
3567 | 41 | |||
3568 | 42 | class Collector: | ||
3569 | 43 | """A cruft collector.""" | ||
3570 | 44 | |||
3571 | 45 | def __init__(self, application, plugin_manager_class=None, | ||
3572 | 46 | whitelist_dirs=None, service=None): | ||
3573 | 47 | """Create a cruft collector. | ||
3574 | 48 | |||
3575 | 49 | :param application: The `Application` class. | ||
3576 | 50 | :type application: This object is used by plugins to access the apt | ||
3577 | 51 | database. It must have an attribute named `apt_cache` and a | ||
3578 | 52 | method named `refresh_apt_cache()`. | ||
3579 | 53 | :param plugin_manager_class: The plugin manager class. If None (the | ||
3580 | 54 | default), then `computerjanitor.PluginManager` is used. | ||
3581 | 55 | :type plugin_manager_class: callable accepting a single argument, | ||
3582 | 56 | which is a sequence of plugin directories. | ||
3583 | 57 | :param whitelist_dirs: Sequence of directories to search for | ||
3584 | 58 | '.whitelist' files. Passed directly to | ||
3585 | 59 | `computerjanitord.whitelist.Whitelist`. | ||
3586 | 60 | :param service: The dbus service; when doing plugin post-cleanup, this | ||
3587 | 61 | will be used to emit a progress signal. | ||
3588 | 62 | """ | ||
3589 | 63 | self.application = application | ||
3590 | 64 | self.service = service | ||
3591 | 65 | self.whitelist = Whitelist(whitelist_dirs) | ||
3592 | 66 | # Keep track of cruft and map between the cruft's name and its Cruft | ||
3593 | 67 | # instance. We'll use the latter when cruft cleanup is requested | ||
3594 | 68 | # through the dbus API. | ||
3595 | 69 | self.cruft = None | ||
3596 | 70 | self.cruft_by_name = None | ||
3597 | 71 | # Set up the plugin manager. | ||
3598 | 72 | plugin_path = os.environ.get('COMPUTER_JANITOR_PLUGINS', | ||
3599 | 73 | DEFAULT_PLUGINS_DIRS) | ||
3600 | 74 | plugin_dirs = plugin_path.split(':') | ||
3601 | 75 | if plugin_manager_class is None: | ||
3602 | 76 | plugin_manager_class = PluginManager | ||
3603 | 77 | self.plugin_manager = plugin_manager_class(application, plugin_dirs) | ||
3604 | 78 | self.load() | ||
3605 | 79 | |||
3606 | 80 | def load(self): | ||
3607 | 81 | """Reload all cruft.""" | ||
3608 | 82 | self.cruft = [] | ||
3609 | 83 | self.cruft_by_name = {} | ||
3610 | 84 | # Ask all the plugins to find their cruft, filtering out whitelisted | ||
3611 | 85 | # cruft. | ||
3612 | 86 | for plugin in self.plugin_manager.get_plugins(): | ||
3613 | 87 | for cruft in plugin.get_cruft(): | ||
3614 | 88 | if not self.whitelist.is_whitelisted(cruft): | ||
3615 | 89 | # Different plugins can give us duplicate cruft names, | ||
3616 | 90 | # however the Cruft class better be the same, otherwise we | ||
3617 | 91 | # won't actually know how to map the name back to a cruft | ||
3618 | 92 | # instance for proper cleanup. | ||
3619 | 93 | if cruft.get_name() in self.cruft_by_name: | ||
3620 | 94 | my_cruft = self.cruft_by_name[cruft.get_name()] | ||
3621 | 95 | if cruft.__class__ is my_cruft.__class__: | ||
3622 | 96 | # We only need one instance of this cruft. | ||
3623 | 97 | continue | ||
3624 | 98 | else: | ||
3625 | 99 | raise DuplicateCruftError(cruft.get_name()) | ||
3626 | 100 | #print ' ', cruft.get_name() | ||
3627 | 101 | self.cruft.append(cruft) | ||
3628 | 102 | self.cruft_by_name[cruft.get_name()] = cruft | ||
3629 | 103 | |||
3630 | 104 | def clean(self, names, dry_run=False): | ||
3631 | 105 | """Clean up the named cruft. | ||
3632 | 106 | |||
3633 | 107 | :param names: The names of the cruft to clean up. | ||
3634 | 108 | :type names: list of strings | ||
3635 | 109 | :param dry_run: Flag indicating whether to do permanent changes. | ||
3636 | 110 | :type dry_run: bool | ||
3637 | 111 | """ | ||
3638 | 112 | # Ensure that all named cruft is known. | ||
3639 | 113 | for name in names: | ||
3640 | 114 | cruft = self.cruft_by_name.get(name, MISSING) | ||
3641 | 115 | if cruft is MISSING: | ||
3642 | 116 | log.error('No such cruft: {0}'.format(name)) | ||
3643 | 117 | raise NoSuchCruftError(name) | ||
3644 | 118 | log.info('cleaning cruft: {0}'.format(cruft.get_name())) | ||
3645 | 119 | if not dry_run: | ||
3646 | 120 | cruft.cleanup() | ||
3647 | 121 | # Do plugin-specific post-cleanup. | ||
3648 | 122 | for plugin in self.plugin_manager.get_plugins(): | ||
3649 | 123 | logging.info('post-cleanup: {0}'.format(plugin)) | ||
3650 | 124 | if self.service is not None: | ||
3651 | 125 | # Notify the client that we're not done yet. | ||
3652 | 126 | # | ||
3653 | 127 | # 2010-02-09 barry: this actually kind of sucks because the | ||
3654 | 128 | # granularity is too coarse. Some plugins will post_cleanup() | ||
3655 | 129 | # very quickly, others will take a long time. Unfortunately, | ||
3656 | 130 | # the computerjanitor.Plugin API doesn't support a more | ||
3657 | 131 | # granular feedback. Plugin.get_plugin(..., callback=foo) | ||
3658 | 132 | # doesn't really cut it because that only gets called during | ||
3659 | 133 | # get_plugins(). | ||
3660 | 134 | self.service.cleanup_status(plugin.__class__.__name__) | ||
3661 | 135 | if dry_run: | ||
3662 | 136 | # For testing purposes. | ||
3663 | 137 | time.sleep(SLEEPY_TIME) | ||
3664 | 138 | else: | ||
3665 | 139 | try: | ||
3666 | 140 | plugin.post_cleanup() | ||
3667 | 141 | except Exception: | ||
3668 | 142 | logging.exception('plugin: {0}'.format(plugin)) | ||
3669 | 143 | # Keep going. | ||
3670 | 144 | # Now we're done. | ||
3671 | 145 | if self.service is not None: | ||
3672 | 146 | self.service.cleanup_status('') | ||
3673 | 147 | # Reload list of crufts. | ||
3674 | 148 | self.application.refresh_apt_cache() | ||
3675 | 149 | self.load() | ||
3676 | 0 | 150 | ||
3677 | === added directory 'computerjanitord/data' | |||
3678 | === added file 'computerjanitord/data/com.ubuntu.ComputerJanitor.conf' | |||
3679 | --- computerjanitord/data/com.ubuntu.ComputerJanitor.conf 1970-01-01 00:00:00 +0000 | |||
3680 | +++ computerjanitord/data/com.ubuntu.ComputerJanitor.conf 2010-03-10 21:40:30 +0000 | |||
3681 | @@ -0,0 +1,15 @@ | |||
3682 | 1 | <!DOCTYPE busconfig PUBLIC | ||
3683 | 2 | "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" | ||
3684 | 3 | "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> | ||
3685 | 4 | <busconfig> | ||
3686 | 5 | <policy user="root"> | ||
3687 | 6 | <allow own="com.ubuntu.ComputerJanitor"/> | ||
3688 | 7 | </policy> | ||
3689 | 8 | |||
3690 | 9 | <policy context="default"> | ||
3691 | 10 | <allow send_interface="com.ubuntu.ComputerJanitor"/> | ||
3692 | 11 | <allow receive_interface="com.ubuntu.ComputerJanitor" | ||
3693 | 12 | receive_sender="com.ubuntu.ComputerJanitor"/> | ||
3694 | 13 | </policy> | ||
3695 | 14 | |||
3696 | 15 | </busconfig> | ||
3697 | 0 | 16 | ||
3698 | === added file 'computerjanitord/data/com.ubuntu.computerjanitor.policy' | |||
3699 | --- computerjanitord/data/com.ubuntu.computerjanitor.policy 1970-01-01 00:00:00 +0000 | |||
3700 | +++ computerjanitord/data/com.ubuntu.computerjanitor.policy 2010-03-10 21:40:30 +0000 | |||
3701 | @@ -0,0 +1,19 @@ | |||
3702 | 1 | <?xml version="1.0" encoding="UTF-8"?> | ||
3703 | 2 | <!DOCTYPE policyconfig PUBLIC | ||
3704 | 3 | "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" | ||
3705 | 4 | "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd"> | ||
3706 | 5 | <policyconfig> | ||
3707 | 6 | |||
3708 | 7 | <vendor>ComputerJanitor</vendor> | ||
3709 | 8 | <vendor_url>https://launchpad.net/computer-janitor</vendor_url> | ||
3710 | 9 | |||
3711 | 10 | <action id="com.ubuntu.computerjanitor.updatesystem"> | ||
3712 | 11 | <description>Clean up packages that are no longer necessary</description> | ||
3713 | 12 | <message>Removing unused packages requires authentication</message> | ||
3714 | 13 | <defaults> | ||
3715 | 14 | <allow_inactive>no</allow_inactive> | ||
3716 | 15 | <allow_active>auth_admin_keep</allow_active> | ||
3717 | 16 | </defaults> | ||
3718 | 17 | </action> | ||
3719 | 18 | |||
3720 | 19 | </policyconfig> | ||
3721 | 0 | 20 | ||
3722 | === added file 'computerjanitord/errors.py' | |||
3723 | --- computerjanitord/errors.py 1970-01-01 00:00:00 +0000 | |||
3724 | +++ computerjanitord/errors.py 2010-03-10 21:40:30 +0000 | |||
3725 | @@ -0,0 +1,57 @@ | |||
3726 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3727 | 2 | # | ||
3728 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3729 | 4 | # it under the terms of the GNU General Public License as published by | ||
3730 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3731 | 6 | # | ||
3732 | 7 | # This program is distributed in the hope that it will be useful, | ||
3733 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3734 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3735 | 10 | # GNU General Public License for more details. | ||
3736 | 11 | # | ||
3737 | 12 | # You should have received a copy of the GNU General Public License | ||
3738 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3739 | 14 | |||
3740 | 15 | """Exceptions for the Computer Janitor daemon.""" | ||
3741 | 16 | |||
3742 | 17 | |||
3743 | 18 | from __future__ import absolute_import, unicode_literals | ||
3744 | 19 | |||
3745 | 20 | __metaclass__ = type | ||
3746 | 21 | __all__ = [ | ||
3747 | 22 | 'DuplicateCruftError', | ||
3748 | 23 | 'NoSuchCruftError', | ||
3749 | 24 | 'PermissionDeniedError', | ||
3750 | 25 | ] | ||
3751 | 26 | |||
3752 | 27 | |||
3753 | 28 | import dbus | ||
3754 | 29 | |||
3755 | 30 | from computerjanitorapp import setup_gettext | ||
3756 | 31 | _ = setup_gettext() | ||
3757 | 32 | |||
3758 | 33 | |||
3759 | 34 | class PermissionDeniedError(dbus.DBusException): | ||
3760 | 35 | """Permission denied by policy""" | ||
3761 | 36 | |||
3762 | 37 | |||
3763 | 38 | class CruftError(dbus.DBusException): | ||
3764 | 39 | MSG = None | ||
3765 | 40 | |||
3766 | 41 | def __init__(self, cruft_name): | ||
3767 | 42 | self.cruft_name = cruft_name | ||
3768 | 43 | |||
3769 | 44 | def __str__(self): | ||
3770 | 45 | return _(self.MSG).format(self) | ||
3771 | 46 | |||
3772 | 47 | |||
3773 | 48 | class DuplicateCruftError(CruftError): | ||
3774 | 49 | """Duplicate cruft name with different cleanup.""" | ||
3775 | 50 | |||
3776 | 51 | MSG = _('Duplicate cruft with different cleanup: {0.cruft_name}') | ||
3777 | 52 | |||
3778 | 53 | |||
3779 | 54 | class NoSuchCruftError(CruftError): | ||
3780 | 55 | """There is no cruft by the given name.""" | ||
3781 | 56 | |||
3782 | 57 | MSG = _('No such cruft: {0.cruft_name}') | ||
3783 | 0 | 58 | ||
3784 | === added file 'computerjanitord/main.py' | |||
3785 | --- computerjanitord/main.py 1970-01-01 00:00:00 +0000 | |||
3786 | +++ computerjanitord/main.py 2010-03-10 21:40:30 +0000 | |||
3787 | @@ -0,0 +1,97 @@ | |||
3788 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3789 | 2 | # | ||
3790 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3791 | 4 | # it under the terms of the GNU General Public License as published by | ||
3792 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3793 | 6 | # | ||
3794 | 7 | # This program is distributed in the hope that it will be useful, | ||
3795 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3796 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3797 | 10 | # GNU General Public License for more details. | ||
3798 | 11 | # | ||
3799 | 12 | # You should have received a copy of the GNU General Public License | ||
3800 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3801 | 14 | |||
3802 | 15 | """Main entry point for Computer Janitor dbus daemon.""" | ||
3803 | 16 | |||
3804 | 17 | from __future__ import absolute_import, unicode_literals | ||
3805 | 18 | |||
3806 | 19 | __metaclass__ = type | ||
3807 | 20 | __all__ = [ | ||
3808 | 21 | 'main', | ||
3809 | 22 | ] | ||
3810 | 23 | |||
3811 | 24 | |||
3812 | 25 | import os | ||
3813 | 26 | import gobject | ||
3814 | 27 | import logging | ||
3815 | 28 | import argparse | ||
3816 | 29 | import warnings | ||
3817 | 30 | import dbus.mainloop.glib | ||
3818 | 31 | import logging.handlers | ||
3819 | 32 | |||
3820 | 33 | from computerjanitorapp import __version__, setup_gettext | ||
3821 | 34 | from computerjanitord.service import Service | ||
3822 | 35 | |||
3823 | 36 | _ = setup_gettext() | ||
3824 | 37 | |||
3825 | 38 | |||
3826 | 39 | # 2010-02-09 barry: computerjanitor.package_cruft has a DeprecationWarning, | ||
3827 | 40 | # but that really needs to be fixed in that package, which in turn needs to be | ||
3828 | 41 | # ripped out of update-manager. | ||
3829 | 42 | warnings.filterwarnings('ignore', category=DeprecationWarning, | ||
3830 | 43 | module='computerjanitor.package_cruft') | ||
3831 | 44 | |||
3832 | 45 | |||
3833 | 46 | class Options: | ||
3834 | 47 | """Command line options.""" | ||
3835 | 48 | |||
3836 | 49 | def __init__(self): | ||
3837 | 50 | self.parser = argparse.ArgumentParser( | ||
3838 | 51 | description=_('Computer janitor dbus daemon'), | ||
3839 | 52 | version=__version__) | ||
3840 | 53 | self.parser.add_argument( | ||
3841 | 54 | '-n', '--dry-run', action='store_true', | ||
3842 | 55 | help=_("""\ | ||
3843 | 56 | Only pretend to do anything permanent. This is useful for testing | ||
3844 | 57 | and debugging.""")) | ||
3845 | 58 | self.parser.add_argument( | ||
3846 | 59 | '-f', '--state-file', metavar='FILE', | ||
3847 | 60 | help=_('Store ignored state in FILE instead of the default.')) | ||
3848 | 61 | self.arguments = self.parser.parse_args() | ||
3849 | 62 | |||
3850 | 63 | |||
3851 | 64 | class ASCIIFormatter(logging.Formatter): | ||
3852 | 65 | """Force the log messages to ASCII.""" | ||
3853 | 66 | def format(self, record): | ||
3854 | 67 | message = logging.Formatter.format(self, record) | ||
3855 | 68 | return message.encode('ascii', 'replace') | ||
3856 | 69 | |||
3857 | 70 | |||
3858 | 71 | def main(): | ||
3859 | 72 | """Main entry point.""" | ||
3860 | 73 | # Set up logging. | ||
3861 | 74 | if os.environ.get('COMPUTER_JANITOR_DEBUG') is not None: | ||
3862 | 75 | level = logging.DEBUG | ||
3863 | 76 | else: | ||
3864 | 77 | level = logging.INFO | ||
3865 | 78 | logging.basicConfig(level=level, format='%(levelname)s: %(message)s') | ||
3866 | 79 | log = logging.getLogger('computerjanitor') | ||
3867 | 80 | # SysLogHandler does not recognize unicode arguments. | ||
3868 | 81 | syslog = logging.handlers.SysLogHandler(str('/dev/log')) | ||
3869 | 82 | # syslog will provide a timestamp. | ||
3870 | 83 | formatter = ASCIIFormatter('computerjanitord:%(levelname)s: %(message)s') | ||
3871 | 84 | syslog.setFormatter(formatter) | ||
3872 | 85 | syslog.setLevel(level) | ||
3873 | 86 | log.addHandler(syslog) | ||
3874 | 87 | options = Options() | ||
3875 | 88 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | ||
3876 | 89 | server = Service(options) | ||
3877 | 90 | try: | ||
3878 | 91 | gobject.MainLoop().run() | ||
3879 | 92 | except KeyboardInterrupt: | ||
3880 | 93 | pass | ||
3881 | 94 | |||
3882 | 95 | |||
3883 | 96 | if __name__ == '__main__': | ||
3884 | 97 | main() | ||
3885 | 0 | 98 | ||
3886 | === added file 'computerjanitord/service.py' | |||
3887 | --- computerjanitord/service.py 1970-01-01 00:00:00 +0000 | |||
3888 | +++ computerjanitord/service.py 2010-03-10 21:40:30 +0000 | |||
3889 | @@ -0,0 +1,260 @@ | |||
3890 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
3891 | 2 | # | ||
3892 | 3 | # This program is free software: you can redistribute it and/or modify | ||
3893 | 4 | # it under the terms of the GNU General Public License as published by | ||
3894 | 5 | # the Free Software Foundation, version 3 of the License. | ||
3895 | 6 | # | ||
3896 | 7 | # This program is distributed in the hope that it will be useful, | ||
3897 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3898 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3899 | 10 | # GNU General Public License for more details. | ||
3900 | 11 | # | ||
3901 | 12 | # You should have received a copy of the GNU General Public License | ||
3902 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3903 | 14 | |||
3904 | 15 | """dbus service for cleaning up crufty packages that are no longer needed.""" | ||
3905 | 16 | |||
3906 | 17 | |||
3907 | 18 | from __future__ import absolute_import, unicode_literals | ||
3908 | 19 | |||
3909 | 20 | __metaclass__ = type | ||
3910 | 21 | __all__ = [ | ||
3911 | 22 | 'Service', | ||
3912 | 23 | ] | ||
3913 | 24 | |||
3914 | 25 | |||
3915 | 26 | import atexit | ||
3916 | 27 | import logging | ||
3917 | 28 | |||
3918 | 29 | import dbus.service | ||
3919 | 30 | |||
3920 | 31 | from computerjanitor import PackageCruft | ||
3921 | 32 | from computerjanitord.application import Application | ||
3922 | 33 | from computerjanitord.authenticator import Authenticator | ||
3923 | 34 | from computerjanitord.collector import Collector | ||
3924 | 35 | from computerjanitord.errors import NoSuchCruftError, PermissionDeniedError | ||
3925 | 36 | from computerjanitord.state import State, DEFAULT_STATE_FILE | ||
3926 | 37 | |||
3927 | 38 | |||
3928 | 39 | log = logging.getLogger('computerjanitor') | ||
3929 | 40 | MISSING = object() | ||
3930 | 41 | |||
3931 | 42 | DBUS_INTERFACE_NAME = 'com.ubuntu.ComputerJanitor' | ||
3932 | 43 | PRIVILEGE = 'com.ubuntu.computerjanitor.updatesystem' | ||
3933 | 44 | |||
3934 | 45 | |||
3935 | 46 | class Service(dbus.service.Object): | ||
3936 | 47 | """Backend dbus service that handles removing crufty packages.""" | ||
3937 | 48 | |||
3938 | 49 | def __init__(self, options): | ||
3939 | 50 | """Create the dbus service. | ||
3940 | 51 | |||
3941 | 52 | :param options: The command line options class. | ||
3942 | 53 | :type options: `Options` | ||
3943 | 54 | """ | ||
3944 | 55 | self.dry_run = options.arguments.dry_run | ||
3945 | 56 | self.state_file = (DEFAULT_STATE_FILE | ||
3946 | 57 | if options.arguments.state_file is None | ||
3947 | 58 | else options.arguments.state_file) | ||
3948 | 59 | self.application = Application() | ||
3949 | 60 | self.state = State() | ||
3950 | 61 | self.state.load(self.state_file) | ||
3951 | 62 | self.collector = Collector(self.application, service=self) | ||
3952 | 63 | self.authenticator = Authenticator() | ||
3953 | 64 | bus_name = dbus.service.BusName( | ||
3954 | 65 | DBUS_INTERFACE_NAME, bus=dbus.SystemBus()) | ||
3955 | 66 | dbus.service.Object.__init__(self, bus_name, '/') | ||
3956 | 67 | # We can't use the decorator because that doesn't work with methods; | ||
3957 | 68 | # self doesn't get passed to the handler. | ||
3958 | 69 | atexit.register(self._exit_handler) | ||
3959 | 70 | |||
3960 | 71 | def _exit_handler(self): | ||
3961 | 72 | """Ensure that the state file is saved at exit.""" | ||
3962 | 73 | if not self.dry_run: | ||
3963 | 74 | self.state.save(self.state_file) | ||
3964 | 75 | |||
3965 | 76 | def _authenticate(self, sender, connection): | ||
3966 | 77 | """Authenticate via PolicyKit. | ||
3967 | 78 | |||
3968 | 79 | :param sender: The dbus client sender. | ||
3969 | 80 | :param connection: The dbus client connection. | ||
3970 | 81 | :raises PermissionDeniedError: when the authentication fails. | ||
3971 | 82 | """ | ||
3972 | 83 | if not self.authenticator.authenticate(sender, connection, PRIVILEGE): | ||
3973 | 84 | log.error('Permission denied: {0} for {1} on {2}'.format( | ||
3974 | 85 | PRIVILEGE, sender, connection)) | ||
3975 | 86 | raise PermissionDeniedError(PRIVILEGE) | ||
3976 | 87 | log.debug('Permission granted: {0} for {1} on {2}'.format( | ||
3977 | 88 | PRIVILEGE, sender, connection)) | ||
3978 | 89 | |||
3979 | 90 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
3980 | 91 | out_signature='as') | ||
3981 | 92 | def find(self): | ||
3982 | 93 | """Find all the non-whitelisted cruft on the system. | ||
3983 | 94 | |||
3984 | 95 | Because this is a read-only interface it does not need authorization | ||
3985 | 96 | to be called. | ||
3986 | 97 | |||
3987 | 98 | :return: A list of matching cruft names. | ||
3988 | 99 | """ | ||
3989 | 100 | return list(cruft.get_name() for cruft in self.collector.cruft) | ||
3990 | 101 | |||
3991 | 102 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
3992 | 103 | out_signature='as', | ||
3993 | 104 | # Must wrap these in str() because Python < 2.6.5 | ||
3994 | 105 | # does not like unicode keyword arguments. | ||
3995 | 106 | sender_keyword=str('sender'), | ||
3996 | 107 | connection_keyword=str('connection')) | ||
3997 | 108 | def load(self, sender=None, connection=None): | ||
3998 | 109 | """Load the state file.""" | ||
3999 | 110 | self._authenticate(sender, connection) | ||
4000 | 111 | self.state.load(self.state_file) | ||
4001 | 112 | return list(self.state.ignore) | ||
4002 | 113 | |||
4003 | 114 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4004 | 115 | # Must wrap these in str() because Python < 2.6.5 | ||
4005 | 116 | # does not like unicode keyword arguments. | ||
4006 | 117 | sender_keyword=str('sender'), | ||
4007 | 118 | connection_keyword=str('connection')) | ||
4008 | 119 | def save(self, sender=None, connection=None): | ||
4009 | 120 | """Save the state file.""" | ||
4010 | 121 | self._authenticate(sender, connection) | ||
4011 | 122 | if not self.dry_run: | ||
4012 | 123 | self.state.save(self.state_file) | ||
4013 | 124 | |||
4014 | 125 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4015 | 126 | in_signature='s', | ||
4016 | 127 | # Must wrap these in str() because Python < 2.6.5 | ||
4017 | 128 | # does not like unicode keyword arguments. | ||
4018 | 129 | sender_keyword=str('sender'), | ||
4019 | 130 | connection_keyword=str('connection')) | ||
4020 | 131 | def ignore(self, name, sender=None, connection=None): | ||
4021 | 132 | """Ignore the named cruft. | ||
4022 | 133 | |||
4023 | 134 | :param name: The name of the cruft to ignore. | ||
4024 | 135 | :type filename: string | ||
4025 | 136 | """ | ||
4026 | 137 | # Make sure this is known cruft first. | ||
4027 | 138 | cruft = self.collector.cruft_by_name.get(name, MISSING) | ||
4028 | 139 | if cruft is MISSING: | ||
4029 | 140 | log.error('ignore(): No such cruft: {0}'.format(name)) | ||
4030 | 141 | raise NoSuchCruftError(name) | ||
4031 | 142 | self._authenticate(sender, connection) | ||
4032 | 143 | if not self.dry_run: | ||
4033 | 144 | self.state.ignore.add(name) | ||
4034 | 145 | |||
4035 | 146 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4036 | 147 | in_signature='s', | ||
4037 | 148 | # Must wrap these in str() because Python < 2.6.5 | ||
4038 | 149 | # does not like unicode keyword arguments. | ||
4039 | 150 | sender_keyword=str('sender'), | ||
4040 | 151 | connection_keyword=str('connection')) | ||
4041 | 152 | def unignore(self, name, sender=None, connection=None): | ||
4042 | 153 | """Unignore the named cruft. | ||
4043 | 154 | |||
4044 | 155 | :param name: The name of the cruft to unignore. | ||
4045 | 156 | :type filename: string | ||
4046 | 157 | """ | ||
4047 | 158 | cruft = self.collector.cruft_by_name.get(name, MISSING) | ||
4048 | 159 | if cruft is MISSING: | ||
4049 | 160 | log.error('ignore(): No such cruft: {0}'.format(name)) | ||
4050 | 161 | raise NoSuchCruftError(name) | ||
4051 | 162 | self._authenticate(sender, connection) | ||
4052 | 163 | if not self.dry_run: | ||
4053 | 164 | # Don't worry if we're already not ignoring the cruft (i.e. don't | ||
4054 | 165 | # raise a KeyError here if 'name' is not in the set). | ||
4055 | 166 | self.state.ignore.discard(name) | ||
4056 | 167 | |||
4057 | 168 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4058 | 169 | out_signature='as') | ||
4059 | 170 | def ignored(self): | ||
4060 | 171 | """Return the list of ignored cruft. | ||
4061 | 172 | |||
4062 | 173 | :return: The names of the ignored cruft. | ||
4063 | 174 | :rtype: list of strings | ||
4064 | 175 | """ | ||
4065 | 176 | return list(self.state.ignore) | ||
4066 | 177 | |||
4067 | 178 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4068 | 179 | in_signature='s', | ||
4069 | 180 | out_signature='s') | ||
4070 | 181 | def get_description(self, name): | ||
4071 | 182 | """Return the description of the named cruft. | ||
4072 | 183 | |||
4073 | 184 | :param name: The cruft name. | ||
4074 | 185 | :type name: string | ||
4075 | 186 | :return: The description of the cruft. | ||
4076 | 187 | :rtype: string | ||
4077 | 188 | """ | ||
4078 | 189 | cruft = self.collector.cruft_by_name.get(name, MISSING) | ||
4079 | 190 | if cruft is MISSING: | ||
4080 | 191 | log.error('get_description(): No such cruft: {0}'.format(name)) | ||
4081 | 192 | raise NoSuchCruftError(name) | ||
4082 | 193 | return cruft.get_description() | ||
4083 | 194 | |||
4084 | 195 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4085 | 196 | in_signature='s', | ||
4086 | 197 | out_signature='s') | ||
4087 | 198 | def get_shortname(self, name): | ||
4088 | 199 | """Return the short name of the named cruft. | ||
4089 | 200 | |||
4090 | 201 | :param name: The cruft name. | ||
4091 | 202 | :type name: string | ||
4092 | 203 | :return: The short nameof the cruft. | ||
4093 | 204 | :rtype: string | ||
4094 | 205 | """ | ||
4095 | 206 | cruft = self.collector.cruft_by_name.get(name, MISSING) | ||
4096 | 207 | if cruft is MISSING: | ||
4097 | 208 | log.error('get_shortname(): No such cruft: {0}'.format(name)) | ||
4098 | 209 | raise NoSuchCruftError(name) | ||
4099 | 210 | return cruft.get_shortname() | ||
4100 | 211 | |||
4101 | 212 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4102 | 213 | in_signature='s', | ||
4103 | 214 | out_signature='st') | ||
4104 | 215 | def get_details(self, name): | ||
4105 | 216 | """Return some extra details about the named cruft. | ||
4106 | 217 | |||
4107 | 218 | :param name: The cruft name. | ||
4108 | 219 | :type name: string | ||
4109 | 220 | :return: Some extra details about the named cruft, specifically its | ||
4110 | 221 | 'type' and the amount of disk space it consumes. The type is | ||
4111 | 222 | simply the name of the cruft instance's class. | ||
4112 | 223 | :rtype: string, uint64 | ||
4113 | 224 | """ | ||
4114 | 225 | cruft = self.collector.cruft_by_name.get(name, MISSING) | ||
4115 | 226 | if cruft is MISSING: | ||
4116 | 227 | log.error('get_shortname(): No such cruft: {0}'.format(name)) | ||
4117 | 228 | raise NoSuchCruftError(name) | ||
4118 | 229 | return cruft.__class__.__name__, cruft.get_disk_usage() | ||
4119 | 230 | |||
4120 | 231 | @dbus.service.method(DBUS_INTERFACE_NAME, | ||
4121 | 232 | in_signature='as', # array of strings | ||
4122 | 233 | # Must wrap these in str() because Python < 2.6.5 | ||
4123 | 234 | # does not like unicode keyword arguments. | ||
4124 | 235 | sender_keyword=str('sender'), | ||
4125 | 236 | connection_keyword=str('connection')) | ||
4126 | 237 | def clean(self, names, sender=None, connection=None): | ||
4127 | 238 | """Clean the named crufts. | ||
4128 | 239 | |||
4129 | 240 | :param names: The names of the cruft to clean. | ||
4130 | 241 | :type names: list of strings | ||
4131 | 242 | """ | ||
4132 | 243 | self._authenticate(sender, connection) | ||
4133 | 244 | self.collector.clean(names, self.dry_run) | ||
4134 | 245 | |||
4135 | 246 | @dbus.service.signal(DBUS_INTERFACE_NAME, | ||
4136 | 247 | signature='s') | ||
4137 | 248 | def cleanup_status(self, cruft): | ||
4138 | 249 | """Signal cleanup status. | ||
4139 | 250 | |||
4140 | 251 | This signal is used to incrementally inform clients that some cleanup | ||
4141 | 252 | work is being done. It is called at the beginning of the cleanup | ||
4142 | 253 | process and after each plugin has completed its `post_cleanup()` | ||
4143 | 254 | method. | ||
4144 | 255 | |||
4145 | 256 | :param done: The name of the next piece of cruft to be cleaned up, or | ||
4146 | 257 | the empty string when there's nothing left to do. | ||
4147 | 258 | :type done: string | ||
4148 | 259 | """ | ||
4149 | 260 | log.debug('cleanup_status: {0}'.format(cruft)) | ||
4150 | 0 | 261 | ||
4151 | === added file 'computerjanitord/state.py' | |||
4152 | --- computerjanitord/state.py 1970-01-01 00:00:00 +0000 | |||
4153 | +++ computerjanitord/state.py 2010-03-10 21:40:30 +0000 | |||
4154 | @@ -0,0 +1,92 @@ | |||
4155 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4156 | 2 | # | ||
4157 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4158 | 4 | # it under the terms of the GNU General Public License as published by | ||
4159 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4160 | 6 | # | ||
4161 | 7 | # This program is distributed in the hope that it will be useful, | ||
4162 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4163 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4164 | 10 | # GNU General Public License for more details. | ||
4165 | 11 | # | ||
4166 | 12 | # You should have received a copy of the GNU General Public License | ||
4167 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4168 | 14 | |||
4169 | 15 | """Maintaining package ignored state.""" | ||
4170 | 16 | |||
4171 | 17 | from __future__ import absolute_import, unicode_literals | ||
4172 | 18 | |||
4173 | 19 | __metaclass__ = type | ||
4174 | 20 | __all__ = [ | ||
4175 | 21 | 'DEFAULT_STATE_FILE', | ||
4176 | 22 | 'State', | ||
4177 | 23 | ] | ||
4178 | 24 | |||
4179 | 25 | |||
4180 | 26 | import ConfigParser | ||
4181 | 27 | import textwrap | ||
4182 | 28 | |||
4183 | 29 | |||
4184 | 30 | DEFAULT_STATE_FILE = '/var/lib/computer-janitor/state.dat' | ||
4185 | 31 | |||
4186 | 32 | |||
4187 | 33 | class State: | ||
4188 | 34 | """Maintain the state of cruft which should be ignored. | ||
4189 | 35 | |||
4190 | 36 | The file's format is a `ConfigParser` style .ini file. Each section is a | ||
4191 | 37 | cruft's `.get_name()` value and contains a single boolean setting | ||
4192 | 38 | `ignore`. If true, then the cruft is ignored. | ||
4193 | 39 | |||
4194 | 40 | For backward compatibility purposes, the setting `enabled` is also | ||
4195 | 41 | recognized (on read only). If `enabled` is false then the cruft is | ||
4196 | 42 | ignored. | ||
4197 | 43 | """ | ||
4198 | 44 | |||
4199 | 45 | def __init__(self): | ||
4200 | 46 | self.ignore = set() | ||
4201 | 47 | |||
4202 | 48 | def load(self, filename): | ||
4203 | 49 | """Load ignored state from a file. | ||
4204 | 50 | |||
4205 | 51 | This reset any previously determined state and re-initializes it with | ||
4206 | 52 | the state stored in the file. | ||
4207 | 53 | |||
4208 | 54 | :param filename: The file to load. | ||
4209 | 55 | :type filename: string | ||
4210 | 56 | """ | ||
4211 | 57 | parser = ConfigParser.ConfigParser() | ||
4212 | 58 | parser.read(filename) | ||
4213 | 59 | # Reset the set of ignored packages. | ||
4214 | 60 | self.ignore = set() | ||
4215 | 61 | for cruft_name in parser.sections(): | ||
4216 | 62 | # For backwards compatibility, recognize both the 'ignore' setting | ||
4217 | 63 | # and the 'enabled' setting. We only write the former. | ||
4218 | 64 | try: | ||
4219 | 65 | ignore = parser.getboolean(cruft_name, 'ignore') | ||
4220 | 66 | except ConfigParser.NoOptionError: | ||
4221 | 67 | try: | ||
4222 | 68 | ignore = not parser.getboolean(cruft_name, 'enabled') | ||
4223 | 69 | except ConfigParser.NoOptionError: | ||
4224 | 70 | # No other settings are recognized. | ||
4225 | 71 | ignore = False | ||
4226 | 72 | if ignore: | ||
4227 | 73 | self.ignore.add(cruft_name) | ||
4228 | 74 | |||
4229 | 75 | def save(self, filename): | ||
4230 | 76 | """Save the ignored state to a file. | ||
4231 | 77 | |||
4232 | 78 | Only the packages being ignored are stored to the file, and writing | ||
4233 | 79 | overwrites the previous contents of the file. | ||
4234 | 80 | |||
4235 | 81 | :param filename: The file to load. | ||
4236 | 82 | :type filename: string | ||
4237 | 83 | """ | ||
4238 | 84 | # It's easier just to write the .ini file directly instead of using | ||
4239 | 85 | # the ConfigParser interface. This way we can guarantee sort order | ||
4240 | 86 | # and can automatically cull unignored packages from the file. | ||
4241 | 87 | with open(filename, 'w') as fp: | ||
4242 | 88 | for cruft_name in self.ignore: | ||
4243 | 89 | print >> fp, textwrap.dedent("""\ | ||
4244 | 90 | [{0}] | ||
4245 | 91 | ignore: true | ||
4246 | 92 | """.format(cruft_name)) | ||
4247 | 0 | 93 | ||
4248 | === added directory 'computerjanitord/tests' | |||
4249 | === added file 'computerjanitord/tests/__init__.py' | |||
4250 | === added directory 'computerjanitord/tests/data' | |||
4251 | === added file 'computerjanitord/tests/data/empty' | |||
4252 | === added directory 'computerjanitord/tests/data/etc' | |||
4253 | === added directory 'computerjanitord/tests/data/etc/apt' | |||
4254 | === added file 'computerjanitord/tests/data/etc/apt/sources.list' | |||
4255 | --- computerjanitord/tests/data/etc/apt/sources.list 1970-01-01 00:00:00 +0000 | |||
4256 | +++ computerjanitord/tests/data/etc/apt/sources.list 2010-03-10 21:40:30 +0000 | |||
4257 | @@ -0,0 +1,1 @@ | |||
4258 | 1 | deb http://archive.ubuntu.com/ubuntu intrepid main restricted | ||
4259 | 0 | 2 | ||
4260 | === added directory 'computerjanitord/tests/data/var' | |||
4261 | === added directory 'computerjanitord/tests/data/var/cache' | |||
4262 | === added directory 'computerjanitord/tests/data/var/cache/apt' | |||
4263 | === added directory 'computerjanitord/tests/data/var/cache/apt/archives' | |||
4264 | === added directory 'computerjanitord/tests/data/var/cache/apt/archives/partial' | |||
4265 | === added directory 'computerjanitord/tests/data/var/lib' | |||
4266 | === added directory 'computerjanitord/tests/data/var/lib/apt' | |||
4267 | === added directory 'computerjanitord/tests/data/var/lib/apt/lists' | |||
4268 | === added file 'computerjanitord/tests/data/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_intrepid_restricted_binary-i386_Packages' | |||
4269 | --- computerjanitord/tests/data/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_intrepid_restricted_binary-i386_Packages 1970-01-01 00:00:00 +0000 | |||
4270 | +++ computerjanitord/tests/data/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_intrepid_restricted_binary-i386_Packages 2010-03-10 21:40:30 +0000 | |||
4271 | @@ -0,0 +1,54 @@ | |||
4272 | 1 | Package: dash | ||
4273 | 2 | Priority: required | ||
4274 | 3 | Section: shells | ||
4275 | 4 | Installed-Size: 236 | ||
4276 | 5 | Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com> | ||
4277 | 6 | Original-Maintainer: Gerrit Pape <pape@smarden.org> | ||
4278 | 7 | Architecture: all | ||
4279 | 8 | Version: 0.5.5.1-3ubuntu1 | ||
4280 | 9 | Depends: debianutils (>= 2.15), dpkg (>= 1.15.0) | ||
4281 | 10 | Pre-Depends: libc6 (>= 2.11~20100104-0ubuntu2) | ||
4282 | 11 | Filename: pool/main/d/dash/dash_0.5.5.1-3ubuntu1_all.deb | ||
4283 | 12 | Size: 104190 | ||
4284 | 13 | MD5sum: a7f08fe3ee941d06c0d98e5e99c02190 | ||
4285 | 14 | SHA1: d2dda78f9a6f82c58c01d34f18b94aebfcb33f19 | ||
4286 | 15 | SHA256: 69709747f854ac1bd671dff37b47c18e589b095ecb8f3116beaed7fc0eeb657e | ||
4287 | 16 | Description: POSIX-compliant shell | ||
4288 | 17 | The Debian Almquist Shell (dash) is a POSIX-compliant shell derived | ||
4289 | 18 | from ash. | ||
4290 | 19 | . | ||
4291 | 20 | Since it executes scripts faster than bash, and has fewer library | ||
4292 | 21 | dependencies (making it more robust against software or hardware | ||
4293 | 22 | failures), it is used as the default system shell on Debian systems. | ||
4294 | 23 | Homepage: http://gondor.apana.org.au/~herbert/dash/ | ||
4295 | 24 | Bugs: https://bugs.launchpad.net/ubuntu/+filebug | ||
4296 | 25 | Origin: Ubuntu | ||
4297 | 26 | Supported: 5y | ||
4298 | 27 | Task: minimal | ||
4299 | 28 | |||
4300 | 29 | Package: gzip | ||
4301 | 30 | Essential: yes | ||
4302 | 31 | Priority: required | ||
4303 | 32 | Section: utils | ||
4304 | 33 | Installed-Size: 284 | ||
4305 | 34 | Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com> | ||
4306 | 35 | Original-Maintainer: Bdale Garbee <bdale@gag.com> | ||
4307 | 36 | Architecture: all | ||
4308 | 37 | Version: 1.3.12-9ubuntu1 | ||
4309 | 38 | Pre-Depends: libc6 (>= 2.4) | ||
4310 | 39 | Suggests: less | ||
4311 | 40 | Filename: pool/main/g/gzip/gzip_1.3.12-9ubuntu1_all.deb | ||
4312 | 41 | Size: 107030 | ||
4313 | 42 | MD5sum: f64beb93d2d1a3348cfc47f1fd176ee1 | ||
4314 | 43 | SHA1: 3dd3e56f551fb85ba2ad385df463adeff1fff2d9 | ||
4315 | 44 | SHA256: 2545f0a28514535006adf9ee8576ca3be2aa6da3d890047f92eeda18c0e3aa57 | ||
4316 | 45 | Description: GNU compression utilities | ||
4317 | 46 | This package provides the standard GNU file compression utilities, which | ||
4318 | 47 | are also the default compression tools for Debian. They typically operate | ||
4319 | 48 | on files with names ending in '.gz', but can also decompress files ending | ||
4320 | 49 | in '.Z' created with 'compress'. | ||
4321 | 50 | Bugs: https://bugs.launchpad.net/ubuntu/+filebug | ||
4322 | 51 | Origin: Ubuntu | ||
4323 | 52 | Supported: 5y | ||
4324 | 53 | Task: minimal | ||
4325 | 54 | |||
4326 | 0 | 55 | ||
4327 | === added directory 'computerjanitord/tests/data/var/lib/apt/lists/partial' | |||
4328 | === added directory 'computerjanitord/tests/data/var/lib/dpkg' | |||
4329 | === added file 'computerjanitord/tests/data/var/lib/dpkg/status' | |||
4330 | --- computerjanitord/tests/data/var/lib/dpkg/status 1970-01-01 00:00:00 +0000 | |||
4331 | +++ computerjanitord/tests/data/var/lib/dpkg/status 2010-03-10 21:40:30 +0000 | |||
4332 | @@ -0,0 +1,28 @@ | |||
4333 | 1 | Package: dash-nodownload | ||
4334 | 2 | Priority: required | ||
4335 | 3 | Section: shells | ||
4336 | 4 | Installed-Size: 236 | ||
4337 | 5 | Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com> | ||
4338 | 6 | Original-Maintainer: Gerrit Pape <pape@smarden.org> | ||
4339 | 7 | Architecture: all | ||
4340 | 8 | Version: 0.5.5.1-3ubuntu1 | ||
4341 | 9 | Depends: debianutils (>= 2.15), dpkg (>= 1.15.0) | ||
4342 | 10 | Pre-Depends: libc6 (>= 2.11~20100104-0ubuntu2) | ||
4343 | 11 | Filename: pool/main/d/dash/dash_0.5.5.1-3ubuntu1_all.deb | ||
4344 | 12 | Size: 104190 | ||
4345 | 13 | MD5sum: a7f08fe3ee941d06c0d98e5e99c02190 | ||
4346 | 14 | SHA1: d2dda78f9a6f82c58c01d34f18b94aebfcb33f19 | ||
4347 | 15 | SHA256: 69709747f854ac1bd671dff37b47c18e589b095ecb8f3116beaed7fc0eeb657e | ||
4348 | 16 | Description: POSIX-compliant shell | ||
4349 | 17 | The Debian Almquist Shell (dash) is a POSIX-compliant shell derived | ||
4350 | 18 | from ash. | ||
4351 | 19 | . | ||
4352 | 20 | Since it executes scripts faster than bash, and has fewer library | ||
4353 | 21 | dependencies (making it more robust against software or hardware | ||
4354 | 22 | failures), it is used as the default system shell on Debian systems. | ||
4355 | 23 | Homepage: http://gondor.apana.org.au/~herbert/dash/ | ||
4356 | 24 | Bugs: https://bugs.launchpad.net/ubuntu/+filebug | ||
4357 | 25 | Origin: Ubuntu | ||
4358 | 26 | Supported: 5y | ||
4359 | 27 | Task: minimal | ||
4360 | 28 | |||
4361 | 0 | 29 | ||
4362 | === added file 'computerjanitord/tests/test_all.py' | |||
4363 | --- computerjanitord/tests/test_all.py 1970-01-01 00:00:00 +0000 | |||
4364 | +++ computerjanitord/tests/test_all.py 2010-03-10 21:40:30 +0000 | |||
4365 | @@ -0,0 +1,41 @@ | |||
4366 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4367 | 2 | # | ||
4368 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4369 | 4 | # it under the terms of the GNU General Public License as published by | ||
4370 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4371 | 6 | # | ||
4372 | 7 | # This program is distributed in the hope that it will be useful, | ||
4373 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4374 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4375 | 10 | # GNU General Public License for more details. | ||
4376 | 11 | # | ||
4377 | 12 | # You should have received a copy of the GNU General Public License | ||
4378 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4379 | 14 | |||
4380 | 15 | """Test suite for Computer Janitor daemon (dbus backend).""" | ||
4381 | 16 | |||
4382 | 17 | from __future__ import absolute_import, unicode_literals | ||
4383 | 18 | |||
4384 | 19 | __metaclass__ = type | ||
4385 | 20 | __all__ = [ | ||
4386 | 21 | 'test_suite', | ||
4387 | 22 | ] | ||
4388 | 23 | |||
4389 | 24 | |||
4390 | 25 | import unittest | ||
4391 | 26 | |||
4392 | 27 | from computerjanitord.tests import test_application | ||
4393 | 28 | from computerjanitord.tests import test_authenticator | ||
4394 | 29 | from computerjanitord.tests import test_collector | ||
4395 | 30 | from computerjanitord.tests import test_state | ||
4396 | 31 | from computerjanitord.tests import test_whitelist | ||
4397 | 32 | |||
4398 | 33 | |||
4399 | 34 | def test_suite(): | ||
4400 | 35 | suite = unittest.TestSuite() | ||
4401 | 36 | suite.addTests(test_application.test_suite()) | ||
4402 | 37 | suite.addTests(test_authenticator.test_suite()) | ||
4403 | 38 | suite.addTests(test_collector.test_suite()) | ||
4404 | 39 | suite.addTests(test_state.test_suite()) | ||
4405 | 40 | suite.addTests(test_whitelist.test_suite()) | ||
4406 | 41 | return suite | ||
4407 | 0 | 42 | ||
4408 | === added file 'computerjanitord/tests/test_application.py' | |||
4409 | --- computerjanitord/tests/test_application.py 1970-01-01 00:00:00 +0000 | |||
4410 | +++ computerjanitord/tests/test_application.py 2010-03-10 21:40:30 +0000 | |||
4411 | @@ -0,0 +1,106 @@ | |||
4412 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4413 | 2 | # | ||
4414 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4415 | 4 | # it under the terms of the GNU General Public License as published by | ||
4416 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4417 | 6 | # | ||
4418 | 7 | # This program is distributed in the hope that it will be useful, | ||
4419 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4420 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4421 | 10 | # GNU General Public License for more details. | ||
4422 | 11 | # | ||
4423 | 12 | # You should have received a copy of the GNU General Public License | ||
4424 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4425 | 14 | |||
4426 | 15 | """Test the plugin application interfaces.""" | ||
4427 | 16 | |||
4428 | 17 | from __future__ import absolute_import, unicode_literals | ||
4429 | 18 | |||
4430 | 19 | __metaclass__ = type | ||
4431 | 20 | __all__ = [ | ||
4432 | 21 | 'ApplicationTestSetupMixin', | ||
4433 | 22 | 'test_suite', | ||
4434 | 23 | ] | ||
4435 | 24 | |||
4436 | 25 | |||
4437 | 26 | import os | ||
4438 | 27 | import apt | ||
4439 | 28 | import apt_pkg | ||
4440 | 29 | import unittest | ||
4441 | 30 | import warnings | ||
4442 | 31 | import pkg_resources | ||
4443 | 32 | |||
4444 | 33 | from contextlib import contextmanager | ||
4445 | 34 | |||
4446 | 35 | import computerjanitor | ||
4447 | 36 | import computerjanitord.application | ||
4448 | 37 | |||
4449 | 38 | from computerjanitord.application import ( | ||
4450 | 39 | Application, MissingLandmarkError, NonDownloadableError) | ||
4451 | 40 | |||
4452 | 41 | |||
4453 | 42 | @contextmanager | ||
4454 | 43 | def landmarks(*packages): | ||
4455 | 44 | # Hack the module global list of known landmark packages. | ||
4456 | 45 | old_landmarks = computerjanitord.application.LANDMARK_PACKAGES[:] | ||
4457 | 46 | computerjanitord.application.LANDMARK_PACKAGES[:] = packages | ||
4458 | 47 | yield | ||
4459 | 48 | computerjanitord.application.LANDMARK_PACKAGES[:] = old_landmarks | ||
4460 | 49 | |||
4461 | 50 | |||
4462 | 51 | class MockCruft: | ||
4463 | 52 | def __init__(self, name): | ||
4464 | 53 | self.name = name | ||
4465 | 54 | |||
4466 | 55 | def get_name(self): | ||
4467 | 56 | warnings.warn('.get_name() is deprecated; use .name', | ||
4468 | 57 | DeprecationWarning) | ||
4469 | 58 | return self.name | ||
4470 | 59 | |||
4471 | 60 | |||
4472 | 61 | class ApplicationTestSetupMixin: | ||
4473 | 62 | """Set up an `Application` instance with test data in its apt_cache.""" | ||
4474 | 63 | |||
4475 | 64 | def setUp(self): | ||
4476 | 65 | self.data_dir = os.path.abspath( | ||
4477 | 66 | pkg_resources.resource_filename('computerjanitord.tests', 'data')) | ||
4478 | 67 | # Make the test insensitive to the platform's architecture. | ||
4479 | 68 | apt_pkg.Config.Set('APT::Architecture', 'i386') | ||
4480 | 69 | self.cache = apt.Cache(rootdir=self.data_dir) | ||
4481 | 70 | self.app = Application(self.cache) | ||
4482 | 71 | |||
4483 | 72 | def tearDown(self): | ||
4484 | 73 | # Clear the cache. | ||
4485 | 74 | cache_dir = os.path.join(self.data_dir, 'var', 'cache', 'apt') | ||
4486 | 75 | for filename in os.listdir(cache_dir): | ||
4487 | 76 | if filename.endswith('.bin'): | ||
4488 | 77 | os.remove(os.path.join(cache_dir, filename)) | ||
4489 | 78 | |||
4490 | 79 | |||
4491 | 80 | class TestApplication(unittest.TestCase, ApplicationTestSetupMixin): | ||
4492 | 81 | """Test the `Application` interface.""" | ||
4493 | 82 | |||
4494 | 83 | def setUp(self): | ||
4495 | 84 | ApplicationTestSetupMixin.setUp(self) | ||
4496 | 85 | |||
4497 | 86 | def tearDown(self): | ||
4498 | 87 | ApplicationTestSetupMixin.tearDown(self) | ||
4499 | 88 | |||
4500 | 89 | def test_verify_apt_cache_good_path(self): | ||
4501 | 90 | # All essential packages are in the cache by default. | ||
4502 | 91 | self.assertEqual(self.app.verify_apt_cache(), None) | ||
4503 | 92 | |||
4504 | 93 | def test_verify_apt_cache_with_nondownloadable_landmark(self): | ||
4505 | 94 | # Test that a missing landmark file causes an exception. | ||
4506 | 95 | with landmarks('gzip', 'dash-nodownload'): | ||
4507 | 96 | self.assertRaises(NonDownloadableError, self.app.verify_apt_cache) | ||
4508 | 97 | |||
4509 | 98 | def test_verify_apt_cache_with_missing_landmark(self): | ||
4510 | 99 | with landmarks('gzip', 'dash', 'i-am-not-here'): | ||
4511 | 100 | self.assertRaises(MissingLandmarkError, self.app.verify_apt_cache) | ||
4512 | 101 | |||
4513 | 102 | |||
4514 | 103 | def test_suite(): | ||
4515 | 104 | suite = unittest.TestSuite() | ||
4516 | 105 | suite.addTests(unittest.makeSuite(TestApplication)) | ||
4517 | 106 | return suite | ||
4518 | 0 | 107 | ||
4519 | === added file 'computerjanitord/tests/test_authenticator.py' | |||
4520 | --- computerjanitord/tests/test_authenticator.py 1970-01-01 00:00:00 +0000 | |||
4521 | +++ computerjanitord/tests/test_authenticator.py 2010-03-10 21:40:30 +0000 | |||
4522 | @@ -0,0 +1,97 @@ | |||
4523 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4524 | 2 | # | ||
4525 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4526 | 4 | # it under the terms of the GNU General Public License as published by | ||
4527 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4528 | 6 | # | ||
4529 | 7 | # This program is distributed in the hope that it will be useful, | ||
4530 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4531 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4532 | 10 | # GNU General Public License for more details. | ||
4533 | 11 | # | ||
4534 | 12 | # You should have received a copy of the GNU General Public License | ||
4535 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4536 | 14 | |||
4537 | 15 | """Test the authenticator.""" | ||
4538 | 16 | |||
4539 | 17 | import unittest | ||
4540 | 18 | |||
4541 | 19 | from computerjanitord.authenticator import Authenticator | ||
4542 | 20 | |||
4543 | 21 | SUCCESS_PID = 801 | ||
4544 | 22 | FAILURE_PID = 999 | ||
4545 | 23 | |||
4546 | 24 | AUTHENTICATED_USER = 'desktop-user' | ||
4547 | 25 | IMPOSTER_USER = 'imposter' | ||
4548 | 26 | EXPECTED_PRIVILEGE = 'com.ubuntu.computerjanitor.cleanpackages' | ||
4549 | 27 | BOGUS_PRIVILEGE = 'com.example.evil-corp.killsystem' | ||
4550 | 28 | |||
4551 | 29 | |||
4552 | 30 | class MockPolicyKit(object): | ||
4553 | 31 | """Mock the PolicyKit's CheckAuthorization() method.""" | ||
4554 | 32 | |||
4555 | 33 | def CheckAuthorization(self, subject, privilege, details, flags, | ||
4556 | 34 | cancellation_id): | ||
4557 | 35 | """See `policykit.CheckAuthorization()`. | ||
4558 | 36 | |||
4559 | 37 | :return: (is_authorized, is_challenge, details) | ||
4560 | 38 | """ | ||
4561 | 39 | if privilege == BOGUS_PRIVILEGE: | ||
4562 | 40 | return False, False, '' | ||
4563 | 41 | assert isinstance(subject, tuple) and len(subject) == 2, ( | ||
4564 | 42 | 'subject is not a 2-tuple') | ||
4565 | 43 | assert subject[0] == 'unix-process', 'Badly formed subject' | ||
4566 | 44 | assert isinstance(subject[1], dict), 'Badly formed subject details' | ||
4567 | 45 | assert subject[1]['start-time'] == 0, 'subject missing start-time' | ||
4568 | 46 | if subject[1]['pid'] == SUCCESS_PID: | ||
4569 | 47 | return True, False, '' | ||
4570 | 48 | else: | ||
4571 | 49 | return False, False, '' | ||
4572 | 50 | |||
4573 | 51 | |||
4574 | 52 | class TestableAuthenticator(Authenticator): | ||
4575 | 53 | """See `Authenticator`.""" | ||
4576 | 54 | |||
4577 | 55 | def _get_policykit_proxy(self): | ||
4578 | 56 | """See `Authenticator`.""" | ||
4579 | 57 | return MockPolicyKit() | ||
4580 | 58 | |||
4581 | 59 | def _get_sender_pid(self, connection, sender): | ||
4582 | 60 | """See `Authenticator`.""" | ||
4583 | 61 | if sender == AUTHENTICATED_USER: | ||
4584 | 62 | return SUCCESS_PID | ||
4585 | 63 | else: | ||
4586 | 64 | return FAILURE_PID | ||
4587 | 65 | |||
4588 | 66 | |||
4589 | 67 | class TestAuthenticator(unittest.TestCase): | ||
4590 | 68 | """Tests of the PolicyKit authenticator.""" | ||
4591 | 69 | |||
4592 | 70 | def setUp(self): | ||
4593 | 71 | """See `unittest.TestCase`.""" | ||
4594 | 72 | self.authenticator = TestableAuthenticator() | ||
4595 | 73 | self.connection = object() | ||
4596 | 74 | |||
4597 | 75 | def tearDown(self): | ||
4598 | 76 | """See `unittest.TestCase`.""" | ||
4599 | 77 | |||
4600 | 78 | def test_good_path(self): | ||
4601 | 79 | # Test for successful authentication. | ||
4602 | 80 | self.assertTrue(self.authenticator.authenticate( | ||
4603 | 81 | AUTHENTICATED_USER, self.connection, EXPECTED_PRIVILEGE)) | ||
4604 | 82 | |||
4605 | 83 | def test_bogus_privilege(self): | ||
4606 | 84 | # Test for bogus privilege fails. | ||
4607 | 85 | self.assertFalse(self.authenticator.authenticate( | ||
4608 | 86 | AUTHENTICATED_USER, self.connection, BOGUS_PRIVILEGE)) | ||
4609 | 87 | |||
4610 | 88 | def test_unauthorized(self): | ||
4611 | 89 | # Test for some imposter not being able to authenticate. | ||
4612 | 90 | self.assertFalse(self.authenticator.authenticate( | ||
4613 | 91 | IMPOSTER_USER, self.connection, EXPECTED_PRIVILEGE)) | ||
4614 | 92 | |||
4615 | 93 | |||
4616 | 94 | def test_suite(): | ||
4617 | 95 | suite = unittest.TestSuite() | ||
4618 | 96 | suite.addTests(unittest.makeSuite(TestAuthenticator)) | ||
4619 | 97 | return suite | ||
4620 | 0 | 98 | ||
4621 | === added file 'computerjanitord/tests/test_collector.py' | |||
4622 | --- computerjanitord/tests/test_collector.py 1970-01-01 00:00:00 +0000 | |||
4623 | +++ computerjanitord/tests/test_collector.py 2010-03-10 21:40:30 +0000 | |||
4624 | @@ -0,0 +1,187 @@ | |||
4625 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4626 | 2 | # | ||
4627 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4628 | 4 | # it under the terms of the GNU General Public License as published by | ||
4629 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4630 | 6 | # | ||
4631 | 7 | # This program is distributed in the hope that it will be useful, | ||
4632 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4633 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4634 | 10 | # GNU General Public License for more details. | ||
4635 | 11 | # | ||
4636 | 12 | # You should have received a copy of the GNU General Public License | ||
4637 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4638 | 14 | |||
4639 | 15 | """Test the cruft collector.""" | ||
4640 | 16 | |||
4641 | 17 | from __future__ import absolute_import, unicode_literals | ||
4642 | 18 | |||
4643 | 19 | __metaclass__ = type | ||
4644 | 20 | __all__ = [ | ||
4645 | 21 | 'test_suite', | ||
4646 | 22 | ] | ||
4647 | 23 | |||
4648 | 24 | |||
4649 | 25 | import os | ||
4650 | 26 | import shutil | ||
4651 | 27 | import tempfile | ||
4652 | 28 | import unittest | ||
4653 | 29 | |||
4654 | 30 | from computerjanitor.plugin import Plugin | ||
4655 | 31 | from computerjanitord.collector import Collector | ||
4656 | 32 | from computerjanitord.errors import DuplicateCruftError | ||
4657 | 33 | from computerjanitord.tests.test_application import ApplicationTestSetupMixin | ||
4658 | 34 | |||
4659 | 35 | |||
4660 | 36 | class MockCruft: | ||
4661 | 37 | """Mock cruft that supports the required `get_name()` interface.""" | ||
4662 | 38 | |||
4663 | 39 | def __init__(self, name): | ||
4664 | 40 | self.name = name | ||
4665 | 41 | |||
4666 | 42 | def get_name(self): | ||
4667 | 43 | return self.name | ||
4668 | 44 | |||
4669 | 45 | |||
4670 | 46 | class MockPlugin(Plugin): | ||
4671 | 47 | cruft_class = MockCruft | ||
4672 | 48 | |||
4673 | 49 | def __init__(self, prefix, shortnames): | ||
4674 | 50 | super(MockPlugin, self).__init__() | ||
4675 | 51 | self.prefix = prefix | ||
4676 | 52 | self.shortnames = shortnames | ||
4677 | 53 | |||
4678 | 54 | def get_cruft(self): | ||
4679 | 55 | for shortname in self.shortnames: | ||
4680 | 56 | yield self.cruft_class('{0}:{1}'.format(self.prefix, shortname)) | ||
4681 | 57 | |||
4682 | 58 | |||
4683 | 59 | class SawAppPlugin(Plugin): | ||
4684 | 60 | """All this plugin does is set a marker attribute on the `Application`. | ||
4685 | 61 | |||
4686 | 62 | This proves that access to the application through the plugin works. | ||
4687 | 63 | """ | ||
4688 | 64 | def get_cruft(self): | ||
4689 | 65 | self.app.saw_app_plugin = True | ||
4690 | 66 | return [] | ||
4691 | 67 | |||
4692 | 68 | |||
4693 | 69 | class MockPluginManager: | ||
4694 | 70 | def __init__(self, app, plugin_dirs): | ||
4695 | 71 | self.app = app | ||
4696 | 72 | # Ignore plugin_dirs | ||
4697 | 73 | |||
4698 | 74 | def get_plugins(self): | ||
4699 | 75 | shortnames = ('one', 'two', 'three') | ||
4700 | 76 | for prefix in ('foo', 'bar', 'baz'): | ||
4701 | 77 | plugin = MockPlugin(prefix, shortnames) | ||
4702 | 78 | plugin.set_application(self.app) | ||
4703 | 79 | yield plugin | ||
4704 | 80 | plugin = SawAppPlugin() | ||
4705 | 81 | plugin.set_application(self.app) | ||
4706 | 82 | yield plugin | ||
4707 | 83 | |||
4708 | 84 | |||
4709 | 85 | class MockCruftExtra(MockCruft): | ||
4710 | 86 | """Cruft with a different class.""" | ||
4711 | 87 | |||
4712 | 88 | |||
4713 | 89 | class MockPluginExtra(MockPlugin): | ||
4714 | 90 | """A mock plugin that returns cruft with a different class.""" | ||
4715 | 91 | |||
4716 | 92 | cruft_class = MockCruftExtra | ||
4717 | 93 | |||
4718 | 94 | |||
4719 | 95 | class IgnoredDuplicateCruftPluginManager(MockPluginManager): | ||
4720 | 96 | """Add an additional piece of ignorable duplication cruft.""" | ||
4721 | 97 | |||
4722 | 98 | def __init__(self, app, plugin_dirs): | ||
4723 | 99 | self.app = app | ||
4724 | 100 | # Ignore plugin_dirs | ||
4725 | 101 | |||
4726 | 102 | def get_plugins(self): | ||
4727 | 103 | yield MockPlugin('one', ('foo', 'bar')) | ||
4728 | 104 | yield MockPlugin('one', ('baz', 'foo')) | ||
4729 | 105 | |||
4730 | 106 | |||
4731 | 107 | class BadDuplicateCruftPluginManager(MockPluginManager): | ||
4732 | 108 | """Add an additional piece of bad duplicate cruft.""" | ||
4733 | 109 | def __init__(self, app, plugin_dirs): | ||
4734 | 110 | self.app = app | ||
4735 | 111 | # Ignore plugin_dirs | ||
4736 | 112 | |||
4737 | 113 | def get_plugins(self): | ||
4738 | 114 | yield MockPlugin('one', ('foo', 'bar')) | ||
4739 | 115 | yield MockPluginExtra('one', ('baz', 'foo')) | ||
4740 | 116 | |||
4741 | 117 | |||
4742 | 118 | class TestCollector(unittest.TestCase, ApplicationTestSetupMixin): | ||
4743 | 119 | """Test the cruft collector.""" | ||
4744 | 120 | |||
4745 | 121 | def setUp(self): | ||
4746 | 122 | # Set up the test data Application. | ||
4747 | 123 | ApplicationTestSetupMixin.setUp(self) | ||
4748 | 124 | self.tempdir = tempfile.mkdtemp() | ||
4749 | 125 | whitelist_dirs = (self.tempdir,) | ||
4750 | 126 | with open(os.path.join(self.tempdir, 'one.whitelist'), 'w') as fp: | ||
4751 | 127 | print >> fp, 'foo:two' | ||
4752 | 128 | print >> fp, 'bar:one' | ||
4753 | 129 | print >> fp, 'baz:three' | ||
4754 | 130 | self.collector = Collector(self.app, MockPluginManager, whitelist_dirs) | ||
4755 | 131 | |||
4756 | 132 | def tearDown(self): | ||
4757 | 133 | shutil.rmtree(self.tempdir) | ||
4758 | 134 | ApplicationTestSetupMixin.tearDown(self) | ||
4759 | 135 | |||
4760 | 136 | def test_cruft_collector(self): | ||
4761 | 137 | cruft_names = set(cruft.get_name() for cruft in self.collector.cruft) | ||
4762 | 138 | self.assertEqual(cruft_names, set(('foo:one', 'foo:three', | ||
4763 | 139 | 'bar:two', 'bar:three', | ||
4764 | 140 | 'baz:one', 'baz:two'))) | ||
4765 | 141 | |||
4766 | 142 | def test_plugin_needs_application(self): | ||
4767 | 143 | # SawAppPlugin returned by the MockPluginManager sets this attribute | ||
4768 | 144 | # on the Application. | ||
4769 | 145 | self.assertTrue(self.app.saw_app_plugin) | ||
4770 | 146 | |||
4771 | 147 | def test_collector_name_mapping(self): | ||
4772 | 148 | cruft_keys = set(self.collector.cruft_by_name) | ||
4773 | 149 | cruft_names = set(cruft.get_name() for cruft in self.collector.cruft) | ||
4774 | 150 | self.assertEqual(cruft_keys, cruft_names) | ||
4775 | 151 | |||
4776 | 152 | |||
4777 | 153 | class TestDuplicateCruftCollector( | ||
4778 | 154 | unittest.TestCase, ApplicationTestSetupMixin): | ||
4779 | 155 | |||
4780 | 156 | def setUp(self): | ||
4781 | 157 | # Set up the test data Application. | ||
4782 | 158 | ApplicationTestSetupMixin.setUp(self) | ||
4783 | 159 | |||
4784 | 160 | def tearDown(self): | ||
4785 | 161 | ApplicationTestSetupMixin.tearDown(self) | ||
4786 | 162 | |||
4787 | 163 | def test_duplicate_cruft_error(self): | ||
4788 | 164 | self.assertRaises(DuplicateCruftError, Collector, | ||
4789 | 165 | self.app, BadDuplicateCruftPluginManager, []) | ||
4790 | 166 | |||
4791 | 167 | def test_duplicate_cruft_error_message(self): | ||
4792 | 168 | try: | ||
4793 | 169 | Collector(self.app, BadDuplicateCruftPluginManager, []) | ||
4794 | 170 | except DuplicateCruftError as error: | ||
4795 | 171 | self.assertEqual( | ||
4796 | 172 | str(error), | ||
4797 | 173 | 'Duplicate cruft with different cleanup: one:foo') | ||
4798 | 174 | else: | ||
4799 | 175 | raise AssertionError('DuplicateCruftError expected') | ||
4800 | 176 | |||
4801 | 177 | def test_ignored_duplicate_cruft(self): | ||
4802 | 178 | collector = Collector(self.app, IgnoredDuplicateCruftPluginManager, []) | ||
4803 | 179 | self.assertEqual(list(cruft.get_name() for cruft in collector.cruft), | ||
4804 | 180 | ['one:foo', 'one:bar', 'one:baz']) | ||
4805 | 181 | |||
4806 | 182 | |||
4807 | 183 | def test_suite(): | ||
4808 | 184 | suite = unittest.TestSuite() | ||
4809 | 185 | suite.addTests(unittest.makeSuite(TestCollector)) | ||
4810 | 186 | suite.addTests(unittest.makeSuite(TestDuplicateCruftCollector)) | ||
4811 | 187 | return suite | ||
4812 | 0 | 188 | ||
4813 | === added file 'computerjanitord/tests/test_state.py' | |||
4814 | --- computerjanitord/tests/test_state.py 1970-01-01 00:00:00 +0000 | |||
4815 | +++ computerjanitord/tests/test_state.py 2010-03-10 21:40:30 +0000 | |||
4816 | @@ -0,0 +1,141 @@ | |||
4817 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4818 | 2 | # | ||
4819 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4820 | 4 | # it under the terms of the GNU General Public License as published by | ||
4821 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4822 | 6 | # | ||
4823 | 7 | # This program is distributed in the hope that it will be useful, | ||
4824 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4825 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4826 | 10 | # GNU General Public License for more details. | ||
4827 | 11 | # | ||
4828 | 12 | # You should have received a copy of the GNU General Public License | ||
4829 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4830 | 14 | |||
4831 | 15 | """Test the package state.""" | ||
4832 | 16 | |||
4833 | 17 | from __future__ import absolute_import, unicode_literals | ||
4834 | 18 | |||
4835 | 19 | __metaclass__ = type | ||
4836 | 20 | __all__ = [ | ||
4837 | 21 | 'test_suite', | ||
4838 | 22 | ] | ||
4839 | 23 | |||
4840 | 24 | |||
4841 | 25 | import os | ||
4842 | 26 | import difflib | ||
4843 | 27 | import tempfile | ||
4844 | 28 | import textwrap | ||
4845 | 29 | import unittest | ||
4846 | 30 | |||
4847 | 31 | from computerjanitord.state import State | ||
4848 | 32 | |||
4849 | 33 | NL = '\n' | ||
4850 | 34 | |||
4851 | 35 | |||
4852 | 36 | class TestState(unittest.TestCase): | ||
4853 | 37 | """Test the `State` class.""" | ||
4854 | 38 | |||
4855 | 39 | def setUp(self): | ||
4856 | 40 | self.state = State() | ||
4857 | 41 | # Create a temporary file with some enabled and disabled packages. | ||
4858 | 42 | fd, self._state_file = tempfile.mkstemp() | ||
4859 | 43 | os.close(fd) | ||
4860 | 44 | with open(self._state_file, 'w') as fp: | ||
4861 | 45 | print >> fp, textwrap.dedent("""\ | ||
4862 | 46 | [deb:foo] | ||
4863 | 47 | ignore: false | ||
4864 | 48 | [deb:bar] | ||
4865 | 49 | ignore: true | ||
4866 | 50 | [deb:baz] | ||
4867 | 51 | ignore: true | ||
4868 | 52 | """) | ||
4869 | 53 | fd, self._state_file_old = tempfile.mkstemp() | ||
4870 | 54 | os.close(fd) | ||
4871 | 55 | with open(self._state_file_old, 'w') as fp: | ||
4872 | 56 | print >> fp, textwrap.dedent("""\ | ||
4873 | 57 | [deb:qux] | ||
4874 | 58 | enabled: false | ||
4875 | 59 | [deb:fno] | ||
4876 | 60 | enabled: true | ||
4877 | 61 | [deb:bla] | ||
4878 | 62 | enabled: true | ||
4879 | 63 | """) | ||
4880 | 64 | fd, self._write_file = tempfile.mkstemp() | ||
4881 | 65 | os.close(fd) | ||
4882 | 66 | |||
4883 | 67 | def tearDown(self): | ||
4884 | 68 | os.remove(self._state_file) | ||
4885 | 69 | os.remove(self._state_file_old) | ||
4886 | 70 | os.remove(self._write_file) | ||
4887 | 71 | |||
4888 | 72 | def assertEqualNdiff(self, expected, got): | ||
4889 | 73 | expected_lines = expected.splitlines() | ||
4890 | 74 | got_lines = got.splitlines() | ||
4891 | 75 | self.assertEqual( | ||
4892 | 76 | expected, got, | ||
4893 | 77 | '\n' + NL.join(difflib.ndiff(expected_lines, got_lines))) | ||
4894 | 78 | |||
4895 | 79 | def test_initially_no_previously_ignored(self): | ||
4896 | 80 | self.assertEqual(self.state.ignore, set()) | ||
4897 | 81 | |||
4898 | 82 | def test_load_state(self): | ||
4899 | 83 | self.state.load(self._state_file) | ||
4900 | 84 | self.assertEqual(self.state.ignore, set(('deb:bar', 'deb:baz'))) | ||
4901 | 85 | |||
4902 | 86 | def test_backward_compatibility_file_format(self): | ||
4903 | 87 | # Here, enabled:false means to ignore the package. | ||
4904 | 88 | self.state.load(self._state_file_old) | ||
4905 | 89 | self.assertEqual(self.state.ignore, set(('deb:qux',))) | ||
4906 | 90 | |||
4907 | 91 | def test_ignore(self): | ||
4908 | 92 | self.state.ignore.add('deb:buz') | ||
4909 | 93 | self.state.ignore.add('deb:baz') | ||
4910 | 94 | self.assertEqual(self.state.ignore, set(('deb:buz', 'deb:baz'))) | ||
4911 | 95 | |||
4912 | 96 | def test_more_ignores(self): | ||
4913 | 97 | self.state.load(self._state_file) | ||
4914 | 98 | self.state.ignore.add('deb:buz') | ||
4915 | 99 | self.state.ignore.add('deb:baz') | ||
4916 | 100 | self.assertEqual(self.state.ignore, | ||
4917 | 101 | set(('deb:bar', 'deb:buz', 'deb:baz'))) | ||
4918 | 102 | |||
4919 | 103 | def test_unignore(self): | ||
4920 | 104 | self.state.load(self._state_file) | ||
4921 | 105 | self.state.ignore.remove('deb:bar') | ||
4922 | 106 | self.assertEqual(self.state.ignore, set(('deb:baz',))) | ||
4923 | 107 | |||
4924 | 108 | def test_write(self): | ||
4925 | 109 | self.state.load(self._state_file) | ||
4926 | 110 | self.state.ignore.add('deb:buz') | ||
4927 | 111 | self.state.ignore.remove('deb:baz') | ||
4928 | 112 | self.state.save(self._write_file) | ||
4929 | 113 | with open(self._write_file) as fp: | ||
4930 | 114 | got = fp.read() | ||
4931 | 115 | expected = textwrap.dedent("""\ | ||
4932 | 116 | [deb:bar] | ||
4933 | 117 | ignore: true | ||
4934 | 118 | |||
4935 | 119 | [deb:buz] | ||
4936 | 120 | ignore: true | ||
4937 | 121 | |||
4938 | 122 | """) | ||
4939 | 123 | self.assertEqualNdiff(expected, got) | ||
4940 | 124 | |||
4941 | 125 | def test_write_new_format(self): | ||
4942 | 126 | self.state.load(self._state_file_old) | ||
4943 | 127 | self.state.save(self._write_file) | ||
4944 | 128 | with open(self._write_file) as fp: | ||
4945 | 129 | got = fp.read() | ||
4946 | 130 | expected = textwrap.dedent("""\ | ||
4947 | 131 | [deb:qux] | ||
4948 | 132 | ignore: true | ||
4949 | 133 | |||
4950 | 134 | """) | ||
4951 | 135 | self.assertEqualNdiff(expected, got) | ||
4952 | 136 | |||
4953 | 137 | |||
4954 | 138 | def test_suite(): | ||
4955 | 139 | suite = unittest.TestSuite() | ||
4956 | 140 | suite.addTests(unittest.makeSuite(TestState)) | ||
4957 | 141 | return suite | ||
4958 | 0 | 142 | ||
4959 | === added file 'computerjanitord/tests/test_whitelist.py' | |||
4960 | --- computerjanitord/tests/test_whitelist.py 1970-01-01 00:00:00 +0000 | |||
4961 | +++ computerjanitord/tests/test_whitelist.py 2010-03-10 21:40:30 +0000 | |||
4962 | @@ -0,0 +1,119 @@ | |||
4963 | 1 | # Copyright (C) 2008, 2009, 2010 Canonical, Ltd. | ||
4964 | 2 | # | ||
4965 | 3 | # This program is free software: you can redistribute it and/or modify | ||
4966 | 4 | # it under the terms of the GNU General Public License as published by | ||
4967 | 5 | # the Free Software Foundation, version 3 of the License. | ||
4968 | 6 | # | ||
4969 | 7 | # This program is distributed in the hope that it will be useful, | ||
4970 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4971 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4972 | 10 | # GNU General Public License for more details. | ||
4973 | 11 | # | ||
4974 | 12 | # You should have received a copy of the GNU General Public License | ||
4975 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4976 | 14 | |||
4977 | 15 | """Test the whitelister.""" | ||
4978 | 16 | |||
4979 | 17 | from __future__ import absolute_import, unicode_literals | ||
4980 | 18 | |||
4981 | 19 | __metaclass__ = type | ||
4982 | 20 | __all__ = [ | ||
4983 | 21 | 'test_suite', | ||
4984 | 22 | ] | ||
4985 | 23 | |||
4986 | 24 | |||
4987 | 25 | import os | ||
4988 | 26 | import shutil | ||
4989 | 27 | import tempfile | ||
4990 | 28 | import unittest | ||
4991 | 29 | |||
4992 | 30 | from computerjanitord.whitelist import Whitelist | ||
4993 | 31 | |||
4994 | 32 | |||
4995 | 33 | class MockCruft: | ||
4996 | 34 | """Mock cruft that supports the required `get_name()` interface.""" | ||
4997 | 35 | |||
4998 | 36 | def __init__(self, shortname): | ||
4999 | 37 | self.name = 'foo:' + shortname | ||
5000 | 38 |
The diff has been truncated for viewing.
This will need a ui exception to make it into Lucid beta 1, but I think it's worth it.