Ejemplo n.º 1
0
    def create_gref(channel):
        def _write_object(obj):
            return station.write(obj.as_object())

        name = request.form["name"]
        protocol = request.form["protocol"]
        user = request.form["user"]
        body = request.form["body"]
        title = request.form["title"]
        gref = Gref(station.store, channel, name)
        root = RootObject(name, channel, protocol)
        root_oid = _write_object(root)

        _title = UpdateObject([root_oid],
                              json.dumps({
                                  "type": "title",
                                  "id": None,
                                  "body": title,
                                  "user": user
                              }))
        title_oid = _write_object(_title)

        _body = UpdateObject([title_oid],
                             json.dumps({
                                 "type": "body",
                                 "id": None,
                                 "body": body
                             }))
        body_oid = _write_object(_body)

        _update_gref(gref, [Tip(body_oid, "")], [])
        return ""
Ejemplo n.º 2
0
 def _update_gref(gref, tips, parents):
     if app.has_signing_key:
         tips = map(
             lambda tip: Tip(tip.tip,
                             app.private_crypto_adaptor.sign(tip.tip)),
             tips)
     station.update_gref(gref, tips, parents)
Ejemplo n.º 3
0
    def handle_describechannels_for_missing_tip(self):
        test_id = u"tests/1"
        test_channel = u"test_channel"
        test_protocol = u"test_protocol"

        gref = Gref(self.station.station.store, test_channel, test_id)

        root_obj = RootObject(test_id, test_channel, test_protocol)
        root_oid = self.station.station.write(root_obj.as_object())
        current_oid = [root_oid]

        for i in xrange(10):
            update_obj = UpdateObject([current_oid.pop()], "loldata")
            current_oid.append(self.station.station.write(update_obj.as_object()))

        update_obj = UpdateObject([current_oid.pop()], "loldata")
        current_oid.append(binascii.hexlify(pygit2.hash(update_obj.as_object())))

        self.station.station.update_gref(gref, [Tip(root_oid, "")])
        self.assertEqual([root_oid], gref.tips())
        current_oid = current_oid.pop()

        self.station.payload = _payload(update_obj, gref, [current_oid])
        handle_describechannels(self.station)
        self.assertEqual([current_oid], gref.tips())
Ejemplo n.º 4
0
        def _write_new_tip(obj):
            our_parents = []
            while parents:
                our_parents.append(parents.pop())
            log.debug("Creating new object with parents: %s" %
                      (str(our_parents)))

            oid = self.station.write(obj.as_object())
            self.station.update_gref(gref, [Tip(oid, "")], our_parents)
            parents.append(oid)
            log.debug("Setting parents to: %s" % (str(parents)))
Ejemplo n.º 5
0
 def update_gref(channel, identifier):
     # adaptor = github_protocol.GithubWriteAdaptor(station, channel)
     gref = Gref(station.store, channel, identifier)
     # Ugly type coercion
     user = request.form["user"]
     body = request.form["body"]
     parents = map(str, json.loads(request.form["parents"]))
     payload = {"type": "comment", "id": None, "body": body, "user": user}
     update_object = UpdateObject(parents, json.dumps(payload))
     oid = station.write(update_object.as_object())
     _update_gref(gref, [Tip(oid, "")], parents)
     return jsonate({"response": "ok"}, False)
Ejemplo n.º 6
0
    def start_listening(self):
        header_byte_count, header_bytes, header_length = reset_header()
        payload = reset_payload()
        listen.setup_processes(self.tones)
        for char in listen.start_analysing_stream():
            char_value = ord(char)
            if char_value & 0b10000000:  # Header byte
                log.info("Header byte: %s" % (bin(char_value)))
                if header_byte_count == 0:  # First header byte
                    header_bytes[0] |= char_value & 0xf
                    header_byte_count += 1
                elif header_byte_count == 1:
                    header_bytes[0] |= (char_value & 0xf) << 4
                    header_byte_count += 1
                elif header_byte_count == 2:
                    header_bytes[1] |= (char_value & 0xf)
                    header_byte_count += 1
                elif header_byte_count == 3:
                    header_bytes[1] |= (char_value & 0xf) << 4
                    header_byte_count += 1
                    header_length = struct.unpack(
                        ">h", ''.join(map(chr, header_bytes)))[0]
                    log.info("Reading %d bytes of payload" % header_length)
                    payload = reset_payload()
                elif header_byte_count == 4:
                    log.warning(
                        "Got header bytes while still expecting payload")
                    payload.append(char)

            else:  # Payload byte
                if header_length == 0:
                    log.warning("Got payload bytes when expecting header")
                payload.append(char)
                if len(payload) % 8 == 0:
                    log.info("Got %d bytes so far" % (len(payload)))
                if len(payload) == header_length:
                    log.info("Got all of object")
                    decoded = base64.b64decode(''.join(payload))
                    log.info("Writing payload to DB")
                    oid = self.station.write(decoded)
                    log.info("Wrote object, got: %s" % (repr(oid)))
                    Gref(self.station.store, "soundstation", "demo")
                    try:
                        self.station.update_gref(g, [Tip(oid, "")], [])  # yolo
                    except:
                        pass

                    header_byte_count, header_bytes, header_length = reset_header(
                    )
                    payload = reset_payload()
Ejemplo n.º 7
0
def handle_describechannels(self):
    if not self.payload:
        log.info(
            "station %s sent empty DESCRIBECHANNELS payload - new database?" %
            (str(self.origin)))
        return
    proto_channels = groundstation.proto.channel_list_pb2.ChannelList()
    proto_channels.ParseFromString(self.payload)
    for channel in proto_channels.channels:
        for gref in channel.grefs:
            _gref = Gref(self.station.store, channel.channelname,
                         gref.identifier)
            # Create a tip object to avoid upsetting fileutils
            tips = [Tip(tip.tip, tip.signature) for tip in gref.tips]
            self.station.update_gref(_gref, tips, True)
Ejemplo n.º 8
0
    def test_handle_listallchannels(self):
        test_id = u"tests/1"
        test_channel = u"test_channel"
        test_protocol = u"test_protocol"

        obj = RootObject(test_id, test_channel, test_protocol)
        gref = Gref(self.station.station.store, test_channel, test_id)

        oid = self.station.station.write(obj.as_object())
        self.station.station.update_gref(gref, [Tip(oid, "")], [])

        handle_listallchannels(self.station)
        response = self.station.stream.pop()
        serialized_response = response.SerializeToString()
        self.assertIsInstance(serialized_response, str)
        assert len(self.station.stream) == 0, "Someone is leaving objects lyind around"
        channel_description = ChannelList()
        channel_description.ParseFromString(response.payload)
        self.assertEqual(channel_description.channels[0].channelname, test_channel)
        self.assertEqual(channel_description.channels[0].grefs[0].identifier, test_id)
        self.assertEqual(channel_description.channels[0].grefs[0].tips[0].tip, oid)
Ejemplo n.º 9
0
 def test_channels(self):
     gref = Gref(self.station.store, "test_channel", "test_id")
     self.station.update_gref(gref, [Tip("foobar", "")], "")
     self.assertEqual(self.station.channels(), ["test_channel"])