Exemplo n.º 1
0
def parse_args(argv, Environment=Environment):
    options, args = parser.parse_args(argv)
    if len(args) == 0:
        error("Please enter a sprinter action: %s" % str(VALID_COMMANDS))
    command = args[0].lower()
    if command not in VALID_COMMANDS:
        error("Please enter a valid sprinter action: %s" % ",".join(VALID_COMMANDS))
    target = args[1] if len(args) > 1 else None
    logging_level = logging.DEBUG if options.verbose else logging.INFO
    # start processing commands
    env = Environment(logging_level=logging_level)
    try:
        if command == "install":
            def handle_install_shutdown(signal, frame):
                if env.phase == PHASE.INSTALL:
                    print "Removing install..."
                    env.directory.remove()
                    env.clear_all()
                signal_handler(signal, frame)
            signal.signal(signal.SIGINT, handle_install_shutdown)
            if options.username or options.auth:
                options = get_credentials(options, parse_domain(target))
                target = Manifest(target,
                                  username=options.username,
                                  password=options.password,
                                  verify_certificate=(not options.allow_bad_certificate))
            env.target = target
            env.namespace = options.namespace
            env.install()

        elif command == "update":
            env.directory = Directory(target)
            env.source = Manifest(env.directory.manifest_path)
            if options.username or options.auth:
                options = get_credentials(options, target)
            env.target = Manifest(env.source.source(),
                                  username=options.username,
                                  password=options.password,
                                  verify_certificate=(not options.allow_bad_certificate))
            env.update(reconfigure=options.reconfigure)

        elif command in ["remove", "deactivate", "activate", "reconfigure"]:
            env.directory = Directory(target)
            env.source = Manifest(env.directory.manifest_path, namespace=target)
            getattr(env, command)()

        elif command == "environments":
            SPRINTER_ROOT = os.path.expanduser(os.path.join("~", ".sprinter"))
            for env in os.listdir(SPRINTER_ROOT):
                print "%s" % env

        elif command == "validate":
            if options.username or options.auth:
                options = get_credentials(parse_domain(target))
            errors = env.validate_manifest(target, username=options.username, password=options.password)
            if len(errors) > 0:
                print "Manifest is invalid!"
                print "\n".join(errors)
            else:
                print "Manifest is valid!"
    except BadCredentialsException, e:
        raise e