Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2014, Michael Warkentin <[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: bower
11 short_description: Manage bower packages with C(bower)
12 description:
13 - Manage bower packages with C(bower).
14 author: "Michael Warkentin (@mwarkentin)"
15 extends_documentation_fragment:
16 - community.general._attributes
17 attributes:
18 check_mode:
19 support: none
20 diff_mode:
21 support: none
22 options:
23 name:
24 type: str
25 description:
26 - The name of a bower package to install.
27 offline:
28 description:
29 - Install packages from local cache, if the packages were installed before.
30 type: bool
31 default: false
32 production:
33 description:
34 - Install with C(--production) flag.
35 type: bool
36 default: false
37 path:
38 type: path
39 description:
40 - The base path where to install the bower packages.
41 required: true
42 relative_execpath:
43 type: path
44 description:
45 - Relative path to bower executable from install path.
46 state:
47 type: str
48 description:
49 - The state of the bower package.
50 default: present
51 choices: ["present", "absent", "latest"]
52 version:
53 type: str
54 description:
55 - The version to be installed.
56 """
57
58 EXAMPLES = r"""
59 - name: Install "bootstrap" bower package.
60 community.general.bower:
61 name: bootstrap
62
63 - name: Install "bootstrap" bower package on version 3.1.1.
64 community.general.bower:
65 name: bootstrap
66 version: '3.1.1'
67
68 - name: Remove the "bootstrap" bower package.
69 community.general.bower:
70 name: bootstrap
71 state: absent
72
73 - name: Install packages based on bower.json.
74 community.general.bower:
75 path: /app/location
76
77 - name: Update packages based on bower.json to their latest version.
78 community.general.bower:
79 path: /app/location
80 state: latest
81
82 # install bower locally and run from there
83 - npm:
84 path: /app/location
85 name: bower
86 global: false
87 - community.general.bower:
88 path: /app/location
89 relative_execpath: node_modules/.bin
90 """
91
92 import json
93 import os
94
95 from ansible.module_utils.basic import AnsibleModule
96
97
98 class Bower:
99 def __init__(self, module, **kwargs):
100 self.module = module
101 self.name = kwargs["name"]
102 self.offline = kwargs["offline"]
103 self.production = kwargs["production"]
104 self.path = kwargs["path"]
105 self.relative_execpath = kwargs["relative_execpath"]
106 self.version = kwargs["version"]
107
108 if kwargs["version"]:
109 self.name_version = f"{self.name}#{self.version}"
110 else:
111 self.name_version = self.name
112
113 def _exec(self, args, run_in_check_mode=False, check_rc=True):
114 if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
115 cmd = []
116
117 if self.relative_execpath:
118 cmd.append(os.path.join(self.path, self.relative_execpath, "bower"))
119 if not os.path.isfile(cmd[-1]):
120 self.module.fail_json(msg=f"bower not found at relative path {self.relative_execpath}")
121 else:
122 cmd.append("bower")
123
124 cmd.extend(args)
125 cmd.extend(["--config.interactive=false", "--allow-root"])
126
127 if self.name:
128 cmd.append(self.name_version)
129
130 if self.offline:
131 cmd.append("--offline")
132
133 if self.production:
134 cmd.append("--production")
135
136 # If path is specified, cd into that path and run the command.
137 cwd = None
138 if self.path:
139 if not os.path.exists(self.path):
140 os.makedirs(self.path)
141 if not os.path.isdir(self.path):
142 self.module.fail_json(msg=f"path {self.path} is not a directory")
143 cwd = self.path
144
145 rc, out, err = self.module.run_command(cmd, check_rc=check_rc, cwd=cwd)
146 return out
147 return ""
148
149 def list(self):
150 cmd = ["list", "--json"]
151
152 installed = list()
153 missing = list()
154 outdated = list()
155 data = json.loads(self._exec(cmd, True, False))
156 if "dependencies" in data:
157 for dep in data["dependencies"]:
158 dep_data = data["dependencies"][dep]
159 if dep_data.get("missing", False):
160 missing.append(dep)
161 elif (
162 "version" in dep_data["pkgMeta"]
163 and "update" in dep_data
164 and dep_data["pkgMeta"]["version"] != dep_data["update"]["latest"]
165 ):
166 outdated.append(dep)
167 elif dep_data.get("incompatible", False):
168 outdated.append(dep)
169 else:
170 installed.append(dep)
171 # Named dependency not installed
172 else:
173 missing.append(self.name)
174
175 return installed, missing, outdated
176
177 def install(self):
178 return self._exec(["install"])
179
180 def update(self):
181 return self._exec(["update"])
182
183 def uninstall(self):
184 return self._exec(["uninstall"])
185
186
187 def main():
188 arg_spec = dict(
189 name=dict(),
190 offline=dict(default=False, type="bool"),
191 production=dict(default=False, type="bool"),
192 path=dict(required=True, type="path"),
193 relative_execpath=dict(type="path"),
194 state=dict(
195 default="present",
196 choices=[
197 "present",
198 "absent",
199 "latest",
200 ],
201 ),
202 version=dict(),
203 )
204 module = AnsibleModule(argument_spec=arg_spec)
205 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
206
207 name = module.params["name"]
208 offline = module.params["offline"]
209 production = module.params["production"]
210 path = module.params["path"]
211 relative_execpath = module.params["relative_execpath"]
212 state = module.params["state"]
213 version = module.params["version"]
214
215 if state == "absent" and not name:
216 module.fail_json(msg="uninstalling a package is only available for named packages")
217
218 bower = Bower(
219 module,
220 name=name,
221 offline=offline,
222 production=production,
223 path=path,
224 relative_execpath=relative_execpath,
225 version=version,
226 )
227
228 changed = False
229 if state == "present":
230 installed, missing, outdated = bower.list()
231 if missing:
232 changed = True
233 bower.install()
234 elif state == "latest":
235 installed, missing, outdated = bower.list()
236 if missing or outdated:
237 changed = True
238 bower.update()
239 else: # Absent
240 installed, missing, outdated = bower.list()
241 if name in installed:
242 changed = True
243 bower.uninstall()
244
245 module.exit_json(changed=changed)
246
247
248 if __name__ == "__main__":
249 main()