blob: 370b61e96621eb2cb5e2013cc5d059fb487b9b4c [file] [log] [blame]
Prathmesh Prabhu5ef88d12017-02-03 15:32:39 -08001import datetime
2import os
3import re
4import sys
mbligh2895ce52008-04-17 15:40:51 +00005
6
7_debug_logger = sys.stderr
8def dprint(msg):
Aviv Keshetdee7d012013-02-12 14:23:08 -08009 #pylint: disable-msg=C0111
Luigi Semenzatoe7064812017-02-03 14:47:59 -080010 print >> _debug_logger, 'tko parser: %s' % msg
mbligh2895ce52008-04-17 15:40:51 +000011
12
13def redirect_parser_debugging(ostream):
Aviv Keshetdee7d012013-02-12 14:23:08 -080014 #pylint: disable-msg=C0111
jadmanski0afbb632008-06-06 21:10:57 +000015 global _debug_logger
16 _debug_logger = ostream
mbligh2895ce52008-04-17 15:40:51 +000017
18
19def get_timestamp(mapping, field):
Aviv Keshetdee7d012013-02-12 14:23:08 -080020 #pylint: disable-msg=C0111
jadmanski0afbb632008-06-06 21:10:57 +000021 val = mapping.get(field, None)
22 if val is not None:
23 val = datetime.datetime.fromtimestamp(int(val))
24 return val
jadmanskia8e302a2008-09-25 19:49:38 +000025
26
27def 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 Keshetdee7d012013-02-12 14:23:08 -080032 a top-level dir.
33 @param start_dir: starting directing for the upward search"""
jadmanskia8e302a2008-09-25 19:49:38 +000034 job_dir = start_dir
35 while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")):
Ningning Xia2d981ee2016-07-06 17:59:54 -070036 if job_dir == "/" or job_dir == '':
jadmanskia8e302a2008-09-25 19:49:38 +000037 return None
38 job_dir = os.path.dirname(job_dir)
39 return job_dir
jadmanski1f99f672009-07-01 16:23:09 +000040
41
42def 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
jamesrena12b8a02010-06-16 23:28:23 +000059
60
61def get_afe_job_id(tag):
62 """ Given a tag return the afe_job_id (if any).
63
Aviv Keshetdee7d012013-02-12 14:23:08 -080064 @param tag: afe_job_id is extracted from this tag
jamesrena12b8a02010-06-16 23:28:23 +000065
Aviv Keshetdee7d012013-02-12 14:23:08 -080066 @return: the afe_job_id as a string if regex matches, else return ''
jamesrena12b8a02010-06-16 23:28:23 +000067 """
Fang Deng49822682014-10-21 16:29:22 -070068 return get_afe_job_id_and_hostname(tag)[0]
jamesrena12b8a02010-06-16 23:28:23 +000069
Fang Deng49822682014-10-21 16:29:22 -070070
71def get_afe_job_id_and_hostname(tag):
72 """ Given a tag return the afe_job_id and hostname (if any).
73
74 Extract job id and hostname if tag is in the format of
75 JOB_ID-OWNER/HOSTNAME. JOB_ID and HOSTNAME must both be present
76 to be considered as a match.
77
78 @param tag: afe_job_id and hostname are extracted from this tag.
79 e.g. "1234-chromeos-test/chromeos1-row1-host1"
80 @return: A tuple (afe_job_id, hostname), both as string if regex
81 matches, else return ('', '').
82 """
83 match = re.search('^([0-9]+)-.+/(.+)$', tag)
84 return (match.group(1), match.group(2)) if match else ('', '')