Planner: add migration script to drop planner tables from autotest_web database
[autotest-zwu.git] / tko / utils.py
blob7399c90bd1b74706e0efd917d4a5892926de953d
1 import os, sys, datetime, re
4 _debug_logger = sys.stderr
5 def dprint(msg):
6 print >> _debug_logger, msg
9 def redirect_parser_debugging(ostream):
10 global _debug_logger
11 _debug_logger = ostream
14 def get_timestamp(mapping, field):
15 val = mapping.get(field, None)
16 if val is not None:
17 val = datetime.datetime.fromtimestamp(int(val))
18 return val
21 def 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
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.
39 @param messages - a set of message strings
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
54 def get_afe_job_id(tag):
55 """ Given a tag return the afe_job_id (if any).
57 @param
58 tag: afe_job_id is extracted from this tag
60 @return
61 returns the afe_job_id if regex matches, else return ''
62 """
64 match = re.search('^([0-9]+)-.+/.+$', tag)
65 if match:
66 return match.group(1)
67 return ''