Example #1
0
    def _get_sources_package(version) -> Optional[str]:
        try:
            version = int(version)
            if version < 14:
                print_error('Sources are not available before API 14')
                return None
        except ValueError:
            pass

        return 'sources;android-%s' % version
Example #2
0
 def _get_default_java_version() -> Optional[str]:
     return_code, stdout, stderr = PlatformHelper.execute_cmd('java -version')
     if return_code != 0:
         print_error('Failed to get java version')
         return None
     # TODO(ashishb): Do I need two different regex for Mac and Linux like _get_all_java_versions here?
     java_version_regex = r'"([0-9]+\.[0-9]+)\..*"'
     version = re.search(java_version_regex, stderr)
     if version is None:
         return None
     print_verbose('version object is %s' % version)
     return version.group(1)
Example #3
0
 def start_avd(self, avd_name, headless_mode, verbose_mode):
     # cmd = '%s -avd %s -no-boot-anim -no-skin' % (self._get_emulator_path(), avd_name)
     cmd = '%s -avd %s -no-boot-anim' % ('./emulator', avd_name)
     if headless_mode:
         cmd = '%s -no-window' % cmd.strip()
     if verbose_mode:
         cmd = '%s -verbose' % cmd.strip()
     return_code, stdout, stderr = PlatformHelper.execute_cmd(cmd, cwd=os.path.dirname(self._get_emulator_path()))
     if return_code != 0:
         print_error('Failed to start emulator\nstdout:\n' + stdout + '\n\nstderr:\n' + stderr)
         print_message('List of valid virtual devices')
         self.list_avds()
Example #4
0
    def _ensure_basic_packages_are_installed(self) -> bool:
        success = True
        installed_packages = self._get_installed_packages()
        basic_packages = self._get_basic_packages()
        num_packages = len(basic_packages)
        for i in range(0, num_packages):
            basic_package = basic_packages[i]
            if basic_package not in installed_packages:
                print_error('Basic packages \"%s\" is not installed' % basic_package)
                success = False
            else:
                print_message('Package %d/%d: \"%s\" is installed' % (i + 1, num_packages, basic_package))

        return success
Example #5
0
    def _install_sdk_packages(self, package_names) -> bool:
        for package_name in package_names:
            exists = self._does_package_exist(package_name)
            if not exists:
                print_message('Package \"%s\" not found' % package_name)
                return False

        print_message('Installing packages [%s]...' % ', '.join(package_names))
        package_names_str = '\"' + '\" \"'.join(package_names) + '\"'
        cmd = 'yes | %s --verbose %s' % (self._get_sdk_manager_path(), package_names_str)
        return_code, stdout, stderr = PlatformHelper.execute_cmd(cmd)
        if return_code != 0:
            print_error('Failed to install packages \"%s\"' % ' '.join(package_names))
            print_error('Stderr is \n%s' % stderr)
            return False
        return True
Example #6
0
    def _get_installed_packages(self) -> [str]:
        cmd = '%s --verbose --list --include_obsolete' % self._get_sdk_manager_path(
        )
        return_code, stdout, stderr = PlatformHelper.execute_cmd(cmd)
        if return_code != 0:
            print_error('Failed to list packages')
            return None

        start_line = 'Installed packages:'.lower()
        end_line = 'Available Packages:'.lower()
        lines = stdout.split('\n')
        i = 0
        while i < len(lines):
            line = lines[i]
            if line.lower().find(start_line) != -1:
                i += 1
                break
            else:
                i += 1
        installed_packages = set()
        for j in range(i, len(lines)):
            line = lines[j]
            if line.lower().find(end_line) != -1:
                break
            line = line.strip()
            if not line:
                continue

            if line.startswith('----'):
                continue
            if line.startswith('Description:'):
                continue
            if line.startswith('Version:'):
                continue
            if line.startswith('Installed Location:'):
                continue
            if line.startswith('Installed Obsolete Packages:'):
                continue
            if line.find('|') > 0:
                line = line[:line.find('|')]
            installed_packages.add(line)
        return sorted(installed_packages)
Example #7
0
    def _get_all_java_versions() -> [str]:
        if PlatformHelper.on_linux():
            return_code, stdout, stderr = PlatformHelper.execute_cmd(_GET_ALL_JAVA_VERSIONS_ON_LINUX)
            java_version_regex = 'java-([0-9]+.*?)/'
        elif PlatformHelper.on_mac():
            java_version_regex = r'"([0-9]+\.[0-9]+)\..*"'
            return_code, stdout, stderr = PlatformHelper.execute_cmd(_GET_ALL_JAVA_VERSIONS_ON_MAC)
        else:
            print_error('Unsupported operating system')
            return []

        if return_code != 0:
            print_error('Failed to list java versions')
            return []
        output = ''
        output += stdout
        output += stderr
        versions = re.findall(java_version_regex, output)
        versions = set(versions)
        print_verbose('Versions are %s' % versions)
        return versions
Example #8
0
 def create_avd(self, avd_name, api_version, arch, api_type):
     if api_type is None:
         api_type = 'google_apis'  # Preferred
     if arch is None:
         if PlatformHelper.is_64bit_architecture():
             arch = 'x86_64'
         else:
             arch = 'x86'
     package_name = AndroidEnhanced._get_system_images_package(api_version, arch, api_type)
     print_verbose('Package is %s' % package_name)
     self.install_api_version(api_version, arch=arch, api_type=api_type)
     # Say no to custom hardware profile.
     print_message('Creating AVD "%s" of type "%s" ' % (avd_name, package_name))
     create_cmd = 'echo no | %s --verbose create avd --name %s --package "%s"' % (
         self._get_avd_manager_path(), avd_name, package_name)
     return_code, stdout, stderr = PlatformHelper.execute_cmd(create_cmd)
     if return_code != 0:
         print_error('Failed to create AVD')
         print_error('stdout: %s' % stdout)
         print_error_and_exit('stderr: %s' % stderr)
     print_message('AVD \"%s\" created successfully' % avd_name)
Example #9
0
    def _ensure_basic_packages_are_installed(self) -> bool:
        success = True
        installed_packages = self._get_installed_packages()
        print_verbose('Installed packages are %s' % installed_packages)
        basic_packages = self._get_basic_packages()
        num_packages = len(basic_packages)
        for i in range(0, num_packages):
            basic_package = basic_packages[i]
            if basic_package == 'tools':
                print_message('Skipping over obsolete package \"%s\"' %
                              basic_package)
                continue
            if basic_package not in installed_packages:
                print_error('Basic packages \"%s\" is not installed' %
                            basic_package)
                success = False
            else:
                print_message('Package %d/%d: \"%s\" is installed' %
                              (i + 1, num_packages, basic_package))

        return success
    def _get_binary(binary_name, binary_paths_relative_to_android_sdk) -> str:
        sdk_location = AndroidSdkHelper._get_location_of_android_sdk()
        if not sdk_location:
            print_verbose('ANDROID_SDK_ROOT not defined')
        else:
            for relative_path in binary_paths_relative_to_android_sdk:
                binary_path = os.path.join(sdk_location, relative_path)
                if os.path.exists(binary_path):
                    print_error('\"%s\" found at \"%s\"' %
                                (binary_name, binary_path))
                    return binary_path
                else:
                    print_error('\"%s\" not found at \"%s\"' %
                                (binary_name, binary_path))

        # Only works on Mac and GNU/Linux
        if PlatformHelper.on_linux() or PlatformHelper.on_mac():
            return_code, stdout, stderr = PlatformHelper.execute_cmd(
                'command -v %s' % binary_name)
            if return_code == 0:
                return stdout.strip()
            else:
                print_error('\"%s\" not in path' % binary_name)
        print_error_and_exit(
            'Set ANDROID_SDK_ROOT environment variable to point to Android SDK root'
        )
Example #11
0
 def list_installed_packages(self):
     installed_packages = self._get_installed_packages()
     if installed_packages:
         print('\n'.join(installed_packages))
     else:
         print_error('No installed packages found')