1 # Copyright (C) 2009, Thomas Leonard
2 # See http://0install.net/0compile.html
4 import sys
, os
, __main__
, tempfile
, shutil
, subprocess
, signal
5 from xml
.dom
import minidom
6 from optparse
import OptionParser
8 from zeroinstall
import SafeException
9 from zeroinstall
.injector
import arch
, handler
, policy
, model
, iface_cache
, selections
, namespaces
, writer
, reader
10 from zeroinstall
.zerostore
import manifest
11 from zeroinstall
.support
import tasks
, basedir
13 from support
import BuildEnv
15 # This is a bit hacky...
17 # We invent a new CPU type which is compatible with the host but worse than
18 # every existing type, and we use * for the OS type so that we don't beat 'Any'
19 # binaries either. This means that we always prefer an existing binary of the
20 # desired version to compiling a new one, but we'll compile a new version from source
21 # rather than use an older binary.
22 arch
.machine_groups
['newbuild'] = arch
.machine_groups
.get(arch
._uname
[-1], 0)
23 arch
.machine_ranks
['newbuild'] = max(arch
.machine_ranks
.values()) + 1
24 host_arch
= '*-newbuild'
26 class AutocompileCache(iface_cache
.IfaceCache
):
28 iface_cache
.IfaceCache
.__init
__(self
)
31 def get_interface(self
, uri
):
32 iface
= iface_cache
.IfaceCache
.get_interface(self
, uri
)
33 if not iface
: return None
34 feed
= iface
._main
_feed
36 # Note: when a feed is updated, a new ZeroInstallFeed object is created,
37 # so record whether we've seen the feed, not the interface.
39 if feed
not in self
.done
:
42 # For each source impl, add a corresponding binary
43 # (the binary has no dependencies as we can't predict them here,
44 # but they're not the same as the source's dependencies)
46 srcs
= [x
for x
in feed
.implementations
.itervalues() if x
.arch
and x
.arch
.endswith('-src')]
48 new_id
= '0compile=' + x
.id
49 if not new_id
in feed
.implementations
:
50 new
= feed
._get
_impl
(new_id
)
51 new
.set_arch(host_arch
)
52 new
.version
= x
.version
56 policy
.iface_cache
= AutocompileCache()
58 class MyHandler(handler
.Handler
):
59 def downloads_changed(self
):
60 self
.compiler
.downloads_changed()
63 def __init__(self
, iface_uri
):
64 self
.iface_uri
= iface_uri
65 self
.handler
= MyHandler()
66 self
.handler
.compiler
= self
68 def downloads_changed(self
):
69 if self
.handler
.monitored_downloads
:
70 self
.note('Downloads in progress:')
71 for x
in self
.handler
.monitored_downloads
:
74 self
.note('No downloads remaining.')
76 def pretty_print_plan(self
, solver
, root
, indent
= '- '):
77 """Display a tree showing the selected implementations."""
78 iface
= solver
.iface_cache
.get_interface(root
)
79 impl
= solver
.selections
[iface
]
81 msg
= 'Failed to select any suitable version (source or binary)'
82 elif impl
.id.startswith('0compile='):
83 real_impl_id
= impl
.id.split('=', 1)[1]
84 real_impl
= impl
.feed
.implementations
[real_impl_id
]
85 msg
= 'Compile %s (%s)' % (real_impl
.get_version(), real_impl
.id)
86 elif impl
.arch
and impl
.arch
.endswith('-src'):
87 msg
= 'Compile %s (%s)' % (impl
.get_version(), impl
.id)
90 msg
= 'Use existing binary %s (%s)' % (impl
.get_version(), impl
.arch
)
92 msg
= 'Use existing architecture-independent package %s' % impl
.get_version()
93 self
.note("%s%s: %s" % (indent
, iface
.get_name(), msg
))
97 for x
in impl
.requires
:
98 self
.pretty_print_plan(solver
, x
.interface
, indent
)
100 def print_details(self
, solver
):
101 """Dump debugging details."""
102 self
.note("\nDetails of all components and versions considered:")
103 for iface
in solver
.details
:
104 self
.note('\n%s\n' % iface
.get_name())
105 for impl
, note
in solver
.details
[iface
]:
106 self
.note('%s (%s) : %s' % (impl
.get_version(), impl
.arch
or '*-*', note
or 'OK'))
107 self
.note("\nEnd details")
110 def compile_and_register(self
, policy
):
111 local_feed_dir
= basedir
.save_config_path('0install.net', '0compile', 'builds', model
._pretty
_escape
(policy
.root
))
112 s
= selections
.Selections(policy
)
114 buildenv
= BuildEnv(need_config
= False)
115 buildenv
.config
.set('compile', 'interface', policy
.root
)
116 buildenv
.config
.set('compile', 'selections', 'selections.xml')
118 version
= s
.selections
[policy
.root
].version
119 local_feed
= os
.path
.join(local_feed_dir
, '%s-%s-%s.xml' % (buildenv
.iface_name
, version
, arch
._uname
[-1]))
120 if os
.path
.exists(local_feed
):
121 raise SafeException("Build metadata file '%s' already exists!" % local_feed
)
123 tmpdir
= tempfile
.mkdtemp(prefix
= '0compile-')
127 # Write configuration for build...
131 sel_file
= open('selections.xml', 'w')
134 doc
.writexml(sel_file
)
141 build
= self
.spawn_build(buildenv
.iface_name
)
146 # Register the result...
148 alg
= manifest
.get_algorithm('sha1new')
149 digest
= alg
.new_digest()
151 for line
in alg
.generate_manifest(buildenv
.distdir
):
155 actual_digest
= alg
.getID(digest
)
157 local_feed_file
= file(local_feed
, 'w')
159 dom
= minidom
.parse(buildenv
.local_iface_file
)
160 impl
, = dom
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'implementation')
161 impl
.setAttribute('id', actual_digest
)
162 dom
.writexml(local_feed_file
)
163 local_feed_file
.write('\n')
165 local_feed_file
.close()
167 self
.note("Implementation metadata written to %s" % local_feed
)
169 self
.note("Storing build in cache...")
170 policy
.solver
.iface_cache
.stores
.add_dir_to_cache(actual_digest
, buildenv
.distdir
)
172 self
.note("Registering feed...")
173 iface
= policy
.solver
.iface_cache
.get_interface(policy
.root
)
174 feed
= iface
.get_feed(local_feed
)
176 self
.note("WARNING: feed %s already registered!" % local_feed
)
178 iface
.extra_feeds
.append(model
.Feed(local_feed
, impl
.getAttribute('arch'), user_override
= True))
179 writer
.save_interface(iface
)
181 # We might have cached an old version
182 new_feed
= policy
.solver
.iface_cache
.get_interface(local_feed
)
183 reader
.update_from_cache(new_feed
)
185 self
.note("\nBuild failed: leaving build directory %s for inspection...\n" % tmpdir
)
188 shutil
.rmtree(tmpdir
)
191 def recursive_build(self
, iface_uri
, version
= None):
192 p
= policy
.Policy(iface_uri
, handler
= self
.handler
, src
= True)
193 iface
= p
.solver
.iface_cache
.get_interface(iface_uri
)
194 p
.solver
.record_details
= True
196 p
.solver
.extra_restrictions
[iface
] = [model
.VersionRestriction(model
.parse_version(version
))]
199 #p.target_arch = arch.Architecture(os_ranks = {'FreeBSD': 0, None: 1}, machine_ranks = {'i386': 0, None: 1, 'newbuild': 2})
201 self
.heading(iface_uri
)
204 self
.note("\nSelecting versions for %s..." % iface
.get_name())
205 solved
= p
.solve_with_downloads()
210 if not p
.solver
.ready
:
211 self
.print_details(p
.solver
)
212 raise SafeException("Can't find all required implementations (source or binary):\n" +
213 '\n'.join(["- %s -> %s" % (iface
, p
.solver
.selections
[iface
])
214 for iface
in p
.solver
.selections
]))
215 self
.note("Selection done.")
217 self
.note("\nPlan:\n")
218 self
.pretty_print_plan(p
.solver
, p
.root
)
221 for dep_iface
, dep_impl
in p
.solver
.selections
.iteritems():
222 if dep_impl
.id.startswith('0compile='):
223 build
= self
.recursive_build(dep_iface
.uri
, dep_impl
.get_version())
226 break # Try again with that dependency built...
228 self
.note("No dependencies need compiling... compile %s itself..." % iface
.get_name())
229 build
= self
.compile_and_register(p
)
234 def spawn_build(self
, iface_name
):
235 subprocess
.check_call([sys
.executable
, sys
.argv
[0], 'build'])
238 self
.handler
.wait_for_blocker(self
.recursive_build(self
.iface_uri
))
240 def heading(self
, msg
):
241 self
.note((' %s ' % msg
).center(76, '='))
246 def note_error(self
, msg
):
247 self
.overall
.insert_at_cursor(msg
+ '\n')
249 class GTKAutoCompiler(AutoCompiler
):
250 def __init__(self
, iface_uri
):
251 AutoCompiler
.__init
__(self
, iface_uri
)
254 import pygtk
; pygtk
.require('2.0')
257 w
= gtk
.Dialog('Autocompile %s' % iface_uri
, None, 0,
258 (gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
,
259 gtk
.STOCK_OK
, gtk
.RESPONSE_OK
))
262 w
.set_default_size(int(gtk
.gdk
.screen_width() * 0.8),
263 int(gtk
.gdk
.screen_height() * 0.8))
265 vpaned
= gtk
.VPaned()
267 w
.set_response_sensitive(gtk
.RESPONSE_OK
, False)
272 tv
.set_property('left-margin', 8)
273 tv
.set_wrap_mode(gtk
.WRAP_WORD_CHAR
)
274 tv
.set_editable(False)
275 swin
= gtk
.ScrolledWindow()
277 swin
.set_policy(gtk
.POLICY_NEVER
, gtk
.POLICY_AUTOMATIC
)
278 buffer = tv
.get_buffer()
280 heading
= buffer.create_tag('heading')
281 heading
.set_property('scale', 1.5)
283 error
= buffer.create_tag('error')
284 error
.set_property('background', 'white')
285 error
.set_property('foreground', 'red')
291 def insert_at_end_and_scroll(self
, data
, *tags
):
292 # TODO: data might not be complete UTF-8; buffer until EOL?
293 self
.vscroll
= self
.widget
.get_vadjustment()
294 near_end
= self
.vscroll
.upper
- self
.vscroll
.page_size
* 1.5 < self
.vscroll
.value
295 end
= self
.buffer.get_end_iter()
296 self
.buffer.insert_with_tags_by_name(end
, data
, *tags
)
298 cursor
= self
.buffer.get_insert()
299 self
.buffer.move_mark(cursor
, end
)
300 self
.tv
.scroll_to_mark(cursor
, 0, False, 0, 0)
302 self
.overall
= AutoScroller()
303 self
.details
= AutoScroller()
305 vpaned
.pack1(self
.overall
.widget
, True, False)
306 vpaned
.pack2(self
.details
.widget
, True, False)
308 self
.closed
= tasks
.Blocker('Window closed')
311 w
.connect('destroy', lambda wd
: self
.closed
.trigger())
313 def response(wd
, resp
):
314 if self
.child
is not None:
315 self
.note_error('Sending TERM signal to build process...')
316 os
.kill(self
.child
.pid
, signal
.SIGTERM
)
318 self
.closed
.trigger()
319 w
.connect('response', response
)
321 def heading(self
, msg
):
322 self
.overall
.insert_at_end_and_scroll(msg
+ '\n', 'heading')
325 self
.overall
.insert_at_end_and_scroll(msg
+ '\n')
327 def note_error(self
, msg
):
328 self
.overall
.insert_at_end_and_scroll(msg
+ '\n', 'error')
333 self
.handler
.wait_for_blocker(self
.recursive_build(self
.iface_uri
))
334 except SafeException
, ex
:
335 self
.note_error(str(ex
))
337 self
.heading('All builds completed successfully!')
338 self
.dialog
.set_response_sensitive(gtk
.RESPONSE_CANCEL
, False)
339 self
.dialog
.set_response_sensitive(gtk
.RESPONSE_OK
, True)
341 self
.handler
.wait_for_blocker(self
.closed
)
344 def spawn_build(self
, iface_name
):
345 assert self
.child
is None
347 self
.details
.insert_at_end_and_scroll('Building %s\n' % iface_name
, 'heading')
349 self
.child
= subprocess
.Popen([sys
.executable
, sys
.argv
[0], 'build'], stdout
= subprocess
.PIPE
, stderr
= subprocess
.STDOUT
)
352 yield tasks
.InputBlocker(self
.child
.stdout
, 'output from child')
353 got
= os
.read(self
.child
.stdout
.fileno(), 100)
355 self
.details
.insert_at_end_and_scroll(got
)
358 code
= self
.child
.returncode
361 self
.details
.insert_at_end_and_scroll('Build process exited with error status %d\n' % code
, 'error')
362 raise SafeException('Build process exited with error status %d' % code
)
363 self
.details
.insert_at_end_and_scroll('Build completed successfully\n', 'heading')
365 def do_autocompile(args
):
366 """autocompile [--gui] URI"""
368 parser
= OptionParser(usage
="usage: %prog autocompile [options]")
370 parser
.add_option('', "--gui", help="graphical interface", action
='store_true')
371 (options
, args2
) = parser
.parse_args(args
)
373 raise __main__
.UsageError()
375 iface_uri
= model
.canonical_iface_uri(args2
[0])
377 compiler
= GTKAutoCompiler(iface_uri
)
379 compiler
= AutoCompiler(iface_uri
)
383 __main__
.commands
+= [do_autocompile
]