<commit_before># This file contains the main class of droidbot
# It can be used after AVD was started, app was installed, and adb had been set up properly
# By configuring and creating a droidbot instance,
# droidbot will start interacting with Android in AVD like a human
import logging
import os
import shutil
import subprocess
import sys
import time
import threading

from xmlrpc.client import ServerProxy
if sys.version.startswith("3"):
    from xmlrpc.server import SimpleXMLRPCServer
    from xmlrpc.server import SimpleXMLRPCRequestHandler
else:
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

from .device import Device
from .app import App

from .adapter.droidbot import DroidBotConn
from .adapter.qemu import QEMUConn

class RPCHandler(SimpleXMLRPCRequestHandler):
    def _dispatch(self, method, params):
        try:
            return self.server.funcs[method](*params)
        except:
            import traceback
            traceback.print_exc()
            raise

class DroidMaster(object):
    """
    The main class of droidmaster
    DroidMaster currently supports QEMU instance pool only
    """
    # this is a single instance class
    instance = None
    POLL_INTERVAL = 1

    def __init__(self,
                 app_path=None,
                 is_emulator=False,
                 output_dir=None,
                 env_policy=None,
                 policy_name=None,
                 random_input=False,
                 script_path=None,
                 event_count=None,
                 event_interval=None,
                 timeout=None,
                 keep_app=None,
                 keep_env=False,
                 cv_mode=False,
                 debug_mode=False,
                 profiling_method=None,
                 grant_perm=False,
                 enable_accessibility_hard=False,
                 qemu_hda=None,
                 qemu_no_graphic=False):
        """
        initiate droidmaster, and
        initiate droidbot's with configurations
        :return:
        """
        logging.basicConfig(level=logging.DEBUG if debug_mode else logging.INFO)
        self.logger = logging.getLogger('DroidMaster')
        DroidMaster.instance = self

        # 1. Save DroidBot Parameters
        self.app_path = app_path
        self.is_emulator = is_emulator

        self.output_dir = output_dir
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)

        self.env_policy = env_policy
        self.policy_name = policy_name
        self.random_input = random_input
        self.script_path = script_path
        self.event_count = event_count
        self.event_interval = event_interval
        self.timeout = timeout
        self.keep_app = keep_app
        self.keep_env = keep_env
        self.cv_mode = cv_mode
        self.debug_mode = debug_mode
        self.profiling_method = profiling_method
        self.grant_perm = grant_perm
        self.enable_accessibility_hard = enable_accessibility_hard

        # 2. Initiate Device Pool
        self.domain = "localhost"
        self.rpc_port = Device().get_random_port()

        self.qemu_hda = qemu_hda
        self.qemu_no_graphic = qemu_no_graphic

        self.device_pool_capacity = 6
        self.device_pool = {}
        self.device_unique_id = 0

        self.app = App(app_path, output_dir=self.output_dir)
        self.qemu_app_hda = "%s_%s" % (self.qemu_hda, self.app.get_package_name())

        for i in range(self.device_pool_capacity):
            adb_port = Device().get_random_port()
            device_serial = "%s:%s" % (self.domain, adb_port)
            qemu_port = Device().get_random_port()
            device = Device(
                device_serial=device_serial,
                is_emulator=self.is_emulator,
                output_dir=self.output_dir,
                cv_mode=self.cv_mode,
                grant_perm=self.grant_perm,
                enable_accessibility_hard=self.enable_accessibility_hard)<commit_msg>fix device error in master mode when there is no device<commit_after># This file contains the main class of droidbot
# It can be used after AVD was started, app was installed, and adb had been set up properly
# By configuring and creating a droidbot instance,
# droidbot will start interacting with Android in AVD like a human
import logging
import os
import shutil
import subprocess
import sys
import time
import threading

from xmlrpc.client import ServerProxy
if sys.version.startswith("3"):
    from xmlrpc.server import SimpleXMLRPCServer
    from xmlrpc.server import SimpleXMLRPCRequestHandler
else:
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

from .device import Device
from .app import App

from .adapter.droidbot import DroidBotConn
from .adapter.qemu import QEMUConn

class RPCHandler(SimpleXMLRPCRequestHandler):
    def _dispatch(self, method, params):
        try:
            return self.server.funcs[method](*params)
        except:
            import traceback
            traceback.print_exc()
            raise

class DroidMaster(object):
    """
    The main class of droidmaster
    DroidMaster currently supports QEMU instance pool only
    """
    # this is a single instance class
    instance = None
    POLL_INTERVAL = 1

    def __init__(self,
                 app_path=None,
                 is_emulator=False,
                 output_dir=None,
                 env_policy=None,
                 policy_name=None,
                 random_input=False,
                 script_path=None,
                 event_count=None,
                 event_interval=None,
                 timeout=None,
                 keep_app=None,
                 keep_env=False,
                 cv_mode=False,
                 debug_mode=False,
                 profiling_method=None,
                 grant_perm=False,
                 enable_accessibility_hard=False,
                 qemu_hda=None,
                 qemu_no_graphic=False):
        """
        initiate droidmaster, and
        initiate droidbot's with configurations
        :return:
        """
        logging.basicConfig(level=logging.DEBUG if debug_mode else logging.INFO)
        self.logger = logging.getLogger('DroidMaster')
        DroidMaster.instance = self

        # 1. Save DroidBot Parameters
        self.app_path = app_path
        self.is_emulator = is_emulator

        self.output_dir = output_dir
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)

        self.env_policy = env_policy
        self.policy_name = policy_name
        self.random_input = random_input
        self.script_path = script_path
        self.event_count = event_count
        self.event_interval = event_interval
        self.timeout = timeout
        self.keep_app = keep_app
        self.keep_env = keep_env
        self.cv_mode = cv_mode
        self.debug_mode = debug_mode
        self.profiling_method = profiling_method
        self.grant_perm = grant_perm
        self.enable_accessibility_hard = enable_accessibility_hard

        # 2. Initiate Device Pool
        self.domain = "localhost"
        self.rpc_port = Device(device_serial="").get_random_port()

        self.qemu_hda = qemu_hda
        self.qemu_no_graphic = qemu_no_graphic

        self.device_pool_capacity = 6
        self.device_pool = {}
        self.device_unique_id = 0

        self.app = App(app_path, output_dir=self.output_dir)
        self.qemu_app_hda = "%s_%s" % (self.qemu_hda, self.app.get_package_name())

        for i in range(self.device_pool_capacity):
            adb_port = Device(device_serial="").get_random_port()
            device_serial = "%s:%s" % (self.domain, adb_port)
            qemu_port = Device(device_serial="").get_random_port()
            device = Device(
                device_serial=device_serial,
                is_emulator=self.is_emulator,
                output_dir=self.output_dir,
                cv_mode=self.cv_mode,
                grant_perm=self.grant_perm,
                enable_accessibility_hard=self.enable_accessibility_hard)
{'fraction_non_alphanumeric': 0.0651908023483366, 'fraction_numerical': 0.0014677103718199608, 'mean_word_length': 2.1706087630864674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '4975390', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2276, 'n_words': 529}