• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import datetime
2import os
3import re
4import logging
5
6
7def dprint(msg):
8    """Log a message on the DEBUG level."""
9    logging.debug('%s', msg)
10
11
12def get_timestamp(mapping, field):
13    #pylint: disable-msg=C0111
14    val = mapping.get(field, None)
15    if val is not None:
16        val = datetime.datetime.fromtimestamp(int(val))
17    return val
18
19
20def find_toplevel_job_dir(start_dir):
21    """ Starting from start_dir and moving upwards, find the top-level
22    of the job results dir. We can't just assume that it corresponds to
23    the actual job.dir, because job.dir may just be a subdir of the "real"
24    job dir that autoserv was launched with. Returns None if it can't find
25    a top-level dir.
26    @param start_dir: starting directing for the upward search"""
27    job_dir = start_dir
28    while not os.path.exists(os.path.join(job_dir, ".autoserv_execute")):
29        if job_dir == "/" or job_dir == '':
30            return None
31        job_dir = os.path.dirname(job_dir)
32    return job_dir
33
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
52
53
54def get_afe_job_id(tag):
55    """ Given a tag return the afe_job_id (if any).
56
57    Tag is in the format of JOB_ID-OWNER/HOSTNAME
58
59    @param tag: afe_job_id and hostname are extracted from this tag.
60                e.g. "1234-chromeos-test/chromeos1-row1-host1"
61    @return: the afe_job_id as a string if regex matches, else return ''
62    """
63    match = re.search('^([0-9]+)-.+/(.+)$', tag)
64    return match.group(1) if match else None
65
66
67def get_skylab_task_id(tag):
68    """ Given a tag return the skylab_task's id (if any).
69
70    Tag is in the format of swarming-TASK_ID/HOSTNAME
71
72    @param tag: afe_job_id and hostname are extracted from this tag.
73                e.g. "1234-chromeos-test/chromeos1-row1-host1"
74    @return: the afe_job_id as a string if regex matches, else return ''
75    """
76    match = re.search('^swarming-([A-Fa-f0-9]+)/(.+)$', tag)
77    return match.group(1) if match else None
78
79
80def is_skylab_task(tag):
81    """Given a tag, determine whether it represents a Skylab task."""
82    return get_skylab_task_id(tag) is not None
83