MCPcopy Create free account
hub / github.com/canonical/cloud-init / prepend_base_command

Function prepend_base_command

cloudinit/subp.py:19–67  ·  view source on GitHub ↗

Ensure user-provided commands start with base_command; warn otherwise. Each command is either a list or string. Perform the following: - If the command is a list, pop the first element if it is None - If the command is a list, insert base_command as the first element if n

(base_command, commands)

Source from the content-addressed store, hash-verified

17
18
19def prepend_base_command(base_command, commands):
20 """Ensure user-provided commands start with base_command; warn otherwise.
21
22 Each command is either a list or string. Perform the following:
23 - If the command is a list, pop the first element if it is None
24 - If the command is a list, insert base_command as the first element if
25 not present.
26 - When the command is a string not starting with 'base-command', warn.
27
28 Allow flexibility to provide non-base-command environment/config setup if
29 needed.
30
31 @commands: List of commands. Each command element is a list or string.
32
33 @return: List of 'fixed up' commands.
34 @raise: TypeError on invalid config item type.
35 """
36 warnings = []
37 errors = []
38 fixed_commands = []
39 for command in commands:
40 if isinstance(command, list):
41 if command[0] is None: # Avoid warnings by specifying None
42 command = command[1:]
43 elif command[0] != base_command: # Automatically prepend
44 command.insert(0, base_command)
45 elif isinstance(command, str):
46 if not command.startswith(f"{base_command} "):
47 warnings.append(command)
48 else:
49 errors.append(str(command))
50 continue
51 fixed_commands.append(command)
52
53 if warnings:
54 LOG.warning(
55 "Non-%s commands in %s config:\n%s",
56 base_command,
57 base_command,
58 "\n".join(warnings),
59 )
60 if errors:
61 raise TypeError(
62 "Invalid {name} config."
63 " These commands are not a string or list:\n{errors}".format(
64 name=base_command, errors="\n".join(errors)
65 )
66 )
67 return fixed_commands
68
69
70class ProcessExecutionError(IOError):

Callers 1

run_commandsFunction · 0.90

Calls 1

formatMethod · 0.80

Tested by

no test coverage detected