OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 """ | 2 """ |
3 A class and functions used for running and controlling child processes. | 3 A class and functions used for running and controlling child processes. |
4 | 4 |
5 @copyright: 2008-2009 Red Hat Inc. | 5 @copyright: 2008-2009 Red Hat Inc. |
6 """ | 6 """ |
7 | 7 |
8 import os, sys, pty, select, termios, fcntl | 8 import os, sys, pty, select, termios, fcntl |
9 | 9 |
10 | 10 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 for fd in reader_fds: | 179 for fd in reader_fds: |
180 os.close(fd) | 180 os.close(fd) |
181 | 181 |
182 _unlock(lock_server_running) | 182 _unlock(lock_server_running) |
183 exit(0) | 183 exit(0) |
184 | 184 |
185 | 185 |
186 # The following is the client part of the module. | 186 # The following is the client part of the module. |
187 | 187 |
188 import subprocess, time, signal, re, threading, logging | 188 import subprocess, time, signal, re, threading, logging |
189 import common, kvm_utils | 189 import virt_utils |
190 | 190 |
191 | 191 |
192 class ExpectError(Exception): | 192 class ExpectError(Exception): |
193 def __init__(self, patterns, output): | 193 def __init__(self, patterns, output): |
194 Exception.__init__(self, patterns, output) | 194 Exception.__init__(self, patterns, output) |
195 self.patterns = patterns | 195 self.patterns = patterns |
196 self.output = output | 196 self.output = output |
197 | 197 |
198 def _pattern_str(self): | 198 def _pattern_str(self): |
199 if len(self.patterns) == 1: | 199 if len(self.patterns) == 1: |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 @param id: ID of an already running server, if accessing a running | 378 @param id: ID of an already running server, if accessing a running |
379 server, or None if starting a new one. | 379 server, or None if starting a new one. |
380 @param auto_close: If True, close() the instance automatically when its | 380 @param auto_close: If True, close() the instance automatically when its |
381 reference count drops to zero (default False). | 381 reference count drops to zero (default False). |
382 @param echo: Boolean indicating whether echo should be initially | 382 @param echo: Boolean indicating whether echo should be initially |
383 enabled for the pseudo terminal running the subprocess. This | 383 enabled for the pseudo terminal running the subprocess. This |
384 parameter has an effect only when starting a new server. | 384 parameter has an effect only when starting a new server. |
385 @param linesep: Line separator to be appended to strings sent to the | 385 @param linesep: Line separator to be appended to strings sent to the |
386 child process by sendline(). | 386 child process by sendline(). |
387 """ | 387 """ |
388 self.id = id or kvm_utils.generate_random_string(8) | 388 self.id = id or virt_utils.generate_random_string(8) |
389 | 389 |
390 # Define filenames for communication with server | 390 # Define filenames for communication with server |
391 base_dir = "/tmp/kvm_spawn" | 391 base_dir = "/tmp/kvm_spawn" |
392 try: | 392 try: |
393 os.makedirs(base_dir) | 393 os.makedirs(base_dir) |
394 except: | 394 except: |
395 pass | 395 pass |
396 (self.shell_pid_filename, | 396 (self.shell_pid_filename, |
397 self.status_filename, | 397 self.status_filename, |
398 self.output_filename, | 398 self.output_filename, |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 | 570 |
571 | 571 |
572 def close(self, sig=signal.SIGKILL): | 572 def close(self, sig=signal.SIGKILL): |
573 """ | 573 """ |
574 Kill the child process if it's alive and remove temporary files. | 574 Kill the child process if it's alive and remove temporary files. |
575 | 575 |
576 @param sig: The signal to send the process when attempting to kill it. | 576 @param sig: The signal to send the process when attempting to kill it. |
577 """ | 577 """ |
578 # Kill it if it's alive | 578 # Kill it if it's alive |
579 if self.is_alive(): | 579 if self.is_alive(): |
580 kvm_utils.kill_process_tree(self.get_pid(), sig) | 580 virt_utils.kill_process_tree(self.get_pid(), sig) |
581 # Wait for the server to exit | 581 # Wait for the server to exit |
582 _wait(self.lock_server_running_filename) | 582 _wait(self.lock_server_running_filename) |
583 # Call all cleanup routines | 583 # Call all cleanup routines |
584 for hook in self.close_hooks: | 584 for hook in self.close_hooks: |
585 hook(self) | 585 hook(self) |
586 # Close reader file descriptors | 586 # Close reader file descriptors |
587 for fd in self.reader_fds.values(): | 587 for fd in self.reader_fds.values(): |
588 try: | 588 try: |
589 os.close(fd) | 589 os.close(fd) |
590 except: | 590 except: |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 if print_func: | 984 if print_func: |
985 for line in data.splitlines(): | 985 for line in data.splitlines(): |
986 print_func(line) | 986 print_func(line) |
987 # Look for patterns | 987 # Look for patterns |
988 o += data | 988 o += data |
989 match = self.match_patterns(filter(o), patterns) | 989 match = self.match_patterns(filter(o), patterns) |
990 if match is not None: | 990 if match is not None: |
991 return match, o | 991 return match, o |
992 | 992 |
993 # Check if the child has terminated | 993 # Check if the child has terminated |
994 if kvm_utils.wait_for(lambda: not self.is_alive(), 5, 0, 0.1): | 994 if virt_utils.wait_for(lambda: not self.is_alive(), 5, 0, 0.1): |
995 raise ExpectProcessTerminatedError(patterns, self.get_status(), o) | 995 raise ExpectProcessTerminatedError(patterns, self.get_status(), o) |
996 else: | 996 else: |
997 # This shouldn't happen | 997 # This shouldn't happen |
998 raise ExpectError(patterns, o) | 998 raise ExpectError(patterns, o) |
999 | 999 |
1000 | 1000 |
1001 def read_until_last_word_matches(self, patterns, timeout=60, | 1001 def read_until_last_word_matches(self, patterns, timeout=60, |
1002 internal_timeout=None, print_func=None): | 1002 internal_timeout=None, print_func=None): |
1003 """ | 1003 """ |
1004 Read using read_nonblocking until the last word of the output matches | 1004 Read using read_nonblocking until the last word of the output matches |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1342 return self.cmd_status_output(cmd, timeout, internal_timeout, | 1342 return self.cmd_status_output(cmd, timeout, internal_timeout, |
1343 print_func) | 1343 print_func) |
1344 | 1344 |
1345 | 1345 |
1346 def get_command_status(self, cmd, timeout=60, internal_timeout=None, | 1346 def get_command_status(self, cmd, timeout=60, internal_timeout=None, |
1347 print_func=None): | 1347 print_func=None): |
1348 """ | 1348 """ |
1349 Alias for cmd_status() for backward compatibility. | 1349 Alias for cmd_status() for backward compatibility. |
1350 """ | 1350 """ |
1351 return self.cmd_status(cmd, timeout, internal_timeout, print_func) | 1351 return self.cmd_status(cmd, timeout, internal_timeout, print_func) |
OLD | NEW |