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 | |
| 132 | |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 133 | def __nuke_subprocess(subproc): |
| 134 | # the process has not terminated within timeout, |
| 135 | # kill it via an escalating series of signals. |
| 136 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 137 | for sig in signal_queue: |
| 138 | try: |
| 139 | os.kill(subproc.pid, sig) |
| 140 | # The process may have died before we could kill it. |
| 141 | except OSError: |
| 142 | pass |
| 143 | |
| 144 | for i in range(5): |
| 145 | rc = subproc.poll() |
| 146 | if rc != None: |
| 147 | return |
| 148 | time.sleep(1) |
| 149 | |
| 150 | |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 151 | def nuke_pid(pid): |
| 152 | # the process has not terminated within timeout, |
| 153 | # kill it via an escalating series of signals. |
| 154 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 155 | for sig in signal_queue: |
| 156 | try: |
| 157 | os.kill(pid, sig) |
| 158 | |
| 159 | # The process may have died before we could kill it. |
| 160 | except OSError: |
| 161 | pass |
| 162 | |
| 163 | try: |
| 164 | for i in range(5): |
| 165 | status = os.waitpid(pid, os.WNOHANG)[0] |
| 166 | if status == pid: |
| 167 | return |
| 168 | time.sleep(1) |
| 169 | |
| 170 | if status != pid: |
| 171 | raise AutoservRunError('Could not kill pid %d' |
| 172 | % pid, None) |
| 173 | |
| 174 | # the process died before we join it. |
| 175 | except OSError: |
| 176 | pass |
| 177 | |
| 178 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 179 | def _process_output(pipe, fbuffer, teefile=None, use_os_read=True): |
| 180 | if use_os_read: |
| 181 | data = os.read(pipe.fileno(), 1024) |
| 182 | else: |
| 183 | data = pipe.read() |
| 184 | fbuffer.write(data) |
| 185 | if teefile: |
| 186 | teefile.write(data) |
| 187 | teefile.flush() |
| 188 | |
| 189 | |
| 190 | def _wait_for_command(subproc, start_time, timeout, stdout_file, stderr_file, |
| 191 | stdout_tee, stderr_tee): |
| 192 | if timeout: |
| 193 | stop_time = start_time + timeout |
| 194 | time_left = stop_time - time.time() |
| 195 | else: |
| 196 | time_left = None # so that select never times out |
| 197 | while not timeout or time_left > 0: |
| 198 | # select will return when stdout is ready (including when it is |
| 199 | # EOF, that is the process has terminated). |
| 200 | ready, _, _ = select.select([subproc.stdout, subproc.stderr], |
| 201 | [], [], time_left) |
| 202 | # os.read() has to be used instead of |
| 203 | # subproc.stdout.read() which will otherwise block |
| 204 | if subproc.stdout in ready: |
| 205 | _process_output(subproc.stdout, stdout_file, |
| 206 | stdout_tee) |
| 207 | if subproc.stderr in ready: |
| 208 | _process_output(subproc.stderr, stderr_file, |
| 209 | stderr_tee) |
| 210 | |
mbligh | 35f4395 | 2008-03-05 17:02:08 +0000 | [diff] [blame] | 211 | exit_status_indication = subproc.poll() |
| 212 | |
| 213 | if exit_status_indication is not None: |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 214 | return exit_status_indication |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 215 | if timeout: |
| 216 | time_left = stop_time - time.time() |
| 217 | |
| 218 | # the process has not terminated within timeout, |
| 219 | # kill it via an escalating series of signals. |
mbligh | 35f4395 | 2008-03-05 17:02:08 +0000 | [diff] [blame] | 220 | if exit_status_indication is None: |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 221 | __nuke_subprocess(subproc) |
mbligh | ff6b402 | 2008-01-10 16:20:51 +0000 | [diff] [blame] | 222 | raise AutoservRunError('Command not complete within %s seconds' |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 223 | % timeout, None) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 224 | |
| 225 | |
| 226 | def run(command, timeout=None, ignore_status=False, |
| 227 | stdout_tee=None, stderr_tee=None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 228 | """ |
| 229 | Run a command on the host. |
| 230 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 231 | Args: |
| 232 | command: the command line string |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 233 | timeout: time limit in seconds before attempting to |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 234 | kill the running process. The run() function |
| 235 | will take a few seconds longer than 'timeout' |
| 236 | to complete if it has to kill the process. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 237 | ignore_status: do not raise an exception, no matter what |
| 238 | the exit code of the command is. |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 239 | stdout_tee: optional file-like object to which stdout data |
| 240 | will be written as it is generated (data will still |
| 241 | be stored in result.stdout) |
| 242 | stderr_tee: likewise for stderr |
| 243 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 244 | Returns: |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 245 | a CmdResult object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 246 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 247 | Raises: |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 248 | AutoservRunError: the exit code of the command |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 249 | execution was not 0 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 250 | """ |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 251 | result = CmdResult(command) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 252 | sp = subprocess.Popen(command, stdout=subprocess.PIPE, |
mbligh | 3c647c3 | 2008-02-08 16:51:02 +0000 | [diff] [blame] | 253 | stderr=subprocess.PIPE, |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 254 | shell=True, executable="/bin/bash") |
| 255 | stdout_file = StringIO.StringIO() |
| 256 | stderr_file = StringIO.StringIO() |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 257 | |
| 258 | try: |
| 259 | # We are holding ends to stdin, stdout pipes |
| 260 | # hence we need to be sure to close those fds no mater what |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 261 | start_time = time.time() |
mbligh | 34faa28 | 2008-01-16 17:44:49 +0000 | [diff] [blame] | 262 | ret = _wait_for_command(sp, start_time, timeout, stdout_file, |
| 263 | stderr_file, stdout_tee, stderr_tee) |
mbligh | e13239a | 2008-03-10 15:06:11 +0000 | [diff] [blame^] | 264 | result.exit_status = ret |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 265 | |
| 266 | result.duration = time.time() - start_time |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 267 | # don't use os.read now, so we get all the rest of the output |
| 268 | _process_output(sp.stdout, stdout_file, stdout_tee, |
| 269 | use_os_read=False) |
| 270 | _process_output(sp.stderr, stderr_file, stderr_tee, |
| 271 | use_os_read=False) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 272 | finally: |
| 273 | # close our ends of the pipes to the sp no matter what |
| 274 | sp.stdout.close() |
| 275 | sp.stderr.close() |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 276 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 277 | result.stdout = stdout_file.getvalue() |
| 278 | result.stderr = stderr_file.getvalue() |
| 279 | |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 280 | if not ignore_status and result.exit_status > 0: |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 281 | raise AutoservRunError("command execution error", result) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 282 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 283 | return result |
| 284 | |
| 285 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 286 | def system(command, timeout=None, ignore_status=False): |
mbligh | 10b3a08 | 2008-01-10 16:35:29 +0000 | [diff] [blame] | 287 | return run(command, timeout, ignore_status, |
| 288 | stdout_tee=sys.stdout, stderr_tee=sys.stderr).exit_status |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | def system_output(command, timeout=None, ignore_status=False): |
mbligh | d60b032 | 2008-02-12 20:58:43 +0000 | [diff] [blame] | 292 | out = run(command, timeout, ignore_status).stdout |
| 293 | if out[-1:] == '\n': out = out[:-1] |
| 294 | return out |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 295 | |
| 296 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 297 | def get_tmp_dir(): |
| 298 | """Return the pathname of a directory on the host suitable |
| 299 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 300 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 301 | The directory and its content will be deleted automatically |
| 302 | at the end of the program execution if they are still present. |
| 303 | """ |
| 304 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 305 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 306 | dir_name= tempfile.mkdtemp(prefix="autoserv-") |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 307 | pid = os.getpid() |
| 308 | if not pid in __tmp_dirs: |
| 309 | __tmp_dirs[pid] = [] |
| 310 | __tmp_dirs[pid].append(dir_name) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 311 | return dir_name |
| 312 | |
| 313 | |
| 314 | @atexit.register |
| 315 | def __clean_tmp_dirs(): |
| 316 | """Erase temporary directories that were created by the get_tmp_dir() |
| 317 | function and that are still present. |
| 318 | """ |
| 319 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 320 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 321 | pid = os.getpid() |
| 322 | if pid not in __tmp_dirs: |
| 323 | return |
| 324 | for dir in __tmp_dirs[pid]: |
| 325 | try: |
| 326 | shutil.rmtree(dir) |
| 327 | except OSError, e: |
| 328 | if e.errno == 2: |
| 329 | pass |
| 330 | __tmp_dirs[pid] = [] |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 331 | |
| 332 | |
| 333 | def unarchive(host, source_material): |
| 334 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 335 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 336 | If the "source_material" is compresses (according to the file |
| 337 | extension) it will be uncompressed. Supported compression formats |
| 338 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 339 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 340 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 341 | Args: |
| 342 | host: the host object on which the archive is located |
| 343 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 344 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 345 | Returns: |
| 346 | The file or directory name of the unarchived source material. |
| 347 | If the material is a tar archive, it will be extracted in the |
| 348 | directory where it is and the path returned will be the first |
| 349 | entry in the archive, assuming it is the topmost directory. |
| 350 | If the material is not an archive, nothing will be done so this |
| 351 | function is "harmless" when it is "useless". |
| 352 | """ |
| 353 | # uncompress |
| 354 | if (source_material.endswith(".gz") or |
| 355 | source_material.endswith(".gzip")): |
| 356 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 357 | source_material= ".".join(source_material.split(".")[:-1]) |
| 358 | elif source_material.endswith("bz2"): |
| 359 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 360 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 361 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 362 | # untar |
| 363 | if source_material.endswith(".tar"): |
| 364 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 365 | sh_escape(os.path.dirname(source_material)), |
| 366 | sh_escape(source_material),)) |
| 367 | source_material= os.path.join(os.path.dirname(source_material), |
| 368 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 369 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 370 | return source_material |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 371 | |
| 372 | |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 373 | def get_server_dir(): |
| 374 | path = os.path.dirname(sys.modules['utils'].__file__) |
| 375 | return os.path.abspath(path) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 376 | |
| 377 | |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 378 | def find_pid(command): |
| 379 | for line in system_output('ps -eo pid,cmd').rstrip().split('\n'): |
| 380 | (pid, cmd) = line.split(None, 1) |
| 381 | if re.search(command, cmd): |
| 382 | return int(pid) |
| 383 | return None |
| 384 | |
| 385 | |
| 386 | def nohup(command, stdout='/dev/null', stderr='/dev/null', background=True, |
| 387 | env = {}): |
| 388 | cmd = ' '.join(key+'='+val for key, val in env.iteritems()) |
| 389 | cmd += ' nohup ' + command |
| 390 | cmd += ' > %s' % stdout |
| 391 | if stdout == stderr: |
| 392 | cmd += ' 2>&1' |
| 393 | else: |
| 394 | cmd += ' 2> %s' % stderr |
| 395 | if background: |
| 396 | cmd += ' &' |
| 397 | system(cmd) |
| 398 | |
| 399 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 400 | class AutoservOptionParser: |
| 401 | """Custom command-line options parser for autoserv. |
| 402 | |
| 403 | We can't use the general getopt methods here, as there will be unknown |
| 404 | extra arguments that we pass down into the control file instead. |
| 405 | Thus we process the arguments by hand, for which we are duly repentant. |
| 406 | Making a single function here just makes it harder to read. Suck it up. |
| 407 | """ |
| 408 | |
| 409 | def __init__(self, args): |
| 410 | self.args = args |
| 411 | |
| 412 | |
| 413 | def parse_opts(self, flag): |
| 414 | if self.args.count(flag): |
| 415 | idx = self.args.index(flag) |
| 416 | self.args[idx : idx+1] = [] |
| 417 | return True |
| 418 | else: |
| 419 | return False |
| 420 | |
| 421 | |
| 422 | def parse_opts_param(self, flag, default = None, split = False): |
| 423 | if self.args.count(flag): |
| 424 | idx = self.args.index(flag) |
| 425 | ret = self.args[idx+1] |
| 426 | self.args[idx : idx+2] = [] |
| 427 | if split: |
| 428 | return ret.split(split) |
| 429 | else: |
| 430 | return ret |
| 431 | else: |
| 432 | return default |
mbligh | c9f342d | 2007-11-28 22:29:23 +0000 | [diff] [blame] | 433 | |
| 434 | |
| 435 | class CmdResult(object): |
| 436 | """ |
| 437 | Command execution result. |
| 438 | |
| 439 | command: String containing the command line itself |
| 440 | exit_status: Integer exit code of the process |
| 441 | stdout: String containing stdout of the process |
| 442 | stderr: String containing stderr of the process |
| 443 | duration: Elapsed wall clock time running the process |
| 444 | """ |
| 445 | |
| 446 | def __init__(self, command = None): |
| 447 | self.command = command |
| 448 | self.exit_status = None |
| 449 | self.stdout = "" |
| 450 | self.stderr = "" |
| 451 | self.duration = 0 |
| 452 | |
| 453 | |
| 454 | def __repr__(self): |
| 455 | wrapper = textwrap.TextWrapper(width = 78, |
| 456 | initial_indent="\n ", |
| 457 | subsequent_indent=" ") |
| 458 | |
| 459 | stdout = self.stdout.rstrip() |
| 460 | if stdout: |
| 461 | stdout = "\nstdout:\n%s" % stdout |
| 462 | |
| 463 | stderr = self.stderr.rstrip() |
| 464 | if stderr: |
| 465 | stderr = "\nstderr:\n%s" % stderr |
| 466 | |
| 467 | return ("* Command: %s\n" |
| 468 | "Exit status: %s\n" |
| 469 | "Duration: %s\n" |
| 470 | "%s" |
| 471 | "%s" |
| 472 | % (wrapper.fill(self.command), self.exit_status, |
| 473 | self.duration, stdout, stderr)) |