<commit_before>import os
import subprocess
import re
import threading
from functools import wraps
from concurrent.futures import Future, wait
import time

from ..logger import Logger


class ADB(object):
    """
    Wrapper for adb commands.

    Import::

        from magneto.utils.adb import ADB

    :required: Android SDK installed and ANDROID_HOME pointing to SDK directory location.
    """

    _lock = threading.Lock()
    device_id = None
    ADB_PATH = os.path.join(os.environ['ANDROID_HOME'], 'platform-tools', 'adb')

    @classmethod
    def exec_cmd(cls, exec_cmd, stdin=None, stdout=None, stderr=None):
        """
        Executes adb commands::

            ADB.exec_cmd('shell uninstall com.android.package')

        In order to retrieve the process output, tap into the stdout::

            import subprocess

            proc = ADB.exec_cmd('shell pm list packages', stdout=subprocess.PIPE)
            list = proc.stdout.readline().strip('\\r\\n')

        :param str exec_cmd: Adb command to execute
        :param stdin:
        :param stdout:
        :param stderr:
        :return: Adb process
        """
        with cls._lock:
            cmd = 'exec {adb_path} {device_id} {command}'.format(
                adb_path=cls.ADB_PATH,
                device_id='-s {0}'.format(cls.device_id) if cls.device_id else '',
                command=exec_cmd
            )

            return subprocess.Popen(cmd, shell=True, stdin=stdin, stdout=stdout, stderr=stderr)

    @classmethod
    def get_log(cls):
        """
        Get log adb logcat.

        :return: Log dump
        """
        dump = []
        p = cls.exec_cmd("logcat -v time -d", stdout=subprocess.PIPE)
        for l in p.stdout:
            dump.append(l.strip())

        return dump

    @classmethod
    def clear_log(cls):
        """
        Clears adb log

        :return: Adb process
        """
        return cls.exec_cmd("logcat -c")

    @classmethod
    def start_activity(cls, package_name, activity_name, extras=None):
        """
        Start app activity.

        Open Gmail app::

            ADB.start_activity('com.google.android.gm', 'com.google.android.gm.ConversationListActivity')

        :param str package_name: Package name
        :param str activity_name: Activity name
        :return:
        """
        Logger.debug('opening activity {}'.format(activity_name))
        cls.exec_cmd("shell am start {0} {1}/{2}".format(extras or '', package_name, activity_name)).wait()

    @classmethod
    def kill_process(cls, package_name):
        """
        Stops given app::

            ADB.kill_process('com.android.chrome')

        :param str package_name: Package name to stop
        :return:
        """
        cls.exec_cmd("shell am force-stop {0}".format(package_name)).wait()

    @classmethod
    def uninstall(cls, package_name):
        Logger.debug('Uninstalling app {}'.format(package_name))
        cls.exec_cmd('uninstall {0}'.format(package_name)).wait()

        # remove app cache
        cls.exec_cmd('shell su -c "rm -rf /data/data/{0}"'.format(package_name)).wait()

    @classmethod
    def install(cls, apk_path, extra_params='', retry=True):
        Logger.debug('Installing app {}'.format(apk_path))
        p = cls.exec_cmd('install {} {}'.format('', apk_path), stdout=subprocess.PIPE)
        result = p.communicate()[0].strip().split('\r\n')[-1]

        if result == 'Success':
            Logger.debug('App installed')<commit_msg>fix ADB.install command<commit_after>import os
import subprocess
import re
import threading
from functools import wraps
from concurrent.futures import Future, wait
import time

from ..logger import Logger


class ADB(object):
    """
    Wrapper for adb commands.

    Import::

        from magneto.utils.adb import ADB

    :required: Android SDK installed and ANDROID_HOME pointing to SDK directory location.
    """

    _lock = threading.Lock()
    device_id = None
    ADB_PATH = os.path.join(os.environ['ANDROID_HOME'], 'platform-tools', 'adb')

    @classmethod
    def exec_cmd(cls, exec_cmd, stdin=None, stdout=None, stderr=None):
        """
        Executes adb commands::

            ADB.exec_cmd('shell uninstall com.android.package')

        In order to retrieve the process output, tap into the stdout::

            import subprocess

            proc = ADB.exec_cmd('shell pm list packages', stdout=subprocess.PIPE)
            list = proc.stdout.readline().strip('\\r\\n')

        :param str exec_cmd: Adb command to execute
        :param stdin:
        :param stdout:
        :param stderr:
        :return: Adb process
        """
        with cls._lock:
            cmd = 'exec {adb_path} {device_id} {command}'.format(
                adb_path=cls.ADB_PATH,
                device_id='-s {0}'.format(cls.device_id) if cls.device_id else '',
                command=exec_cmd
            )

            return subprocess.Popen(cmd, shell=True, stdin=stdin, stdout=stdout, stderr=stderr)

    @classmethod
    def get_log(cls):
        """
        Get log adb logcat.

        :return: Log dump
        """
        dump = []
        p = cls.exec_cmd("logcat -v time -d", stdout=subprocess.PIPE)
        for l in p.stdout:
            dump.append(l.strip())

        return dump

    @classmethod
    def clear_log(cls):
        """
        Clears adb log

        :return: Adb process
        """
        return cls.exec_cmd("logcat -c")

    @classmethod
    def start_activity(cls, package_name, activity_name, extras=None):
        """
        Start app activity.

        Open Gmail app::

            ADB.start_activity('com.google.android.gm', 'com.google.android.gm.ConversationListActivity')

        :param str package_name: Package name
        :param str activity_name: Activity name
        :return:
        """
        Logger.debug('opening activity {}'.format(activity_name))
        cls.exec_cmd("shell am start {0} {1}/{2}".format(extras or '', package_name, activity_name)).wait()

    @classmethod
    def kill_process(cls, package_name):
        """
        Stops given app::

            ADB.kill_process('com.android.chrome')

        :param str package_name: Package name to stop
        :return:
        """
        cls.exec_cmd("shell am force-stop {0}".format(package_name)).wait()

    @classmethod
    def uninstall(cls, package_name):
        Logger.debug('Uninstalling app {}'.format(package_name))
        cls.exec_cmd('uninstall {0}'.format(package_name)).wait()

        # remove app cache
        cls.exec_cmd('shell su -c "rm -rf /data/data/{0}"'.format(package_name)).wait()

    @classmethod
    def install(cls, apk_path, extra_params='', retry=True):
        Logger.debug('Installing app {}'.format(apk_path))
        p = cls.exec_cmd('install {} {}'.format(extra_params, apk_path), stdout=subprocess.PIPE)
        result = p.communicate()[0].strip().split('\r\n')[-1]

        if result == 'Success':
            Logger.debug('App installed')
{'fraction_non_alphanumeric': 0.12288984263233191, 'fraction_numerical': 0.002575107296137339, 'mean_word_length': 2.3385864374403056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4471408', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2091, 'n_words': 509}