More updates to support Python 3
[zeroinstall/solver.git] / zeroinstall / cmd / whatchanged.py
blob1ac8ad9d633998f5bc2fc1c53f9403f334c35d1c
1 """
2 The B{0install whatchanged} command-line interface.
3 """
5 # Copyright (C) 2012, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from __future__ import print_function
10 import os
12 from zeroinstall import _, SafeException
13 from zeroinstall.cmd import UsageError
15 syntax = "APP-NAME"
17 def add_options(parser):
18 parser.add_option("", "--full", help=_("show diff of the XML"), action='store_true')
20 def handle(config, options, args):
21 if len(args) != 1:
22 raise UsageError()
24 name = args[0]
25 app = config.app_mgr.lookup_app(name, missing_ok = False)
26 history = app.get_history()
28 if not history:
29 raise SafeException(_("Invalid application: no selections found! Try '0install destroy {name}'").format(name = name))
31 import time
33 last_checked = app.get_last_checked()
34 if last_checked is not None:
35 print(_("Last checked : {date}").format(date = time.ctime(last_checked)))
37 last_attempt = app.get_last_check_attempt()
38 if last_attempt is not None:
39 print(_("Last attempted update: {date}").format(date = time.ctime(last_attempt)))
41 print(_("Last update : {date}").format(date = history[0]))
42 current_sels = app.get_selections(snapshot_date = history[0])
44 if len(history) == 1:
45 print(_("No previous history to compare against."))
46 print(_("Use '0install select {name}' to see the current selections.").format(name = name))
47 return
49 print(_("Previous update : {date}").format(date = history[1]))
51 def get_selections_path(date):
52 return os.path.join(app.path, 'selections-{date}.xml'.format(date = date))
54 print()
56 if options.full:
57 import difflib, sys
58 def load_lines(date):
59 with open(get_selections_path(date), 'r') as stream:
60 return stream.readlines()
61 old_lines = load_lines(history[1])
62 new_lines = load_lines(history[0])
63 for line in difflib.unified_diff(old_lines, new_lines, fromfile = history[1], tofile = history[0]):
64 sys.stdout.write(line)
65 else:
66 changes = show_changes(app.get_selections(snapshot_date = history[1]).selections, current_sels.selections)
67 if not changes:
68 print(_("No changes to versions (use --full to see all changes)."))
70 print()
71 print(_("To run using the previous selections, use:"))
72 print("0install run {path}".format(path = get_selections_path(history[1])))
74 def show_changes(old_selections, new_selections):
75 changes = False
77 for iface, old_sel in old_selections.items():
78 new_sel = new_selections.get(iface, None)
79 if new_sel is None:
80 print(_("No longer used: %s") % iface)
81 changes = True
82 elif old_sel.version != new_sel.version:
83 print(_("%s: %s -> %s") % (iface, old_sel.version, new_sel.version))
84 changes = True
86 for iface, new_sel in new_selections.items():
87 if iface not in old_selections:
88 print(_("%s: new -> %s") % (iface, new_sel.version))
89 changes = True
91 return changes