コード例 #1
0
def prune_dead():
    for beacon in aggressor.beacons():
        if beacon['alive'] == 'false':
            bid = beacon['id']
            engine.message('removing beacon {} ({}@{})'.format(
                bid, beacon['user'], beacon['computer']))
            aggressor.beacon_remove(bid)
コード例 #2
0
def prune_old(hours):
    for beacon in aggressor.beacons():
        last = int(beacon['last'])

        if older_than(last, hours):
            bid = beacon['id']
            last_hours = last / 1000 / 60 / 60
            engine.message('removing beacon {} ({}@{}) ({} hours old)'.format(
                bid, beacon['user'], beacon['computer'], int(last_hours)))
            aggressor.beacon_remove(bid)
コード例 #3
0
def _(bid):
    aggressor.beacon_remove(bid)
コード例 #4
0
def cleanup(hours=80, dry=True):
    # collect notes
    notes = collections.defaultdict(set)
    for beacon in aggressor.beacons():
        # skip 'killing'
        if beacon['note'] == 'killing':
            continue

        ident = '{}@{}'.format(beacon['user'], beacon['computer'])
        notes[ident].add(beacon['note'])

    # remove dead
    if not dry:
        prune_dead()
    else:
        engine.message('not pruning dead beacons')

    # remove old
    #if not dry:
    #    prune_old(int(hours))
    #else:
    #    engine.message('not pruning old beacons')

    # collect beacons
    by_ident = collections.defaultdict(list)
    for beacon in aggressor.beacons():
        # skip dead beacons
        if beacon['alive'] == 'false':
            continue

        ident = '{}@{}'.format(beacon['user'], beacon['computer'])
        by_ident[ident].append(beacon)

    # sort beacons by newest
    for ident, beacons in by_ident.items():
        beacons = sorted(beacons, key=lambda b: int(b['last']))
        by_ident[ident] = beacons

    # de-duplicate
    for ident, beacons in by_ident.items():
        if len(beacons) > 1:
            # pick a beacon. to choose a selected beacon we:
            #   - find all beacons with last times within 2 hours of the newest beacon
            #   - pick the newest beacon of those with a note
            #   - or: pick the newest beacon
            #newest_beacon = beacons[0]
            #for beacon in beacons[1:]:
            #    if last_difference(newest_beacon['last'], beacon['last']) > 1.0:
            #        if beacon['note']:
            #            # newest beacon with a note
            #            picked_beacon = beacon
            #            break
            #else:
            #    # newest beacon
            #    picked_beacon = beacons[0]

            picked_beacon = list(
                filter(lambda b: b['note'] != 'killing', beacons))[0]
            beacons.remove(picked_beacon)

            # kill or remove the other beacons
            for beacon in beacons:
                if {'keep', 'test'} & set(beacon['note'].split()):
                    # special note. don't kill
                    engine.message(
                        'not touching beaacon with keep note {} {}'.format(
                            ident, beacon['note']))
                elif last_difference(picked_beacon['last'],
                                     beacon['last']) > 2.0:
                    # probably dead. just remove
                    engine.message('removing older beacon {} {}'.format(
                        ident, beacon['id']))
                    if not dry:
                        aggressor.beacon_remove(beacon['id'])
                else:
                    # kill and remove
                    engine.message('killing older beacon {} {}'.format(
                        ident, beacon['id']))
                    if not dry:
                        suicide(beacon['id'])

            # pick shortest note
            picked_note = None
            for note in notes[ident]:
                if not picked_note or len(picked_note) > note:
                    picked_note = note

            if picked_note:
                engine.message('{} picked note: {}'.format(ident, picked_note))
                if not dry:
                    aggressor.bnote(picked_beacon['id'], picked_note)
コード例 #5
0
def _(bid, output, when):
    global killing

    if output == 'beacon exit.' and bid in killing:
        killing.remove(bid)
        aggressor.beacon_remove(bid)