New 0solve command for use as Solver backend
[zeroinstall/solver.git] / zeroinstall / injector / cli_solve.py
blobc02f55d0e5a74e8303b174352b015d55e2d593c8
1 """
2 The B{0solve} command-line interface.
4 This code is here, rather than in B{0solve} itself, simply so that it gets byte-compiled at
5 install time.
6 """
8 from zeroinstall import _
9 import os, sys
10 from optparse import OptionParser
12 from zeroinstall import SafeException, NeedDownload
13 from zeroinstall.injector import model, autopolicy, selections, handler
14 from zeroinstall.injector.iface_cache import iface_cache
16 def main(command_args):
17 """Act as if 0solve was run with the given arguments.
18 @arg command_args: array of arguments (e.g. C{sys.argv[1:]})
19 @type command_args: [str]
20 """
21 parser = OptionParser(usage=_("usage: %prog [options] interface [args]"))
22 parser.add_option("", "--before", help=_("choose a version before this"), metavar='VERSION')
23 parser.add_option("", "--cpu", help=_("target CPU type"), metavar='CPU')
24 parser.add_option("", "--not-before", help=_("minimum version to choose"), metavar='VERSION')
25 parser.add_option("", "--os", help=_("target operation system type"), metavar='OS')
26 parser.add_option("-o", "--offline", help=_("try to avoid using the network"), action='store_true')
27 parser.add_option("-r", "--refresh", help=_("refresh all used interfaces"), action='store_true')
28 parser.add_option("-s", "--source", help=_("select source code"), action='store_true')
29 parser.add_option("", "--with-store", help=_("add an implementation cache"), action='append', metavar='DIR')
30 parser.disable_interspersed_args()
32 (options, args) = parser.parse_args(command_args)
34 if options.with_store:
35 from zeroinstall import zerostore
36 for x in options.with_store:
37 iface_cache.stores.stores.append(zerostore.Store(os.path.abspath(x)))
39 iface_uri = model.canonical_iface_uri(args[0])
40 root_iface = iface_cache.get_interface(iface_uri)
42 policy = autopolicy.AutoPolicy(iface_uri, handler = handler.BatchHandler(), download_only = True, src = options.source)
44 if options.before or options.not_before:
45 policy.solver.extra_restrictions[root_iface] = [model.VersionRangeRestriction(model.parse_version(options.before), model.parse_version(options.not_before))]
47 if options.os or options.cpu:
48 from zeroinstall.injector import arch
49 policy.target_arch = arch.get_architecture(options.os, options.cpu)
51 if options.offline:
52 policy.network_use = model.network_offline
54 downloaded = policy.solve_and_download_impls(refresh = options.refresh, select_only = True)
55 if downloaded:
56 policy.handler.wait_for_blocker(downloaded)
58 try:
59 sels = selections.Selections(policy)
60 doc = sels.toDOM()
61 doc.writexml(sys.stdout)
62 sys.stdout.write('\n')
63 except SafeException, ex:
64 error(unicode(ex))
65 sys.exit(1)