mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-06 10:40:32 -07:00
Add android sdk module (#9236) * adds simple implementation of adding and removing android sdk packages * adds package update * adds simple installed packages parsing * moves parsing logic to a separate class * adds absent state for sdkmanager packages and setup for tests * adds output for installing and removing packages * removes version from Package object since it is not possible to specify version for a package while using sdkmanager * adds 'latest' state * adds tests * fixes crash when sdkmanager is invoked from python with LC_ALL=C * fixes latest state * adds sdk_root parameter * adds channel parameter * simplifies regexps, removes unused named groups * minor refactoring of sdkmanager parsing * adds java dependency variable for different distributions * adds RETURN documentation * adds check for nonexisting package * adds check for non-accepted licenses * removes excessive methods from sdkmanager * removes unused 'update' module parameter, packages may be updated using 'latest' state * minor refactoring * adds EXAMPLES doc section * adds DOCUMENTATION section and license headers * fixes formatting issues * removes diff_params * adds maintainer * fixes sanity check issues in sdkmanager * adds java dependency for macos and moves some tests to a separate FreeBSD configuration * fixes dependencies setup for OSX * fixes dependencies setup for OSX (2) * fixes dependencies setup for OSX (3) * Apply minor suggestions from code review Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * applies code review suggestions * changes force_lang from C.UTF-8 to auto in sdkmanager (as per discussion https://github.com/ansible-collections/community.general/pull/9236#discussion_r1881114326) * Revert "changes force_lang from C.UTF-8 to auto in sdkmanager (as per discussion https://github.com/ansible-collections/community.general/pull/9236#discussion_r1881114326)" This reverts commit619f28dd58
. * fixes some more comments from review * minor sanity issue fix * uses the 'changed' test instead of checking the 'changed' attribute * adds 'accept_licenses' parameter. Installation is now performed independently for each package specified. * removes "Accept licenses" task from examples * fixes docs sanity issues * applies minor suggestions from code review * fixes regexps. The previous version didn't match versions like "32.1.0 rc1". Also, this allows to simplify the parsing logic as there is no need to skip table headers anymore. * renamed sdkmanager.py to android_sdkmanager.py * applies minor suggestions from code review Co-authored-by: Felix Fontein <felix@fontein.de> * updates BOTMETA * reordered BOTMETA --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Felix Fontein <felix@fontein.de> (cherry picked from commit2b2872f0ef
) Co-authored-by: Stanislav Shamilov <shamilovstas@protonmail.com>
148 lines
5 KiB
Python
148 lines
5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2024, Stanislav Shamilov <shamilovstas@protonmail.com>
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
import re
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
|
|
|
__state_map = {
|
|
"present": "--install",
|
|
"absent": "--uninstall"
|
|
}
|
|
|
|
# sdkmanager --help 2>&1 | grep -A 2 -- --channel
|
|
__channel_map = {
|
|
"stable": 0,
|
|
"beta": 1,
|
|
"dev": 2,
|
|
"canary": 3
|
|
}
|
|
|
|
|
|
def __map_channel(channel_name):
|
|
if channel_name not in __channel_map:
|
|
raise ValueError("Unknown channel name '%s'" % channel_name)
|
|
return __channel_map[channel_name]
|
|
|
|
|
|
def sdkmanager_runner(module, **kwargs):
|
|
return CmdRunner(
|
|
module,
|
|
command='sdkmanager',
|
|
arg_formats=dict(
|
|
state=cmd_runner_fmt.as_map(__state_map),
|
|
name=cmd_runner_fmt.as_list(),
|
|
installed=cmd_runner_fmt.as_fixed("--list_installed"),
|
|
list=cmd_runner_fmt.as_fixed('--list'),
|
|
newer=cmd_runner_fmt.as_fixed("--newer"),
|
|
sdk_root=cmd_runner_fmt.as_opt_eq_val("--sdk_root"),
|
|
channel=cmd_runner_fmt.as_func(lambda x: ["{0}={1}".format("--channel", __map_channel(x))])
|
|
),
|
|
force_lang="C.UTF-8", # Without this, sdkmanager binary crashes
|
|
**kwargs
|
|
)
|
|
|
|
|
|
class Package:
|
|
def __init__(self, name):
|
|
self.name = name
|
|
|
|
def __hash__(self):
|
|
return hash(self.name)
|
|
|
|
def __ne__(self, other):
|
|
if not isinstance(other, Package):
|
|
return True
|
|
return self.name != other.name
|
|
|
|
def __eq__(self, other):
|
|
if not isinstance(other, Package):
|
|
return False
|
|
|
|
return self.name == other.name
|
|
|
|
|
|
class SdkManagerException(Exception):
|
|
pass
|
|
|
|
|
|
class AndroidSdkManager(object):
|
|
_RE_INSTALLED_PACKAGES_HEADER = re.compile(r'^Installed packages:$')
|
|
_RE_UPDATABLE_PACKAGES_HEADER = re.compile(r'^Available Updates:$')
|
|
|
|
# Example: ' platform-tools | 27.0.0 | Android SDK Platform-Tools 27 | platform-tools '
|
|
_RE_INSTALLED_PACKAGE = re.compile(r'^\s*(?P<name>\S+)\s*\|\s*[0-9][^|]*\b\s*\|\s*.+\s*\|\s*(\S+)\s*$')
|
|
|
|
# Example: ' platform-tools | 27.0.0 | 35.0.2'
|
|
_RE_UPDATABLE_PACKAGE = re.compile(r'^\s*(?P<name>\S+)\s*\|\s*[0-9][^|]*\b\s*\|\s*[0-9].*\b\s*$')
|
|
|
|
_RE_UNKNOWN_PACKAGE = re.compile(r'^Warning: Failed to find package \'(?P<package>\S+)\'\s*$')
|
|
_RE_ACCEPT_LICENSE = re.compile(r'^The following packages can not be installed since their licenses or those of '
|
|
r'the packages they depend on were not accepted')
|
|
|
|
def __init__(self, module):
|
|
self.runner = sdkmanager_runner(module)
|
|
|
|
def get_installed_packages(self):
|
|
with self.runner('installed sdk_root channel') as ctx:
|
|
rc, stdout, stderr = ctx.run()
|
|
return self._parse_packages(stdout, self._RE_INSTALLED_PACKAGES_HEADER, self._RE_INSTALLED_PACKAGE)
|
|
|
|
def get_updatable_packages(self):
|
|
with self.runner('list newer sdk_root channel') as ctx:
|
|
rc, stdout, stderr = ctx.run()
|
|
return self._parse_packages(stdout, self._RE_UPDATABLE_PACKAGES_HEADER, self._RE_UPDATABLE_PACKAGE)
|
|
|
|
def apply_packages_changes(self, packages, accept_licenses=False):
|
|
""" Install or delete packages, depending on the `module.vars.state` parameter """
|
|
if len(packages) == 0:
|
|
return 0, '', ''
|
|
|
|
if accept_licenses:
|
|
license_prompt_answer = 'y'
|
|
else:
|
|
license_prompt_answer = 'N'
|
|
for package in packages:
|
|
with self.runner('state name sdk_root channel', data=license_prompt_answer) as ctx:
|
|
rc, stdout, stderr = ctx.run(name=package.name)
|
|
|
|
for line in stdout.splitlines():
|
|
if self._RE_ACCEPT_LICENSE.match(line):
|
|
raise SdkManagerException("Licenses for some packages were not accepted")
|
|
|
|
if rc != 0:
|
|
self._try_parse_stderr(stderr)
|
|
return rc, stdout, stderr
|
|
return 0, '', ''
|
|
|
|
def _try_parse_stderr(self, stderr):
|
|
data = stderr.splitlines()
|
|
for line in data:
|
|
unknown_package_regex = self._RE_UNKNOWN_PACKAGE.match(line)
|
|
if unknown_package_regex:
|
|
package = unknown_package_regex.group('package')
|
|
raise SdkManagerException("Unknown package %s" % package)
|
|
|
|
@staticmethod
|
|
def _parse_packages(stdout, header_regexp, row_regexp):
|
|
data = stdout.splitlines()
|
|
|
|
section_found = False
|
|
packages = set()
|
|
|
|
for line in data:
|
|
if not section_found:
|
|
section_found = header_regexp.match(line)
|
|
continue
|
|
else:
|
|
p = row_regexp.match(line)
|
|
if p:
|
|
packages.add(Package(p.group('name')))
|
|
return packages
|