mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-26 21:33:12 +00:00
Reformat everything.
This commit is contained in:
@@ -165,25 +165,25 @@ from ansible_collections.community.general.plugins.module_utils.cmd_runner impor
|
||||
class Npm:
|
||||
def __init__(self, module, **kwargs):
|
||||
self.module = module
|
||||
self.glbl = kwargs['glbl']
|
||||
self.name = kwargs['name']
|
||||
self.version = kwargs['version']
|
||||
self.path = kwargs['path']
|
||||
self.registry = kwargs['registry']
|
||||
self.production = kwargs['production']
|
||||
self.ignore_scripts = kwargs['ignore_scripts']
|
||||
self.unsafe_perm = kwargs['unsafe_perm']
|
||||
self.state = kwargs['state']
|
||||
self.no_optional = kwargs['no_optional']
|
||||
self.no_bin_links = kwargs['no_bin_links']
|
||||
self.force = kwargs['force']
|
||||
self.glbl = kwargs["glbl"]
|
||||
self.name = kwargs["name"]
|
||||
self.version = kwargs["version"]
|
||||
self.path = kwargs["path"]
|
||||
self.registry = kwargs["registry"]
|
||||
self.production = kwargs["production"]
|
||||
self.ignore_scripts = kwargs["ignore_scripts"]
|
||||
self.unsafe_perm = kwargs["unsafe_perm"]
|
||||
self.state = kwargs["state"]
|
||||
self.no_optional = kwargs["no_optional"]
|
||||
self.no_bin_links = kwargs["no_bin_links"]
|
||||
self.force = kwargs["force"]
|
||||
|
||||
if kwargs['executable']:
|
||||
self.executable = kwargs['executable'].split(' ')
|
||||
if kwargs["executable"]:
|
||||
self.executable = kwargs["executable"].split(" ")
|
||||
else:
|
||||
self.executable = [module.get_bin_path('npm', True)]
|
||||
self.executable = [module.get_bin_path("npm", True)]
|
||||
|
||||
if kwargs['version'] and kwargs['state'] != 'absent':
|
||||
if kwargs["version"] and kwargs["state"] != "absent":
|
||||
self.name_version = f"{self.name}@{kwargs['version']}"
|
||||
else:
|
||||
self.name_version = self.name
|
||||
@@ -193,16 +193,16 @@ class Npm:
|
||||
command=self.executable,
|
||||
arg_formats=dict(
|
||||
exec_args=cmd_runner_fmt.as_list(),
|
||||
global_=cmd_runner_fmt.as_bool('--global'),
|
||||
production=cmd_runner_fmt.as_bool('--production'),
|
||||
ignore_scripts=cmd_runner_fmt.as_bool('--ignore-scripts'),
|
||||
unsafe_perm=cmd_runner_fmt.as_bool('--unsafe-perm'),
|
||||
global_=cmd_runner_fmt.as_bool("--global"),
|
||||
production=cmd_runner_fmt.as_bool("--production"),
|
||||
ignore_scripts=cmd_runner_fmt.as_bool("--ignore-scripts"),
|
||||
unsafe_perm=cmd_runner_fmt.as_bool("--unsafe-perm"),
|
||||
name_version=cmd_runner_fmt.as_list(),
|
||||
registry=cmd_runner_fmt.as_opt_val('--registry'),
|
||||
no_optional=cmd_runner_fmt.as_bool('--no-optional'),
|
||||
no_bin_links=cmd_runner_fmt.as_bool('--no-bin-links'),
|
||||
force=cmd_runner_fmt.as_bool('--force'),
|
||||
)
|
||||
registry=cmd_runner_fmt.as_opt_val("--registry"),
|
||||
no_optional=cmd_runner_fmt.as_bool("--no-optional"),
|
||||
no_bin_links=cmd_runner_fmt.as_bool("--no-bin-links"),
|
||||
force=cmd_runner_fmt.as_bool("--force"),
|
||||
),
|
||||
)
|
||||
|
||||
def _exec(self, args, run_in_check_mode=False, check_rc=True, add_package_name=True):
|
||||
@@ -217,40 +217,40 @@ class Npm:
|
||||
cwd = self.path
|
||||
|
||||
params = dict(self.module.params)
|
||||
params['exec_args'] = args
|
||||
params['global_'] = self.glbl
|
||||
params['production'] = self.production and ('install' in args or 'update' in args or 'ci' in args)
|
||||
params['name_version'] = self.name_version if add_package_name else None
|
||||
params["exec_args"] = args
|
||||
params["global_"] = self.glbl
|
||||
params["production"] = self.production and ("install" in args or "update" in args or "ci" in args)
|
||||
params["name_version"] = self.name_version if add_package_name else None
|
||||
|
||||
with self.runner(
|
||||
"exec_args global_ production ignore_scripts unsafe_perm name_version registry no_optional no_bin_links force",
|
||||
check_rc=check_rc, cwd=cwd
|
||||
check_rc=check_rc,
|
||||
cwd=cwd,
|
||||
) as ctx:
|
||||
rc, out, err = ctx.run(**params)
|
||||
return out
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def list(self):
|
||||
cmd = ['list', '--json', '--long']
|
||||
cmd = ["list", "--json", "--long"]
|
||||
|
||||
installed = list()
|
||||
missing = list()
|
||||
data = {}
|
||||
try:
|
||||
data = json.loads(self._exec(cmd, True, False, False) or '{}')
|
||||
except (getattr(json, 'JSONDecodeError', ValueError)) as e:
|
||||
data = json.loads(self._exec(cmd, True, False, False) or "{}")
|
||||
except getattr(json, "JSONDecodeError", ValueError) as e:
|
||||
self.module.fail_json(msg=f"Failed to parse NPM output with error {e}")
|
||||
if 'dependencies' in data:
|
||||
for dep, props in data['dependencies'].items():
|
||||
|
||||
if 'missing' in props and props['missing']:
|
||||
if "dependencies" in data:
|
||||
for dep, props in data["dependencies"].items():
|
||||
if "missing" in props and props["missing"]:
|
||||
missing.append(dep)
|
||||
elif 'invalid' in props and props['invalid']:
|
||||
elif "invalid" in props and props["invalid"]:
|
||||
missing.append(dep)
|
||||
else:
|
||||
installed.append(dep)
|
||||
if 'version' in props and props['version']:
|
||||
if "version" in props and props["version"]:
|
||||
dep_version = f"{dep}@{props['version']}"
|
||||
installed.append(dep_version)
|
||||
if self.name_version and self.name_version not in installed:
|
||||
@@ -262,25 +262,25 @@ class Npm:
|
||||
return installed, missing
|
||||
|
||||
def install(self):
|
||||
return self._exec(['install'])
|
||||
return self._exec(["install"])
|
||||
|
||||
def ci_install(self):
|
||||
return self._exec(['ci'])
|
||||
return self._exec(["ci"])
|
||||
|
||||
def update(self):
|
||||
return self._exec(['update'])
|
||||
return self._exec(["update"])
|
||||
|
||||
def uninstall(self):
|
||||
return self._exec(['uninstall'])
|
||||
return self._exec(["uninstall"])
|
||||
|
||||
def list_outdated(self):
|
||||
outdated = list()
|
||||
data = self._exec(['outdated'], True, False)
|
||||
data = self._exec(["outdated"], True, False)
|
||||
for dep in data.splitlines():
|
||||
if dep:
|
||||
# node.js v0.10.22 changed the `npm outdated` module separator
|
||||
# from "@" to " ". Split on both for backwards compatibility.
|
||||
pkg, other = re.split(r'\s|@', dep, 1)
|
||||
pkg, other = re.split(r"\s|@", dep, 1)
|
||||
outdated.append(pkg)
|
||||
|
||||
return outdated
|
||||
@@ -288,61 +288,63 @@ class Npm:
|
||||
|
||||
def main():
|
||||
arg_spec = dict(
|
||||
name=dict(type='str'),
|
||||
path=dict(type='path'),
|
||||
version=dict(type='str'),
|
||||
production=dict(default=False, type='bool'),
|
||||
executable=dict(type='path'),
|
||||
registry=dict(type='str'),
|
||||
state=dict(default='present', choices=['present', 'absent', 'latest']),
|
||||
ignore_scripts=dict(default=False, type='bool'),
|
||||
unsafe_perm=dict(default=False, type='bool'),
|
||||
ci=dict(default=False, type='bool'),
|
||||
no_optional=dict(default=False, type='bool'),
|
||||
no_bin_links=dict(default=False, type='bool'),
|
||||
force=dict(default=False, type='bool'),
|
||||
name=dict(type="str"),
|
||||
path=dict(type="path"),
|
||||
version=dict(type="str"),
|
||||
production=dict(default=False, type="bool"),
|
||||
executable=dict(type="path"),
|
||||
registry=dict(type="str"),
|
||||
state=dict(default="present", choices=["present", "absent", "latest"]),
|
||||
ignore_scripts=dict(default=False, type="bool"),
|
||||
unsafe_perm=dict(default=False, type="bool"),
|
||||
ci=dict(default=False, type="bool"),
|
||||
no_optional=dict(default=False, type="bool"),
|
||||
no_bin_links=dict(default=False, type="bool"),
|
||||
force=dict(default=False, type="bool"),
|
||||
)
|
||||
arg_spec['global'] = dict(default=False, type='bool')
|
||||
arg_spec["global"] = dict(default=False, type="bool")
|
||||
module = AnsibleModule(
|
||||
argument_spec=arg_spec,
|
||||
required_if=[
|
||||
('state', 'absent', ['name']),
|
||||
("state", "absent", ["name"]),
|
||||
("global", False, ["path"]),
|
||||
],
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
name = module.params['name']
|
||||
path = module.params['path']
|
||||
version = module.params['version']
|
||||
glbl = module.params['global']
|
||||
state = module.params['state']
|
||||
name = module.params["name"]
|
||||
path = module.params["path"]
|
||||
version = module.params["version"]
|
||||
glbl = module.params["global"]
|
||||
state = module.params["state"]
|
||||
|
||||
npm = Npm(module,
|
||||
name=name,
|
||||
path=path,
|
||||
version=version,
|
||||
glbl=glbl,
|
||||
production=module.params['production'],
|
||||
executable=module.params['executable'],
|
||||
registry=module.params['registry'],
|
||||
ignore_scripts=module.params['ignore_scripts'],
|
||||
unsafe_perm=module.params['unsafe_perm'],
|
||||
state=state,
|
||||
no_optional=module.params['no_optional'],
|
||||
no_bin_links=module.params['no_bin_links'],
|
||||
force=module.params['force'])
|
||||
npm = Npm(
|
||||
module,
|
||||
name=name,
|
||||
path=path,
|
||||
version=version,
|
||||
glbl=glbl,
|
||||
production=module.params["production"],
|
||||
executable=module.params["executable"],
|
||||
registry=module.params["registry"],
|
||||
ignore_scripts=module.params["ignore_scripts"],
|
||||
unsafe_perm=module.params["unsafe_perm"],
|
||||
state=state,
|
||||
no_optional=module.params["no_optional"],
|
||||
no_bin_links=module.params["no_bin_links"],
|
||||
force=module.params["force"],
|
||||
)
|
||||
|
||||
changed = False
|
||||
if module.params['ci']:
|
||||
if module.params["ci"]:
|
||||
npm.ci_install()
|
||||
changed = True
|
||||
elif state == 'present':
|
||||
elif state == "present":
|
||||
installed, missing = npm.list()
|
||||
if missing:
|
||||
changed = True
|
||||
npm.install()
|
||||
elif state == 'latest':
|
||||
elif state == "latest":
|
||||
installed, missing = npm.list()
|
||||
outdated = npm.list_outdated()
|
||||
if missing:
|
||||
@@ -360,5 +362,5 @@ def main():
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user