Conditions | 6 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | from contextlib import contextmanager |
||
8 | @contextmanager |
||
|
|||
9 | def run_interactive_shell_command(command, **kwargs): |
||
10 | """ |
||
11 | Runs a single command in shell and provides stdout, stderr and stdin |
||
12 | streams. |
||
13 | |||
14 | This function creates a context manager that sets up the process (using |
||
15 | ``subprocess.Popen()``), returns to caller and waits for process to exit on |
||
16 | leaving. |
||
17 | |||
18 | By default the process is opened in ``universal_newlines`` mode and creates |
||
19 | pipes for all streams (stdout, stderr and stdin) using ``subprocess.PIPE`` |
||
20 | special value. These pipes are closed automatically, so if you want to get |
||
21 | the contents of the streams you should retrieve them before the context |
||
22 | manager exits. |
||
23 | |||
24 | >>> with run_interactive_shell_command(["echo", "TEXT"]) as p: |
||
25 | ... stdout = p.stdout |
||
26 | ... stdout_text = stdout.read() |
||
27 | >>> stdout_text |
||
28 | 'TEXT\\n' |
||
29 | >>> stdout.closed |
||
30 | True |
||
31 | |||
32 | Custom streams provided are not closed except of ``subprocess.PIPE``. |
||
33 | |||
34 | >>> from tempfile import TemporaryFile |
||
35 | >>> stream = TemporaryFile() |
||
36 | >>> with run_interactive_shell_command(["echo", "TEXT"], |
||
37 | ... stdout=stream) as p: |
||
38 | ... stderr = p.stderr |
||
39 | >>> stderr.closed |
||
40 | True |
||
41 | >>> stream.closed |
||
42 | False |
||
43 | |||
44 | :param command: The command to run on shell. This parameter can either |
||
45 | be a sequence of arguments that are directly passed to |
||
46 | the process or a string. A string gets splitted beforehand |
||
47 | using ``shlex.split()``. If providing ``shell=True`` as a |
||
48 | keyword-argument, no ``shlex.split()`` is performed and the |
||
49 | command string goes directly to ``subprocess.Popen()``. |
||
50 | :param kwargs: Additional keyword arguments to pass to |
||
51 | ``subprocess.Popen`` that are used to spawn the process. |
||
52 | :return: A context manager yielding the process started from the |
||
53 | command. |
||
54 | """ |
||
55 | if not kwargs.get("shell", False) and isinstance(command, str): |
||
56 | command = shlex.split(command) |
||
57 | |||
58 | args = {"stdout": PIPE, |
||
59 | "stderr": PIPE, |
||
60 | "stdin": PIPE, |
||
61 | "universal_newlines": True} |
||
62 | args.update(kwargs) |
||
63 | |||
64 | process = Popen(command, **args) |
||
65 | try: |
||
66 | yield process |
||
67 | finally: |
||
68 | if args["stdout"] is PIPE: |
||
69 | process.stdout.close() |
||
70 | if args["stderr"] is PIPE: |
||
71 | process.stderr.close() |
||
72 | if args["stdin"] is PIPE: |
||
73 | process.stdin.close() |
||
74 | |||
75 | process.wait() |
||
76 | |||
161 |