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