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.
5 """Module that contains trybot patch pool code."""
11 def ChromiteFilter(patch):
12 """Used with FilterFn to isolate patches to chromite."""
13 return patch.project == constants.CHROMITE_PROJECT
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)
22 def BranchFilter(branch, patch):
23 """Used with FilterFn to isolate patches based on a specific upstream."""
24 return patch.tracking_branch == branch
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)
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])
38 def Filter(self, **kwargs):
39 """Returns a new pool with only patches that match constraints.
42 **kwargs: constraints in the form of attr=value. I.e.,
43 project='chromiumos/chromite', tracking_branch='master'.
45 def AttributeFilter(patch):
47 if getattr(patch, key, object()) != kwargs[key]:
52 return self.FilterFn(AttributeFilter)
54 def FilterFn(self, filter_fn, negate=False):
55 """Returns a new pool with only patches that match constraints.
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.
64 f = lambda p : not filter_fn(p)
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))
71 def FilterManifest(self, negate=False):
72 """Return a patch pool with only patches to the manifest."""
73 return self.FilterFn(ManifestFilter, negate=negate)
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)
80 for source in [self.local_patches, self.remote_patches,