Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -140,43 +140,41 @@ from ansible.module_utils.basic import AnsibleModule
class Yarn:
def __init__(self, module, **kwargs):
self.module = module
self.globally = kwargs['globally']
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.executable = kwargs['executable']
self.globally = kwargs["globally"]
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.executable = kwargs["executable"]
# Specify a version of package if version arg passed in
self.name_version = None
if kwargs['version'] and self.name is not None:
if kwargs["version"] and self.name is not None:
self.name_version = f"{self.name}@{self.version!s}"
elif self.name is not None:
self.name_version = self.name
def _exec(self, args, run_in_check_mode=False, check_rc=True, unsupported_with_global=False):
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
with_global_arg = self.globally and not unsupported_with_global
if with_global_arg:
# Yarn global arg is inserted before the command (e.g. `yarn global {some-command}`)
args.insert(0, 'global')
args.insert(0, "global")
cmd = self.executable + args
if self.production:
cmd.append('--production')
cmd.append("--production")
if self.ignore_scripts:
cmd.append('--ignore-scripts')
cmd.append("--ignore-scripts")
if self.registry:
cmd.append('--registry')
cmd.append("--registry")
cmd.append(self.registry)
# If path is specified, cd into that path and run the command.
@@ -189,7 +187,7 @@ class Yarn:
self.module.fail_json(msg=f"Path provided {self.path} is not a directory")
cwd = self.path
if not os.path.isfile(os.path.join(self.path, 'package.json')):
if not os.path.isfile(os.path.join(self.path, "package.json")):
self.module.fail_json(msg="Package.json does not exist in provided path.")
rc, out, err = self.module.run_command(cmd, check_rc=check_rc, cwd=cwd)
@@ -201,18 +199,18 @@ class Yarn:
try:
# We need to filter for errors, since Yarn warnings are included in stderr
for line in err.splitlines():
if json.loads(line)['type'] == 'error':
if json.loads(line)["type"] == "error":
self.module.fail_json(msg=err)
except Exception:
self.module.fail_json(msg=f"Unexpected stderr output from Yarn: {err}", stderr=err)
def list(self):
cmd = ['list', '--depth=0', '--json']
cmd = ["list", "--depth=0", "--json"]
installed = list()
missing = list()
if not os.path.isfile(os.path.join(self.path, 'yarn.lock')):
if not os.path.isfile(os.path.join(self.path, "yarn.lock")):
missing.append(self.name)
return installed, missing
@@ -222,13 +220,13 @@ class Yarn:
self._process_yarn_error(error)
for json_line in result.strip().split('\n'):
for json_line in result.strip().split("\n"):
data = json.loads(json_line)
if data['type'] == 'tree':
dependencies = data['data']['trees']
if data["type"] == "tree":
dependencies = data["data"]["trees"]
for dep in dependencies:
name, version = dep['name'].rsplit('@', 1)
name, version = dep["name"].rsplit("@", 1)
installed.append(name)
if self.name not in installed:
@@ -239,23 +237,23 @@ class Yarn:
def install(self):
if self.name_version:
# Yarn has a separate command for installing packages by name...
return self._exec(['add', self.name_version])
return self._exec(["add", self.name_version])
# And one for installing all packages in package.json
return self._exec(['install', '--non-interactive'])
return self._exec(["install", "--non-interactive"])
def update(self):
return self._exec(['upgrade', '--latest'])
return self._exec(["upgrade", "--latest"])
def uninstall(self):
return self._exec(['remove', self.name])
return self._exec(["remove", self.name])
def list_outdated(self):
outdated = list()
if not os.path.isfile(os.path.join(self.path, 'yarn.lock')):
if not os.path.isfile(os.path.join(self.path, "yarn.lock")):
return outdated
cmd_result, err = self._exec(['outdated', '--json'], True, False, unsupported_with_global=True)
cmd_result, err = self._exec(["outdated", "--json"], True, False, unsupported_with_global=True)
# the package.json in the global dir is missing a license field, so warnings are expected on stderr
self._process_yarn_error(err)
@@ -268,7 +266,7 @@ class Yarn:
data = json.loads(outdated_packages_data)
try:
outdated_dependencies = data['data']['body']
outdated_dependencies = data["data"]["body"]
except KeyError:
return outdated
@@ -282,66 +280,64 @@ class Yarn:
def main():
arg_spec = dict(
name=dict(),
path=dict(type='path'),
path=dict(type="path"),
version=dict(),
production=dict(default=False, type='bool'),
executable=dict(type='path'),
production=dict(default=False, type="bool"),
executable=dict(type="path"),
registry=dict(),
state=dict(default='present', choices=['present', 'absent', 'latest']),
ignore_scripts=dict(default=False, type='bool'),
)
arg_spec['global'] = dict(default=False, type='bool')
module = AnsibleModule(
argument_spec=arg_spec,
supports_check_mode=True
state=dict(default="present", choices=["present", "absent", "latest"]),
ignore_scripts=dict(default=False, type="bool"),
)
arg_spec["global"] = dict(default=False, type="bool")
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)
name = module.params['name']
path = module.params['path']
version = module.params['version']
globally = module.params['global']
production = module.params['production']
registry = module.params['registry']
state = module.params['state']
ignore_scripts = module.params['ignore_scripts']
name = module.params["name"]
path = module.params["path"]
version = module.params["version"]
globally = module.params["global"]
production = module.params["production"]
registry = module.params["registry"]
state = module.params["state"]
ignore_scripts = module.params["ignore_scripts"]
# When installing globally, users should not be able to define a path for installation.
# Require a path if global is False, though!
if path is None and globally is False:
module.fail_json(msg='Path must be specified when not using global arg')
module.fail_json(msg="Path must be specified when not using global arg")
elif path and globally is True:
module.fail_json(msg='Cannot specify path if doing global installation')
module.fail_json(msg="Cannot specify path if doing global installation")
if state == 'absent' and not name:
module.fail_json(msg='Package must be explicitly named when uninstalling.')
if state == 'latest':
version = 'latest'
if state == "absent" and not name:
module.fail_json(msg="Package must be explicitly named when uninstalling.")
if state == "latest":
version = "latest"
if module.params['executable']:
executable = module.params['executable'].split(' ')
if module.params["executable"]:
executable = module.params["executable"].split(" ")
else:
executable = [module.get_bin_path('yarn', True)]
executable = [module.get_bin_path("yarn", True)]
# When installing globally, use the defined path for global node_modules
if globally:
_rc, out, _err = module.run_command(executable + ['global', 'dir'], check_rc=True)
_rc, out, _err = module.run_command(executable + ["global", "dir"], check_rc=True)
path = out.strip()
yarn = Yarn(module,
name=name,
path=path,
version=version,
globally=globally,
production=production,
executable=executable,
registry=registry,
ignore_scripts=ignore_scripts)
yarn = Yarn(
module,
name=name,
path=path,
version=version,
globally=globally,
production=production,
executable=executable,
registry=registry,
ignore_scripts=ignore_scripts,
)
changed = False
out = ''
err = ''
if state == 'present':
out = ""
err = ""
if state == "present":
if not name:
changed = True
out, err = yarn.install()
@@ -351,8 +347,7 @@ def main():
changed = True
out, err = yarn.install()
elif state == 'latest':
elif state == "latest":
if not name:
changed = True
out, err = yarn.install()
@@ -375,5 +370,5 @@ def main():
module.exit_json(changed=changed, out=out, err=err)
if __name__ == '__main__':
if __name__ == "__main__":
main()