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