blob: f987b0f7143e9fa931509868a5e1e4a8610e1958 [file] [log] [blame]
Khen Nursimulu37a9bf82016-10-16 20:11:31 -04001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import os
17import subprocess
18import socket
19import time
20import re
21
22
23def run_command_to_completion_with_raw_stdout(cmd):
24 try:
25 env = os.environ.copy()
26 proc = subprocess.Popen(
27 cmd,
28 env=env,
29 shell=True,
30 stdout=subprocess.PIPE
31 )
32 out, err = proc.communicate()
33 return out, err, proc.returncode
34 except OSError as e:
35 print 'Exception {} when running command:{}'.format(repr(e), cmd)
36 return None
37
38
39def run_command_to_completion_with_stdout_in_list(cmd):
40 stdout_response = []
41 try:
42 command = cmd
43 env = os.environ.copy()
44 proc = subprocess.Popen(
45 command,
46 env=env,
47 shell=True,
48 stdout=subprocess.PIPE,
49 stderr=subprocess.PIPE,
50 bufsize=1
51 )
52 with proc.stdout:
53 for line in iter(proc.stdout.readline, b''):
54 stdout_response.append(line)
55
56 if proc.wait() != 0:
57 err_msg = 'Command {} did not complete successfully '.format(cmd)
58 return [], err_msg, proc.returncode
59
60 return stdout_response, None, proc.returncode
61 except OSError as e:
62 print 'Exception {} when running command:{}'.format(repr(e), cmd)
63 return None
64
65
66def run_long_running_command_with_timeout(cmd, timeout,
67 return_word_number_x_of_each_line=-1):
68 captured_stdout = []
69 try:
70 t0 = time.time()
71 env = os.environ.copy()
72 proc = subprocess.Popen(
73 cmd,
74 env=env,
75 shell=True,
76 stdout=subprocess.PIPE,
77 stderr=subprocess.PIPE,
78 bufsize=1
79 )
80 for line in iter(proc.stdout.readline, b''):
81 if return_word_number_x_of_each_line != -1:
82 ansi_escape = re.compile(r'\x1b[^m]*m')
83 line = ansi_escape.sub('', line)
Khen Nursimulu9b9f1ad2017-01-10 15:43:32 -050084 if len(line.split()) > return_word_number_x_of_each_line:
85 captured_stdout.append(
86 line.split()[return_word_number_x_of_each_line])
Khen Nursimulu37a9bf82016-10-16 20:11:31 -040087 else:
88 captured_stdout.append(line)
89 if time.time() - t0 > timeout:
90 try:
91 proc.terminate()
92 proc.wait()
93 # In principle this 'reset' should not be required.
94 # However, without it, the terminal is left in a funny
95 # state and required
96 subprocess.Popen(['reset']).wait()
97 except Exception as e:
98 print "Received exception {} when killing process " \
99 "started with {}".format(repr(e), cmd)
100 break
101 return captured_stdout
102 except Exception as e:
103 print 'Exception {} when running command:{}'.format(repr(e), cmd)
104 return None
105
106def is_open(ip_port):
107 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
108 try:
109 items = ip_port.split(':')
110 s.connect((items[0], int(items[1])))
111 s.shutdown(2)
112 return True
113 except:
114 return False
115
116def is_valid_ip(ip):
117 try:
118 socket.inet_aton(ip)
119 return True
120 except socket.error:
121 return False