1 import os, sys, datetime, re 2 3 4 _debug_logger = sys.stderr 5 def dprint(msg): 6 #pylint: disable-msg=C0111 7 print >> _debug_logger, msg 8 9 10 def redirect_parser_debugging(ostream): 11 #pylint: disable-msg=C0111 12 global _debug_logger 13 _debug_logger = ostream 14 15 16 def get_timestamp(mapping, field): 17 #pylint: disable-msg=C0111 18 val = mapping.get(field, None) 19 if val is not None: 20 val = datetime.datetime.fromtimestamp(int(val)) 21 return val 22 23 24 def 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 29 a top-level dir. 30 @param start_dir: starting directing for the upward search""" 31 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 37 38 39 def 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 56 57 58 def get_afe_job_id(tag): 59 """ Given a tag return the afe_job_id (if any). 60 61 @param tag: afe_job_id is extracted from this tag 62 63 @return: the afe_job_id as a string if regex matches, else return '' 64 """ 65 return get_afe_job_id_and_hostname(tag)[0] 66 67 68 def get_afe_job_id_and_hostname(tag): 69 """ Given a tag return the afe_job_id and hostname (if any). 70 71 Extract job id and hostname if tag is in the format of 72 JOB_ID-OWNER/HOSTNAME. JOB_ID and HOSTNAME must both be present 73 to be considered as a match. 74 75 @param tag: afe_job_id and hostname are extracted from this tag. 76 e.g. "1234-chromeos-test/chromeos1-row1-host1" 77 @return: A tuple (afe_job_id, hostname), both as string if regex 78 matches, else return ('', ''). 79 """ 80 match = re.search('^([0-9]+)-.+/(.+)$', tag) 81 return (match.group(1), match.group(2)) if match else ('', '') 82