Prathmesh Prabhu | 5ef88d1 | 2017-02-03 15:32:39 -0800 | [diff] [blame] | 1 | import datetime |
| 2 | import os |
| 3 | import re |
| 4 | import sys |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | _debug_logger = sys.stderr |
| 8 | def dprint(msg): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 9 | #pylint: disable-msg=C0111 |
Luigi Semenzato | e706481 | 2017-02-03 14:47:59 -0800 | [diff] [blame] | 10 | print >> _debug_logger, 'tko parser: %s' % msg |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def redirect_parser_debugging(ostream): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 14 | #pylint: disable-msg=C0111 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 15 | global _debug_logger |
| 16 | _debug_logger = ostream |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def get_timestamp(mapping, field): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 20 | #pylint: disable-msg=C0111 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 21 | val = mapping.get(field, None) |
| 22 | if val is not None: |
| 23 | val = datetime.datetime.fromtimestamp(int(val)) |
| 24 | return val |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def find_toplevel_job_dir(start_dir): |
| 28 | """ Starting from start_dir and moving upwards, find the top-level |
| 29 | of the job results dir. We can't just assume that it corresponds to |
| 30 | the actual job.dir, because job.dir may just be a subdir of the "real" |
| 31 | 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] | 32 | a top-level dir. |
| 33 | @param start_dir: starting directing for the upward search""" |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 34 | job_dir = start_dir |
| 35 | while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")): |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 36 | if job_dir == "/" or job_dir == '': |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 37 | return None |
| 38 | job_dir = os.path.dirname(job_dir) |
| 39 | return job_dir |
jadmanski | 1f99f67 | 2009-07-01 16:23:09 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def drop_redundant_messages(messages): |
| 43 | """ Given a set of message strings discard any 'redundant' messages which |
| 44 | are simple a substring of the existing ones. |
| 45 | |
| 46 | @param messages - a set of message strings |
| 47 | |
| 48 | @return - a subset of messages with unnecessary strings dropped |
| 49 | """ |
| 50 | sorted_messages = sorted(messages, key=len, reverse=True) |
| 51 | filtered_messages = set() |
| 52 | for message in sorted_messages: |
| 53 | for filtered_message in filtered_messages: |
| 54 | if message in filtered_message: |
| 55 | break |
| 56 | else: |
| 57 | filtered_messages.add(message) |
| 58 | return filtered_messages |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def get_afe_job_id(tag): |
| 62 | """ Given a tag return the afe_job_id (if any). |
| 63 | |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 64 | Tag is in the format of JOB_ID-OWNER/HOSTNAME |
Fang Deng | 4982268 | 2014-10-21 16:29:22 -0700 | [diff] [blame] | 65 | |
| 66 | @param tag: afe_job_id and hostname are extracted from this tag. |
| 67 | e.g. "1234-chromeos-test/chromeos1-row1-host1" |
Prathmesh Prabhu | 11130e1 | 2018-04-17 13:35:09 -0700 | [diff] [blame] | 68 | @return: the afe_job_id as a string if regex matches, else return '' |
Fang Deng | 4982268 | 2014-10-21 16:29:22 -0700 | [diff] [blame] | 69 | """ |
| 70 | match = re.search('^([0-9]+)-.+/(.+)$', tag) |
Prathmesh Prabhu | ebd6c24 | 2018-04-17 13:56:58 -0700 | [diff] [blame] | 71 | return match.group(1) if match else None |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def get_skylab_task_id(tag): |
| 75 | """ Given a tag return the skylab_task's id (if any). |
| 76 | |
| 77 | Tag is in the format of swarming-TASK_ID/HOSTNAME |
| 78 | |
| 79 | @param tag: afe_job_id and hostname are extracted from this tag. |
| 80 | e.g. "1234-chromeos-test/chromeos1-row1-host1" |
| 81 | @return: the afe_job_id as a string if regex matches, else return '' |
| 82 | """ |
| 83 | match = re.search('^swarming-([A-Fa-f0-9]+)/(.+)$', tag) |
| 84 | return match.group(1) if match else None |
| 85 | |
| 86 | |
| 87 | def is_skylab_task(tag): |
| 88 | return get_skylab_task_id(tag) is not None |