blob: a3e6015f188d631ca4e98bcd2f0a413b183aa304 [file] [log] [blame]
jamesrena12b8a02010-06-16 23:28:23 +00001import os, sys, datetime, re
mbligh2895ce52008-04-17 15:40:51 +00002
3
4_debug_logger = sys.stderr
5def dprint(msg):
Aviv Keshetdee7d012013-02-12 14:23:08 -08006 #pylint: disable-msg=C0111
jadmanski0afbb632008-06-06 21:10:57 +00007 print >> _debug_logger, msg
mbligh2895ce52008-04-17 15:40:51 +00008
9
10def redirect_parser_debugging(ostream):
Aviv Keshetdee7d012013-02-12 14:23:08 -080011 #pylint: disable-msg=C0111
jadmanski0afbb632008-06-06 21:10:57 +000012 global _debug_logger
13 _debug_logger = ostream
mbligh2895ce52008-04-17 15:40:51 +000014
15
16def get_timestamp(mapping, field):
Aviv Keshetdee7d012013-02-12 14:23:08 -080017 #pylint: disable-msg=C0111
jadmanski0afbb632008-06-06 21:10:57 +000018 val = mapping.get(field, None)
19 if val is not None:
20 val = datetime.datetime.fromtimestamp(int(val))
21 return val
jadmanskia8e302a2008-09-25 19:49:38 +000022
23
24def find_toplevel_job_dir(start_dir):
25 """ Starting from start_dir and moving upwards, find the top-level
26 of the job results dir. We can't just assume that it corresponds to
27 the actual job.dir, because job.dir may just be a subdir of the "real"
28 job dir that autoserv was launched with. Returns None if it can't find
Aviv Keshetdee7d012013-02-12 14:23:08 -080029 a top-level dir.
30 @param start_dir: starting directing for the upward search"""
jadmanskia8e302a2008-09-25 19:49:38 +000031 job_dir = start_dir
32 while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")):
33 if job_dir == "/":
34 return None
35 job_dir = os.path.dirname(job_dir)
36 return job_dir
jadmanski1f99f672009-07-01 16:23:09 +000037
38
39def drop_redundant_messages(messages):
40 """ Given a set of message strings discard any 'redundant' messages which
41 are simple a substring of the existing ones.
42
43 @param messages - a set of message strings
44
45 @return - a subset of messages with unnecessary strings dropped
46 """
47 sorted_messages = sorted(messages, key=len, reverse=True)
48 filtered_messages = set()
49 for message in sorted_messages:
50 for filtered_message in filtered_messages:
51 if message in filtered_message:
52 break
53 else:
54 filtered_messages.add(message)
55 return filtered_messages
jamesrena12b8a02010-06-16 23:28:23 +000056
57
58def get_afe_job_id(tag):
59 """ Given a tag return the afe_job_id (if any).
60
Aviv Keshetdee7d012013-02-12 14:23:08 -080061 @param tag: afe_job_id is extracted from this tag
jamesrena12b8a02010-06-16 23:28:23 +000062
Aviv Keshetdee7d012013-02-12 14:23:08 -080063 @return: the afe_job_id as a string if regex matches, else return ''
jamesrena12b8a02010-06-16 23:28:23 +000064 """
65
66 match = re.search('^([0-9]+)-.+/.+$', tag)
67 if match:
68 return match.group(1)
69 return ''