mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | Miscellaneous small functions. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 9 | __author__ = """ |
| 10 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 11 | poirier@google.com (Benjamin Poirier), |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 12 | stutsman@google.com (Ryan Stutsman) |
| 13 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 14 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 15 | import atexit, os, select, shutil, signal, StringIO, subprocess, tempfile |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 16 | import time, types, urllib, re, sys, textwrap |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 17 | import hosts |
mbligh | f31b0c0 | 2007-11-29 18:19:22 +0000 | [diff] [blame] | 18 | from common.error import * |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 19 | |
mbligh | ea397bb | 2008-02-02 19:17:51 +0000 | [diff] [blame] | 20 | from common.utils import * |
| 21 | |
| 22 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 23 | # A dictionary of pid and a list of tmpdirs for that pid |
| 24 | __tmp_dirs = {} |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def sh_escape(command): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 28 | """ |
| 29 | Escape special characters from a command so that it can be passed |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 30 | as a double quoted (" ") string in a (ba)sh command. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 31 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 32 | Args: |
| 33 | command: the command string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 34 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 35 | Returns: |
| 36 | The escaped command string. The required englobing double |
| 37 | quotes are NOT added and so should be added at some point by |
| 38 | the caller. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 39 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 40 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 41 | """ |
| 42 | command= command.replace("\\", "\\\\") |
| 43 | command= command.replace("$", r'\$') |
| 44 | command= command.replace('"', r'\"') |
| 45 | command= command.replace('`', r'\`') |
| 46 | return command |
| 47 | |
| 48 | |
| 49 | def scp_remote_escape(filename): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 50 | """ |
| 51 | Escape special characters from a filename so that it can be passed |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 52 | to scp (within double quotes) as a remote file. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 53 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 54 | Bis-quoting has to be used with scp for remote files, "bis-quoting" |
| 55 | as in quoting x 2 |
| 56 | scp does not support a newline in the filename |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 57 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 58 | Args: |
| 59 | filename: the filename string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 60 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 61 | Returns: |
| 62 | The escaped filename string. The required englobing double |
| 63 | quotes are NOT added and so should be added at some point by |
| 64 | the caller. |
| 65 | """ |
| 66 | escape_chars= r' !"$&' "'" r'()*,:;<=>?[\]^`{|}' |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 67 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 68 | new_name= [] |
| 69 | for char in filename: |
| 70 | if char in escape_chars: |
| 71 | new_name.append("\\%s" % (char,)) |
| 72 | else: |
| 73 | new_name.append(char) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 74 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 75 | return sh_escape("".join(new_name)) |
| 76 | |
| 77 | |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 78 | def get(location, local_copy = False): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 79 | """Get a file or directory to a local temporary directory. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 80 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 81 | Args: |
| 82 | location: the source of the material to get. This source may |
| 83 | be one of: |
| 84 | * a local file or directory |
| 85 | * a URL (http or ftp) |
| 86 | * a python file-like object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 87 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 88 | Returns: |
| 89 | The location of the file or directory where the requested |
| 90 | content was saved. This will be contained in a temporary |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 91 | directory on the local host. If the material to get was a |
| 92 | directory, the location will contain a trailing '/' |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 93 | """ |
| 94 | tmpdir = get_tmp_dir() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 95 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 96 | # location is a file-like object |
| 97 | if hasattr(location, "read"): |
| 98 | tmpfile = os.path.join(tmpdir, "file") |
| 99 | tmpfileobj = file(tmpfile, 'w') |
| 100 | shutil.copyfileobj(location, tmpfileobj) |
| 101 | tmpfileobj.close() |
| 102 | return tmpfile |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 103 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 104 | if isinstance(location, types.StringTypes): |
| 105 | # location is a URL |
| 106 | if location.startswith('http') or location.startswith('ftp'): |
| 107 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 108 | urllib.urlretrieve(location, tmpfile) |
| 109 | return tmpfile |
| 110 | # location is a local path |
| 111 | elif os.path.exists(os.path.abspath(location)): |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 112 | if not local_copy: |
mbligh | 59f70aa | 2007-10-25 14:44:38 +0000 | [diff] [blame] | 113 | if os.path.isdir(location): |
| 114 | return location.rstrip('/') + '/' |
| 115 | else: |
| 116 | return location |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 117 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 118 | if os.path.isdir(location): |
| 119 | tmpfile += '/' |
| 120 | shutil.copytree(location, tmpfile, symlinks=True) |
| 121 | return tmpfile |
| 122 | shutil.copyfile(location, tmpfile) |
| 123 | return tmpfile |
| 124 | # location is just a string, dump it to a file |
| 125 | else: |
| 126 | tmpfd, tmpfile = tempfile.mkstemp(dir=tmpdir) |
| 127 | tmpfileobj = os.fdopen(tmpfd, 'w') |
| 128 | tmpfileobj.write(location) |
| 129 | tmpfileobj.close() |
| 130 | return tmpfile |
| 131 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame^] | 132 | def nuke_subprocess(subproc): |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 133 | # the process has not terminated within timeout, |
| 134 | # kill it via an escalating series of signals. |
| 135 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 136 | for sig in signal_queue: |
| 137 | try: |
| 138 | os.kill(subproc.pid, sig) |
| 139 | # The process may have died before we could kill it. |
| 140 | except OSError: |
| 141 | pass |
| 142 | |
| 143 | for i in range(5): |
| 144 | rc = subproc.poll() |
| 145 | if rc != None: |
| 146 | return |
| 147 | time.sleep(1) |
| 148 | |
| 149 | |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 150 | def nuke_pid(pid): |
| 151 | # the process has not terminated within timeout, |
| 152 | # kill it via an escalating series of signals. |
| 153 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 154 | for sig in signal_queue: |
| 155 | try: |
| 156 | os.kill(pid, sig) |
| 157 | |
| 158 | # The process may have died before we could kill it. |
| 159 | except OSError: |
| 160 | pass |
| 161 | |
| 162 | try: |
| 163 | for i in range(5): |
| 164 | status = os.waitpid(pid, os.WNOHANG)[0] |
| 165 | if status == pid: |
| 166 | return |
| 167 | time.sleep(1) |
| 168 | |
| 169 | if status != pid: |
| 170 | raise AutoservRunError('Could not kill pid %d' |
| 171 | % pid, None) |
| 172 | |
| 173 | # the process died before we join it. |
| 174 | except OSError: |
| 175 | pass |
| 176 | |
| 177 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 178 | def _process_output(pipe, fbuffer, teefile=None, use_os_read=True): |
| 179 | if use_os_read: |
| 180 | data = os.read(pipe.fileno(), 1024) |
| 181 | else: |
| 182 | data = pipe.read() |
| 183 | fbuffer.write(data) |
| 184 | if teefile: |
| 185 | teefile.write(data) |
| 186 | teefile.flush() |
| 187 | |
| 188 | |
| 189 | def _wait_for_command(subproc, start_time, timeout, stdout_file, stderr_file, |
| 190 | stdout_tee, stderr_tee): |
| 191 | if timeout: |
| 192 | stop_time = start_time + timeout |
| 193 | time_left = stop_time - time.time() |
| 194 | else: |
| 195 | time_left = None # so that select never times out |
| 196 | while not timeout or time_left > 0: |
| 197 | # select will return when stdout is ready (including when it is |
| 198 | # EOF, that is the process has terminated). |
| 199 | ready, _, _ = select.select([subproc.stdout, subproc.stderr], |
| 200 | [], [], time_left) |
| 201 | # os.read() has to be used instead of |
| 202 | # subproc.stdout.read() which will otherwise block |
| 203 | if subproc.stdout in ready: |
| 204 | _process_output(subproc.stdout, stdout_file, |
| 205 | stdout_tee) |
| 206 | if subproc.stderr in ready: |
| 207 | _process_output(subproc.stderr, stderr_file, |
| 208 | stderr_tee) |
| 209 | |
mbligh | 35f4395 | 2008-03-05 17:02:08 +0000 | [diff] [blame] | 210 | exit_status_indication = subproc.poll() |
| 211 | |
| 212 | if exit_status_indication is not None: |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 213 | return exit_status_indication |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 214 | if timeout: |
| 215 | time_left = stop_time - time.time() |
| 216 | |
| 217 | # the process has not terminated within timeout, |
| 218 | # kill it via an escalating series of signals. |
mbligh | 35f4395 | 2008-03-05 17:02:08 +0000 | [diff] [blame] | 219 | if exit_status_indication is None: |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame^] | 220 | nuke_subprocess(subproc) |
mbligh | ff6b402 | 2008-01-10 16:20:51 +0000 | [diff] [blame] | 221 | raise AutoservRunError('Command not complete within %s seconds' |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 222 | % timeout, None) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def run(command, timeout=None, ignore_status=False, |
| 226 | stdout_tee=None, stderr_tee=None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 227 | """ |
| 228 | Run a command on the host. |
| 229 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 230 | Args: |
| 231 | command: the command line string |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 232 | timeout: time limit in seconds before attempting to |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 233 | kill the running process. The run() function |
| 234 | will take a few seconds longer than 'timeout' |
| 235 | to complete if it has to kill the process. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 236 | ignore_status: do not raise an exception, no matter what |
| 237 | the exit code of the command is. |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 238 | stdout_tee: optional file-like object to which stdout data |
| 239 | will be written as it is generated (data will still |
| 240 | be stored in result.stdout) |
| 241 | stderr_tee: likewise for stderr |
| 242 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 243 | Returns: |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 244 | a CmdResult object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 245 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 246 | Raises: |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 247 | AutoservRunError: the exit code of the command |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 248 | execution was not 0 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 249 | """ |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame^] | 250 | return join_bg_job(run_bg(command), timeout, ignore_status, |
| 251 | stdout_tee, stderr_tee) |
| 252 | |
| 253 | |
| 254 | def run_bg(command): |
| 255 | """Run the command in a subprocess and return the subprocess.""" |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 256 | result = CmdResult(command) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 257 | sp = subprocess.Popen(command, stdout=subprocess.PIPE, |
mbligh | 3c647c3 | 2008-02-08 16:51:02 +0000 | [diff] [blame] | 258 | stderr=subprocess.PIPE, |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 259 | shell=True, executable="/bin/bash") |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame^] | 260 | return sp, result |
| 261 | |
| 262 | |
| 263 | def join_bg_job(bg_job, timeout=None, ignore_status=False, |
| 264 | stdout_tee=None, stderr_tee=None): |
| 265 | """Join the subprocess with the current thread. See run description.""" |
| 266 | sp, result = bg_job |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 267 | stdout_file = StringIO.StringIO() |
| 268 | stderr_file = StringIO.StringIO() |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 269 | |
| 270 | try: |
| 271 | # We are holding ends to stdin, stdout pipes |
| 272 | # hence we need to be sure to close those fds no mater what |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 273 | start_time = time.time() |
mbligh | 34faa28 | 2008-01-16 17:44:49 +0000 | [diff] [blame] | 274 | ret = _wait_for_command(sp, start_time, timeout, stdout_file, |
| 275 | stderr_file, stdout_tee, stderr_tee) |
mbligh | e13239a | 2008-03-10 15:06:11 +0000 | [diff] [blame] | 276 | result.exit_status = ret |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 277 | |
| 278 | result.duration = time.time() - start_time |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 279 | # don't use os.read now, so we get all the rest of the output |
| 280 | _process_output(sp.stdout, stdout_file, stdout_tee, |
| 281 | use_os_read=False) |
| 282 | _process_output(sp.stderr, stderr_file, stderr_tee, |
| 283 | use_os_read=False) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 284 | finally: |
| 285 | # close our ends of the pipes to the sp no matter what |
| 286 | sp.stdout.close() |
| 287 | sp.stderr.close() |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 288 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 289 | result.stdout = stdout_file.getvalue() |
| 290 | result.stderr = stderr_file.getvalue() |
| 291 | |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 292 | if not ignore_status and result.exit_status > 0: |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 293 | raise AutoservRunError("command execution error", result) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 294 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 295 | return result |
| 296 | |
| 297 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 298 | def system(command, timeout=None, ignore_status=False): |
mbligh | 10b3a08 | 2008-01-10 16:35:29 +0000 | [diff] [blame] | 299 | return run(command, timeout, ignore_status, |
| 300 | stdout_tee=sys.stdout, stderr_tee=sys.stderr).exit_status |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 301 | |
| 302 | |
| 303 | def system_output(command, timeout=None, ignore_status=False): |
mbligh | d60b032 | 2008-02-12 20:58:43 +0000 | [diff] [blame] | 304 | out = run(command, timeout, ignore_status).stdout |
| 305 | if out[-1:] == '\n': out = out[:-1] |
| 306 | return out |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 307 | |
| 308 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 309 | def get_tmp_dir(): |
| 310 | """Return the pathname of a directory on the host suitable |
| 311 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 312 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 313 | The directory and its content will be deleted automatically |
| 314 | at the end of the program execution if they are still present. |
| 315 | """ |
| 316 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 317 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 318 | dir_name= tempfile.mkdtemp(prefix="autoserv-") |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 319 | pid = os.getpid() |
| 320 | if not pid in __tmp_dirs: |
| 321 | __tmp_dirs[pid] = [] |
| 322 | __tmp_dirs[pid].append(dir_name) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 323 | return dir_name |
| 324 | |
| 325 | |
| 326 | @atexit.register |
| 327 | def __clean_tmp_dirs(): |
| 328 | """Erase temporary directories that were created by the get_tmp_dir() |
| 329 | function and that are still present. |
| 330 | """ |
| 331 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 332 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 333 | pid = os.getpid() |
| 334 | if pid not in __tmp_dirs: |
| 335 | return |
| 336 | for dir in __tmp_dirs[pid]: |
| 337 | try: |
| 338 | shutil.rmtree(dir) |
| 339 | except OSError, e: |
| 340 | if e.errno == 2: |
| 341 | pass |
| 342 | __tmp_dirs[pid] = [] |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 343 | |
| 344 | |
| 345 | def unarchive(host, source_material): |
| 346 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 347 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 348 | If the "source_material" is compresses (according to the file |
| 349 | extension) it will be uncompressed. Supported compression formats |
| 350 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 351 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 352 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 353 | Args: |
| 354 | host: the host object on which the archive is located |
| 355 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 356 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 357 | Returns: |
| 358 | The file or directory name of the unarchived source material. |
| 359 | If the material is a tar archive, it will be extracted in the |
| 360 | directory where it is and the path returned will be the first |
| 361 | entry in the archive, assuming it is the topmost directory. |
| 362 | If the material is not an archive, nothing will be done so this |
| 363 | function is "harmless" when it is "useless". |
| 364 | """ |
| 365 | # uncompress |
| 366 | if (source_material.endswith(".gz") or |
| 367 | source_material.endswith(".gzip")): |
| 368 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 369 | source_material= ".".join(source_material.split(".")[:-1]) |
| 370 | elif source_material.endswith("bz2"): |
| 371 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 372 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 373 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 374 | # untar |
| 375 | if source_material.endswith(".tar"): |
| 376 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 377 | sh_escape(os.path.dirname(source_material)), |
| 378 | sh_escape(source_material),)) |
| 379 | source_material= os.path.join(os.path.dirname(source_material), |
| 380 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 381 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 382 | return source_material |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 383 | |
| 384 | |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 385 | def get_server_dir(): |
| 386 | path = os.path.dirname(sys.modules['utils'].__file__) |
| 387 | return os.path.abspath(path) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 388 | |
| 389 | |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 390 | def find_pid(command): |
| 391 | for line in system_output('ps -eo pid,cmd').rstrip().split('\n'): |
| 392 | (pid, cmd) = line.split(None, 1) |
| 393 | if re.search(command, cmd): |
| 394 | return int(pid) |
| 395 | return None |
| 396 | |
| 397 | |
| 398 | def nohup(command, stdout='/dev/null', stderr='/dev/null', background=True, |
| 399 | env = {}): |
| 400 | cmd = ' '.join(key+'='+val for key, val in env.iteritems()) |
| 401 | cmd += ' nohup ' + command |
| 402 | cmd += ' > %s' % stdout |
| 403 | if stdout == stderr: |
| 404 | cmd += ' 2>&1' |
| 405 | else: |
| 406 | cmd += ' 2> %s' % stderr |
| 407 | if background: |
| 408 | cmd += ' &' |
| 409 | system(cmd) |
| 410 | |
| 411 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 412 | class AutoservOptionParser: |
| 413 | """Custom command-line options parser for autoserv. |
| 414 | |
| 415 | We can't use the general getopt methods here, as there will be unknown |
| 416 | extra arguments that we pass down into the control file instead. |
| 417 | Thus we process the arguments by hand, for which we are duly repentant. |
| 418 | Making a single function here just makes it harder to read. Suck it up. |
| 419 | """ |
| 420 | |
| 421 | def __init__(self, args): |
| 422 | self.args = args |
| 423 | |
| 424 | |
| 425 | def parse_opts(self, flag): |
| 426 | if self.args.count(flag): |
| 427 | idx = self.args.index(flag) |
| 428 | self.args[idx : idx+1] = [] |
| 429 | return True |
| 430 | else: |
| 431 | return False |
| 432 | |
| 433 | |
| 434 | def parse_opts_param(self, flag, default = None, split = False): |
| 435 | if self.args.count(flag): |
| 436 | idx = self.args.index(flag) |
| 437 | ret = self.args[idx+1] |
| 438 | self.args[idx : idx+2] = [] |
| 439 | if split: |
| 440 | return ret.split(split) |
| 441 | else: |
| 442 | return ret |
| 443 | else: |
| 444 | return default |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 445 | |
| 446 | |
| 447 | class CmdResult(object): |
| 448 | """ |
| 449 | Command execution result. |
| 450 | |
| 451 | command: String containing the command line itself |
| 452 | exit_status: Integer exit code of the process |
| 453 | stdout: String containing stdout of the process |
| 454 | stderr: String containing stderr of the process |
| 455 | duration: Elapsed wall clock time running the process |
| 456 | """ |
| 457 | |
| 458 | def __init__(self, command = None): |
| 459 | self.command = command |
| 460 | self.exit_status = None |
| 461 | self.stdout = "" |
| 462 | self.stderr = "" |
| 463 | self.duration = 0 |
| 464 | |
| 465 | |
| 466 | def __repr__(self): |
| 467 | wrapper = textwrap.TextWrapper(width = 78, |
| 468 | initial_indent="\n ", |
| 469 | subsequent_indent=" ") |
| 470 | |
| 471 | stdout = self.stdout.rstrip() |
| 472 | if stdout: |
| 473 | stdout = "\nstdout:\n%s" % stdout |
| 474 | |
| 475 | stderr = self.stderr.rstrip() |
| 476 | if stderr: |
| 477 | stderr = "\nstderr:\n%s" % stderr |
| 478 | |
| 479 | return ("* Command: %s\n" |
| 480 | "Exit status: %s\n" |
| 481 | "Duration: %s\n" |
| 482 | "%s" |
| 483 | "%s" |
| 484 | % (wrapper.fill(self.command), self.exit_status, |
| 485 | self.duration, stdout, stderr)) |