mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. Released under the GPL v2 |
| 3 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 4 | import os, pickle, random, re, resource, select, shutil, signal, StringIO |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 5 | import socket, struct, subprocess, sys, time, textwrap, urlparse |
mbligh | 25284cd | 2009-06-08 16:17:24 +0000 | [diff] [blame] | 6 | import warnings, smtplib, logging, urllib2 |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import error, barrier, logging_manager |
mbligh | 81edd79 | 2008-08-26 16:54:02 +0000 | [diff] [blame] | 8 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 9 | def deprecated(func): |
| 10 | """This is a decorator which can be used to mark functions as deprecated. |
| 11 | It will result in a warning being emmitted when the function is used.""" |
| 12 | def new_func(*args, **dargs): |
| 13 | warnings.warn("Call to deprecated function %s." % func.__name__, |
| 14 | category=DeprecationWarning) |
| 15 | return func(*args, **dargs) |
| 16 | new_func.__name__ = func.__name__ |
| 17 | new_func.__doc__ = func.__doc__ |
| 18 | new_func.__dict__.update(func.__dict__) |
| 19 | return new_func |
| 20 | |
| 21 | |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 22 | class _NullStream(object): |
| 23 | def write(self, data): |
| 24 | pass |
| 25 | |
| 26 | |
| 27 | def flush(self): |
| 28 | pass |
| 29 | |
| 30 | |
| 31 | TEE_TO_LOGS = object() |
| 32 | _the_null_stream = _NullStream() |
| 33 | |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 34 | DEFAULT_STDOUT_LEVEL = logging.DEBUG |
| 35 | DEFAULT_STDERR_LEVEL = logging.ERROR |
| 36 | |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 37 | def get_stream_tee_file(stream, level): |
| 38 | if stream is None: |
| 39 | return _the_null_stream |
| 40 | if stream is TEE_TO_LOGS: |
| 41 | return logging_manager.LoggingFile(level=level) |
| 42 | return stream |
| 43 | |
| 44 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 45 | class BgJob(object): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 46 | def __init__(self, command, stdout_tee=None, stderr_tee=None, verbose=True, |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 47 | stdin=None, stderr_level=DEFAULT_STDERR_LEVEL): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 48 | self.command = command |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 49 | self.stdout_tee = get_stream_tee_file(stdout_tee, DEFAULT_STDOUT_LEVEL) |
| 50 | self.stderr_tee = get_stream_tee_file(stderr_tee, stderr_level) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 51 | self.result = CmdResult(command) |
mbligh | bd96b45 | 2008-09-03 23:14:27 +0000 | [diff] [blame] | 52 | if verbose: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 53 | logging.debug("Running '%s'" % command) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 54 | self.sp = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 55 | stderr=subprocess.PIPE, |
| 56 | preexec_fn=self._reset_sigpipe, shell=True, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 57 | executable="/bin/bash", |
| 58 | stdin=stdin) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def output_prepare(self, stdout_file=None, stderr_file=None): |
| 62 | self.stdout_file = stdout_file |
| 63 | self.stderr_file = stderr_file |
| 64 | |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 65 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 66 | def process_output(self, stdout=True, final_read=False): |
| 67 | """output_prepare must be called prior to calling this""" |
| 68 | if stdout: |
| 69 | pipe, buf, tee = self.sp.stdout, self.stdout_file, self.stdout_tee |
| 70 | else: |
| 71 | pipe, buf, tee = self.sp.stderr, self.stderr_file, self.stderr_tee |
| 72 | |
| 73 | if final_read: |
| 74 | # read in all the data we can from pipe and then stop |
| 75 | data = [] |
| 76 | while select.select([pipe], [], [], 0)[0]: |
| 77 | data.append(os.read(pipe.fileno(), 1024)) |
| 78 | if len(data[-1]) == 0: |
| 79 | break |
| 80 | data = "".join(data) |
| 81 | else: |
| 82 | # perform a single read |
| 83 | data = os.read(pipe.fileno(), 1024) |
| 84 | buf.write(data) |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 85 | tee.write(data) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def cleanup(self): |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 89 | self.stdout_tee.flush() |
| 90 | self.stderr_tee.flush() |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 91 | self.sp.stdout.close() |
| 92 | self.sp.stderr.close() |
| 93 | self.result.stdout = self.stdout_file.getvalue() |
| 94 | self.result.stderr = self.stderr_file.getvalue() |
| 95 | |
| 96 | |
| 97 | def _reset_sigpipe(self): |
| 98 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 99 | |
mbligh | 81edd79 | 2008-08-26 16:54:02 +0000 | [diff] [blame] | 100 | |
| 101 | def ip_to_long(ip): |
| 102 | # !L is a long in network byte order |
| 103 | return struct.unpack('!L', socket.inet_aton(ip))[0] |
| 104 | |
| 105 | |
| 106 | def long_to_ip(number): |
| 107 | # See above comment. |
| 108 | return socket.inet_ntoa(struct.pack('!L', number)) |
| 109 | |
| 110 | |
| 111 | def create_subnet_mask(bits): |
mbligh | 81edd79 | 2008-08-26 16:54:02 +0000 | [diff] [blame] | 112 | return (1 << 32) - (1 << 32-bits) |
| 113 | |
| 114 | |
| 115 | def format_ip_with_mask(ip, mask_bits): |
| 116 | masked_ip = ip_to_long(ip) & create_subnet_mask(mask_bits) |
| 117 | return "%s/%s" % (long_to_ip(masked_ip), mask_bits) |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 118 | |
mbligh | de0d47e | 2008-03-28 14:37:18 +0000 | [diff] [blame] | 119 | |
jadmanski | e80d471 | 2008-10-03 16:15:59 +0000 | [diff] [blame] | 120 | def normalize_hostname(alias): |
| 121 | ip = socket.gethostbyname(alias) |
| 122 | return socket.gethostbyaddr(ip)[0] |
| 123 | |
| 124 | |
mbligh | d6d043c | 2008-09-27 21:00:45 +0000 | [diff] [blame] | 125 | def get_ip_local_port_range(): |
| 126 | match = re.match(r'\s*(\d+)\s*(\d+)\s*$', |
| 127 | read_one_line('/proc/sys/net/ipv4/ip_local_port_range')) |
| 128 | return (int(match.group(1)), int(match.group(2))) |
| 129 | |
| 130 | |
| 131 | def set_ip_local_port_range(lower, upper): |
| 132 | write_one_line('/proc/sys/net/ipv4/ip_local_port_range', |
| 133 | '%d %d\n' % (lower, upper)) |
| 134 | |
mbligh | 315b941 | 2008-10-01 03:34:11 +0000 | [diff] [blame] | 135 | |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 136 | |
| 137 | def send_email(mail_from, mail_to, subject, body): |
| 138 | """ |
| 139 | Sends an email via smtp |
| 140 | |
| 141 | mail_from: string with email address of sender |
| 142 | mail_to: string or list with email address(es) of recipients |
| 143 | subject: string with subject of email |
| 144 | body: (multi-line) string with body of email |
| 145 | """ |
| 146 | if isinstance(mail_to, str): |
| 147 | mail_to = [mail_to] |
| 148 | msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (mail_from, ','.join(mail_to), |
| 149 | subject, body) |
| 150 | try: |
| 151 | mailer = smtplib.SMTP('localhost') |
| 152 | try: |
| 153 | mailer.sendmail(mail_from, mail_to, msg) |
| 154 | finally: |
| 155 | mailer.quit() |
| 156 | except Exception, e: |
| 157 | # Emails are non-critical, not errors, but don't raise them |
| 158 | print "Sending email failed. Reason: %s" % repr(e) |
| 159 | |
| 160 | |
jadmanski | 5182e16 | 2008-05-13 21:48:16 +0000 | [diff] [blame] | 161 | def read_one_line(filename): |
mbligh | 6e8840c | 2008-07-11 18:05:49 +0000 | [diff] [blame] | 162 | return open(filename, 'r').readline().rstrip('\n') |
jadmanski | 5182e16 | 2008-05-13 21:48:16 +0000 | [diff] [blame] | 163 | |
| 164 | |
mbligh | b9d0551 | 2008-10-18 13:53:27 +0000 | [diff] [blame] | 165 | def write_one_line(filename, line): |
| 166 | open_write_close(filename, line.rstrip('\n') + '\n') |
| 167 | |
| 168 | |
| 169 | def open_write_close(filename, data): |
mbligh | 618ac9e | 2008-10-06 17:14:32 +0000 | [diff] [blame] | 170 | f = open(filename, 'w') |
mbligh | b9d0551 | 2008-10-18 13:53:27 +0000 | [diff] [blame] | 171 | try: |
| 172 | f.write(data) |
| 173 | finally: |
| 174 | f.close() |
jadmanski | 5182e16 | 2008-05-13 21:48:16 +0000 | [diff] [blame] | 175 | |
| 176 | |
mbligh | de0d47e | 2008-03-28 14:37:18 +0000 | [diff] [blame] | 177 | def read_keyval(path): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 178 | """ |
| 179 | Read a key-value pair format file into a dictionary, and return it. |
| 180 | Takes either a filename or directory name as input. If it's a |
| 181 | directory name, we assume you want the file to be called keyval. |
| 182 | """ |
| 183 | if os.path.isdir(path): |
| 184 | path = os.path.join(path, 'keyval') |
| 185 | keyval = {} |
jadmanski | 5896298 | 2009-04-21 19:54:34 +0000 | [diff] [blame] | 186 | if os.path.exists(path): |
| 187 | for line in open(path): |
| 188 | line = re.sub('#.*', '', line).rstrip() |
| 189 | if not re.search(r'^[-\.\w]+=', line): |
| 190 | raise ValueError('Invalid format line: %s' % line) |
| 191 | key, value = line.split('=', 1) |
| 192 | if re.search('^\d+$', value): |
| 193 | value = int(value) |
| 194 | elif re.search('^(\d+\.)?\d+$', value): |
| 195 | value = float(value) |
| 196 | keyval[key] = value |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 197 | return keyval |
mbligh | de0d47e | 2008-03-28 14:37:18 +0000 | [diff] [blame] | 198 | |
| 199 | |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 200 | def write_keyval(path, dictionary, type_tag=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 201 | """ |
| 202 | Write a key-value pair format file out to a file. This uses append |
| 203 | mode to open the file, so existing text will not be overwritten or |
| 204 | reparsed. |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 205 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 206 | If type_tag is None, then the key must be composed of alphanumeric |
| 207 | characters (or dashes+underscores). However, if type-tag is not |
| 208 | null then the keys must also have "{type_tag}" as a suffix. At |
| 209 | the moment the only valid values of type_tag are "attr" and "perf". |
| 210 | """ |
| 211 | if os.path.isdir(path): |
| 212 | path = os.path.join(path, 'keyval') |
| 213 | keyval = open(path, 'a') |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 214 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 215 | if type_tag is None: |
mbligh | 97227ea | 2009-03-11 17:09:50 +0000 | [diff] [blame] | 216 | key_regex = re.compile(r'^[-\.\w]+$') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 217 | else: |
| 218 | if type_tag not in ('attr', 'perf'): |
| 219 | raise ValueError('Invalid type tag: %s' % type_tag) |
| 220 | escaped_tag = re.escape(type_tag) |
mbligh | 97227ea | 2009-03-11 17:09:50 +0000 | [diff] [blame] | 221 | key_regex = re.compile(r'^[-\.\w]+\{%s\}$' % escaped_tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 222 | try: |
mbligh | 6955e23 | 2009-07-11 00:58:47 +0000 | [diff] [blame] | 223 | for key in sorted(dictionary.keys()): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 224 | if not key_regex.search(key): |
| 225 | raise ValueError('Invalid key: %s' % key) |
mbligh | 6955e23 | 2009-07-11 00:58:47 +0000 | [diff] [blame] | 226 | keyval.write('%s=%s\n' % (key, dictionary[key])) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 227 | finally: |
| 228 | keyval.close() |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 229 | |
| 230 | |
| 231 | def is_url(path): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 232 | """Return true if path looks like a URL""" |
| 233 | # for now, just handle http and ftp |
| 234 | url_parts = urlparse.urlparse(path) |
| 235 | return (url_parts[0] in ('http', 'ftp')) |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 236 | |
| 237 | |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 238 | def urlopen(url, data=None, timeout=5): |
| 239 | """Wrapper to urllib2.urlopen with timeout addition.""" |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 240 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 241 | # Save old timeout |
| 242 | old_timeout = socket.getdefaulttimeout() |
| 243 | socket.setdefaulttimeout(timeout) |
| 244 | try: |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 245 | return urllib2.urlopen(url, data=data) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 246 | finally: |
| 247 | socket.setdefaulttimeout(old_timeout) |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 248 | |
| 249 | |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 250 | def urlretrieve(url, filename, data=None, timeout=300): |
| 251 | """Retrieve a file from given url.""" |
| 252 | logging.debug('Fetching %s -> %s', url, filename) |
| 253 | |
| 254 | src_file = urlopen(url, data=data, timeout=timeout) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 255 | try: |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 256 | dest_file = open(filename, 'wb') |
| 257 | try: |
| 258 | shutil.copyfileobj(src_file, dest_file) |
| 259 | finally: |
| 260 | dest_file.close() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 261 | finally: |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 262 | src_file.close() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 263 | |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 264 | |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 265 | def get_file(src, dest, permissions=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 266 | """Get a file from src, which can be local or a remote URL""" |
mbligh | 25284cd | 2009-06-08 16:17:24 +0000 | [diff] [blame] | 267 | if src == dest: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 268 | return |
mbligh | 25284cd | 2009-06-08 16:17:24 +0000 | [diff] [blame] | 269 | |
| 270 | if is_url(src): |
mbligh | b289619 | 2009-07-11 00:12:37 +0000 | [diff] [blame] | 271 | urlretrieve(src, dest) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 272 | else: |
| 273 | shutil.copyfile(src, dest) |
mbligh | 25284cd | 2009-06-08 16:17:24 +0000 | [diff] [blame] | 274 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 275 | if permissions: |
| 276 | os.chmod(dest, permissions) |
| 277 | return dest |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 278 | |
| 279 | |
| 280 | def unmap_url(srcdir, src, destdir='.'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 281 | """ |
| 282 | Receives either a path to a local file or a URL. |
| 283 | returns either the path to the local file, or the fetched URL |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 284 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 285 | unmap_url('/usr/src', 'foo.tar', '/tmp') |
| 286 | = '/usr/src/foo.tar' |
| 287 | unmap_url('/usr/src', 'http://site/file', '/tmp') |
| 288 | = '/tmp/file' |
| 289 | (after retrieving it) |
| 290 | """ |
| 291 | if is_url(src): |
| 292 | url_parts = urlparse.urlparse(src) |
| 293 | filename = os.path.basename(url_parts[2]) |
| 294 | dest = os.path.join(destdir, filename) |
| 295 | return get_file(src, dest) |
| 296 | else: |
| 297 | return os.path.join(srcdir, src) |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 298 | |
| 299 | |
| 300 | def update_version(srcdir, preserve_srcdir, new_version, install, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 301 | *args, **dargs): |
| 302 | """ |
| 303 | Make sure srcdir is version new_version |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 304 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 305 | If not, delete it and install() the new version. |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 306 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 307 | In the preserve_srcdir case, we just check it's up to date, |
| 308 | and if not, we rerun install, without removing srcdir |
| 309 | """ |
| 310 | versionfile = os.path.join(srcdir, '.version') |
| 311 | install_needed = True |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 312 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 313 | if os.path.exists(versionfile): |
| 314 | old_version = pickle.load(open(versionfile)) |
| 315 | if old_version == new_version: |
| 316 | install_needed = False |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 317 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 318 | if install_needed: |
| 319 | if not preserve_srcdir and os.path.exists(srcdir): |
| 320 | shutil.rmtree(srcdir) |
| 321 | install(*args, **dargs) |
| 322 | if os.path.exists(srcdir): |
| 323 | pickle.dump(new_version, open(versionfile, 'w')) |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 324 | |
| 325 | |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 326 | def get_stderr_level(stderr_is_expected): |
| 327 | if stderr_is_expected: |
| 328 | return DEFAULT_STDOUT_LEVEL |
| 329 | return DEFAULT_STDERR_LEVEL |
| 330 | |
| 331 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 332 | def run(command, timeout=None, ignore_status=False, |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 333 | stdout_tee=None, stderr_tee=None, verbose=True, stdin=None, |
| 334 | stderr_is_expected=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 335 | """ |
| 336 | Run a command on the host. |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 337 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 338 | Args: |
| 339 | command: the command line string |
| 340 | timeout: time limit in seconds before attempting to |
| 341 | kill the running process. The run() function |
| 342 | will take a few seconds longer than 'timeout' |
| 343 | to complete if it has to kill the process. |
| 344 | ignore_status: do not raise an exception, no matter what |
| 345 | the exit code of the command is. |
| 346 | stdout_tee: optional file-like object to which stdout data |
| 347 | will be written as it is generated (data will still |
| 348 | be stored in result.stdout) |
| 349 | stderr_tee: likewise for stderr |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 350 | verbose: if True, log the command being run |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 351 | stdin: stdin to pass to the executed process |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 352 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 353 | Returns: |
| 354 | a CmdResult object |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 355 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 356 | Raises: |
| 357 | CmdError: the exit code of the command |
| 358 | execution was not 0 |
| 359 | """ |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 360 | if stderr_is_expected is None: |
| 361 | stderr_is_expected = ignore_status |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 362 | bg_job = join_bg_jobs( |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 363 | (BgJob(command, stdout_tee, stderr_tee, verbose, stdin=stdin, |
| 364 | stderr_level=get_stderr_level(stderr_is_expected)),), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 365 | timeout)[0] |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 366 | if not ignore_status and bg_job.result.exit_status: |
jadmanski | 9c1098b | 2008-09-02 14:18:48 +0000 | [diff] [blame] | 367 | raise error.CmdError(command, bg_job.result, |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 368 | "Command returned non-zero exit status") |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 369 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 370 | return bg_job.result |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 371 | |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 372 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 373 | def run_parallel(commands, timeout=None, ignore_status=False, |
| 374 | stdout_tee=None, stderr_tee=None): |
| 375 | """Beahves the same as run with the following exceptions: |
| 376 | |
| 377 | - commands is a list of commands to run in parallel. |
| 378 | - ignore_status toggles whether or not an exception should be raised |
| 379 | on any error. |
| 380 | |
| 381 | returns a list of CmdResult objects |
| 382 | """ |
| 383 | bg_jobs = [] |
| 384 | for command in commands: |
showard | b45a466 | 2009-07-15 14:27:56 +0000 | [diff] [blame] | 385 | bg_jobs.append(BgJob(command, stdout_tee, stderr_tee, |
| 386 | stderr_level=get_stderr_level(ignore_status))) |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 387 | |
| 388 | # Updates objects in bg_jobs list with their process information |
| 389 | join_bg_jobs(bg_jobs, timeout) |
| 390 | |
| 391 | for bg_job in bg_jobs: |
| 392 | if not ignore_status and bg_job.result.exit_status: |
| 393 | raise error.CmdError(command, bg_job.result, |
| 394 | "Command returned non-zero exit status") |
| 395 | |
| 396 | return [bg_job.result for bg_job in bg_jobs] |
| 397 | |
| 398 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 399 | @deprecated |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 400 | def run_bg(command): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 401 | """Function deprecated. Please use BgJob class instead.""" |
| 402 | bg_job = BgJob(command) |
| 403 | return bg_job.sp, bg_job.result |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 404 | |
| 405 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 406 | def join_bg_jobs(bg_jobs, timeout=None): |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 407 | """Joins the bg_jobs with the current thread. |
| 408 | |
| 409 | Returns the same list of bg_jobs objects that was passed in. |
| 410 | """ |
mbligh | ae69f26 | 2009-04-17 20:14:56 +0000 | [diff] [blame] | 411 | ret, timeout_error = 0, False |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 412 | for bg_job in bg_jobs: |
| 413 | bg_job.output_prepare(StringIO.StringIO(), StringIO.StringIO()) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 414 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 415 | try: |
| 416 | # We are holding ends to stdin, stdout pipes |
| 417 | # hence we need to be sure to close those fds no mater what |
| 418 | start_time = time.time() |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 419 | timeout_error = _wait_for_commands(bg_jobs, start_time, timeout) |
| 420 | |
| 421 | for bg_job in bg_jobs: |
| 422 | # Process stdout and stderr |
| 423 | bg_job.process_output(stdout=True,final_read=True) |
| 424 | bg_job.process_output(stdout=False,final_read=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 425 | finally: |
| 426 | # close our ends of the pipes to the sp no matter what |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 427 | for bg_job in bg_jobs: |
| 428 | bg_job.cleanup() |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 429 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 430 | if timeout_error: |
| 431 | # TODO: This needs to be fixed to better represent what happens when |
| 432 | # running in parallel. However this is backwards compatable, so it will |
| 433 | # do for the time being. |
| 434 | raise error.CmdError(bg_jobs[0].command, bg_jobs[0].result, |
| 435 | "Command(s) did not complete within %d seconds" |
| 436 | % timeout) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 437 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 438 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 439 | return bg_jobs |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 440 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 441 | |
| 442 | def _wait_for_commands(bg_jobs, start_time, timeout): |
| 443 | # This returns True if it must return due to a timeout, otherwise False. |
| 444 | |
mbligh | f0b4a0a | 2008-09-03 20:46:16 +0000 | [diff] [blame] | 445 | # To check for processes which terminate without producing any output |
| 446 | # a 1 second timeout is used in select. |
| 447 | SELECT_TIMEOUT = 1 |
| 448 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 449 | select_list = [] |
| 450 | reverse_dict = {} |
| 451 | for bg_job in bg_jobs: |
| 452 | select_list.append(bg_job.sp.stdout) |
| 453 | select_list.append(bg_job.sp.stderr) |
| 454 | reverse_dict[bg_job.sp.stdout] = (bg_job,True) |
| 455 | reverse_dict[bg_job.sp.stderr] = (bg_job,False) |
| 456 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 457 | if timeout: |
| 458 | stop_time = start_time + timeout |
| 459 | time_left = stop_time - time.time() |
| 460 | else: |
| 461 | time_left = None # so that select never times out |
| 462 | while not timeout or time_left > 0: |
| 463 | # select will return when stdout is ready (including when it is |
| 464 | # EOF, that is the process has terminated). |
mbligh | f0b4a0a | 2008-09-03 20:46:16 +0000 | [diff] [blame] | 465 | ready, _, _ = select.select(select_list, [], [], SELECT_TIMEOUT) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 466 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 467 | # os.read() has to be used instead of |
| 468 | # subproc.stdout.read() which will otherwise block |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 469 | for fileno in ready: |
| 470 | bg_job,stdout = reverse_dict[fileno] |
| 471 | bg_job.process_output(stdout) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 472 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 473 | remaining_jobs = [x for x in bg_jobs if x.result.exit_status is None] |
| 474 | if len(remaining_jobs) == 0: |
| 475 | return False |
| 476 | for bg_job in remaining_jobs: |
| 477 | bg_job.result.exit_status = bg_job.sp.poll() |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 478 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 479 | if timeout: |
| 480 | time_left = stop_time - time.time() |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 481 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 482 | # Kill all processes which did not complete prior to timeout |
| 483 | for bg_job in [x for x in bg_jobs if x.result.exit_status is None]: |
mbligh | 7afc3a6 | 2008-11-27 00:35:44 +0000 | [diff] [blame] | 484 | print '* Warning: run process timeout (%s) fired' % timeout |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 485 | nuke_subprocess(bg_job.sp) |
mbligh | 095dc64 | 2008-10-01 03:41:35 +0000 | [diff] [blame] | 486 | bg_job.result.exit_status = bg_job.sp.poll() |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 487 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 488 | return True |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 489 | |
| 490 | |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 491 | def pid_is_alive(pid): |
| 492 | """ |
| 493 | True if process pid exists and is not yet stuck in Zombie state. |
| 494 | Zombies are impossible to move between cgroups, etc. |
| 495 | pid can be integer, or text of integer. |
| 496 | """ |
| 497 | path = '/proc/%s/stat' % pid |
| 498 | |
| 499 | try: |
| 500 | stat = read_one_line(path) |
| 501 | except IOError: |
| 502 | if not os.path.exists(path): |
| 503 | # file went away |
| 504 | return False |
| 505 | raise |
| 506 | |
| 507 | return stat.split()[2] != 'Z' |
| 508 | |
| 509 | |
| 510 | def signal_pid(pid, sig): |
| 511 | """ |
| 512 | Sends a signal to a process id. Returns True if the process terminated |
| 513 | successfully, False otherwise. |
| 514 | """ |
| 515 | try: |
| 516 | os.kill(pid, sig) |
| 517 | except OSError: |
| 518 | # The process may have died before we could kill it. |
| 519 | pass |
| 520 | |
| 521 | for i in range(5): |
| 522 | if not pid_is_alive(pid): |
| 523 | return True |
| 524 | time.sleep(1) |
| 525 | |
| 526 | # The process is still alive |
| 527 | return False |
| 528 | |
| 529 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 530 | def nuke_subprocess(subproc): |
jadmanski | 09f9203 | 2008-09-17 14:05:27 +0000 | [diff] [blame] | 531 | # check if the subprocess is still alive, first |
| 532 | if subproc.poll() is not None: |
| 533 | return subproc.poll() |
| 534 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 535 | # the process has not terminated within timeout, |
| 536 | # kill it via an escalating series of signals. |
| 537 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 538 | for sig in signal_queue: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 539 | signal_pid(subproc.pid, sig) |
| 540 | if subproc.poll() is not None: |
| 541 | return subproc.poll() |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 542 | |
| 543 | |
| 544 | def nuke_pid(pid): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 545 | # the process has not terminated within timeout, |
| 546 | # kill it via an escalating series of signals. |
| 547 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 548 | for sig in signal_queue: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 549 | if signal_pid(pid, sig): |
| 550 | return |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 551 | |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 552 | # no signal successfully terminated the process |
| 553 | raise error.AutoservRunError('Could not kill %d' % pid, None) |
mbligh | 298ed59 | 2009-06-15 21:52:57 +0000 | [diff] [blame] | 554 | |
| 555 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 556 | def system(command, timeout=None, ignore_status=False): |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 557 | """This function returns the exit status of command.""" |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 558 | return run(command, timeout=timeout, ignore_status=ignore_status, |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 559 | stdout_tee=TEE_TO_LOGS, stderr_tee=TEE_TO_LOGS).exit_status |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 560 | |
| 561 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 562 | def system_parallel(commands, timeout=None, ignore_status=False): |
| 563 | """This function returns a list of exit statuses for the respective |
| 564 | list of commands.""" |
| 565 | return [bg_jobs.exit_status for bg_jobs in |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 566 | run_parallel(commands, timeout=timeout, ignore_status=ignore_status, |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 567 | stdout_tee=TEE_TO_LOGS, stderr_tee=TEE_TO_LOGS)] |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 568 | |
| 569 | |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 570 | def system_output(command, timeout=None, ignore_status=False, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 571 | retain_output=False): |
| 572 | if retain_output: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 573 | out = run(command, timeout=timeout, ignore_status=ignore_status, |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 574 | stdout_tee=TEE_TO_LOGS, stderr_tee=TEE_TO_LOGS).stdout |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 575 | else: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 576 | out = run(command, timeout=timeout, ignore_status=ignore_status).stdout |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 577 | if out[-1:] == '\n': out = out[:-1] |
| 578 | return out |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 579 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 580 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 581 | def system_output_parallel(commands, timeout=None, ignore_status=False, |
| 582 | retain_output=False): |
| 583 | if retain_output: |
showard | 108d73e | 2009-06-22 18:14:41 +0000 | [diff] [blame] | 584 | out = [bg_job.stdout for bg_job |
| 585 | in run_parallel(commands, timeout=timeout, |
| 586 | ignore_status=ignore_status, |
| 587 | stdout_tee=TEE_TO_LOGS, stderr_tee=TEE_TO_LOGS)] |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 588 | else: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 589 | out = [bg_job.stdout for bg_job in run_parallel(commands, |
| 590 | timeout=timeout, ignore_status=ignore_status)] |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 591 | for x in out: |
| 592 | if out[-1:] == '\n': out = out[:-1] |
| 593 | return out |
| 594 | |
| 595 | |
mbligh | 9846795 | 2008-11-19 00:25:45 +0000 | [diff] [blame] | 596 | def strip_unicode(input): |
| 597 | if type(input) == list: |
| 598 | return [strip_unicode(i) for i in input] |
| 599 | elif type(input) == dict: |
| 600 | output = {} |
| 601 | for key in input.keys(): |
| 602 | output[str(key)] = strip_unicode(input[key]) |
| 603 | return output |
| 604 | elif type(input) == unicode: |
| 605 | return str(input) |
| 606 | else: |
| 607 | return input |
| 608 | |
| 609 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 610 | def get_cpu_percentage(function, *args, **dargs): |
| 611 | """Returns a tuple containing the CPU% and return value from function call. |
| 612 | |
| 613 | This function calculates the usage time by taking the difference of |
| 614 | the user and system times both before and after the function call. |
| 615 | """ |
| 616 | child_pre = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 617 | self_pre = resource.getrusage(resource.RUSAGE_SELF) |
| 618 | start = time.time() |
| 619 | to_return = function(*args, **dargs) |
| 620 | elapsed = time.time() - start |
| 621 | self_post = resource.getrusage(resource.RUSAGE_SELF) |
| 622 | child_post = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 623 | |
| 624 | # Calculate CPU Percentage |
| 625 | s_user, s_system = [a - b for a, b in zip(self_post, self_pre)[:2]] |
| 626 | c_user, c_system = [a - b for a, b in zip(child_post, child_pre)[:2]] |
| 627 | cpu_percent = (s_user + c_user + s_system + c_system) / elapsed |
| 628 | |
| 629 | return cpu_percent, to_return |
| 630 | |
| 631 | |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 632 | """ |
| 633 | This function is used when there is a need to run more than one |
| 634 | job simultaneously starting exactly at the same time. It basically returns |
| 635 | a modified control file (containing the synchronization code prepended) |
| 636 | whenever it is ready to run the control file. The synchronization |
| 637 | is done using barriers to make sure that the jobs start at the same time. |
| 638 | |
| 639 | Here is how the synchronization is done to make sure that the tests |
| 640 | start at exactly the same time on the client. |
| 641 | sc_bar is a server barrier and s_bar, c_bar are the normal barriers |
| 642 | |
| 643 | Job1 Job2 ...... JobN |
| 644 | Server: | sc_bar |
| 645 | Server: | s_bar ...... s_bar |
| 646 | Server: | at.run() at.run() ...... at.run() |
| 647 | ----------|------------------------------------------------------ |
| 648 | Client | sc_bar |
| 649 | Client | c_bar c_bar ...... c_bar |
| 650 | Client | <run test> <run test> ...... <run test> |
| 651 | |
| 652 | |
| 653 | PARAMS: |
| 654 | control_file : The control file which to which the above synchronization |
| 655 | code would be prepended to |
| 656 | host_name : The host name on which the job is going to run |
| 657 | host_num (non negative) : A number to identify the machine so that we have |
| 658 | different sets of s_bar_ports for each of the machines. |
| 659 | instance : The number of the job |
| 660 | num_jobs : Total number of jobs that are going to run in parallel with |
| 661 | this job starting at the same time |
| 662 | port_base : Port number that is used to derive the actual barrier ports. |
| 663 | |
| 664 | RETURN VALUE: |
| 665 | The modified control file. |
| 666 | |
| 667 | """ |
| 668 | def get_sync_control_file(control, host_name, host_num, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 669 | instance, num_jobs, port_base=63100): |
| 670 | sc_bar_port = port_base |
| 671 | c_bar_port = port_base |
| 672 | if host_num < 0: |
| 673 | print "Please provide a non negative number for the host" |
| 674 | return None |
| 675 | s_bar_port = port_base + 1 + host_num # The set of s_bar_ports are |
| 676 | # the same for a given machine |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 677 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 678 | sc_bar_timeout = 180 |
| 679 | s_bar_timeout = c_bar_timeout = 120 |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 680 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 681 | # The barrier code snippet is prepended into the conrol file |
| 682 | # dynamically before at.run() is called finally. |
| 683 | control_new = [] |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 684 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 685 | # jobid is the unique name used to identify the processes |
| 686 | # trying to reach the barriers |
| 687 | jobid = "%s#%d" % (host_name, instance) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 688 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 689 | rendv = [] |
| 690 | # rendvstr is a temp holder for the rendezvous list of the processes |
| 691 | for n in range(num_jobs): |
| 692 | rendv.append("'%s#%d'" % (host_name, n)) |
| 693 | rendvstr = ",".join(rendv) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 694 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 695 | if instance == 0: |
| 696 | # Do the setup and wait at the server barrier |
| 697 | # Clean up the tmp and the control dirs for the first instance |
| 698 | control_new.append('if os.path.exists(job.tmpdir):') |
| 699 | control_new.append("\t system('umount -f %s > /dev/null" |
| 700 | "2> /dev/null' % job.tmpdir," |
| 701 | "ignore_status=True)") |
| 702 | control_new.append("\t system('rm -rf ' + job.tmpdir)") |
| 703 | control_new.append( |
| 704 | 'b0 = job.barrier("%s", "sc_bar", %d, port=%d)' |
| 705 | % (jobid, sc_bar_timeout, sc_bar_port)) |
| 706 | control_new.append( |
mbligh | 9c12f77 | 2009-06-22 19:03:55 +0000 | [diff] [blame] | 707 | 'b0.rendezvous_servers("PARALLEL_MASTER", "%s")' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 708 | % jobid) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 709 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 710 | elif instance == 1: |
| 711 | # Wait at the server barrier to wait for instance=0 |
| 712 | # process to complete setup |
| 713 | b0 = barrier.barrier("PARALLEL_MASTER", "sc_bar", sc_bar_timeout, |
| 714 | port=sc_bar_port) |
mbligh | 9c12f77 | 2009-06-22 19:03:55 +0000 | [diff] [blame] | 715 | b0.rendezvous_servers("PARALLEL_MASTER", jobid) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 716 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 717 | if(num_jobs > 2): |
| 718 | b1 = barrier.barrier(jobid, "s_bar", s_bar_timeout, |
| 719 | port=s_bar_port) |
mbligh | 9c12f77 | 2009-06-22 19:03:55 +0000 | [diff] [blame] | 720 | b1.rendezvous(rendvstr) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 721 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 722 | else: |
| 723 | # For the rest of the clients |
| 724 | b2 = barrier.barrier(jobid, "s_bar", s_bar_timeout, port=s_bar_port) |
mbligh | 9c12f77 | 2009-06-22 19:03:55 +0000 | [diff] [blame] | 725 | b2.rendezvous(rendvstr) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 726 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 727 | # Client side barrier for all the tests to start at the same time |
| 728 | control_new.append('b1 = job.barrier("%s", "c_bar", %d, port=%d)' |
| 729 | % (jobid, c_bar_timeout, c_bar_port)) |
mbligh | 9c12f77 | 2009-06-22 19:03:55 +0000 | [diff] [blame] | 730 | control_new.append("b1.rendezvous(%s)" % rendvstr) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 731 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 732 | # Stick in the rest of the control file |
| 733 | control_new.append(control) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 734 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 735 | return "\n".join(control_new) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 736 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 737 | |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 738 | def get_arch(run_function=run): |
| 739 | """ |
| 740 | Get the hardware architecture of the machine. |
| 741 | run_function is used to execute the commands. It defaults to |
| 742 | utils.run() but a custom method (if provided) should be of the |
| 743 | same schema as utils.run. It should return a CmdResult object and |
| 744 | throw a CmdError exception. |
| 745 | """ |
| 746 | arch = run_function('/bin/uname -m').stdout.rstrip() |
| 747 | if re.match(r'i\d86$', arch): |
| 748 | arch = 'i386' |
| 749 | return arch |
| 750 | |
| 751 | |
showard | 4745ecd | 2009-05-26 19:34:56 +0000 | [diff] [blame] | 752 | def get_num_logical_cpus_per_socket(run_function=run): |
mbligh | 9fd9afe | 2009-04-28 18:27:25 +0000 | [diff] [blame] | 753 | """ |
| 754 | Get the number of cores (including hyperthreading) per cpu. |
| 755 | run_function is used to execute the commands. It defaults to |
| 756 | utils.run() but a custom method (if provided) should be of the |
| 757 | same schema as utils.run. It should return a CmdResult object and |
| 758 | throw a CmdError exception. |
| 759 | """ |
showard | 4745ecd | 2009-05-26 19:34:56 +0000 | [diff] [blame] | 760 | siblings = run_function('grep "^siblings" /proc/cpuinfo').stdout.rstrip() |
| 761 | num_siblings = map(int, |
| 762 | re.findall(r'^siblings\s*:\s*(\d+)\s*$', |
| 763 | siblings, re.M)) |
| 764 | if len(num_siblings) == 0: |
| 765 | raise error.TestError('Unable to find siblings info in /proc/cpuinfo') |
| 766 | if min(num_siblings) != max(num_siblings): |
| 767 | raise error.TestError('Number of siblings differ %r' % |
| 768 | num_siblings) |
| 769 | return num_siblings[0] |
mbligh | 9fd9afe | 2009-04-28 18:27:25 +0000 | [diff] [blame] | 770 | |
| 771 | |
jadmanski | 4f90925 | 2008-12-01 20:47:10 +0000 | [diff] [blame] | 772 | def merge_trees(src, dest): |
| 773 | """ |
| 774 | Merges a source directory tree at 'src' into a destination tree at |
| 775 | 'dest'. If a path is a file in both trees than the file in the source |
| 776 | tree is APPENDED to the one in the destination tree. If a path is |
| 777 | a directory in both trees then the directories are recursively merged |
| 778 | with this function. In any other case, the function will skip the |
| 779 | paths that cannot be merged (instead of failing). |
| 780 | """ |
| 781 | if not os.path.exists(src): |
| 782 | return # exists only in dest |
| 783 | elif not os.path.exists(dest): |
| 784 | if os.path.isfile(src): |
| 785 | shutil.copy2(src, dest) # file only in src |
| 786 | else: |
| 787 | shutil.copytree(src, dest, symlinks=True) # dir only in src |
| 788 | return |
| 789 | elif os.path.isfile(src) and os.path.isfile(dest): |
| 790 | # src & dest are files in both trees, append src to dest |
| 791 | destfile = open(dest, "a") |
| 792 | try: |
| 793 | srcfile = open(src) |
| 794 | try: |
| 795 | destfile.write(srcfile.read()) |
| 796 | finally: |
| 797 | srcfile.close() |
| 798 | finally: |
| 799 | destfile.close() |
| 800 | elif os.path.isdir(src) and os.path.isdir(dest): |
| 801 | # src & dest are directories in both trees, so recursively merge |
| 802 | for name in os.listdir(src): |
| 803 | merge_trees(os.path.join(src, name), os.path.join(dest, name)) |
| 804 | else: |
| 805 | # src & dest both exist, but are incompatible |
| 806 | return |
| 807 | |
| 808 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 809 | class CmdResult(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 810 | """ |
| 811 | Command execution result. |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 812 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 813 | command: String containing the command line itself |
| 814 | exit_status: Integer exit code of the process |
| 815 | stdout: String containing stdout of the process |
| 816 | stderr: String containing stderr of the process |
| 817 | duration: Elapsed wall clock time running the process |
| 818 | """ |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 819 | |
| 820 | |
mbligh | cd63a21 | 2009-05-01 23:04:38 +0000 | [diff] [blame] | 821 | def __init__(self, command="", stdout="", stderr="", |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 822 | exit_status=None, duration=0): |
| 823 | self.command = command |
| 824 | self.exit_status = exit_status |
| 825 | self.stdout = stdout |
| 826 | self.stderr = stderr |
| 827 | self.duration = duration |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 828 | |
| 829 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 830 | def __repr__(self): |
| 831 | wrapper = textwrap.TextWrapper(width = 78, |
| 832 | initial_indent="\n ", |
| 833 | subsequent_indent=" ") |
| 834 | |
| 835 | stdout = self.stdout.rstrip() |
| 836 | if stdout: |
| 837 | stdout = "\nstdout:\n%s" % stdout |
| 838 | |
| 839 | stderr = self.stderr.rstrip() |
| 840 | if stderr: |
| 841 | stderr = "\nstderr:\n%s" % stderr |
| 842 | |
| 843 | return ("* Command: %s\n" |
| 844 | "Exit status: %s\n" |
| 845 | "Duration: %s\n" |
| 846 | "%s" |
| 847 | "%s" |
| 848 | % (wrapper.fill(self.command), self.exit_status, |
| 849 | self.duration, stdout, stderr)) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 850 | |
| 851 | |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 852 | class run_randomly: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 853 | def __init__(self, run_sequentially=False): |
| 854 | # Run sequentially is for debugging control files |
| 855 | self.test_list = [] |
| 856 | self.run_sequentially = run_sequentially |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 857 | |
| 858 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 859 | def add(self, *args, **dargs): |
| 860 | test = (args, dargs) |
| 861 | self.test_list.append(test) |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 862 | |
| 863 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 864 | def run(self, fn): |
| 865 | while self.test_list: |
| 866 | test_index = random.randint(0, len(self.test_list)-1) |
| 867 | if self.run_sequentially: |
| 868 | test_index = 0 |
| 869 | (args, dargs) = self.test_list.pop(test_index) |
| 870 | fn(*args, **dargs) |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 871 | |
| 872 | |
showard | 5deef7f | 2009-09-09 18:16:58 +0000 | [diff] [blame] | 873 | def import_site_module(path, module, dummy=None, modulefile=None): |
| 874 | """ |
| 875 | Try to import the site specific module if it exists. |
| 876 | |
| 877 | @param path full filename of the source file calling this (ie __file__) |
| 878 | @param module full module name |
| 879 | @param dummy dummy value to return in case there is no symbol to import |
| 880 | @param modulefile module filename |
| 881 | |
| 882 | @return site specific module or dummy |
| 883 | |
| 884 | @raises ImportError if the site file exists but imports fails |
| 885 | """ |
| 886 | short_module = module[module.rfind(".") + 1:] |
| 887 | |
| 888 | if not modulefile: |
| 889 | modulefile = short_module + ".py" |
| 890 | |
| 891 | if os.path.exists(os.path.join(os.path.dirname(path), modulefile)): |
| 892 | return __import__(module, {}, {}, [short_module]) |
| 893 | return dummy |
| 894 | |
| 895 | |
mbligh | dd66937 | 2009-02-03 21:57:18 +0000 | [diff] [blame] | 896 | def import_site_symbol(path, module, name, dummy=None, modulefile=None): |
| 897 | """ |
| 898 | Try to import site specific symbol from site specific file if it exists |
| 899 | |
| 900 | @param path full filename of the source file calling this (ie __file__) |
| 901 | @param module full module name |
| 902 | @param name symbol name to be imported from the site file |
| 903 | @param dummy dummy value to return in case there is no symbol to import |
| 904 | @param modulefile module filename |
| 905 | |
| 906 | @return site specific symbol or dummy |
| 907 | |
showard | 5deef7f | 2009-09-09 18:16:58 +0000 | [diff] [blame] | 908 | @raises ImportError if the site file exists but imports fails |
mbligh | dd66937 | 2009-02-03 21:57:18 +0000 | [diff] [blame] | 909 | """ |
showard | 5deef7f | 2009-09-09 18:16:58 +0000 | [diff] [blame] | 910 | module = import_site_module(path, module, modulefile=modulefile) |
| 911 | if not module: |
| 912 | return dummy |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 913 | |
showard | 5deef7f | 2009-09-09 18:16:58 +0000 | [diff] [blame] | 914 | # special unique value to tell us if the symbol can't be imported |
| 915 | cant_import = object() |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 916 | |
showard | 5deef7f | 2009-09-09 18:16:58 +0000 | [diff] [blame] | 917 | obj = getattr(module, name, cant_import) |
| 918 | if obj is cant_import: |
| 919 | logging.error("unable to import site symbol '%s', using non-site " |
| 920 | "implementation", name) |
| 921 | return dummy |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 922 | |
| 923 | return obj |
| 924 | |
| 925 | |
| 926 | def import_site_class(path, module, classname, baseclass, modulefile=None): |
| 927 | """ |
| 928 | Try to import site specific class from site specific file if it exists |
| 929 | |
| 930 | Args: |
| 931 | path: full filename of the source file calling this (ie __file__) |
| 932 | module: full module name |
| 933 | classname: class name to be loaded from site file |
mbligh | 0a8c332 | 2009-04-28 18:32:19 +0000 | [diff] [blame] | 934 | baseclass: base class object to return when no site file present or |
| 935 | to mixin when site class exists but is not inherited from baseclass |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 936 | modulefile: module filename |
| 937 | |
mbligh | 0a8c332 | 2009-04-28 18:32:19 +0000 | [diff] [blame] | 938 | Returns: baseclass if site specific class does not exist, the site specific |
| 939 | class if it exists and is inherited from baseclass or a mixin of the |
| 940 | site specific class and baseclass when the site specific class exists |
| 941 | and is not inherited from baseclass |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 942 | |
| 943 | Raises: ImportError if the site file exists but imports fails |
| 944 | """ |
| 945 | |
mbligh | dd66937 | 2009-02-03 21:57:18 +0000 | [diff] [blame] | 946 | res = import_site_symbol(path, module, classname, None, modulefile) |
mbligh | 0a8c332 | 2009-04-28 18:32:19 +0000 | [diff] [blame] | 947 | if res: |
| 948 | if not issubclass(res, baseclass): |
| 949 | # if not a subclass of baseclass then mix in baseclass with the |
| 950 | # site specific class object and return the result |
| 951 | res = type(classname, (res, baseclass), {}) |
| 952 | else: |
| 953 | res = baseclass |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 954 | |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 955 | return res |
| 956 | |
| 957 | |
| 958 | def import_site_function(path, module, funcname, dummy, modulefile=None): |
| 959 | """ |
| 960 | Try to import site specific function from site specific file if it exists |
| 961 | |
| 962 | Args: |
| 963 | path: full filename of the source file calling this (ie __file__) |
| 964 | module: full module name |
| 965 | funcname: function name to be imported from site file |
| 966 | dummy: dummy function to return in case there is no function to import |
| 967 | modulefile: module filename |
| 968 | |
| 969 | Returns: site specific function object or dummy |
| 970 | |
| 971 | Raises: ImportError if the site file exists but imports fails |
| 972 | """ |
| 973 | |
mbligh | dd66937 | 2009-02-03 21:57:18 +0000 | [diff] [blame] | 974 | return import_site_symbol(path, module, funcname, dummy, modulefile) |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 975 | |
| 976 | |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 977 | def _get_pid_path(program_name): |
| 978 | my_path = os.path.dirname(__file__) |
| 979 | return os.path.abspath(os.path.join(my_path, "..", "..", |
| 980 | "%s.pid" % program_name)) |
| 981 | |
| 982 | |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 983 | def write_pid(program_name): |
| 984 | """ |
| 985 | Try to drop <program_name>.pid in the main autotest directory. |
| 986 | |
| 987 | Args: |
| 988 | program_name: prefix for file name |
| 989 | """ |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 990 | pidfile = open(_get_pid_path(program_name), "w") |
| 991 | try: |
| 992 | pidfile.write("%s\n" % os.getpid()) |
| 993 | finally: |
| 994 | pidfile.close() |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 995 | |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 996 | |
| 997 | def delete_pid_file_if_exists(program_name): |
| 998 | """ |
| 999 | Tries to remove <program_name>.pid from the main autotest directory. |
| 1000 | """ |
| 1001 | pidfile_path = _get_pid_path(program_name) |
| 1002 | |
| 1003 | try: |
| 1004 | os.remove(pidfile_path) |
| 1005 | except OSError: |
| 1006 | if not os.path.exists(pidfile_path): |
| 1007 | return |
| 1008 | raise |
| 1009 | |
| 1010 | |
| 1011 | def get_pid_from_file(program_name): |
| 1012 | """ |
| 1013 | Reads the pid from <program_name>.pid in the autotest directory. |
| 1014 | |
| 1015 | @param program_name the name of the program |
| 1016 | @return the pid if the file exists, None otherwise. |
| 1017 | """ |
| 1018 | pidfile_path = _get_pid_path(program_name) |
| 1019 | if not os.path.exists(pidfile_path): |
| 1020 | return None |
| 1021 | |
| 1022 | pidfile = open(_get_pid_path(program_name), 'r') |
| 1023 | |
| 1024 | try: |
| 1025 | try: |
| 1026 | pid = int(pidfile.readline()) |
| 1027 | except IOError: |
| 1028 | if not os.path.exists(pidfile_path): |
| 1029 | return None |
| 1030 | raise |
| 1031 | finally: |
| 1032 | pidfile.close() |
| 1033 | |
| 1034 | return pid |
| 1035 | |
| 1036 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 1037 | def program_is_alive(program_name): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 1038 | """ |
| 1039 | Checks if the process is alive and not in Zombie state. |
| 1040 | |
| 1041 | @param program_name the name of the program |
| 1042 | @return True if still alive, False otherwise |
| 1043 | """ |
| 1044 | pid = get_pid_from_file(program_name) |
| 1045 | if pid is None: |
| 1046 | return False |
| 1047 | return pid_is_alive(pid) |
| 1048 | |
| 1049 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 1050 | def signal_program(program_name, sig=signal.SIGTERM): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 1051 | """ |
| 1052 | Sends a signal to the process listed in <program_name>.pid |
| 1053 | |
| 1054 | @param program_name the name of the program |
| 1055 | @param sig signal to send |
| 1056 | """ |
| 1057 | pid = get_pid_from_file(program_name) |
| 1058 | if pid: |
| 1059 | signal_pid(pid, sig) |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 1060 | |
| 1061 | |
| 1062 | def get_relative_path(path, reference): |
| 1063 | """Given 2 absolute paths "path" and "reference", compute the path of |
| 1064 | "path" as relative to the directory "reference". |
| 1065 | |
| 1066 | @param path the absolute path to convert to a relative path |
| 1067 | @param reference an absolute directory path to which the relative |
| 1068 | path will be computed |
| 1069 | """ |
| 1070 | # normalize the paths (remove double slashes, etc) |
| 1071 | assert(os.path.isabs(path)) |
| 1072 | assert(os.path.isabs(reference)) |
| 1073 | |
| 1074 | path = os.path.normpath(path) |
| 1075 | reference = os.path.normpath(reference) |
| 1076 | |
| 1077 | # we could use os.path.split() but it splits from the end |
| 1078 | path_list = path.split(os.path.sep)[1:] |
| 1079 | ref_list = reference.split(os.path.sep)[1:] |
| 1080 | |
| 1081 | # find the longest leading common path |
| 1082 | for i in xrange(min(len(path_list), len(ref_list))): |
| 1083 | if path_list[i] != ref_list[i]: |
| 1084 | # decrement i so when exiting this loop either by no match or by |
| 1085 | # end of range we are one step behind |
| 1086 | i -= 1 |
| 1087 | break |
| 1088 | i += 1 |
| 1089 | # drop the common part of the paths, not interested in that anymore |
| 1090 | del path_list[:i] |
| 1091 | |
| 1092 | # for each uncommon component in the reference prepend a ".." |
| 1093 | path_list[:0] = ['..'] * (len(ref_list) - i) |
| 1094 | |
| 1095 | return os.path.join(*path_list) |
mbligh | 277a0e4 | 2009-07-11 00:11:45 +0000 | [diff] [blame] | 1096 | |
| 1097 | |
| 1098 | def sh_escape(command): |
| 1099 | """ |
| 1100 | Escape special characters from a command so that it can be passed |
| 1101 | as a double quoted (" ") string in a (ba)sh command. |
| 1102 | |
| 1103 | Args: |
| 1104 | command: the command string to escape. |
| 1105 | |
| 1106 | Returns: |
| 1107 | The escaped command string. The required englobing double |
| 1108 | quotes are NOT added and so should be added at some point by |
| 1109 | the caller. |
| 1110 | |
| 1111 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 1112 | """ |
| 1113 | command = command.replace("\\", "\\\\") |
| 1114 | command = command.replace("$", r'\$') |
| 1115 | command = command.replace('"', r'\"') |
| 1116 | command = command.replace('`', r'\`') |
| 1117 | return command |