blob: de862479048684d792938cdcde17d42be48304f5 [file] [log] [blame]
Prathmesh Prabhu5ef88d12017-02-03 15:32:39 -08001import datetime
2import os
3import re
Evan Benne5d1c182020-10-28 13:47:41 +11004import logging
mbligh2895ce52008-04-17 15:40:51 +00005
6
mbligh2895ce52008-04-17 15:40:51 +00007def dprint(msg):
Greg Edelstone8eca6e2020-11-04 11:37:48 -07008 """Log a message on the DEBUG level."""
Evan Benne5d1c182020-10-28 13:47:41 +11009 logging.debug('%s', msg)
mbligh2895ce52008-04-17 15:40:51 +000010
11
12def get_timestamp(mapping, field):
Aviv Keshetdee7d012013-02-12 14:23:08 -080013 #pylint: disable-msg=C0111
jadmanski0afbb632008-06-06 21:10:57 +000014 val = mapping.get(field, None)
15 if val is not None:
16 val = datetime.datetime.fromtimestamp(int(val))
17 return val
jadmanskia8e302a2008-09-25 19:49:38 +000018
19
20def 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 Keshetdee7d012013-02-12 14:23:08 -080025 a top-level dir.
26 @param start_dir: starting directing for the upward search"""
jadmanskia8e302a2008-09-25 19:49:38 +000027 job_dir = start_dir
28 while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")):
Ningning Xia2d981ee2016-07-06 17:59:54 -070029 if job_dir == "/" or job_dir == '':
jadmanskia8e302a2008-09-25 19:49:38 +000030 return None
31 job_dir = os.path.dirname(job_dir)
32 return job_dir
jadmanski1f99f672009-07-01 16:23:09 +000033
34
35def 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
jamesrena12b8a02010-06-16 23:28:23 +000052
53
54def get_afe_job_id(tag):
55 """ Given a tag return the afe_job_id (if any).
56
Prathmesh Prabhu17905882018-04-18 22:09:08 -070057 Tag is in the format of JOB_ID-OWNER/HOSTNAME
Fang Deng49822682014-10-21 16:29:22 -070058
59 @param tag: afe_job_id and hostname are extracted from this tag.
60 e.g. "1234-chromeos-test/chromeos1-row1-host1"
Prathmesh Prabhu11130e12018-04-17 13:35:09 -070061 @return: the afe_job_id as a string if regex matches, else return ''
Fang Deng49822682014-10-21 16:29:22 -070062 """
63 match = re.search('^([0-9]+)-.+/(.+)$', tag)
Prathmesh Prabhuebd6c242018-04-17 13:56:58 -070064 return match.group(1) if match else None
Prathmesh Prabhu17905882018-04-18 22:09:08 -070065
66
67def 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
80def is_skylab_task(tag):
Greg Edelstone8eca6e2020-11-04 11:37:48 -070081 """Given a tag, determine whether it represents a Skylab task."""
Prathmesh Prabhu17905882018-04-18 22:09:08 -070082 return get_skylab_task_id(tag) is not None