blob: 7399c90bd1b74706e0efd917d4a5892926de953d [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):
jadmanski0afbb632008-06-06 21:10:57 +00006 print >> _debug_logger, msg
mbligh2895ce52008-04-17 15:40:51 +00007
8
9def redirect_parser_debugging(ostream):
jadmanski0afbb632008-06-06 21:10:57 +000010 global _debug_logger
11 _debug_logger = ostream
mbligh2895ce52008-04-17 15:40:51 +000012
13
14def get_timestamp(mapping, field):
jadmanski0afbb632008-06-06 21:10:57 +000015 val = mapping.get(field, None)
16 if val is not None:
17 val = datetime.datetime.fromtimestamp(int(val))
18 return val
jadmanskia8e302a2008-09-25 19:49:38 +000019
20
21def find_toplevel_job_dir(start_dir):
22 """ Starting from start_dir and moving upwards, find the top-level
23 of the job results dir. We can't just assume that it corresponds to
24 the actual job.dir, because job.dir may just be a subdir of the "real"
25 job dir that autoserv was launched with. Returns None if it can't find
26 a top-level dir. """
27 job_dir = start_dir
28 while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")):
29 if job_dir == "/":
30 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
57 @param
58 tag: afe_job_id is extracted from this tag
59
60 @return
61 returns the afe_job_id if regex matches, else return ''
62 """
63
64 match = re.search('^([0-9]+)-.+/.+$', tag)
65 if match:
66 return match.group(1)
67 return ''