blob: a0b2422e3ea70fd9a799a1af59461170083ae7a8 [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)
84 captured_stdout.append(
85 line.split()[return_word_number_x_of_each_line])
86 else:
87 captured_stdout.append(line)
88 if time.time() - t0 > timeout:
89 try:
90 proc.terminate()
91 proc.wait()
92 # In principle this 'reset' should not be required.
93 # However, without it, the terminal is left in a funny
94 # state and required
95 subprocess.Popen(['reset']).wait()
96 except Exception as e:
97 print "Received exception {} when killing process " \
98 "started with {}".format(repr(e), cmd)
99 break
100 return captured_stdout
101 except Exception as e:
102 print 'Exception {} when running command:{}'.format(repr(e), cmd)
103 return None
104
105def is_open(ip_port):
106 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
107 try:
108 items = ip_port.split(':')
109 s.connect((items[0], int(items[1])))
110 s.shutdown(2)
111 return True
112 except:
113 return False
114
115def is_valid_ip(ip):
116 try:
117 socket.inet_aton(ip)
118 return True
119 except socket.error:
120 return False