1 # Copyright (C) 2009, Thomas Leonard
2 # See http://0install.net/0compile.html
4 import sys
, os
, __main__
, tempfile
, subprocess
, signal
, shutil
5 from xml
.dom
import minidom
6 from optparse
import OptionParser
7 from logging
import warn
9 from zeroinstall
import SafeException
10 from zeroinstall
.injector
import arch
, handler
, driver
, requirements
, model
, iface_cache
, namespaces
, writer
, reader
, qdom
11 from zeroinstall
.injector
.config
import load_config
12 from zeroinstall
.zerostore
import manifest
, NotStored
13 from zeroinstall
.support
import tasks
, basedir
, ro_rmtree
15 from support
import BuildEnv
, uname
, XMLNS_0COMPILE
17 # This is a bit hacky...
19 # We invent a new CPU type which is compatible with the host but worse than
20 # every existing type, and we use * for the OS type so that we don't beat 'Any'
21 # binaries either. This means that we always prefer an existing binary of the
22 # desired version to compiling a new one, but we'll compile a new version from source
23 # rather than use an older binary.
24 arch
.machine_groups
['newbuild'] = arch
.machine_groups
.get(uname
[4], 0)
25 arch
.machine_ranks
['newbuild'] = max(arch
.machine_ranks
.values()) + 1
26 host_arch
= '*-newbuild'
28 class NewBuildImplementation(model
.ZeroInstallImplementation
):
29 # Assume that this (potential) binary is available so that we can select it as a
31 def is_available(self
, stores
):
34 def get_commands(src_impl
):
35 """Estimate the commands that the generated binary would have."""
36 cmd
= src_impl
.commands
.get('compile', None)
38 warn("Source has no compile command! %s", src_impl
)
41 for elem
in cmd
.qdom
.childNodes
:
42 if elem
.uri
== XMLNS_0COMPILE
and elem
.name
== 'implementation':
43 # Assume there's always a run command. Doesn't do any harm to have extra ones,
44 # and there are various ways this might get created.
46 for e
in elem
.childNodes
:
47 if e
.uri
== namespaces
.XMLNS_IFACE
and e
.name
== 'command':
48 commands
.append(e
.getAttribute('name'))
52 class AutocompileCache(iface_cache
.IfaceCache
):
54 iface_cache
.IfaceCache
.__init
__(self
)
57 def get_feed(self
, url
, force
= False):
58 feed
= iface_cache
.IfaceCache
.get_feed(self
, url
, force
)
59 if not feed
: return None
61 if feed
not in self
.done
:
64 # For each source impl, add a corresponding binary
65 # (the binary has no dependencies as we can't predict them here,
66 # but they're not the same as the source's dependencies)
68 srcs
= [x
for x
in feed
.implementations
.itervalues() if x
.arch
and x
.arch
.endswith('-src')]
70 new_id
= '0compile=' + x
.id
71 if not new_id
in feed
.implementations
:
72 new
= NewBuildImplementation(feed
, new_id
, None)
73 feed
.implementations
[new_id
] = new
74 new
.set_arch(host_arch
)
75 new
.version
= x
.version
77 # Give it some dummy commands in case we're using it as a <runner>, etc (otherwise it can't be selected)
78 for cmd_name
in get_commands(x
):
79 cmd
= qdom
.Element(namespaces
.XMLNS_IFACE
, 'command', {'path': 'new-build', 'name': cmd_name
})
80 new
.commands
[cmd_name
] = model
.Command(cmd
, None)
85 def __init__(self
, config
, iface_uri
, options
):
86 self
.iface_uri
= iface_uri
87 self
.options
= options
90 def pretty_print_plan(self
, solver
, root
, indent
= '- '):
91 """Display a tree showing the selected implementations."""
92 iface
= self
.config
.iface_cache
.get_interface(root
)
93 impl
= solver
.selections
[iface
]
95 msg
= 'Failed to select any suitable version (source or binary)'
96 elif impl
.id.startswith('0compile='):
97 real_impl_id
= impl
.id.split('=', 1)[1]
98 real_impl
= impl
.feed
.implementations
[real_impl_id
]
99 msg
= 'Compile %s (%s)' % (real_impl
.get_version(), real_impl
.id)
100 elif impl
.arch
and impl
.arch
.endswith('-src'):
101 msg
= 'Compile %s (%s)' % (impl
.get_version(), impl
.id)
104 msg
= 'Use existing binary %s (%s)' % (impl
.get_version(), impl
.arch
)
106 msg
= 'Use existing architecture-independent package %s' % impl
.get_version()
107 self
.note("%s%s: %s" % (indent
, iface
.get_name(), msg
))
110 indent
= ' ' + indent
111 for x
in impl
.requires
:
112 self
.pretty_print_plan(solver
, x
.interface
, indent
)
114 def print_details(self
, solver
):
115 """Dump debugging details."""
116 self
.note("\nFailed. Details of all components and versions considered:")
117 for iface
in solver
.details
:
118 self
.note('\n%s\n' % iface
.get_name())
119 for impl
, note
in solver
.details
[iface
]:
120 self
.note('%s (%s) : %s' % (impl
.get_version(), impl
.arch
or '*-*', note
or 'OK'))
121 self
.note("\nEnd details\n")
124 def compile_and_register(self
, sels
, forced_iface_uri
= None):
125 """If forced_iface_uri, register as an implementation of this interface,
126 ignoring the any <feed-for>, etc."""
128 buildenv
= BuildEnv(need_config
= False)
129 buildenv
.config
.set('compile', 'interface', sels
.interface
)
130 buildenv
.config
.set('compile', 'selections', 'selections.xml')
132 # Download any required packages now, so we can use the GUI to request confirmation, etc
133 download_missing
= sels
.download_missing(self
.config
, include_packages
= True)
135 yield download_missing
136 tasks
.check(download_missing
)
138 tmpdir
= tempfile
.mkdtemp(prefix
= '0compile-')
142 # Write configuration for build...
146 sel_file
= open('selections.xml', 'w')
149 doc
.writexml(sel_file
)
156 build
= self
.spawn_build(buildenv
.iface_name
)
161 # Register the result...
162 dom
= minidom
.parse(buildenv
.local_iface_file
)
164 feed_for_elem
, = dom
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'feed-for')
165 claimed_iface
= feed_for_elem
.getAttribute('interface')
167 if forced_iface_uri
is not None:
168 if forced_iface_uri
!= claimed_iface
:
169 self
.note("WARNING: registering as feed for {forced}, though feed claims to be for {claimed}".format(
170 forced
= forced_iface_uri
,
171 claimed
= claimed_iface
))
173 forced_iface_uri
= claimed_iface
# (the top-level interface being built)
175 version
= sels
.selections
[sels
.interface
].version
177 site_package_versions_dir
= basedir
.save_data_path('0install.net', 'site-packages',
178 model
._pretty
_escape
(forced_iface_uri
))
179 leaf
= '%s-%s' % (version
, uname
[4])
180 site_package_dir
= os
.path
.join(site_package_versions_dir
, leaf
)
181 self
.note("Storing build in %s" % site_package_dir
)
183 # 1. Copy new version in under a temporary name. Names starting with '.' are ignored by 0install.
184 tmp_distdir
= os
.path
.join(site_package_versions_dir
, '.new-' + leaf
)
185 shutil
.copytree(buildenv
.distdir
, tmp_distdir
, symlinks
= True)
187 # 2. Rename the previous build to .old-VERSION (deleting that if it already existed)
188 if os
.path
.exists(site_package_dir
):
189 self
.note("(moving previous build out of the way)")
190 previous_build_dir
= os
.path
.join(site_package_versions_dir
, '.old-' + leaf
)
191 if os
.path
.exists(previous_build_dir
):
192 shutil
.rmtree(previous_build_dir
)
193 os
.rename(site_package_dir
, previous_build_dir
)
195 previous_build_dir
= None
197 # 3. Rename the new version immediately after renaming away the old one to minimise time when there's
199 os
.rename(tmp_distdir
, site_package_dir
)
201 # 4. Delete the old version.
202 if previous_build_dir
:
203 self
.note("(deleting previous build)")
204 shutil
.rmtree(previous_build_dir
)
206 local_feed
= os
.path
.join(site_package_dir
, '0install', 'feed.xml')
207 assert os
.path
.exists(local_feed
), "Feed %s not found!" % local_feed
209 # Reload - our 0install will detect the new feed automatically
210 iface
= self
.config
.iface_cache
.get_interface(forced_iface_uri
)
211 reader
.update_from_cache(iface
)
212 self
.config
.iface_cache
.get_feed(local_feed
, force
= True)
214 # Write it out - 0install will add the feed so that older 0install versions can find it
215 writer
.save_interface(iface
)
217 self
.note("\nBuild failed: leaving build directory %s for inspection...\n" % tmpdir
)
223 def recursive_build(self
, iface_uri
, version
= None):
224 """Build an implementation of iface_uri and register it as a feed.
225 @param version: the version to build, or None to build any version
228 r
= requirements
.Requirements(iface_uri
)
230 r
.command
= 'compile'
232 d
= driver
.Driver(self
.config
, r
)
233 iface
= self
.config
.iface_cache
.get_interface(iface_uri
)
234 d
.solver
.record_details
= True
236 d
.solver
.extra_restrictions
[iface
] = [model
.VersionRestriction(model
.parse_version(version
))]
239 #p.target_arch = arch.Architecture(os_ranks = {'FreeBSD': 0, None: 1}, machine_ranks = {'i386': 0, None: 1, 'newbuild': 2})
242 self
.heading(iface_uri
)
243 self
.note("\nSelecting versions for %s..." % iface
.get_name())
244 solved
= d
.solve_with_downloads()
249 if not d
.solver
.ready
:
250 self
.print_details(d
.solver
)
251 raise SafeException("Can't find all required implementations (source or binary):\n" +
252 '\n'.join(["- %s -> %s" % (iface
, d
.solver
.selections
[iface
])
253 for iface
in d
.solver
.selections
]))
254 self
.note("Selection done.")
256 self
.note("\nPlan:\n")
257 self
.pretty_print_plan(d
.solver
, r
.interface_uri
)
261 for dep_iface
, dep_impl
in d
.solver
.selections
.iteritems():
262 if dep_impl
.id.startswith('0compile='):
264 self
.note("Build dependencies that need to be compiled first:\n")
265 self
.note("- {iface} {version}".format(iface
= dep_iface
.uri
, version
= model
.format_version(dep_impl
.version
)))
266 needed
.append((dep_iface
, dep_impl
))
269 self
.note("No dependencies need compiling... compile %s itself..." % iface
.get_name())
270 build
= self
.compile_and_register(d
.solver
.selections
,
271 # force the interface in the recursive case
272 iface_uri
if iface_uri
!= self
.iface_uri
else None)
277 # Compile the first missing build dependency...
278 dep_iface
, dep_impl
= needed
[0]
282 #details = d.solver.details[self.config.iface_cache.get_interface(dep_iface.uri)]
286 build
= self
.recursive_build(dep_iface
.uri
, dep_impl
.get_version())
289 # Try again with that dependency built...
291 def spawn_build(self
, iface_name
):
292 subprocess
.check_call([sys
.executable
, sys
.argv
[0], 'build'])
295 tasks
.wait_for_blocker(self
.recursive_build(self
.iface_uri
))
297 def heading(self
, msg
):
298 self
.note((' %s ' % msg
).center(76, '='))
303 def note_error(self
, msg
):
304 self
.overall
.insert_at_cursor(msg
+ '\n')
306 class GUIHandler(handler
.Handler
):
307 def downloads_changed(self
):
308 self
.compiler
.downloads_changed()
310 def confirm_import_feed(self
, pending
, valid_sigs
):
311 return handler
.Handler
.confirm_import_feed(self
, pending
, valid_sigs
)
314 def confirm_install(self
, message
):
315 from zeroinstall
.injector
.download
import DownloadAborted
316 from zeroinstall
.gtkui
import gtkutils
318 box
= gtk
.MessageDialog(self
.compiler
.dialog
,
319 gtk
.DIALOG_DESTROY_WITH_PARENT
,
320 gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_CANCEL
,
322 box
.set_position(gtk
.WIN_POS_CENTER
)
324 install
= gtkutils
.MixedButton('Install', gtk
.STOCK_OK
)
325 install
.set_flags(gtk
.CAN_DEFAULT
)
326 box
.add_action_widget(install
, gtk
.RESPONSE_OK
)
328 box
.set_default_response(gtk
.RESPONSE_OK
)
331 response
= gtkutils
.DialogResponse(box
)
335 if response
.response
!= gtk
.RESPONSE_OK
:
336 raise DownloadAborted()
338 class GTKAutoCompiler(AutoCompiler
):
339 def __init__(self
, config
, iface_uri
, options
):
340 config
.handler
.compiler
= self
342 AutoCompiler
.__init
__(self
, config
, iface_uri
, options
)
345 import pygtk
; pygtk
.require('2.0')
348 w
= gtk
.Dialog('Autocompile %s' % iface_uri
, None, 0,
349 (gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
,
350 gtk
.STOCK_OK
, gtk
.RESPONSE_OK
))
353 w
.set_default_size(int(gtk
.gdk
.screen_width() * 0.8),
354 int(gtk
.gdk
.screen_height() * 0.8))
356 vpaned
= gtk
.VPaned()
358 w
.set_response_sensitive(gtk
.RESPONSE_OK
, False)
363 tv
.set_property('left-margin', 8)
364 tv
.set_wrap_mode(gtk
.WRAP_WORD_CHAR
)
365 tv
.set_editable(False)
366 swin
= gtk
.ScrolledWindow()
368 swin
.set_policy(gtk
.POLICY_NEVER
, gtk
.POLICY_AUTOMATIC
)
369 buffer = tv
.get_buffer()
371 heading
= buffer.create_tag('heading')
372 heading
.set_property('scale', 1.5)
374 error
= buffer.create_tag('error')
375 error
.set_property('background', 'white')
376 error
.set_property('foreground', 'red')
382 def insert_at_end_and_scroll(self
, data
, *tags
):
383 vscroll
= self
.widget
.get_vadjustment()
385 # Widget has been destroyed
388 near_end
= vscroll
.upper
- vscroll
.page_size
* 1.5 < vscroll
.value
389 end
= self
.buffer.get_end_iter()
390 self
.buffer.insert_with_tags_by_name(end
, data
, *tags
)
392 cursor
= self
.buffer.get_insert()
393 self
.buffer.move_mark(cursor
, end
)
394 self
.tv
.scroll_to_mark(cursor
, 0, False, 0, 0)
396 def set_text(self
, text
):
397 self
.buffer.set_text(text
)
399 self
.overall
= AutoScroller()
400 self
.details
= AutoScroller()
402 vpaned
.pack1(self
.overall
.widget
, True, False)
403 vpaned
.pack2(self
.details
.widget
, True, False)
405 self
.closed
= tasks
.Blocker('Window closed')
408 w
.connect('destroy', lambda wd
: self
.closed
.trigger())
410 def response(wd
, resp
):
411 if self
.child
is not None:
412 self
.note_error('Sending TERM signal to build process group %d...' % self
.child
.pid
)
413 os
.kill(-self
.child
.pid
, signal
.SIGTERM
)
415 self
.closed
.trigger()
416 w
.connect('response', response
)
418 def downloads_changed(self
):
419 if self
.config
.handler
.monitored_downloads
:
420 msg
= 'Downloads in progress:\n'
421 for x
in self
.config
.handler
.monitored_downloads
:
422 msg
+= '- {url}\n'.format(url
= x
.url
)
425 self
.details
.set_text(msg
)
427 def heading(self
, msg
):
428 self
.overall
.insert_at_end_and_scroll(msg
+ '\n', 'heading')
431 self
.overall
.insert_at_end_and_scroll(msg
+ '\n')
433 def note_error(self
, msg
):
434 self
.overall
.insert_at_end_and_scroll(msg
+ '\n', 'error')
439 tasks
.wait_for_blocker(self
.recursive_build(self
.iface_uri
))
440 except SafeException
, ex
:
441 self
.note_error(str(ex
))
443 self
.heading('All builds completed successfully!')
444 self
.dialog
.set_response_sensitive(gtk
.RESPONSE_CANCEL
, False)
445 self
.dialog
.set_response_sensitive(gtk
.RESPONSE_OK
, True)
447 tasks
.wait_for_blocker(self
.closed
)
450 def spawn_build(self
, iface_name
):
451 assert self
.child
is None
453 self
.details
.insert_at_end_and_scroll('Building %s\n' % iface_name
, 'heading')
455 # Group all the child processes so we can kill them easily
456 def become_group_leader():
458 devnull
= os
.open(os
.devnull
, os
.O_RDONLY
)
460 self
.child
= subprocess
.Popen([sys
.executable
, '-u', sys
.argv
[0], 'build'],
462 stdout
= subprocess
.PIPE
, stderr
= subprocess
.STDOUT
,
463 preexec_fn
= become_group_leader
)
468 decoder
= codecs
.getincrementaldecoder('utf-8')(errors
= 'replace')
471 yield tasks
.InputBlocker(self
.child
.stdout
, 'output from child')
472 got
= os
.read(self
.child
.stdout
.fileno(), 100)
473 chars
= decoder
.decode(got
, final
= not got
)
474 self
.details
.insert_at_end_and_scroll(chars
)
478 code
= self
.child
.returncode
481 self
.details
.insert_at_end_and_scroll('Build process exited with error status %d\n' % code
, 'error')
482 raise SafeException('Build process exited with error status %d' % code
)
483 self
.details
.insert_at_end_and_scroll('Build completed successfully\n', 'heading')
486 def confirm_import_feed(self
, pending
, valid_sigs
):
487 from zeroinstall
.gtkui
import trust_box
488 box
= trust_box
.TrustBox(pending
, valid_sigs
, parent
= self
.dialog
)
492 def do_autocompile(args
):
493 """autocompile [--gui] URI"""
495 parser
= OptionParser(usage
="usage: %prog autocompile [options]")
497 parser
.add_option('', "--gui", help="graphical interface", action
='store_true')
498 (options
, args2
) = parser
.parse_args(args
)
500 raise __main__
.UsageError()
505 h
= handler
.ConsoleHandler()
507 h
= handler
.Handler()
508 config
= load_config(handler
= h
)
509 config
._iface
_cache
= AutocompileCache()
511 iface_uri
= model
.canonical_iface_uri(args2
[0])
513 compiler
= GTKAutoCompiler(config
, iface_uri
, options
)
515 compiler
= AutoCompiler(config
, iface_uri
, options
)
519 __main__
.commands
+= [do_autocompile
]