<commit_before>#!/usr/bin/env python2
# encoding=utf_8

from __future__ import unicode_literals

import os
import re
import select
import socket
import subprocess
from subprocess import Popen, PIPE, STDOUT
from sys import stderr, stdout
from time import sleep

from common import PollWrapper, SocketWrapper


block_add_pattern = re.compile(
    r'KERNEL\[[^]]*\]\s*add\s*(?P<path>\S*)\s*\(block\)')
block_remove_pattern = re.compile(
    r'KERNEL\[[^]]*\]\s*remove\s*(?P<path>\S*)\s*\(block\)')


class ServerSocketWrapper(SocketWrapper):
    interact_g = None

    def interact_generator(self, plugged):
        self.prepare_receive_message()
        while True:
            ret = self.receive_message()
            if ret is not None:
                items, args = ret
                command = items[0]
                break
            yield

        print 'Received message {}, {}'.format(command, args)

        if command == 'mount':
            mountpoint = args.pop('mountpoint', None)
            if mountpoint is None:
                self.prepare_send_message(
                    'error', desc='The "mountpoint" argument is required.')
                while self.send_message() is None:
                    yield
                yield True

            def f(dev):
                for field, value in args.iteritems():
                    if field in dev and dev[field] == value:
                        return True
                return False
            matches = filter(f, plugged.itervalues())

            if len(matches) > 1:
                self.prepare_send_message(
                    'error', desc='Multiple devices matched')
                while self.send_message() is None:
                    yield
                yield True
            if len(matches) == 0:
                self.prepare_send_message('error', desc='No devices matched')
                while self.send_message() is None:
                    yield
                yield True

            dev_path = '/dev/' + matches[0]['name']
            # FIXME: Fork or something.
            if subprocess.call(('mount', dev_path, mountpoint)) == 0:
                matches[0]['mountpoint'] = mountpoint
                self.prepare_send_message('success')
                while self.send_message() is None:
                    yield
                yield True
            else:
                self.prepare_send_message('error', desc='Mount failed')
                while self.send_message() is None:
                    yield
                yield True
        elif command == 'unmount':
            def f(dev):
                for field, value in args.iteritems():
                    if 'mountpoint' in dev and field in dev and \
                            dev[field] == value:
                        return True
                return False
            matches = filter(f, plugged.itervalues())

            if len(matches) > 1:
                self.prepare_send_message(
                    'error', desc='Multiple devices matched')
                while self.send_message() is None:
                    yield
                yield True
            if len(matches) == 0:
                self.prepare_send_message('error', desc='No devices matched')
                while self.send_message() is None:<commit_msg>Device must match all filters, not just one<commit_after>#!/usr/bin/env python2
# encoding=utf_8

from __future__ import unicode_literals

import os
import re
import select
import socket
import subprocess
from subprocess import Popen, PIPE, STDOUT
from sys import stderr, stdout
from time import sleep

from common import PollWrapper, SocketWrapper


block_add_pattern = re.compile(
    r'KERNEL\[[^]]*\]\s*add\s*(?P<path>\S*)\s*\(block\)')
block_remove_pattern = re.compile(
    r'KERNEL\[[^]]*\]\s*remove\s*(?P<path>\S*)\s*\(block\)')


class ServerSocketWrapper(SocketWrapper):
    interact_g = None

    def interact_generator(self, plugged):
        self.prepare_receive_message()
        while True:
            ret = self.receive_message()
            if ret is not None:
                items, args = ret
                command = items[0]
                break
            yield

        print 'Received message {}, {}'.format(command, args)

        if command == 'mount':
            mountpoint = args.pop('mountpoint', None)
            if mountpoint is None:
                self.prepare_send_message(
                    'error', desc='The "mountpoint" argument is required.')
                while self.send_message() is None:
                    yield
                yield True

            def f(dev):
                for field, value in args.iteritems():
                    if field in dev and dev[field] == value:
                        return True
                return False
            matches = filter(f, plugged.itervalues())

            if len(matches) > 1:
                self.prepare_send_message(
                    'error', desc='Multiple devices matched')
                while self.send_message() is None:
                    yield
                yield True
            if len(matches) == 0:
                self.prepare_send_message('error', desc='No devices matched')
                while self.send_message() is None:
                    yield
                yield True

            dev_path = '/dev/' + matches[0]['name']
            # FIXME: Fork or something.
            if subprocess.call(('mount', dev_path, mountpoint)) == 0:
                matches[0]['mountpoint'] = mountpoint
                self.prepare_send_message('success')
                while self.send_message() is None:
                    yield
                yield True
            else:
                self.prepare_send_message('error', desc='Mount failed')
                while self.send_message() is None:
                    yield
                yield True
        elif command == 'unmount':
            def f(dev):
                if 'mountpoint' not in dev:
                    return False
                for field, value in args.iteritems():
                    if field not in dev or dev[field] != value:
                        return False
                return True
            matches = filter(f, plugged.itervalues())

            if len(matches) > 1:
                self.prepare_send_message(
                    'error', desc='Multiple devices matched')
                while self.send_message() is None:
                    yield
                yield True
            if len(matches) == 0:
                self.prepare_send_message('error', desc='No devices matched')
                while self.send_message() is None:
{'fraction_non_alphanumeric': 0.09471630141098769, 'fraction_numerical': 0.003002101471029721, 'mean_word_length': 1.4992498124531133, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2031095', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1613, 'n_words': 476}