Prathmesh Prabhu | 5ef88d1 | 2017-02-03 15:32:39 -0800 | [diff] [blame] | 1 | import datetime |
| 2 | import os |
| 3 | import re |
Evan Benn | e5d1c18 | 2020-10-28 13:47:41 +1100 | [diff] [blame] | 4 | import logging |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 5 | |
| 6 | |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 7 | def dprint(msg): |
Greg Edelston | e8eca6e | 2020-11-04 11:37:48 -0700 | [diff] [blame] | 8 | """Log a message on the DEBUG level.""" |
Evan Benn | e5d1c18 | 2020-10-28 13:47:41 +1100 | [diff] [blame] | 9 | logging.debug('%s', msg) |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | def get_timestamp(mapping, field): |
Aviv Keshet | dee7d01 | 2013-02-12 14:23:08 -0800 | [diff] [blame] | 13 | #pylint: disable-msg=C0111 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 14 | val = mapping.get(field, None) |
| 15 | if val is not None: |
| 16 | val = datetime.datetime.fromtimestamp(int(val)) |
| 17 | return val |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | def find_toplevel_job_dir(start_dir): |
| 21 | """ Starting from start_dir and moving upwards, find the top-level |
| 22 | of the job results dir. We can't just assume that it corresponds to |
| 23 | the actual job.dir, because job.dir may just be a subdir of the "real" |
| 24 | 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] | 25 | a top-level dir. |
| 26 | @param start_dir: starting directing for the upward search""" |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 27 | job_dir = start_dir |
| 28 | while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")): |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 29 | if job_dir == "/" or job_dir == '': |
jadmanski | a8e302a | 2008-09-25 19:49:38 +0000 | [diff] [blame] | 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 | |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 57 | Tag is in the format of JOB_ID-OWNER/HOSTNAME |
Fang Deng | 4982268 | 2014-10-21 16:29:22 -0700 | [diff] [blame] | 58 | |
| 59 | @param tag: afe_job_id and hostname are extracted from this tag. |
| 60 | e.g. "1234-chromeos-test/chromeos1-row1-host1" |
Prathmesh Prabhu | 11130e1 | 2018-04-17 13:35:09 -0700 | [diff] [blame] | 61 | @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] | 62 | """ |
| 63 | match = re.search('^([0-9]+)-.+/(.+)$', tag) |
Prathmesh Prabhu | ebd6c24 | 2018-04-17 13:56:58 -0700 | [diff] [blame] | 64 | return match.group(1) if match else None |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def get_skylab_task_id(tag): |
| 68 | """ Given a tag return the skylab_task's id (if any). |
| 69 | |
| 70 | Tag is in the format of swarming-TASK_ID/HOSTNAME |
| 71 | |
| 72 | @param tag: afe_job_id and hostname are extracted from this tag. |
| 73 | e.g. "1234-chromeos-test/chromeos1-row1-host1" |
| 74 | @return: the afe_job_id as a string if regex matches, else return '' |
| 75 | """ |
| 76 | match = re.search('^swarming-([A-Fa-f0-9]+)/(.+)$', tag) |
| 77 | return match.group(1) if match else None |
| 78 | |
| 79 | |
| 80 | def is_skylab_task(tag): |
Greg Edelston | e8eca6e | 2020-11-04 11:37:48 -0700 | [diff] [blame] | 81 | """Given a tag, determine whether it represents a Skylab task.""" |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 82 | return get_skylab_task_id(tag) is not None |