Beispiel #1
0
def load_options( argv, setup_methods=[], builtin_methods=[] ):
   
   parser = modconf.build_parser( argv[0], conf.CONFIG_DESCRIPTION, conf.CONFIG_OPTIONS )
   opts = parser.parse_args( argv[1:] )
   
   # load everything into a dictionary and return it
   config = None 
   config_str = None
   config_file_path = None
   
   if hasattr( opts, "config" ) and opts.config != None:
      config_file_path = opts.config[0]
   else:
      config_file_path = conf.CONFIG_FILENAME
   
   config_str = storage.read_file( config_file_path )
   
   config = {}
   method_name, args, kw = read_params( getattr(opts, 'params', [] ) )
   
   if config_str is None:
      
      # possibly calling 'setup', so fill in empty information
      if method_name not in setup_methods:
         raise Exception("Failed to load configuration from %s" % config_file_path)
      
      # calling 'setup', so populate the config
      conf.fill_defaults( config )
      
      if not hasattr(opts, 'user_id') and method_name in setup_methods:
         raise Exception("--user_id option is required for this method")
      
      config['user_id'] = opts.user_id
      config_file_path = conf.CONFIG_FILENAME
      config_str = conf.make_config_str( config )
      
      config_with_opts = conf.load_config( config_file_path, config_str, opts )
      if config_with_opts != None:
         config.update( config_with_opts )
      else:
         raise Exception("Failed to parse command-line args")
      
   else:
      config = conf.load_config( config_file_path, config_str, opts )
      if config is None:
         raise Exception("Failed to parse configuration from %s" % config_file_path)
   
   config['params'] = getattr( opts, 'params', [] )
   
   # set up the key directories
   setup_key_directories( config )
   
   # generate syndicate_host and syndicate_port from URL, if needed
   if config.get('url', None) != None:
      # use https if no :// is given 
      url = config['url']
      
      host, port, no_tls = parse_url( url )
      
      config['syndicate_host'] = host 
      config['syndicate_port'] = port
      config['no_tls'] = no_tls
      
      
   # what do we need for authentication?
   auth_opts = None
   if method_name in setup_methods:
      # setup methods need password
      auth_opts = [msconfig.AUTH_METHOD_PASSWORD]
      
   elif method_name in builtin_methods:
      # builtin methods that don't do setup need no password
      auth_opts = [msconfig.AUTH_METHOD_NONE]
   
   else:
      auth_opts = api.method_auth_options( method_name )
      
   have_auth_tokens = False
   
   if msconfig.AUTH_METHOD_NONE in auth_opts:
      # no authentication needed
      config['user_pkey'] = None 
      config['password'] = None 
      have_auth_tokens = True
   
   else:
      # obtain the user's private key and/or password, if present
      if not config.has_key('user_pkey') and msconfig.AUTH_METHOD_PUBKEY in auth_opts:
         try:
            config['user_pkey'] = load_user_private_key( config, config['user_id'] )
            have_auth_tokens = True
         except Exception, e:
            log.warning("No private key found.")
            config['user_pkey'] = None
            pass
         
      if not have_auth_tokens and not config.has_key('password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts:
         config['password'] = getpass.getpass("Syndicate password: ")
         have_auth_tokens = True
         
      elif config.has_key('password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts:
         have_auth_tokens = True
Beispiel #2
0
def usage(progname):
    parser = modconf.build_parser(progname, CONFIG_DESCRIPTION)
    parser.print_help()
    sys.exit(1)
Beispiel #3
0
def usage( progname ):
   parser = modconf.build_parser( progname, CONFIG_DESCRIPTION )
   parser.print_help()
   sys.exit(1)
   
Beispiel #4
0
def load_options(argv, setup_methods=[], builtin_methods=[]):

    parser = modconf.build_parser(argv[0], conf.CONFIG_DESCRIPTION,
                                  conf.CONFIG_OPTIONS)
    opts = parser.parse_args(argv[1:])

    # load everything into a dictionary and return it
    config = None
    config_str = None
    config_file_path = None

    if hasattr(opts, "config") and opts.config != None:
        config_file_path = opts.config[0]
    else:
        config_file_path = conf.CONFIG_FILENAME

    config_str = storage.read_file(config_file_path)

    config = {}
    method_name, args, kw = read_params(getattr(opts, 'params', []))

    if config_str is None:

        # possibly calling 'setup', so fill in empty information
        if method_name not in setup_methods:
            raise Exception("Failed to load configuration from %s" %
                            config_file_path)

        # calling 'setup', so populate the config
        conf.fill_defaults(config)

        if not hasattr(opts, 'user_id') and method_name in setup_methods:
            raise Exception("--user_id option is required for this method")

        config['user_id'] = opts.user_id
        config_file_path = conf.CONFIG_FILENAME
        config_str = conf.make_config_str(config)

        config_with_opts = conf.load_config(config_file_path, config_str, opts)
        if config_with_opts != None:
            config.update(config_with_opts)
        else:
            raise Exception("Failed to parse command-line args")

    else:
        config = conf.load_config(config_file_path, config_str, opts)
        if config is None:
            raise Exception("Failed to parse configuration from %s" %
                            config_file_path)

    config['params'] = getattr(opts, 'params', [])

    # set up the key directories
    setup_key_directories(config)

    # generate syndicate_host and syndicate_port from URL, if needed
    if config.get('url', None) != None:
        # use https if no :// is given
        url = config['url']

        host, port, no_tls = parse_url(url)

        config['syndicate_host'] = host
        config['syndicate_port'] = port
        config['no_tls'] = no_tls

    # what do we need for authentication?
    auth_opts = None
    if method_name in setup_methods:
        # setup methods need password
        auth_opts = [msconfig.AUTH_METHOD_PASSWORD]

    elif method_name in builtin_methods:
        # builtin methods that don't do setup need no password
        auth_opts = [msconfig.AUTH_METHOD_NONE]

    else:
        auth_opts = api.method_auth_options(method_name)

    have_auth_tokens = False

    if msconfig.AUTH_METHOD_NONE in auth_opts:
        # no authentication needed
        config['user_pkey'] = None
        config['password'] = None
        have_auth_tokens = True

    else:
        # obtain the user's private key and/or password, if present
        if not config.has_key(
                'user_pkey') and msconfig.AUTH_METHOD_PUBKEY in auth_opts:
            try:
                config['user_pkey'] = load_user_private_key(
                    config, config['user_id'])
                have_auth_tokens = True
            except Exception, e:
                log.warning("No private key found.")
                config['user_pkey'] = None
                pass

        if not have_auth_tokens and not config.has_key(
                'password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts:
            config['password'] = getpass.getpass("Syndicate password: ")
            have_auth_tokens = True

        elif config.has_key(
                'password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts:
            have_auth_tokens = True