26"""Act as if 0launch was run with the given arguments. 27 @param command_args: array of arguments (e.g. C{sys.argv[1:]}) 28 @type command_args: [str] 29 @type config: L{zeroinstall.injector.config.Config} | None""" 30# Ensure stdin, stdout and stderr FDs exist, to avoid confusion 31forstdin(0,1,2): 32try: 33os.fstat(std) 34exceptOSError: 35fd=os.open(os.devnull,os.O_RDONLY) 36iffd!=std: 37os.dup2(fd,std) 38os.close(fd) 39 40parser=OptionParser(usage=_("usage: %prog [options] interface [args]\n" 41" %prog --list [search-term]\n" 42" %prog --import [signed-interface-files]\n" 43" %prog --feed [interface]")) 44parser.add_option("","--before",help=_("choose a version before this"),metavar='VERSION') 45parser.add_option("","--command",help=_("command to select"),metavar='COMMAND') 46parser.add_option("-c","--console",help=_("never use GUI"),action='store_false',dest='gui') 47parser.add_option("","--cpu",help=_("target CPU type"),metavar='CPU') 48parser.add_option("-d","--download-only",help=_("fetch but don't run"),action='store_true') 49parser.add_option("-D","--dry-run",help=_("just print actions"),action='store_true') 50parser.add_option("-f","--feed",help=_("add or remove a feed"),action='store_true') 51parser.add_option("","--get-selections",help=_("write selected versions as XML"),action='store_true',dest='xml') 52parser.add_option("-g","--gui",help=_("show graphical policy editor"),action='store_true') 53parser.add_option("-i","--import",help=_("import from files, not from the network"),action='store_true') 54parser.add_option("-l","--list",help=_("list all known interfaces"),action='store_true') 55parser.add_option("-m","--main",help=_("name of the file to execute")) 56parser.add_option("","--message",help=_("message to display when interacting with user")) 57parser.add_option("","--not-before",help=_("minimum version to choose"),metavar='VERSION') 58parser.add_option("","--os",help=_("target operation system type"),metavar='OS') 59parser.add_option("-o","--offline",help=_("try to avoid using the network"),action='store_true') 60parser.add_option("-r","--refresh",help=_("refresh all used interfaces"),action='store_true') 61parser.add_option("","--select-only",help=_("only download the feeds"),action='store_true') 62parser.add_option("","--set-selections",help=_("run versions specified in XML file"),metavar='FILE') 63parser.add_option("","--show",help=_("show where components are installed"),action='store_true') 64parser.add_option("-s","--source",help=_("select source code"),action='store_true') 65parser.add_option("-v","--verbose",help=_("more verbose output"),action='count') 66parser.add_option("","--with-store",help=_("add an implementation cache"),action='append',metavar='DIR') 67parser.add_option("-w","--wrapper",help=_("execute program using a debugger, etc"),metavar='COMMAND') 68parser.disable_interspersed_args() 69 70# (hack to support double meaning of --version) 71allow_version_expr=any(notarg.startswith('-')forargincommand_args) 72ifallow_version_expr: 73parser.add_option("","--version",help=_("specify version constraint (e.g. '3' or '3..')"),metavar='RANGE') 74parser.add_option("","--version-for",help=_("set version constraints for a specific interface"), 75nargs=2,metavar='URI RANGE',action='append') 76else: 77parser.add_option("-V","--version",help=_("display version information"),action='store_true') 78 79(options,args)=parser.parse_args(command_args) 80 81ifoptions.verbose: 82logger=logging.getLogger() 83ifoptions.verbose==1: 84logger.setLevel(logging.INFO) 85else: 86logger.setLevel(logging.DEBUG) 87importzeroinstall 88logging.info(_("Running 0launch %(version)s %(args)s; Python %(python_version)s"),{'version':zeroinstall.version,'args':repr(args),'python_version':sys.version}) 89 90ifoptions.select_onlyoroptions.show: 91options.download_only=True 92 93ifconfigisNone: 94config=load_config() 95config.handler.dry_run=bool(options.dry_run) 96 97ifoptions.with_store: 98fromzeroinstallimportzerostore 99forxinoptions.with_store:100config.stores.stores.append(zerostore.Store(os.path.abspath(x)))101logging.info(_("Stores search path is now %s"),config.stores.stores)102103ifoptions.set_selections:104args=[options.set_selections]+args105106try:107ifoptions.list:108fromzeroinstall.cmdimportlist109list.handle(config,options,args)110elifoptions.versionandnotallow_version_expr:111importzeroinstall112print("0launch (zero-install) "+zeroinstall.version)113print("Copyright (C) 2010 Thomas Leonard")114print(_("This program comes with ABSOLUTELY NO WARRANTY,"115"\nto the extent permitted by law."116"\nYou may redistribute copies of this program"117"\nunder the terms of the GNU Lesser General Public License."118"\nFor more information about these matters, see the file named COPYING."))119elifgetattr(options,'import'):120# (import is a keyword)121cmd=__import__('zeroinstall.cmd.import',globals(),locals(),["import"],0)122cmd.handle(config,options,args)123elifoptions.feed:124fromzeroinstall.cmdimportadd_feed125add_feed.handle(config,options,args,add_ok=True,remove_ok=True)126elifoptions.select_only:127fromzeroinstall.cmdimportselect128ifnotoptions.show:129options.quiet=True130select.handle(config,options,args)131elifoptions.download_onlyoroptions.xmloroptions.show:132fromzeroinstall.cmdimportdownload133download.handle(config,options,args)134else:135iflen(args)<1:136ifoptions.gui:137# Show preferences138fromzeroinstallimporthelpers139helpers.get_selections_gui(None,[])140else:141raiseUsageError()142else:143fromzeroinstall.cmdimportrun144run.handle(config,options,args)145exceptNeedDownloadasex:146# This only happens for dry runs147print(ex)148exceptKeyboardInterrupt:149logging.info("KeyboardInterrupt")150sys.exit(1)151exceptUsageError:152parser.print_help()153sys.exit(1)154exceptSafeExceptionasex:155ifoptions.verbose:raise156try:157print(support.unicode(ex),file=sys.stderr)158except:159print(repr(ex),file=sys.stderr)160sys.exit(1)