Debian Codegraph @

ansible_collections/community/general/plugins/modules/android_sdk.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2024, Stanislav Shamilov <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: android_sdk
11 short_description: Manages Android SDK packages
12 description:
13 - Manages Android SDK packages.
14 - Allows installation from different channels (stable, beta, dev, canary).
15 - Allows installation of packages to a non-default SDK root directory.
16 author: Stanislav Shamilov (@shamilovstas)
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: none
24 version_added: 10.2.0
25 options:
26 accept_licenses:
27 description:
28 - If this is set to V(true), the module attempts to accept license prompts generated by C(sdkmanager) during package
29 installation. Otherwise, every license prompt is rejected.
30 type: bool
31 default: false
32 name:
33 description:
34 - A name of an Android SDK package (for instance, V(build-tools;34.0.0)).
35 aliases: ['package', 'pkg']
36 type: list
37 elements: str
38 state:
39 description:
40 - Indicates the desired package(s) state.
41 - V(present) ensures that package(s) is/are present.
42 - V(absent) ensures that package(s) is/are absent.
43 - V(latest) ensures that package(s) is/are installed and updated to the latest version(s).
44 choices: ['present', 'absent', 'latest']
45 default: present
46 type: str
47 sdk_root:
48 description:
49 - Provides path for an alternative directory to install Android SDK packages to. By default, all packages are installed
50 to the directory where C(sdkmanager) is installed.
51 type: path
52 channel:
53 description:
54 - Indicates what channel must C(sdkmanager) use for installation of packages.
55 choices: ['stable', 'beta', 'dev', 'canary']
56 default: stable
57 type: str
58 requirements:
59 - C(java) >= 17
60 - C(sdkmanager) Command line tool for installing Android SDK packages.
61 notes:
62 - For some of the packages installed by C(sdkmanager) is it necessary to accept licenses. Usually it is done through command
63 line prompt in a form of a Y/N question when a licensed package is requested to be installed. If there are several packages
64 requested for installation and at least two of them belong to different licenses, the C(sdkmanager) tool prompts for these
65 licenses in a loop. In order to install packages, the module must be able to answer these license prompts. Currently,
66 it is only possible to answer one license prompt at a time, meaning that instead of installing multiple packages as a
67 single invocation of the C(sdkmanager --install) command, it is done by executing the command independently for each package.
68 This makes sure that at most only one license prompt needs to be answered. At the time of writing this module, a C(sdkmanager)'s
69 package may belong to at most one license type that needs to be accepted. However, if this changes in the future, the
70 module may hang as there might be more prompts generated by the C(sdkmanager) tool which the module is unable to answer.
71 If this becomes the case, file an issue and in the meantime, consider accepting all the licenses in advance, as it is
72 described in the C(sdkmanager) L(documentation,https://developer.android.com/tools/sdkmanager#accept-licenses), for instance,
73 using the M(ansible.builtin.command) module.
74 seealso:
75 - name: sdkmanager tool documentation
76 description: Detailed information of how to install and use sdkmanager command line tool.
77 link: https://developer.android.com/tools/sdkmanager
78 """
79
80 EXAMPLES = r"""
81 - name: Install build-tools;34.0.0
82 community.general.android_sdk:
83 name: build-tools;34.0.0
84 accept_licenses: true
85 state: present
86
87 - name: Install build-tools;34.0.0 and platform-tools
88 community.general.android_sdk:
89 name:
90 - build-tools;34.0.0
91 - platform-tools
92 accept_licenses: true
93 state: present
94
95 - name: Delete build-tools;34.0.0
96 community.general.android_sdk:
97 name: build-tools;34.0.0
98 state: absent
99
100 - name: Install platform-tools or update if installed
101 community.general.android_sdk:
102 name: platform-tools
103 accept_licenses: true
104 state: latest
105
106 - name: Install build-tools;34.0.0 to a different SDK root
107 community.general.android_sdk:
108 name: build-tools;34.0.0
109 accept_licenses: true
110 state: present
111 sdk_root: "/path/to/new/root"
112
113 - name: Install a package from another channel
114 community.general.android_sdk:
115 name: some-package-present-in-canary-channel
116 accept_licenses: true
117 state: present
118 channel: canary
119 """
120
121 RETURN = r"""
122 installed:
123 description: A list of packages that have been installed.
124 returned: when packages have changed
125 type: list
126 sample: ["build-tools;34.0.0", "platform-tools"]
127
128 removed:
129 description: A list of packages that have been removed.
130 returned: when packages have changed
131 type: list
132 sample: ["build-tools;34.0.0", "platform-tools"]
133 """
134
135 from ansible_collections.community.general.plugins.module_utils._android_sdkmanager import AndroidSdkManager, Package
136 from ansible_collections.community.general.plugins.module_utils._mh.module_helper import StateModuleHelper
137
138
139 class AndroidSdk(StateModuleHelper):
140 module = dict(
141 argument_spec=dict(
142 state=dict(type="str", default="present", choices=["present", "absent", "latest"]),
143 package=dict(type="list", elements="str", aliases=["pkg", "name"]),
144 sdk_root=dict(type="path"),
145 channel=dict(type="str", default="stable", choices=["stable", "beta", "dev", "canary"]),
146 accept_licenses=dict(type="bool", default=False),
147 ),
148 supports_check_mode=True,
149 )
150
151 def __init_module__(self):
152 self.sdkmanager = AndroidSdkManager(self.module)
153 self.vars.set("installed", [], change=True)
154 self.vars.set("removed", [], change=True)
155
156 def _parse_packages(self):
157 arg_pkgs = set(self.vars.package)
158 if len(arg_pkgs) < len(self.vars.package):
159 self.do_raise("Packages may not repeat")
160 return {Package(p) for p in arg_pkgs}
161
162 def state_present(self):
163 packages = self._parse_packages()
164 installed = self.sdkmanager.get_installed_packages()
165 pending_installation = packages.difference(installed)
166
167 self.vars.installed = AndroidSdk._map_packages_to_names(pending_installation)
168 if not self.check_mode:
169 rc, stdout, stderr = self.sdkmanager.apply_packages_changes(pending_installation, self.vars.accept_licenses)
170 if rc != 0:
171 self.do_raise(f"Could not install packages: {stderr}")
172
173 def state_absent(self):
174 packages = self._parse_packages()
175 installed = self.sdkmanager.get_installed_packages()
176 to_be_deleted = packages.intersection(installed)
177 self.vars.removed = AndroidSdk._map_packages_to_names(to_be_deleted)
178 if not self.check_mode:
179 rc, stdout, stderr = self.sdkmanager.apply_packages_changes(to_be_deleted)
180 if rc != 0:
181 self.do_raise(f"Could not uninstall packages: {stderr}")
182
183 def state_latest(self):
184 packages = self._parse_packages()
185 installed = self.sdkmanager.get_installed_packages()
186 updatable = self.sdkmanager.get_updatable_packages()
187 not_installed = packages.difference(installed)
188 to_be_installed = not_installed.union(updatable)
189 self.vars.installed = AndroidSdk._map_packages_to_names(to_be_installed)
190
191 if not self.check_mode:
192 rc, stdout, stderr = self.sdkmanager.apply_packages_changes(to_be_installed, self.vars.accept_licenses)
193 if rc != 0:
194 self.do_raise(f"Could not install packages: {stderr}")
195
196 @staticmethod
197 def _map_packages_to_names(packages):
198 return [x.name for x in packages]
199
200
201 def main():
202 AndroidSdk.execute()
203
204
205 if __name__ == "__main__":
206 main()