Debian Codegraph @

spark/utils/command.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2016-2018 Matthias Klumpp <[email protected]>
4 # Copyright (C) 2012-2013 Paul Tagliamonte <[email protected]>
5 #
6 # Licensed under the GNU Lesser General Public License Version 3
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation, either version 3 of the license, or
11 # (at your option) any later version.
12 #
13 # This software is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with this software. If not, see <http://www.gnu.org/licenses/>.
20
21 import shlex
22 import subprocess
23 from io import StringIO
24
25
26 class SubprocessError(Exception):
27 def __init__(self, out, err, ret, cmd):
28 self.out = out
29 self.err = err
30 self.ret = ret
31 self.cmd = cmd
32 super(SubprocessError, self).__init__(
33 "%s: %d\n%s" % (str(self.cmd), self.ret, str(self.err))
34 )
35
36 def __str__(self):
37 return "%s: %d\n%s" % (str(self.cmd), self.ret, str(self.err))
38
39
40 # Input may be a byte string, a unicode string, or a file-like object
41 def run_command(command, input=None):
42 if not isinstance(command, list):
43 command = shlex.split(command)
44
45 if not input:
46 input = None
47 elif isinstance(input, str):
48 input = input.encode('utf-8')
49 elif not isinstance(input, bytes):
50 input = input.read()
51
52 try:
53 pipe = subprocess.Popen(
54 command,
55 shell=False,
56 stdin=subprocess.PIPE,
57 stdout=subprocess.PIPE,
58 stderr=subprocess.PIPE,
59 )
60 except OSError:
61 return (None, None, -1)
62
63 (output, stderr) = pipe.communicate(input=input)
64 (output, stderr) = (c.decode('utf-8', errors='ignore') for c in (output, stderr))
65 return (output, stderr, pipe.returncode)
66
67
68 def safe_run(cmd, input=None, expected=0):
69 if not isinstance(expected, tuple):
70 expected = (expected,)
71
72 out, err, ret = run_command(cmd, input=input)
73
74 if ret not in expected:
75 raise SubprocessError(out, err, ret, cmd)
76
77 return out, err, ret
78
79
80 def run_logged(jlog, cmd: list[str], return_output=False, **kwargs):
81 '''Run a command and log output to the job logfile.
82
83 Parameters
84 ----------
85 jlog JobLog
86 The job log to use
87 cmd
88 Command to execute
89 return_output
90 Whether to save the output to return as string when the function completes.
91
92 Returns
93 -------
94 ret : int
95 Return code of the process
96 outbuf : str or None
97 Process output as string if `return_output` was True
98 '''
99 p = subprocess.Popen(
100 cmd, **kwargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False
101 )
102
103 # capture live output and send it to all places that are interested in
104 # logging it (except for our stdout).
105 outbuf = StringIO()
106 while True:
107 line_b = p.stdout.readline()
108 if p.poll() is not None:
109 break
110 line_s = str(line_b, 'utf-8', 'replace')
111 jlog.write(line_s)
112 if return_output:
113 outbuf.write(line_s)
114
115 ret = p.returncode
116 if ret:
117 jlog.write('Command {0} failed with error code {1}'.format(' '.join(cmd), ret))
118
119 return ret, outbuf.getvalue()