Ejemplo n.º 1
0
def get_dirs(base_dir, create=False):
    " Get, and optionally create the required working directories."
    ret = (os.path.join(base_dir, '__wikibot_tmp__'),
            os.path.join(base_dir, 'hgrepo'),
            os.path.join(base_dir, 'bot_storage'),
            os.path.join(os.path.join(base_dir, 'bot_storage'), # required?
                         '.hg'))

    if create:
        for value in ret:
            if os.path.exists(value):
                raise IOError("Directory already exists: %s" % value)
        print
        for value in ret:
            os.makedirs(value)
            if not is_writable(value):
                raise IOError("Couldn't write to: %s" % value)
            print "Created: %s" % value

        print
        print "You need to MANUALLY fn-pull the wikitext repo into:"
        print ret[1]

    else:
        for value in ret:
            if not is_writable(value):
                raise IOError("Directory doesn't exist or isn't writable: %s"
                              % value)
    return ret[:3]
Ejemplo n.º 2
0
def setup(ui_, repo, params, stored_cfg):
    """ INTERNAL: Setup to run an Infocalypse extension command. """
    # REDFLAG: choose another name. Confusion w/ fcp param
    # REDFLAG: add an hg param and get rid of this line.
    #params['VERBOSITY'] = 1

    check_uri(ui_, params.get('INSERT_URI'))
    check_uri(ui_, params.get('REQUEST_URI'))

    if not is_writable(os.path.expanduser(stored_cfg.defaults['TMP_DIR'])):
        raise util.Abort("Can't write to temp dir: %s\n" %
                         stored_cfg.defaults['TMP_DIR'])

    verbosity = params.get('VERBOSITY', 1)
    set_debug_vars(verbosity, params)

    callbacks = UICallbacks(ui_)
    callbacks.verbosity = verbosity

    if not repo is None:
        # BUG:? shouldn't this be reading TMP_DIR from stored_cfg
        cache = BundleCache(repo, ui_, params['TMP_DIR'])

    try:
        async_socket = PolledSocket(params['FCP_HOST'], params['FCP_PORT'])
        connection = FCPConnection(async_socket, True,
                                   callbacks.connection_state)
    except socket.error, err:  # Not an IOError until 2.6.
        ui_.warn("Connection to FCP server [%s:%i] failed.\n" %
                 (params['FCP_HOST'], params['FCP_PORT']))
        raise err
Ejemplo n.º 3
0
def setup_sm(ui_, repo, runner, params):
    """ INTERNAL: Helper function which sets up an UpdateStateMachine
        instance. """
    assert is_writable(os.path.expanduser(params['TMP_DIR']))

    verbosity = params.get('VERBOSITY', 1)
    set_debug_vars(verbosity, params)

    callbacks = UICallbacks(ui_)
    callbacks.verbosity = verbosity
    # DCI: bundle cache needed for inserting?
    cache = BundleCache(repo, ui_, params['TMP_DIR'])

    # For Infocalypse repositories
    ctx = UpdateContext(None)
    ctx.repo = repo
    ctx.ui_ = ui_
    ctx.bundle_cache = cache
    update_sm = UpdateStateMachine(runner, ctx)

    update_sm.params = params.copy()
    update_sm.transition_callback = callbacks.transition_callback
    update_sm.monitor_callback = callbacks.monitor_callback

    # Modify only after copy.
    update_sm.params['FREENET_BUILD'] = runner.connection.node_hello[1]['Build']

    return update_sm
Ejemplo n.º 4
0
def setup(ui_, repo, params, stored_cfg):
    """ INTERNAL: Setup to run an Infocalypse extension command. """
    # REDFLAG: choose another name. Confusion w/ fcp param
    # REDFLAG: add an hg param and get rid of this line.
    #params['VERBOSITY'] = 1

    check_uri(ui_, params.get('INSERT_URI'))
    check_uri(ui_, params.get('REQUEST_URI'))

    if not is_writable(os.path.expanduser(stored_cfg.defaults['TMP_DIR'])):
        raise util.Abort("Can't write to temp dir: %s\n"
                         % stored_cfg.defaults['TMP_DIR'])

    verbosity = params.get('VERBOSITY', 1)
    set_debug_vars(verbosity, params)

    callbacks = UICallbacks(ui_)
    callbacks.verbosity = verbosity

    if not repo is None:
        # BUG:? shouldn't this be reading TMP_DIR from stored_cfg
        cache = BundleCache(repo, ui_, params['TMP_DIR'])

    try:
        async_socket = PolledSocket(params['FCP_HOST'], params['FCP_PORT'])
        connection = FCPConnection(async_socket, True,
                                   callbacks.connection_state)
    except socket.error, err: # Not an IOError until 2.6.
        ui_.warn("Connection to FCP server [%s:%i] failed.\n"
                % (params['FCP_HOST'], params['FCP_PORT']))
        raise err
Ejemplo n.º 5
0
def setup_sm(ui_, repo, runner, params):
    """ INTERNAL: Helper function which sets up an UpdateStateMachine
        instance. """
    assert is_writable(os.path.expanduser(params['TMP_DIR']))

    verbosity = params.get('VERBOSITY', 1)
    set_debug_vars(verbosity, params)

    callbacks = UICallbacks(ui_)
    callbacks.verbosity = verbosity
    # DCI: bundle cache needed for inserting?
    cache = BundleCache(repo, ui_, params['TMP_DIR'])

    # For Infocalypse repositories
    ctx = UpdateContext(None)
    ctx.repo = repo
    ctx.ui_ = ui_
    ctx.bundle_cache = cache
    update_sm = UpdateStateMachine(runner, ctx)

    update_sm.params = params.copy()
    update_sm.transition_callback = callbacks.transition_callback
    update_sm.monitor_callback = callbacks.monitor_callback

    # Modify only after copy.
    update_sm.params['FREENET_BUILD'] = runner.connection.node_hello[1][
        'Build']

    return update_sm
Ejemplo n.º 6
0
def execute_setup(ui_, host, port, tmp, cfg_file=None):
    """ Run the setup command. """
    def connection_failure(msg):
        """ INTERNAL: Display a warning string. """
        ui_.warn(msg)
        ui_.warn("It looks like your FCP host or port might be wrong.\n")
        ui_.warn("Set them with --fcphost and/or --fcpport and try again.\n")
        raise util.Abort("Connection to FCP server failed.")

    # Fix defaults.
    if host == '':
        host = '127.0.0.1'
    if port == 0:
        port = 9481

    if cfg_file is None:
        cfg_file = os.path.expanduser(DEFAULT_CFG_PATH)

    existing_name = ui_.config('infocalypse', 'cfg_file', None)
    if not existing_name is None:
        existing_name = os.path.expanduser(existing_name)
        ui_.status(MSG_HGRC_SET % existing_name)
        cfg_file = existing_name

    if os.path.exists(cfg_file):
        ui_.status(MSG_CFG_EXISTS % cfg_file)
        raise util.Abort("Refusing to modify existing configuration.")

    tmp = setup_tmp_dir(ui_, tmp)

    if not is_writable(tmp):
        raise util.Abort("Can't write to temp dir: %s\n" % tmp)

    # Test FCP connection.
    timeout_secs = 20
    connection = None
    default_private_key = None
    try:
        ui_.status("Testing FCP connection [%s:%i]...\n" % (host, port))

        connection = FCPConnection(PolledSocket(host, port))

        started = time.time()
        while (not connection.is_connected()
               and time.time() - started < timeout_secs):
            connection.socket.poll()
            time.sleep(.25)

        if not connection.is_connected():
            connection_failure(("\nGave up after waiting %i secs for an " +
                                "FCP NodeHello.\n") % timeout_secs)

        ui_.status("Looks good.\nGenerating a default private key...\n")

        # Hmmm... this waits on a socket. Will an ioerror cause an abort?
        # Lazy, but I've never seen this call fail except for IO reasons.
        client = FCPClient(connection)
        client.message_callback = lambda x, y: None  # Disable chatty default.
        default_private_key = client.generate_ssk()[1]['InsertURI']

    except FCPError:
        # Protocol error.
        connection_failure("\nMaybe that's not an FCP server?\n")

    except socket.error:  # Not an IOError until 2.6.
        # Horked.
        connection_failure("\nSocket level error.\n")

    except IOError:
        # Horked.
        connection_failure("\nSocket level error.\n")

    cfg = Config()
    cfg.defaults['HOST'] = host
    cfg.defaults['PORT'] = port
    cfg.defaults['TMP_DIR'] = tmp
    cfg.defaults['DEFAULT_PRIVATE_KEY'] = default_private_key
    Config.to_file(cfg, cfg_file)

    ui_.status("""\nFinished setting configuration.
FCP host: %s
FCP port: %i
Temp dir: %s
cfg file: %s

Default private key:
%s

The config file was successfully written!

""" % (host, port, tmp, cfg_file, default_private_key))
Ejemplo n.º 7
0
def execute_setup(ui_, host, port, tmp, cfg_file = None):
    """ Run the setup command. """
    def connection_failure(msg):
        """ INTERNAL: Display a warning string. """
        ui_.warn(msg)
        ui_.warn("It looks like your FCP host or port might be wrong.\n")
        ui_.warn("Set them with --fcphost and/or --fcpport and try again.\n")
        raise util.Abort("Connection to FCP server failed.")

    # Fix defaults.
    if host == '':
        host = '127.0.0.1'
    if port == 0:
        port = 9481

    if cfg_file is None:
        cfg_file = os.path.expanduser(DEFAULT_CFG_PATH)

    existing_name = ui_.config('infocalypse', 'cfg_file', None)
    if not existing_name is None:
        existing_name = os.path.expanduser(existing_name)
        ui_.status(MSG_HGRC_SET % existing_name)
        cfg_file = existing_name

    if os.path.exists(cfg_file):
        ui_.status(MSG_CFG_EXISTS % cfg_file)
        raise util.Abort("Refusing to modify existing configuration.")

    tmp = setup_tmp_dir(ui_, tmp)

    if not is_writable(tmp):
        raise util.Abort("Can't write to temp dir: %s\n" % tmp)

    # Test FCP connection.
    timeout_secs = 20
    connection = None
    default_private_key = None
    try:
        ui_.status("Testing FCP connection [%s:%i]...\n" % (host, port))

        connection = FCPConnection(PolledSocket(host, port))

        started = time.time()
        while (not connection.is_connected() and
               time.time() - started < timeout_secs):
            connection.socket.poll()
            time.sleep(.25)

        if not connection.is_connected():
            connection_failure(("\nGave up after waiting %i secs for an "
                               + "FCP NodeHello.\n") % timeout_secs)

        ui_.status("Looks good.\nGenerating a default private key...\n")

        # Hmmm... this waits on a socket. Will an ioerror cause an abort?
        # Lazy, but I've never seen this call fail except for IO reasons.
        client = FCPClient(connection)
        client.message_callback = lambda x, y:None # Disable chatty default.
        default_private_key = client.generate_ssk()[1]['InsertURI']

    except FCPError:
        # Protocol error.
        connection_failure("\nMaybe that's not an FCP server?\n")

    except socket.error: # Not an IOError until 2.6.
        # Horked.
        connection_failure("\nSocket level error.\n")

    except IOError:
        # Horked.
        connection_failure("\nSocket level error.\n")

    cfg = Config()
    cfg.defaults['HOST'] = host
    cfg.defaults['PORT'] = port
    cfg.defaults['TMP_DIR'] = tmp
    cfg.defaults['DEFAULT_PRIVATE_KEY'] = default_private_key
    Config.to_file(cfg, cfg_file)

    ui_.status("""\nFinished setting configuration.
FCP host: %s
FCP port: %i
Temp dir: %s
cfg file: %s

Default private key:
%s

The config file was successfully written!

""" % (host, port, tmp, cfg_file, default_private_key))