<commit_before># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Finds android browsers that can be controlled by telemetry."""

import os
import logging as real_logging
import re
import subprocess
import sys

from telemetry import adb_commands
from telemetry import android_browser_backend
from telemetry import android_platform
from telemetry import browser
from telemetry import possible_browser

CHROME_PACKAGE_NAMES = {
  'android-chrome': 'com.google.android.apps.chrome',
  'android-chrome-beta': 'com.chrome.beta',
  'android-chrome-dev': 'com.google.android.apps.chrome_dev',
  'android-jb-system-chrome': 'com.android.chrome'
}

ALL_BROWSER_TYPES = ','.join(['android-content-shell'] +
                             CHROME_PACKAGE_NAMES.keys())

CHROME_ACTIVITY = 'com.google.android.apps.chrome.Main'
CHROME_COMMAND_LINE = '/data/local/chrome-command-line'
CHROME_DEVTOOLS_REMOTE_PORT = 'localabstract:chrome_devtools_remote'

CONTENT_SHELL_PACKAGE = 'org.chromium.content_shell'
CONTENT_SHELL_ACTIVITY = '.ContentShellActivity'
CONTENT_SHELL_COMMAND_LINE = '/data/local/tmp/content-shell-command-line'
CONTENT_SHELL_DEVTOOLS_REMOTE_PORT = (
    'localabstract:content_shell_devtools_remote')

# adb shell pm list packages
# adb
# intents to run (pass -D url for the rest)
#   com.android.chrome/.Main
#   com.google.android.apps.chrome/.Main

class PossibleAndroidBrowser(possible_browser.PossibleBrowser):
  """A launchable android browser instance."""
  def __init__(self, browser_type, options, *args):
    super(PossibleAndroidBrowser, self).__init__(
        browser_type, options)
    self._args = args

  def __repr__(self):
    return 'PossibleAndroidBrowser(browser_type=%s)' % self.browser_type

  def Create(self):
    backend = android_browser_backend.AndroidBrowserBackend(
        self._options, *self._args)
    platform = android_platform.AndroidPlatform(
        self._args[0].Adb(), self._args[1],
        self._args[1] + self._args[4])
    b = browser.Browser(backend, platform)
    backend.SetBrowser(b)
    return b

def FindAllAvailableBrowsers(options, logging=real_logging):
  """Finds all the desktop browsers available on this machine."""
  if not adb_commands.IsAndroidSupported():
    return []

  # See if adb even works.
  try:
    with open(os.devnull, 'w') as devnull:
      proc = subprocess.Popen(['adb', 'devices'],
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              stdin=devnull)
      stdout, _ = proc.communicate()
      if re.search(re.escape('????????????\tno permissions'), stdout) != None:
        logging.warn(
            ('adb devices reported a permissions error. Consider '
            'restarting adb as root:'))
        logging.warn('  adb kill-server')
        logging.warn('  sudo `which adb` devices\n\n')
  except OSError:
    if sys.platform.startswith('linux'):
      os.environ['PATH'] = os.pathsep.join([
          os.path.join(os.path.dirname(__file__),
                       '../../../third_party/android_tools/sdk/platform-tools'),
          os.environ['PATH']])
    else:
      logging.info('No adb command found. ' +
                   'Will not try searching for Android browsers.')
      return []

  device = None
  if options.android_device:
    devices = [options.android_device]
  else:
    devices = adb_commands.GetAttachedDevices()<commit_msg>[Telemetry] Only add android_tools' adb to the path if it exists.<commit_after># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Finds android browsers that can be controlled by telemetry."""

import os
import logging as real_logging
import re
import subprocess
import sys

from telemetry import adb_commands
from telemetry import android_browser_backend
from telemetry import android_platform
from telemetry import browser
from telemetry import possible_browser

CHROME_PACKAGE_NAMES = {
  'android-chrome': 'com.google.android.apps.chrome',
  'android-chrome-beta': 'com.chrome.beta',
  'android-chrome-dev': 'com.google.android.apps.chrome_dev',
  'android-jb-system-chrome': 'com.android.chrome'
}

ALL_BROWSER_TYPES = ','.join(['android-content-shell'] +
                             CHROME_PACKAGE_NAMES.keys())

CHROME_ACTIVITY = 'com.google.android.apps.chrome.Main'
CHROME_COMMAND_LINE = '/data/local/chrome-command-line'
CHROME_DEVTOOLS_REMOTE_PORT = 'localabstract:chrome_devtools_remote'

CONTENT_SHELL_PACKAGE = 'org.chromium.content_shell'
CONTENT_SHELL_ACTIVITY = '.ContentShellActivity'
CONTENT_SHELL_COMMAND_LINE = '/data/local/tmp/content-shell-command-line'
CONTENT_SHELL_DEVTOOLS_REMOTE_PORT = (
    'localabstract:content_shell_devtools_remote')

# adb shell pm list packages
# adb
# intents to run (pass -D url for the rest)
#   com.android.chrome/.Main
#   com.google.android.apps.chrome/.Main

class PossibleAndroidBrowser(possible_browser.PossibleBrowser):
  """A launchable android browser instance."""
  def __init__(self, browser_type, options, *args):
    super(PossibleAndroidBrowser, self).__init__(
        browser_type, options)
    self._args = args

  def __repr__(self):
    return 'PossibleAndroidBrowser(browser_type=%s)' % self.browser_type

  def Create(self):
    backend = android_browser_backend.AndroidBrowserBackend(
        self._options, *self._args)
    platform = android_platform.AndroidPlatform(
        self._args[0].Adb(), self._args[1],
        self._args[1] + self._args[4])
    b = browser.Browser(backend, platform)
    backend.SetBrowser(b)
    return b

def FindAllAvailableBrowsers(options, logging=real_logging):
  """Finds all the desktop browsers available on this machine."""
  if not adb_commands.IsAndroidSupported():
    return []

  # See if adb even works.
  try:
    with open(os.devnull, 'w') as devnull:
      proc = subprocess.Popen(['adb', 'devices'],
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              stdin=devnull)
      stdout, _ = proc.communicate()
      if re.search(re.escape('????????????\tno permissions'), stdout) != None:
        logging.warn(
            ('adb devices reported a permissions error. Consider '
            'restarting adb as root:'))
        logging.warn('  adb kill-server')
        logging.warn('  sudo `which adb` devices\n\n')
  except OSError:
    platform_tools_path = os.path.join(
        os.path.dirname(__file__), '..', '..', '..',
        'third_party', 'android_tools', 'sdk', 'platform-tools')
    if (sys.platform.startswith('linux') and
        os.path.exists(os.path.join(platform_tools_path, 'adb'))):
      os.environ['PATH'] = os.pathsep.join([platform_tools_path,
                                            os.environ['PATH']])
    else:
      logging.info('No adb command found. ' +
                   'Will not try searching for Android browsers.')
      return []

  device = None
  if options.android_device:
    devices = [options.android_device]
  else:
    devices = adb_commands.GetAttachedDevices()
{'fraction_non_alphanumeric': 0.11471801925722146, 'fraction_numerical': 0.002200825309491059, 'mean_word_length': 3.325401546698394, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5930931', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2187, 'n_words': 507}