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 | |
| 15 | |
| 16 | import atexit |
| 17 | import os |
| 18 | import os.path |
| 19 | import select |
| 20 | import shutil |
| 21 | import signal |
| 22 | import StringIO |
| 23 | import subprocess |
| 24 | import tempfile |
| 25 | import time |
| 26 | import types |
| 27 | import urllib |
| 28 | |
| 29 | import hosts |
| 30 | import errors |
| 31 | |
| 32 | |
| 33 | __tmp_dirs= [] |
| 34 | |
| 35 | |
| 36 | def sh_escape(command): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 37 | """ |
| 38 | Escape special characters from a command so that it can be passed |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 39 | as a double quoted (" ") string in a (ba)sh command. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 40 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 41 | Args: |
| 42 | command: the command string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 43 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 44 | Returns: |
| 45 | The escaped command string. The required englobing double |
| 46 | quotes are NOT added and so should be added at some point by |
| 47 | the caller. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 48 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 49 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 50 | """ |
| 51 | command= command.replace("\\", "\\\\") |
| 52 | command= command.replace("$", r'\$') |
| 53 | command= command.replace('"', r'\"') |
| 54 | command= command.replace('`', r'\`') |
| 55 | return command |
| 56 | |
| 57 | |
| 58 | def scp_remote_escape(filename): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 59 | """ |
| 60 | Escape special characters from a filename so that it can be passed |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 61 | to scp (within double quotes) as a remote file. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 62 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 63 | Bis-quoting has to be used with scp for remote files, "bis-quoting" |
| 64 | as in quoting x 2 |
| 65 | scp does not support a newline in the filename |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 66 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 67 | Args: |
| 68 | filename: the filename string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 69 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 70 | Returns: |
| 71 | The escaped filename string. The required englobing double |
| 72 | quotes are NOT added and so should be added at some point by |
| 73 | the caller. |
| 74 | """ |
| 75 | escape_chars= r' !"$&' "'" r'()*,:;<=>?[\]^`{|}' |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 76 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 77 | new_name= [] |
| 78 | for char in filename: |
| 79 | if char in escape_chars: |
| 80 | new_name.append("\\%s" % (char,)) |
| 81 | else: |
| 82 | new_name.append(char) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 83 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 84 | return sh_escape("".join(new_name)) |
| 85 | |
| 86 | |
| 87 | def get(location): |
| 88 | """Get a file or directory to a local temporary directory. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 89 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 90 | Args: |
| 91 | location: the source of the material to get. This source may |
| 92 | be one of: |
| 93 | * a local file or directory |
| 94 | * a URL (http or ftp) |
| 95 | * a python file-like object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 96 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 97 | Returns: |
| 98 | The location of the file or directory where the requested |
| 99 | content was saved. This will be contained in a temporary |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 100 | directory on the local host. If the material to get was a |
| 101 | directory, the location will contain a trailing '/' |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 102 | """ |
| 103 | tmpdir = get_tmp_dir() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 104 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 105 | # location is a file-like object |
| 106 | if hasattr(location, "read"): |
| 107 | tmpfile = os.path.join(tmpdir, "file") |
| 108 | tmpfileobj = file(tmpfile, 'w') |
| 109 | shutil.copyfileobj(location, tmpfileobj) |
| 110 | tmpfileobj.close() |
| 111 | return tmpfile |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 112 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 113 | if isinstance(location, types.StringTypes): |
| 114 | # location is a URL |
| 115 | if location.startswith('http') or location.startswith('ftp'): |
| 116 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 117 | urllib.urlretrieve(location, tmpfile) |
| 118 | return tmpfile |
| 119 | # location is a local path |
| 120 | elif os.path.exists(os.path.abspath(location)): |
| 121 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 122 | if os.path.isdir(location): |
| 123 | tmpfile += '/' |
| 124 | shutil.copytree(location, tmpfile, symlinks=True) |
| 125 | return tmpfile |
| 126 | shutil.copyfile(location, tmpfile) |
| 127 | return tmpfile |
| 128 | # location is just a string, dump it to a file |
| 129 | else: |
| 130 | tmpfd, tmpfile = tempfile.mkstemp(dir=tmpdir) |
| 131 | tmpfileobj = os.fdopen(tmpfd, 'w') |
| 132 | tmpfileobj.write(location) |
| 133 | tmpfileobj.close() |
| 134 | return tmpfile |
| 135 | |
| 136 | |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 137 | def run(command, timeout=None, ignore_status=False): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 138 | """ |
| 139 | Run a command on the host. |
| 140 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 141 | Args: |
| 142 | command: the command line string |
| 143 | timeout: time limit in seconds before attempting to |
| 144 | kill the running process. The run() function |
| 145 | will take a few seconds longer than 'timeout' |
| 146 | to complete if it has to kill the process. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 147 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 148 | Returns: |
| 149 | a hosts.CmdResult object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 150 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 151 | Raises: |
| 152 | AutoservRunError: the exit code of the command |
| 153 | execution was not 0 |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 154 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 155 | TODO(poirier): Add a "tee" option to send the command's |
| 156 | stdout and stderr to python's stdout and stderr? At |
| 157 | the moment, there is no way to see the command's |
| 158 | output as it is running. |
| 159 | TODO(poirier): Should a timeout raise an exception? Should |
| 160 | exceptions be raised at all? |
| 161 | """ |
| 162 | result= hosts.CmdResult() |
| 163 | result.command= command |
| 164 | sp= subprocess.Popen(command, stdout=subprocess.PIPE, |
| 165 | stderr=subprocess.PIPE, close_fds=True, shell=True, |
| 166 | executable="/bin/bash") |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 167 | |
| 168 | try: |
| 169 | # We are holding ends to stdin, stdout pipes |
| 170 | # hence we need to be sure to close those fds no mater what |
| 171 | start_time= time.time() |
| 172 | if timeout: |
| 173 | stop_time= start_time + timeout |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 174 | time_left= stop_time - time.time() |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 175 | while time_left > 0: |
| 176 | # select will return when stdout is ready |
| 177 | # (including when it is EOF, that is the |
| 178 | # process has terminated). |
| 179 | (retval, tmp, tmp) = select.select( |
| 180 | [sp.stdout], [], [], time_left) |
| 181 | if len(retval): |
| 182 | # os.read() has to be used instead of |
| 183 | # sp.stdout.read() which will |
| 184 | # otherwise block |
| 185 | result.stdout += os.read( |
| 186 | sp.stdout.fileno(), 1024) |
| 187 | |
| 188 | (pid, exit_status_indication) = os.waitpid( |
| 189 | sp.pid, os.WNOHANG) |
| 190 | if pid: |
| 191 | stop_time= time.time() |
| 192 | time_left= stop_time - time.time() |
| 193 | |
| 194 | # the process has not terminated within timeout, |
| 195 | # kill it via an escalating series of signals. |
| 196 | if not pid: |
| 197 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 198 | for sig in signal_queue: |
| 199 | try: |
| 200 | os.kill(sp.pid, sig) |
| 201 | # handle race condition in which |
| 202 | # process died before we could kill it. |
| 203 | except OSError: |
| 204 | pass |
| 205 | |
| 206 | for i in range(5): |
| 207 | (pid, exit_status_indication |
| 208 | ) = os.waitpid(sp.pid, |
| 209 | os.WNOHANG) |
| 210 | if pid: |
| 211 | break |
| 212 | else: |
| 213 | time.sleep(1) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 214 | if pid: |
| 215 | break |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 216 | else: |
| 217 | exit_status_indication = os.waitpid(sp.pid, 0)[1] |
| 218 | |
| 219 | result.duration = time.time() - start_time |
| 220 | result.aborted = exit_status_indication & 127 |
| 221 | if result.aborted: |
| 222 | result.exit_status= None |
| 223 | else: |
| 224 | result.exit_status= exit_status_indication / 256 |
| 225 | result.stdout += sp.stdout.read() |
| 226 | result.stderr = sp.stderr.read() |
| 227 | |
| 228 | finally: |
| 229 | # close our ends of the pipes to the sp no matter what |
| 230 | sp.stdout.close() |
| 231 | sp.stderr.close() |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 232 | |
| 233 | if not ignore_status and result.exit_status > 0: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 234 | raise errors.AutoservRunError("command execution error", |
| 235 | result) |
mbligh | 0dd2ae0 | 2007-08-01 17:31:10 +0000 | [diff] [blame] | 236 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 237 | return result |
| 238 | |
| 239 | |
| 240 | def get_tmp_dir(): |
| 241 | """Return the pathname of a directory on the host suitable |
| 242 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 243 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 244 | The directory and its content will be deleted automatically |
| 245 | at the end of the program execution if they are still present. |
| 246 | """ |
| 247 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 248 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 249 | dir_name= tempfile.mkdtemp(prefix="autoserv-") |
| 250 | __tmp_dirs.append(dir_name) |
| 251 | return dir_name |
| 252 | |
| 253 | |
| 254 | @atexit.register |
| 255 | def __clean_tmp_dirs(): |
| 256 | """Erase temporary directories that were created by the get_tmp_dir() |
| 257 | function and that are still present. |
| 258 | """ |
| 259 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 260 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 261 | for dir in __tmp_dirs: |
| 262 | shutil.rmtree(dir) |
| 263 | __tmp_dirs= [] |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 264 | |
| 265 | |
| 266 | def unarchive(host, source_material): |
| 267 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 268 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 269 | If the "source_material" is compresses (according to the file |
| 270 | extension) it will be uncompressed. Supported compression formats |
| 271 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 272 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 273 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 274 | Args: |
| 275 | host: the host object on which the archive is located |
| 276 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 277 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 278 | Returns: |
| 279 | The file or directory name of the unarchived source material. |
| 280 | If the material is a tar archive, it will be extracted in the |
| 281 | directory where it is and the path returned will be the first |
| 282 | entry in the archive, assuming it is the topmost directory. |
| 283 | If the material is not an archive, nothing will be done so this |
| 284 | function is "harmless" when it is "useless". |
| 285 | """ |
| 286 | # uncompress |
| 287 | if (source_material.endswith(".gz") or |
| 288 | source_material.endswith(".gzip")): |
| 289 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 290 | source_material= ".".join(source_material.split(".")[:-1]) |
| 291 | elif source_material.endswith("bz2"): |
| 292 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 293 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 294 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 295 | # untar |
| 296 | if source_material.endswith(".tar"): |
| 297 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 298 | sh_escape(os.path.dirname(source_material)), |
| 299 | sh_escape(source_material),)) |
| 300 | source_material= os.path.join(os.path.dirname(source_material), |
| 301 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 302 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 303 | return source_material |