예제 #1
0
def get_tube(initCond, initDelta, waypoint, TC_Simulate, beta):
    initCond[:3] -= waypoint

    from config import normalize, num_dim_observable, observe
    import numpy as np
    import torch
    # initCond: n array
    # initDelta: n array
    # beta = loadTrainedModel()

    T_MAX = 10.0

    # find circumscribed ball
    r = np.sqrt(
        ((normalize(initCond) - normalize(initCond + initDelta))**2).sum())
    center = initCond
    ref_trace = TC_Simulate(center, T_MAX).tolist()
    ellipsoids = []
    reachsets = [
        waypoint.repeat(2) + np.array(
            [initCond - initDelta, initCond + initDelta]).T[:3, :].reshape(-1),
    ]

    # for point in tqdm(trace[1::]):
    for point in ref_trace[1::]:
        P = beta(
            torch.tensor(center.tolist() + [r, point[0]]).view(1, -1).cuda())
        P = P.view(num_dim_observable, num_dim_observable)
        reachsets.append(
            waypoint.repeat(2) + ellipsoid2AArectangle(
                P.cpu().detach().numpy(), observe(np.array(point[1::]))))
        ellipsoids.append(
            [observe(np.array(point[1::])),
             P.cpu().detach().numpy()])
    return ellipsoids, reachsets
예제 #2
0
def execute_info(ui_, repo, params, stored_cfg):
    """ Run the info command. """
    request_uri = params['REQUEST_URI']
    if request_uri is None or not is_usk_file(request_uri):
        ui_.status("Only works with USK file URIs.\n")
        return

    usk_hash = normalize(request_uri)
    max_index = stored_cfg.get_index(request_uri)
    if max_index is None:
        ui_.status(NO_INFO_FMT % usk_hash)
        return

    insert_uri = str(stored_cfg.get_insert_uri(usk_hash))

    # fix index
    request_uri = get_usk_for_usk_version(request_uri, max_index)

    trusted = stored_cfg.trusted_notifiers(usk_hash)
    if not trusted:
        trusted = '   None'
    else:
        trusted = '   ' + '\n   '.join(trusted)

    ui_.status(INFO_FMT %
               (usk_hash, max_index or -1, trusted, request_uri, insert_uri))

    update_sm = setup(ui_, repo, params, stored_cfg)
    try:
        ui_.status('Freenet head(s): %s\n' % ' '.join([
            ver[:12]
            for ver in read_freenet_heads(params, update_sm, request_uri)
        ]))
    finally:
        cleanup(update_sm)
예제 #3
0
def execute_info(ui_, repo, params, stored_cfg):
    """ Run the info command. """
    request_uri = params['REQUEST_URI']
    if request_uri is None or not is_usk_file(request_uri):
        ui_.status("Only works with USK file URIs.\n")
        return

    usk_hash = normalize(request_uri)
    max_index = stored_cfg.get_index(request_uri)
    if max_index is None:
        ui_.status(NO_INFO_FMT % usk_hash)
        return

    insert_uri = str(stored_cfg.get_insert_uri(usk_hash))

    # fix index
    request_uri = get_usk_for_usk_version(request_uri, max_index)

    trusted = stored_cfg.trusted_notifiers(usk_hash)
    if not trusted:
        trusted = '   None'
    else:
        trusted = '   ' + '\n   '.join(trusted)

    ui_.status(INFO_FMT %
               (usk_hash, max_index or -1, trusted, request_uri, insert_uri))

    update_sm = setup(ui_, repo, params, stored_cfg)
    try:
        ui_.status('Freenet head(s): %s\n' %
                   ' '.join([ver[:12] for ver in
                             read_freenet_heads(params, update_sm,
                                                request_uri)]))
    finally:
        cleanup(update_sm)
예제 #4
0
def execute_insert_patch(ui_, repo, params, stored_cfg):
    """ Create and hg bundle containing all changes not already in the
        infocalypse repo in Freenet and insert it to a CHK.

        Returns a machine readable patch notification message.
        """
    try:
        update_sm = setup(ui_, repo, params, stored_cfg)
        out_file = make_temp_file(update_sm.ctx.bundle_cache.base_dir)

        ui_.status("Reading repo state from Freenet...\n")
        freenet_heads = read_freenet_heads(params, update_sm,
                                           params['REQUEST_URI'])

        # This may eventually change to support other patch types.
        create_patch_bundle(ui_, repo, freenet_heads, out_file)

        # Make an FCP file insert request which will run on the
        # on the state machine.
        request = StatefulRequest(update_sm)
        request.tag = 'patch_bundle_insert'
        request.in_params.definition = PUT_FILE_DEF
        request.in_params.fcp_params = update_sm.params.copy()
        request.in_params.fcp_params['URI'] = 'CHK@'
        request.in_params.file_name = out_file
        request.in_params.send_data = True

        # Must do this here because file gets deleted.
        chk_len = os.path.getsize(out_file)

        ui_.status("Inserting %i byte patch bundle...\n" %
                   os.path.getsize(out_file))
        update_sm.start_single_request(request)
        run_until_quiescent(update_sm, params['POLL_SECS'])

        freenet_heads = list(freenet_heads)
        freenet_heads.sort()
        heads = [hexlify(head) for head in repo.heads()]
        heads.sort()
        if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING,))):
            chk = update_sm.get_state(RUNNING_SINGLE_REQUEST).\
                  final_msg[1]['URI']
            ui_.status("Patch CHK:\n%s\n" %
                       chk)
            # ':', '|' not in freenet base64
            ret = ':'.join(('B', normalize(params['REQUEST_URI']), str(chk_len),
                            ':'.join([base[:12] for base in freenet_heads]),
                            '|', ':'.join([head[:12] for head in heads]), chk))

            ui_.status("\nNotification:\n%s\n" % ret
                        + '\n')
            return ret

        raise util.Abort("Patch CHK insert failed.")

    finally:
        # Cleans up out file.
        cleanup(update_sm)
예제 #5
0
def execute_insert_patch(ui_, repo, params, stored_cfg):
    """ Create and hg bundle containing all changes not already in the
        infocalypse repo in Freenet and insert it to a CHK.

        Returns a machine readable patch notification message.
        """
    try:
        update_sm = setup(ui_, repo, params, stored_cfg)
        out_file = make_temp_file(update_sm.ctx.bundle_cache.base_dir)

        ui_.status("Reading repo state from Freenet...\n")
        freenet_heads = read_freenet_heads(params, update_sm,
                                           params['REQUEST_URI'])

        # This may eventually change to support other patch types.
        create_patch_bundle(ui_, repo, freenet_heads, out_file)

        # Make an FCP file insert request which will run on the
        # on the state machine.
        request = StatefulRequest(update_sm)
        request.tag = 'patch_bundle_insert'
        request.in_params.definition = PUT_FILE_DEF
        request.in_params.fcp_params = update_sm.params.copy()
        request.in_params.fcp_params['URI'] = 'CHK@'
        request.in_params.file_name = out_file
        request.in_params.send_data = True

        # Must do this here because file gets deleted.
        chk_len = os.path.getsize(out_file)

        ui_.status("Inserting %i byte patch bundle...\n" %
                   os.path.getsize(out_file))
        update_sm.start_single_request(request)
        run_until_quiescent(update_sm, params['POLL_SECS'])

        freenet_heads = list(freenet_heads)
        freenet_heads.sort()
        heads = [hexlify(head) for head in repo.heads()]
        heads.sort()
        if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING, ))):
            chk = update_sm.get_state(RUNNING_SINGLE_REQUEST).\
                  final_msg[1]['URI']
            ui_.status("Patch CHK:\n%s\n" % chk)
            # ':', '|' not in freenet base64
            ret = ':'.join(
                ('B', normalize(params['REQUEST_URI']), str(chk_len),
                 ':'.join([base[:12] for base in freenet_heads]), '|',
                 ':'.join([head[:12] for head in heads]), chk))

            ui_.status("\nNotification:\n%s\n" % ret + '\n')
            return ret

        raise util.Abort("Patch CHK insert failed.")

    finally:
        # Cleans up out file.
        cleanup(update_sm)
예제 #6
0
def infocalypse_create(ui_, repo, local_identity=None, **opts):
    """ Create a new Infocalypse repository in Freenet.
    :type local_identity: Local_WoT_ID
    :param local_identity: If specified the new repository is associated with
                           that identity.
    """
    params, stored_cfg = get_config_info(ui_, opts)

    if opts['uri'] and opts['wot']:
        ui_.warn("Please specify only one of --uri or --wot.\n")
        return
    elif opts['uri']:
        insert_uri = parse_repo_path(opts['uri'])
    elif opts['wot']:
        opts['wot'] = parse_repo_path(opts['wot'])
        nick_prefix, repo_name, repo_edition = opts['wot'].split('/', 2)

        if not repo_name.endswith('.R1') and not repo_name.endswith('.R0'):
            ui_.warn("Warning: Creating repository without redundancy. (R0 or"
                     " R1)\n")

        from wot_id import Local_WoT_ID

        local_identity = Local_WoT_ID(nick_prefix)

        insert_uri = local_identity.insert_uri.clone()

        insert_uri.name = repo_name
        insert_uri.edition = repo_edition
        # Before passing along into execute_create().
        insert_uri = str(insert_uri)
    else:
        ui_.warn("Please set the insert key with either --uri or --wot.\n")
        return

    # This is a WoT repository.
    if local_identity:
        # Prompt whether to replace in the case of conflicting names.
        from wot import build_repo_list

        request_usks = build_repo_list(ui_, local_identity)
        names = map(lambda x: USK(x).get_repo_name(), request_usks)
        new_name = USK(insert_uri).get_repo_name()

        if new_name in names:
            replace = ui_.prompt("A repository with the name '{0}' is already"
                                 " published by {1}. Replace it? [y/N]"
                                 .format(new_name, local_identity),
                                 default='n')

            if replace.lower() != 'y':
                raise util.Abort("A repository with this name already exists.")

            # Remove the existing repository from each configuration section.
            existing_usk = request_usks[names.index(new_name)]

            existing_dir = None
            for directory, request_usk in stored_cfg.request_usks.iteritems():
                if request_usk == existing_usk:
                    if existing_dir:
                        raise util.Abort("Configuration lists the same "
                                         "request USK multiple times.")
                    existing_dir = directory

            assert existing_dir

            existing_hash = normalize(existing_usk)

            # Config file changes will not be written until a successful insert
            # below.
            del stored_cfg.version_table[existing_hash]
            del stored_cfg.request_usks[existing_dir]
            del stored_cfg.insert_usks[existing_hash]
            del stored_cfg.wot_identities[existing_hash]

        # Add "vcs" context. No-op if the identity already has it.
        msg_params = {'Message': 'AddContext',
                      'Identity': local_identity.identity_id,
                      'Context': 'vcs'}

        import fcp
        import wot
        node = fcp.FCPNode(**wot.get_fcpopts(fcphost=opts["fcphost"],
                                             fcpport=opts["fcpport"]))
        atexit.register(node.shutdown)
        vcs_response =\
            node.fcpPluginMessage(plugin_name="plugins.WebOfTrust.WebOfTrust",
                                  plugin_params=msg_params)[0]

        if vcs_response['header'] != 'FCPPluginReply' or\
                'Replies.Message' not in vcs_response or\
                vcs_response['Replies.Message'] != 'ContextAdded':
            raise util.Abort("Failed to add context. Got {0}\n.".format(
                             vcs_response))

    set_target_version(ui_, repo, opts, params,
                       "Only inserting to version(s): %s\n")
    params['INSERT_URI'] = insert_uri
    inserted_to = execute_create(ui_, repo, params, stored_cfg)

    if inserted_to and local_identity:
        # creation returns a list of request URIs; use the first.
        stored_cfg.set_wot_identity(inserted_to[0], local_identity)
        Config.to_file(stored_cfg)

        import wot
        wot.update_repo_listing(ui_, local_identity, 
                                fcphost=opts["fcphost"],
                                fcpport=opts["fcpport"])
예제 #7
0
def execute_wiki_submit(ui_, repo, params, stored_cfg):
    """ Insert and overlayed wiki change submission CHK into freenet and
        return a notification message string. """
    update_sm = None
    try:
        # Read submitter out of stored_cfg
        submitter = stored_cfg.defaults.get('FMS_ID', None)
        assert not submitter is None
        assert submitter.find('@') == -1

        # Get version, i.e. just the hg parent == hg head
        version = get_hg_version(repo)

        params['ISWIKI'] = True
        read_freesite_cfg(ui_, repo, params, stored_cfg)
        if not params.get('OVERLAYED', False):
            raise util.Abort("Can't submit from non-overlayed wiki edits!")
        if not params.get('CLIENT_WIKI_GROUP', None):
            # DCI: test code path
            raise util.Abort("No wiki_group in fnwiki.cfg. Don't " +
                             "know where to post to!")

        ui_.status("\nPreparing to submit to %s FMS group as %s.\n" %
                   (params['CLIENT_WIKI_GROUP'], submitter))

        # Create submission zip file in RAM.
        overlay = get_file_funcs(os.path.join(repo.root, params['WIKI_ROOT']),
                                 True)
        try:
            raw_bytes = bundle_wikitext(overlay, version, submitter)
        except NoChangesError:
            raise util.Abort("There are no overlayed changes to submit.")
        # Punt if it's too big.
        if len(raw_bytes) >= FREENET_BLOCK_LEN:
            raise util.Abort("Too many changes. Change .zip must be <32K")

        update_sm = setup(ui_, repo, params, stored_cfg)

        # Make an FCP file insert request which will run on the
        # on the state machine.
        request = StatefulRequest(update_sm)
        request.tag = 'submission_zip_insert'
        request.in_params.definition = PUT_FILE_DEF
        request.in_params.fcp_params = update_sm.params.copy()
        request.in_params.fcp_params['URI'] = 'CHK@'
        request.in_params.send_data = raw_bytes

        ui_.status("Inserting %i byte submission CHK...\n" % len(raw_bytes))
        update_sm.start_single_request(request)
        run_until_quiescent(update_sm, params['POLL_SECS'])

        heads = [hexlify(head) for head in repo.heads()]
        heads.sort()
        if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING, ))):
            chk = update_sm.get_state(RUNNING_SINGLE_REQUEST).\
                  final_msg[1]['URI']
            ui_.status("Patch CHK:\n%s\n" % chk)
            # ':', '|' not in freenet base64
            # DCI: why normalize???
            # (usk_hash, base_version, chk, length)
            ret = ':'.join(
                ('W', normalize(params['REQUEST_URI']), version[:12], chk,
                 str(len(raw_bytes))))

            ui_.status("\nNotification:\n%s\n" % ret + '\n')

            return ret, params['CLIENT_WIKI_GROUP']

        raise util.Abort("Submission CHK insert failed.")

    finally:
        # Cleans up out file.
        cleanup(update_sm)
예제 #8
0
def infocalypse_create(ui_, repo, local_identity=None, **opts):
    """ Create a new Infocalypse repository in Freenet.
    :type local_identity: Local_WoT_ID
    :param local_identity: If specified the new repository is associated with
                           that identity.
    """
    params, stored_cfg = get_config_info(ui_, opts)

    if opts['uri'] and opts['wot']:
        ui_.warn("Please specify only one of --uri or --wot.\n")
        return
    elif opts['uri']:
        insert_uri = parse_repo_path(opts['uri'])
    elif opts['wot']:
        opts['wot'] = parse_repo_path(opts['wot'])
        nick_prefix, repo_name, repo_edition = opts['wot'].split('/', 2)

        if not repo_name.endswith('.R1') and not repo_name.endswith('.R0'):
            ui_.warn("Warning: Creating repository without redundancy. (R0 or"
                     " R1)\n")

        from wot_id import Local_WoT_ID

        local_identity = Local_WoT_ID(nick_prefix)

        insert_uri = local_identity.insert_uri.clone()

        insert_uri.name = repo_name
        insert_uri.edition = repo_edition
        # Before passing along into execute_create().
        insert_uri = str(insert_uri)
    else:
        ui_.warn("Please set the insert key with either --uri or --wot.\n")
        return

    # This is a WoT repository.
    if local_identity:
        # Prompt whether to replace in the case of conflicting names.
        from wot import build_repo_list

        request_usks = build_repo_list(ui_, local_identity)
        names = map(lambda x: USK(x).get_repo_name(), request_usks)
        new_name = USK(insert_uri).get_repo_name()

        if new_name in names:
            replace = ui_.prompt("A repository with the name '{0}' is already"
                                 " published by {1}. Replace it? [y/N]".format(
                                     new_name, local_identity),
                                 default='n')

            if replace.lower() != 'y':
                raise util.Abort("A repository with this name already exists.")

            # Remove the existing repository from each configuration section.
            existing_usk = request_usks[names.index(new_name)]

            existing_dir = None
            for directory, request_usk in stored_cfg.request_usks.iteritems():
                if request_usk == existing_usk:
                    if existing_dir:
                        raise util.Abort("Configuration lists the same "
                                         "request USK multiple times.")
                    existing_dir = directory

            assert existing_dir

            existing_hash = normalize(existing_usk)

            # Config file changes will not be written until a successful insert
            # below.
            del stored_cfg.version_table[existing_hash]
            del stored_cfg.request_usks[existing_dir]
            del stored_cfg.insert_usks[existing_hash]
            del stored_cfg.wot_identities[existing_hash]

        # Add "vcs" context. No-op if the identity already has it.
        msg_params = {
            'Message': 'AddContext',
            'Identity': local_identity.identity_id,
            'Context': 'vcs'
        }

        import fcp
        import wot
        node = fcp.FCPNode(**wot.get_fcpopts(fcphost=opts["fcphost"],
                                             fcpport=opts["fcpport"]))
        atexit.register(node.shutdown)
        vcs_response =\
            node.fcpPluginMessage(plugin_name="plugins.WebOfTrust.WebOfTrust",
                                  plugin_params=msg_params)[0]

        if vcs_response['header'] != 'FCPPluginReply' or\
                'Replies.Message' not in vcs_response or\
                vcs_response['Replies.Message'] != 'ContextAdded':
            raise util.Abort(
                "Failed to add context. Got {0}\n.".format(vcs_response))

    set_target_version(ui_, repo, opts, params,
                       "Only inserting to version(s): %s\n")
    params['INSERT_URI'] = insert_uri
    inserted_to = execute_create(ui_, repo, params, stored_cfg)

    if inserted_to and local_identity:
        # creation returns a list of request URIs; use the first.
        stored_cfg.set_wot_identity(inserted_to[0], local_identity)
        Config.to_file(stored_cfg)

        import wot
        wot.update_repo_listing(ui_,
                                local_identity,
                                fcphost=opts["fcphost"],
                                fcpport=opts["fcpport"])
예제 #9
0
def execute_wiki_submit(ui_, repo, params, stored_cfg):
    """ Insert and overlayed wiki change submission CHK into freenet and
        return a notification message string. """
    update_sm = None
    try:
        # Read submitter out of stored_cfg
        submitter = stored_cfg.defaults.get('FMS_ID', None)
        assert not submitter is None
        assert submitter.find('@') == -1

        # Get version, i.e. just the hg parent == hg head
        version = get_hg_version(repo)

        params['ISWIKI'] = True
        read_freesite_cfg(ui_, repo, params, stored_cfg)
        if not params.get('OVERLAYED', False):
            raise util.Abort("Can't submit from non-overlayed wiki edits!")
        if not params.get('CLIENT_WIKI_GROUP', None):
            # DCI: test code path
            raise util.Abort("No wiki_group in fnwiki.cfg. Don't " +
                             "know where to post to!")

        ui_.status("\nPreparing to submit to %s FMS group as %s.\n" %
                   (params['CLIENT_WIKI_GROUP'], submitter))

        # Create submission zip file in RAM.
        overlay = get_file_funcs(os.path.join(repo.root, params['WIKI_ROOT']),
                                 True)
        try:
            raw_bytes = bundle_wikitext(overlay, version, submitter)
        except NoChangesError:
            raise util.Abort("There are no overlayed changes to submit.")
        # Punt if it's too big.
        if len(raw_bytes) >= FREENET_BLOCK_LEN:
            raise util.Abort("Too many changes. Change .zip must be <32K")

        update_sm = setup(ui_, repo, params, stored_cfg)

        # Make an FCP file insert request which will run on the
        # on the state machine.
        request = StatefulRequest(update_sm)
        request.tag = 'submission_zip_insert'
        request.in_params.definition = PUT_FILE_DEF
        request.in_params.fcp_params = update_sm.params.copy()
        request.in_params.fcp_params['URI'] = 'CHK@'
        request.in_params.send_data = raw_bytes

        ui_.status("Inserting %i byte submission CHK...\n" % len(raw_bytes))
        update_sm.start_single_request(request)
        run_until_quiescent(update_sm, params['POLL_SECS'])

        heads = [hexlify(head) for head in repo.heads()]
        heads.sort()
        if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING,))):
            chk = update_sm.get_state(RUNNING_SINGLE_REQUEST).\
                  final_msg[1]['URI']
            ui_.status("Patch CHK:\n%s\n" %
                       chk)
            # ':', '|' not in freenet base64
            # DCI: why normalize???
            # (usk_hash, base_version, chk, length)
            ret = ':'.join(('W',
                            normalize(params['REQUEST_URI']),
                            version[:12],
                            chk,
                            str(len(raw_bytes))))

            ui_.status("\nNotification:\n%s\n" % ret
                        + '\n')

            return ret, params['CLIENT_WIKI_GROUP']

        raise util.Abort("Submission CHK insert failed.")

    finally:
        # Cleans up out file.
        cleanup(update_sm)