summaryrefslogtreecommitdiff
path: root/src/tox/execute/request.py
blob: 328499b2b27d43b2bb47187f57b46bc67aa8504c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Module declaring a command execution request."""
import sys
from pathlib import Path
from typing import Dict, List, Sequence, Union


class ExecuteRequest:
    """Defines a commands execution request"""

    def __init__(self, cmd: Sequence[Union[str, Path]], cwd: Path, env: Dict[str, str], allow_stdin: bool):
        if len(cmd) == 0:
            raise ValueError("cannot execute an empty command")
        self.cmd: List[str] = [str(i) for i in cmd]
        self.cwd = cwd
        self.env = env
        self.allow_stdin = allow_stdin

    @property
    def shell_cmd(self):
        return shell_cmd(self.cmd)


def shell_cmd(cmd: Sequence[str]) -> str:
    if sys.platform.startswith("win"):
        from subprocess import list2cmdline

        return list2cmdline(tuple(str(x) for x in cmd))
    else:
        from shlex import quote as shlex_quote

        return " ".join(shlex_quote(str(x)) for x in cmd)