Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / buildbot / trybot_patch_pool.py
1 # Copyright (c) 2011-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
5 """Module that contains trybot patch pool code."""
6
7 import constants
8 import functools
9
10
11 def ChromiteFilter(patch):
12   """Used with FilterFn to isolate patches to chromite."""
13   return patch.project == constants.CHROMITE_PROJECT
14
15
16 def ManifestFilter(patch):
17   """Used with FilterFn to isolate patches to the manifest."""
18   return patch.project in (constants.MANIFEST_PROJECT,
19                            constants.MANIFEST_INT_PROJECT)
20
21
22 def BranchFilter(branch, patch):
23   """Used with FilterFn to isolate patches based on a specific upstream."""
24   return patch.tracking_branch == branch
25
26
27 class TrybotPatchPool(object):
28   """Represents patches specified by the user to test."""
29   def __init__(self, gerrit_patches=(), local_patches=(), remote_patches=()):
30     self.gerrit_patches = tuple(gerrit_patches)
31     self.local_patches = tuple(local_patches)
32     self.remote_patches = tuple(remote_patches)
33
34   def __nonzero__(self):
35     """Returns True if the pool has no patches."""
36     return any([self.gerrit_patches, self.local_patches, self.remote_patches])
37
38   def Filter(self, **kwargs):
39     """Returns a new pool with only patches that match constraints.
40
41     Args:
42       **kwargs: constraints in the form of attr=value.  I.e.,
43                 project='chromiumos/chromite', tracking_branch='master'.
44     """
45     def AttributeFilter(patch):
46       for key in kwargs:
47         if getattr(patch, key, object()) != kwargs[key]:
48           return False
49       else:
50         return True
51
52     return self.FilterFn(AttributeFilter)
53
54   def FilterFn(self, filter_fn, negate=False):
55     """Returns a new pool with only patches that match constraints.
56
57     Args:
58       filter_fn: Functor that accepts a 'patch' argument, and returns whether to
59                  include the patch in the results.
60       negate: Return patches that don't pass the filter_fn.
61     """
62     f = filter_fn
63     if negate:
64       f = lambda p : not filter_fn(p)
65
66     return self.__class__(
67         gerrit_patches=filter(f, self.gerrit_patches),
68         local_patches=filter(f, self.local_patches),
69         remote_patches=filter(f, self.remote_patches))
70
71   def FilterManifest(self, negate=False):
72     """Return a patch pool with only patches to the manifest."""
73     return self.FilterFn(ManifestFilter, negate=negate)
74
75   def FilterBranch(self, branch, negate=False):
76     """Return a patch pool with only patches based on a particular branch."""
77     return self.FilterFn(functools.partial(BranchFilter, branch), negate=negate)
78
79   def __iter__(self):
80     for source in [self.local_patches, self.remote_patches,
81                    self.gerrit_patches]:
82       for patch in source:
83         yield patch