2 The B{0install whatchanged} command-line interface.
5 # Copyright (C) 2012, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from __future__
import print_function
12 from zeroinstall
import _
, SafeException
13 from zeroinstall
.cmd
import UsageError
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
):
25 app
= config
.app_mgr
.lookup_app(name
, missing_ok
= False)
26 history
= app
.get_history()
29 raise SafeException(_("Invalid application: no selections found! Try '0install destroy {name}'").format(name
= name
))
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])
45 print(_("No previous history to compare against."))
46 print(_("Use '0install select {name}' to see the current selections.").format(name
= name
))
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
))
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
)
66 changes
= show_changes(app
.get_selections(snapshot_date
= history
[1]).selections
, current_sels
.selections
)
68 print(_("No changes to versions (use --full to see all changes)."))
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
):
77 for iface
, old_sel
in old_selections
.items():
78 new_sel
= new_selections
.get(iface
, None)
80 print(_("No longer used: %s") % iface
)
82 elif old_sel
.version
!= new_sel
.version
:
83 print(_("%s: %s -> %s") % (iface
, old_sel
.version
, new_sel
.version
))
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
))