1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import common
6from autotest_lib.client.common_lib import priorities
7
8import base_event, forgiving_config_parser, manifest_versions, task
9
10class BuildEvent(base_event.BaseEvent):
11    """Base class for events that come from the build system.
12
13    For example, a new build completing or a new version of Chromium.
14
15    @var _revision: The last git revision we checked for new build artifacts.
16    """
17
18
19    def __init__(self, keyword, manifest_versions, always_handle):
20        """Constructor.
21
22        @param keyword: the keyword/name of this event, e.g. nightly.
23        @param manifest_versions: ManifestVersions instance to use for querying.
24        @param always_handle: If True, make ShouldHandle() always return True.
25        """
26        super(BuildEvent, self).__init__(keyword, manifest_versions,
27                                         always_handle)
28        self._revision = None
29
30
31    def Merge(self, to_merge):
32        """Merge this event with to_merge, changing all mutable properties.
33
34        self._revision REMAINS UNCHANGED.  That's part of this instance's
35        identity.
36
37        @param to_merge: A BuildEvent instance to merge into this isntance.
38        """
39        super(BuildEvent, self).Merge(to_merge)
40
41
42    def Prepare(self):
43        """Perform any one-time setup that must occur before [Should]Handle().
44
45        Creates initial revision checkpoint.
46        """
47        self._revision = self._mv.GetCheckpoint()
48
49
50    def ShouldHandle(self):
51        """True if there's been a new successful build since |self._revision|
52
53        @return True if there's been a new build, false otherwise.
54        """
55        if super(BuildEvent, self).ShouldHandle():
56            return True
57        else:
58            return self._mv.AnyManifestsSinceRev(self._revision)
59
60
61    def _AllPerBranchBuildsSince(self, board, revision):
62        """Get all per-branch, per-board builds since git |revision|.
63
64        @param board: the board whose builds we want.
65        @param revision: the revision to look back until.
66        @return {branch: [build-name1, build-name2]}
67        """
68        all_branch_manifests = self._mv.ManifestsSinceRev(revision, board)
69        all_branch_builds = {}
70        for (type, milestone), manifests in all_branch_manifests.iteritems():
71            branch_name = task.PickBranchName(type, milestone)
72            for manifest in manifests:
73                build = base_event.BuildName(board, type, milestone, manifest)
74                all_branch_builds.setdefault(branch_name, []).append(build)
75        return all_branch_builds
76
77
78    def GetBranchBuildsForBoard(self, board):
79        return self._AllPerBranchBuildsSince(board, self._revision)
80
81
82class NewBuild(BuildEvent):
83    KEYWORD = 'new_build'
84    PRIORITY = priorities.Priority.POSTBUILD
85    TIMEOUT = 12  # 12 hours, and builds come out every 6
86
87
88    def __init__(self, mv, always_handle):
89        """Constructor.
90
91        @param mv: ManifestVersions instance to use for querying.
92        @param always_handle: If True, make ShouldHandle() always return True.
93        """
94        super(NewBuild, self).__init__(self.KEYWORD, mv, always_handle)
95
96
97    def UpdateCriteria(self):
98        self._revision = self._mv.GetCheckpoint()
99