mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 3 | # Copyright 2008 Google Inc. Released under the GPL v2 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 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 | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 15 | import atexit, os, re, shutil, textwrap, sys, tempfile, types, urllib |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 16 | |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame^] | 17 | from autotest_lib.client.common_lib import utils |
mbligh | ea397bb | 2008-02-02 19:17:51 +0000 | [diff] [blame] | 18 | |
| 19 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 20 | # A dictionary of pid and a list of tmpdirs for that pid |
| 21 | __tmp_dirs = {} |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 22 | |
| 23 | |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame^] | 24 | ############# we need pass throughs for the methods in client/common_lib/utils |
| 25 | def run(command, timeout=None, ignore_status=False, |
| 26 | stdout_tee=None, stderr_tee=None): |
| 27 | return utils.run(command, timeout, ignore_status, |
| 28 | stdout_tee, stderr_tee) |
| 29 | |
| 30 | |
| 31 | def run_bg(command): |
| 32 | return utils.run_bg(command) |
| 33 | |
| 34 | |
| 35 | def join_bg_job(bg_job, timeout=None, ignore_status=False, |
| 36 | stdout_tee=None, stderr_tee=None): |
| 37 | return utils.join_bg_job(bg_job, timeout, ignore_status, |
| 38 | stdout_tee, stderr_tee) |
| 39 | |
| 40 | |
| 41 | def nuke_subprocess(subproc): |
| 42 | return utils.nuke_subprocess(subproc) |
| 43 | |
| 44 | |
| 45 | def nuke_pid(pid): |
| 46 | return utils.nuke_pid(pid) |
| 47 | |
| 48 | |
| 49 | def system(command, timeout=None, ignore_status=False): |
| 50 | return utils.system(command, timeout, ignore_status) |
| 51 | |
| 52 | |
| 53 | def system_output(command, timeout=None, ignore_status=False, |
| 54 | retain_output=False): |
| 55 | return utils.system_output(command, timeout, ignore_status, |
| 56 | retain_output) |
| 57 | |
| 58 | |
| 59 | def read_keyval(path): |
| 60 | return utils.read_keyval(path) |
| 61 | |
| 62 | |
| 63 | def write_keyval(path, dictionary): |
| 64 | return utils.write_keyval(path, dictionary) |
| 65 | |
| 66 | |
| 67 | #################################################################### |
| 68 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 69 | def sh_escape(command): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 70 | """ |
| 71 | Escape special characters from a command so that it can be passed |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 72 | as a double quoted (" ") string in a (ba)sh command. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 73 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 74 | Args: |
| 75 | command: the command string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 76 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 77 | Returns: |
| 78 | The escaped command string. The required englobing double |
| 79 | quotes are NOT added and so should be added at some point by |
| 80 | the caller. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 81 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 82 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 83 | """ |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame^] | 84 | command = command.replace("\\", "\\\\") |
| 85 | command = command.replace("$", r'\$') |
| 86 | command = command.replace('"', r'\"') |
| 87 | command = command.replace('`', r'\`') |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 88 | return command |
| 89 | |
| 90 | |
| 91 | def scp_remote_escape(filename): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 92 | """ |
| 93 | Escape special characters from a filename so that it can be passed |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 94 | to scp (within double quotes) as a remote file. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 95 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 96 | Bis-quoting has to be used with scp for remote files, "bis-quoting" |
| 97 | as in quoting x 2 |
| 98 | scp does not support a newline in the filename |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 99 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 100 | Args: |
| 101 | filename: the filename string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 102 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 103 | Returns: |
| 104 | The escaped filename string. The required englobing double |
| 105 | quotes are NOT added and so should be added at some point by |
| 106 | the caller. |
| 107 | """ |
| 108 | escape_chars= r' !"$&' "'" r'()*,:;<=>?[\]^`{|}' |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 109 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 110 | new_name= [] |
| 111 | for char in filename: |
| 112 | if char in escape_chars: |
| 113 | new_name.append("\\%s" % (char,)) |
| 114 | else: |
| 115 | new_name.append(char) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 116 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 117 | return sh_escape("".join(new_name)) |
| 118 | |
| 119 | |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 120 | def get(location, local_copy = False): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 121 | """Get a file or directory to a local temporary directory. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 122 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 123 | Args: |
| 124 | location: the source of the material to get. This source may |
| 125 | be one of: |
| 126 | * a local file or directory |
| 127 | * a URL (http or ftp) |
| 128 | * a python file-like object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 129 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 130 | Returns: |
| 131 | The location of the file or directory where the requested |
| 132 | content was saved. This will be contained in a temporary |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 133 | directory on the local host. If the material to get was a |
| 134 | directory, the location will contain a trailing '/' |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 135 | """ |
| 136 | tmpdir = get_tmp_dir() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 137 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 138 | # location is a file-like object |
| 139 | if hasattr(location, "read"): |
| 140 | tmpfile = os.path.join(tmpdir, "file") |
| 141 | tmpfileobj = file(tmpfile, 'w') |
| 142 | shutil.copyfileobj(location, tmpfileobj) |
| 143 | tmpfileobj.close() |
| 144 | return tmpfile |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 145 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 146 | if isinstance(location, types.StringTypes): |
| 147 | # location is a URL |
| 148 | if location.startswith('http') or location.startswith('ftp'): |
| 149 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 150 | urllib.urlretrieve(location, tmpfile) |
| 151 | return tmpfile |
| 152 | # location is a local path |
| 153 | elif os.path.exists(os.path.abspath(location)): |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 154 | if not local_copy: |
mbligh | 59f70aa | 2007-10-25 14:44:38 +0000 | [diff] [blame] | 155 | if os.path.isdir(location): |
| 156 | return location.rstrip('/') + '/' |
| 157 | else: |
| 158 | return location |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 159 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 160 | if os.path.isdir(location): |
| 161 | tmpfile += '/' |
| 162 | shutil.copytree(location, tmpfile, symlinks=True) |
| 163 | return tmpfile |
| 164 | shutil.copyfile(location, tmpfile) |
| 165 | return tmpfile |
| 166 | # location is just a string, dump it to a file |
| 167 | else: |
| 168 | tmpfd, tmpfile = tempfile.mkstemp(dir=tmpdir) |
| 169 | tmpfileobj = os.fdopen(tmpfd, 'w') |
| 170 | tmpfileobj.write(location) |
| 171 | tmpfileobj.close() |
| 172 | return tmpfile |
| 173 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 174 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 175 | def get_tmp_dir(): |
| 176 | """Return the pathname of a directory on the host suitable |
| 177 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 178 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 179 | The directory and its content will be deleted automatically |
| 180 | at the end of the program execution if they are still present. |
| 181 | """ |
| 182 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 183 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 184 | dir_name= tempfile.mkdtemp(prefix="autoserv-") |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 185 | pid = os.getpid() |
| 186 | if not pid in __tmp_dirs: |
| 187 | __tmp_dirs[pid] = [] |
| 188 | __tmp_dirs[pid].append(dir_name) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 189 | return dir_name |
| 190 | |
| 191 | |
| 192 | @atexit.register |
| 193 | def __clean_tmp_dirs(): |
| 194 | """Erase temporary directories that were created by the get_tmp_dir() |
| 195 | function and that are still present. |
| 196 | """ |
| 197 | global __tmp_dirs |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 198 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 199 | pid = os.getpid() |
| 200 | if pid not in __tmp_dirs: |
| 201 | return |
| 202 | for dir in __tmp_dirs[pid]: |
| 203 | try: |
| 204 | shutil.rmtree(dir) |
| 205 | except OSError, e: |
| 206 | if e.errno == 2: |
| 207 | pass |
| 208 | __tmp_dirs[pid] = [] |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 209 | |
| 210 | |
| 211 | def unarchive(host, source_material): |
| 212 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 213 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 214 | If the "source_material" is compresses (according to the file |
| 215 | extension) it will be uncompressed. Supported compression formats |
| 216 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 217 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 218 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 219 | Args: |
| 220 | host: the host object on which the archive is located |
| 221 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 222 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 223 | Returns: |
| 224 | The file or directory name of the unarchived source material. |
| 225 | If the material is a tar archive, it will be extracted in the |
| 226 | directory where it is and the path returned will be the first |
| 227 | entry in the archive, assuming it is the topmost directory. |
| 228 | If the material is not an archive, nothing will be done so this |
| 229 | function is "harmless" when it is "useless". |
| 230 | """ |
| 231 | # uncompress |
| 232 | if (source_material.endswith(".gz") or |
| 233 | source_material.endswith(".gzip")): |
| 234 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 235 | source_material= ".".join(source_material.split(".")[:-1]) |
| 236 | elif source_material.endswith("bz2"): |
| 237 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 238 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 239 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 240 | # untar |
| 241 | if source_material.endswith(".tar"): |
| 242 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 243 | sh_escape(os.path.dirname(source_material)), |
| 244 | sh_escape(source_material),)) |
| 245 | source_material= os.path.join(os.path.dirname(source_material), |
| 246 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 247 | |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 248 | return source_material |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 249 | |
| 250 | |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 251 | def get_server_dir(): |
mbligh | 8fc3b91 | 2008-05-06 20:43:02 +0000 | [diff] [blame] | 252 | path = os.path.dirname(sys.modules['autotest_lib.server.utils'].__file__) |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 253 | return os.path.abspath(path) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 254 | |
| 255 | |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 256 | def find_pid(command): |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame^] | 257 | for line in utils.system_output('ps -eo pid,cmd').rstrip().split('\n'): |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 258 | (pid, cmd) = line.split(None, 1) |
| 259 | if re.search(command, cmd): |
| 260 | return int(pid) |
| 261 | return None |
| 262 | |
| 263 | |
| 264 | def nohup(command, stdout='/dev/null', stderr='/dev/null', background=True, |
| 265 | env = {}): |
| 266 | cmd = ' '.join(key+'='+val for key, val in env.iteritems()) |
| 267 | cmd += ' nohup ' + command |
| 268 | cmd += ' > %s' % stdout |
| 269 | if stdout == stderr: |
| 270 | cmd += ' 2>&1' |
| 271 | else: |
| 272 | cmd += ' 2> %s' % stderr |
| 273 | if background: |
| 274 | cmd += ' &' |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame^] | 275 | utils.system(cmd) |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 276 | |
| 277 | |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 278 | def default_mappings(machines): |
| 279 | """ |
| 280 | Returns a simple mapping in which all machines are assigned to the |
| 281 | same key. Provides the default behavior for |
| 282 | form_ntuples_from_machines. """ |
| 283 | mappings = {} |
| 284 | failures = [] |
| 285 | |
| 286 | mach = machines[0] |
| 287 | mappings['ident'] = [mach] |
| 288 | if len(machines) > 1: |
| 289 | machines = machines[1:] |
| 290 | for machine in machines: |
| 291 | mappings['ident'].append(machine) |
| 292 | |
| 293 | return (mappings, failures) |
| 294 | |
| 295 | |
| 296 | def form_ntuples_from_machines(machines, n=2, mapping_func=default_mappings): |
| 297 | """Returns a set of ntuples from machines where the machines in an |
| 298 | ntuple are in the same mapping, and a set of failures which are |
| 299 | (machine name, reason) tuples.""" |
| 300 | ntuples = [] |
| 301 | (mappings, failures) = mapping_func(machines) |
| 302 | |
| 303 | # now run through the mappings and create n-tuples. |
| 304 | # throw out the odd guys out |
| 305 | for key in mappings: |
| 306 | key_machines = mappings[key] |
| 307 | total_machines = len(key_machines) |
| 308 | |
| 309 | # form n-tuples |
| 310 | while len(key_machines) >= n: |
| 311 | ntuples.append(key_machines[0:n]) |
| 312 | key_machines = key_machines[n:] |
| 313 | |
| 314 | for mach in key_machines: |
| 315 | failures.append((mach, "machine can not be tupled")) |
| 316 | |
| 317 | return (ntuples, failures) |
| 318 | |
| 319 | |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 320 | class AutoservOptionParser: |
| 321 | """Custom command-line options parser for autoserv. |
| 322 | |
| 323 | We can't use the general getopt methods here, as there will be unknown |
| 324 | extra arguments that we pass down into the control file instead. |
| 325 | Thus we process the arguments by hand, for which we are duly repentant. |
| 326 | Making a single function here just makes it harder to read. Suck it up. |
| 327 | """ |
| 328 | |
| 329 | def __init__(self, args): |
| 330 | self.args = args |
| 331 | |
| 332 | |
| 333 | def parse_opts(self, flag): |
| 334 | if self.args.count(flag): |
| 335 | idx = self.args.index(flag) |
| 336 | self.args[idx : idx+1] = [] |
| 337 | return True |
| 338 | else: |
| 339 | return False |
| 340 | |
| 341 | |
| 342 | def parse_opts_param(self, flag, default = None, split = False): |
| 343 | if self.args.count(flag): |
| 344 | idx = self.args.index(flag) |
| 345 | ret = self.args[idx+1] |
| 346 | self.args[idx : idx+2] = [] |
| 347 | if split: |
| 348 | return ret.split(split) |
| 349 | else: |
| 350 | return ret |
| 351 | else: |
| 352 | return default |