jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 1 | import os, sys, datetime, re |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | _debug_logger = sys.stderr |
| 5 | def dprint(msg): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 6 | #pylint: disable-msg=C0111 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 7 | print >> _debug_logger, msg |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | def redirect_parser_debugging(ostream): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 11 | #pylint: disable-msg=C0111 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 12 | global _debug_logger |
| 13 | _debug_logger = ostream |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def get_timestamp(mapping, field): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 17 | #pylint: disable-msg=C0111 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 18 | val = mapping.get(field, None) |
| 19 | if val is not None: |
| 20 | val = datetime.datetime.fromtimestamp(int(val)) |
| 21 | return val |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def find_toplevel_job_dir(start_dir): |
| 25 | """ Starting from start_dir and moving upwards, find the top-level |
| 26 | of the job results dir. We can't just assume that it corresponds to |
| 27 | the actual job.dir, because job.dir may just be a subdir of the "real" |
| 28 | job dir that autoserv was launched with. Returns None if it can't find |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 29 | a top-level dir. |
| 30 | @param start_dir: starting directing for the upward search""" |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 31 | job_dir = start_dir |
| 32 | while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")): |
| 33 | if job_dir == "/": |
| 34 | return None |
| 35 | job_dir = os.path.dirname(job_dir) |
| 36 | return job_dir |
jadmanski | 1f99f67 | 2009-07-01 16:23:09 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def drop_redundant_messages(messages): |
| 40 | """ Given a set of message strings discard any 'redundant' messages which |
| 41 | are simple a substring of the existing ones. |
| 42 | |
| 43 | @param messages - a set of message strings |
| 44 | |
| 45 | @return - a subset of messages with unnecessary strings dropped |
| 46 | """ |
| 47 | sorted_messages = sorted(messages, key=len, reverse=True) |
| 48 | filtered_messages = set() |
| 49 | for message in sorted_messages: |
| 50 | for filtered_message in filtered_messages: |
| 51 | if message in filtered_message: |
| 52 | break |
| 53 | else: |
| 54 | filtered_messages.add(message) |
| 55 | return filtered_messages |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | def get_afe_job_id(tag): |
| 59 | """ Given a tag return the afe_job_id (if any). |
| 60 | |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 61 | @param tag: afe_job_id is extracted from this tag |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 62 | |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 63 | @return: the afe_job_id as a string if regex matches, else return '' |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 64 | """ |
| 65 | |
| 66 | match = re.search('^([0-9]+)-.+/.+$', tag) |
| 67 | if match: |
| 68 | return match.group(1) |
| 69 | return '' |