2 Convenience routines for performing common operations.
6 # Copyright (C) 2009, Thomas Leonard
7 # See the README file for details, or visit http://0install.net.
9 import os
, sys
, logging
10 from zeroinstall
import support
11 from zeroinstall
import _
13 def get_selections_gui(iface_uri
, gui_args
, test_callback
= None):
14 """Run the GUI to choose and download a set of implementations.
15 If the GUI itself is due for a check, refresh it first.
16 The user may ask the GUI to submit a bug report about the program. In that case,
17 the GUI may ask us to test it. test_callback is called in that case with the implementations
18 to be tested; the callback will typically call L{zeroinstall.injector.run.test_selections} and return the result of that.
19 @param iface_uri: the required program
21 @param gui_args: any additional arguments for the GUI itself
23 @param test_callback: function to use to try running the program
24 @type test_callback: L{zeroinstall.injector.selections.Selections} -> str
25 @return: the selected implementations
26 @rtype: L{zeroinstall.injector.selections.Selections}
29 from zeroinstall
.injector
import selections
, autopolicy
, namespaces
, model
, run
, qdom
30 from StringIO
import StringIO
32 gui_policy
= autopolicy
.AutoPolicy(namespaces
.injector_gui_uri
)
33 if iface_uri
!= namespaces
.injector_gui_uri
and (gui_policy
.need_download() or gui_policy
.stale_feeds
):
34 # The GUI itself needs updating. Do that first.
35 logging
.info("The GUI could do with updating first.")
36 gui_sel
= get_selections_gui(namespaces
.injector_gui_uri
, ['--refresh'])
38 logging
.info("Aborted at user request")
39 return None # Aborted by user
41 # Try to start the GUI without using the network.
42 gui_policy
.freshness
= 0
43 gui_policy
.network_use
= model
.network_offline
44 gui_policy
.recalculate()
45 assert gui_policy
.ready
# Should always be some version available
46 gui_sel
= selections
.Selections(gui_policy
)
49 cli
, gui
= socket
.socketpair()
54 # We are the child (GUI)
58 # We used to use pipes to support Python2.3...
59 os
.dup2(gui
.fileno(), 1)
60 os
.dup2(gui
.fileno(), 0)
61 run
.execute_selections(gui_sel
, gui_args
+ ['--', iface_uri
])
64 traceback
.print_exc(file = sys
.stderr
)
68 # We are the parent (CLI)
73 logging
.info("Waiting for selections from GUI...")
75 reply
= support
.read_bytes(cli
.fileno(), len('Length:') + 9, null_ok
= True)
77 if not reply
.startswith('Length:'):
78 raise Exception("Expected Length:, but got %s" % repr(reply
))
79 xml
= support
.read_bytes(cli
.fileno(), int(reply
.split(':', 1)[1], 16))
81 dom
= qdom
.parse(StringIO(xml
))
82 sels
= selections
.Selections(dom
)
84 if dom
.getAttribute('run-test'):
85 logging
.info("Testing program, as requested by GUI...")
86 if test_callback
is None:
87 output
= "Can't test: no test_callback was passed to get_selections_gui()\n"
89 output
= test_callback(sels
)
90 logging
.info("Sending results to GUI...")
91 output
= ('Length:%8x\n' % len(output
)) + output
92 logging
.debug("Sending: %s", repr(output
))
94 sent
= cli
.send(output
)
95 output
= output
[sent
:]
100 pid
, status
= os
.waitpid(child
, 0)
103 logging
.info("User cancelled the GUI; aborting")
104 return None # Aborted
106 raise Exception("Error from GUI: code = %d" % status
)
109 for sock
in [cli
, gui
]:
110 if sock
is not None: sock
.close()
114 def ensure_cached(uri
):
115 """Ensure that an implementation of uri is cached.
116 If not, it downloads one. It uses the GUI if a display is
117 available, or the console otherwise.
118 @param uri: the required interface
120 @return: a new policy for this program, or None if the user cancelled
121 @rtype: L{zeroinstall.injector.selections.Selections}
123 from zeroinstall
.injector
import autopolicy
, selections
125 p
= autopolicy
.AutoPolicy(uri
, download_only
= True)
126 p
.freshness
= 0 # Don't check for updates
128 if p
.need_download() or not p
.ready
:
129 if os
.environ
.get('DISPLAY', None):
130 return get_selections_gui(uri
, [])
132 p
.recalculate_with_dl()
133 p
.start_downloading_impls()
134 p
.handler
.wait_for_downloads()
136 return selections
.Selections(p
)