Fix issues with spaces in environment variables
This commit is contained in:
@@ -112,24 +112,25 @@ class DockerContainer:
|
|||||||
|
|
||||||
def call(self, args: Sequence[Union[str, PathLike]], env: Dict[str, str] = {},
|
def call(self, args: Sequence[Union[str, PathLike]], env: Dict[str, str] = {},
|
||||||
capture_output=False, cwd: Optional[Union[str, PathLike]] = None) -> str:
|
capture_output=False, cwd: Optional[Union[str, PathLike]] = None) -> str:
|
||||||
env_exports = '\n'.join(f'export {k}={v}' for k, v in env.items())
|
|
||||||
chdir = f'cd {cwd}' if cwd else ''
|
chdir = f'cd {cwd}' if cwd else ''
|
||||||
|
env_assignments = ' '.join(f'{shlex.quote(k)}={shlex.quote(v)}'
|
||||||
|
for k, v in env.items())
|
||||||
command = ' '.join(shlex.quote(str(a)) for a in args)
|
command = ' '.join(shlex.quote(str(a)) for a in args)
|
||||||
end_of_message = str(uuid.uuid4())
|
end_of_message = str(uuid.uuid4())
|
||||||
|
|
||||||
# log the command we're executing
|
# log the command we're executing
|
||||||
print(f' + {command}')
|
print(f' + {command}')
|
||||||
|
|
||||||
# Write a command to the remote shell. First we write the
|
# Write a command to the remote shell. First we change the
|
||||||
# environment variables, exported inside the subshell. We change the
|
# cwd, if that's required. Then, we use the `env` utility to run
|
||||||
# cwd, if that's required. Then, the command is written. Finally, the
|
# `command` inside the specified environment. We use `env` because it
|
||||||
# remote shell is told to write a footer - this will show up in the
|
# can cope with spaces and strange characters in the name or value.
|
||||||
# output so we know when to stop reading, and will include the
|
# Finally, the remote shell is told to write a footer - this will show
|
||||||
# returncode of `command`.
|
# up in the output so we know when to stop reading, and will include
|
||||||
|
# the returncode of `command`.
|
||||||
self.bash_stdin.write(f'''(
|
self.bash_stdin.write(f'''(
|
||||||
{env_exports}
|
|
||||||
{chdir}
|
{chdir}
|
||||||
{command}
|
env {env_assignments} {command}
|
||||||
printf "%04d%s\n" $? {end_of_message}
|
printf "%04d%s\n" $? {end_of_message}
|
||||||
)
|
)
|
||||||
''')
|
''')
|
||||||
|
|||||||
Reference in New Issue
Block a user