blob: 8a5ed0a413c8133b0c2eca8303c6acebb6fad666 [file] [log] [blame]
Chris Masonefad911a2012-03-29 12:30:26 -07001# 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
Chris Masone67f06d62012-04-12 15:16:56 -07005
Chris Masone96f16632012-04-04 18:36:03 -07006import logging, re
Chris Masone67f06d62012-04-12 15:16:56 -07007import deduping_scheduler, forgiving_config_parser
8from distutils import version
Chris Masone96f16632012-04-04 18:36:03 -07009
10
11class MalformedConfigEntry(Exception):
12 """Raised to indicate a failure to parse a Task out of a config."""
13 pass
Chris Masonefad911a2012-03-29 12:30:26 -070014
15
Chris Masone67f06d62012-04-12 15:16:56 -070016BARE_BRANCHES = ['factory', 'firmware']
17
18
19def PickBranchName(type, milestone):
20 if type in BARE_BRANCHES:
21 return type
22 return milestone
23
24
Chris Masone013859b2012-04-01 13:45:26 -070025class Task(object):
Chris Masonefad911a2012-03-29 12:30:26 -070026 """Represents an entry from the scheduler config. Can schedule itself.
27
28 Each entry from the scheduler config file maps one-to-one to a
Chris Masone013859b2012-04-01 13:45:26 -070029 Task. Each instance has enough info to schedule itself
Chris Masonefad911a2012-03-29 12:30:26 -070030 on-demand with the AFE.
31
32 This class also overrides __hash__() and all comparitor methods to enable
33 correct use in dicts, sets, etc.
34 """
35
Chris Masone96f16632012-04-04 18:36:03 -070036
37 @staticmethod
38 def CreateFromConfigSection(config, section):
39 """Create a Task from a section of a config file.
40
41 The section to parse should look like this:
42 [TaskName]
43 suite: suite_to_run # Required
44 run_on: event_on which to run # Required
45 branch_specs: factory,firmware,>=R12 # Optional
46 pool: pool_of_devices # Optional
47
Chris Masone67f06d62012-04-12 15:16:56 -070048 By default, Tasks run on all release branches, not factory or firmware.
49
Chris Masone96f16632012-04-04 18:36:03 -070050 @param config: a ForgivingConfigParser.
51 @param section: the section to parse into a Task.
52 @return keyword, Task object pair. One or both will be None on error.
53 @raise MalformedConfigEntry if there's a problem parsing |section|.
54 """
55 keyword = config.getstring(section, 'run_on')
56 suite = config.getstring(section, 'suite')
57 branches = config.getstring(section, 'branch_specs')
58 pool = config.getstring(section, 'pool')
59 if not keyword:
60 raise MalformedConfigEntry('No event to |run_on|.')
61 if not suite:
62 raise MalformedConfigEntry('No |suite|')
63 specs = []
64 if branches:
65 specs = re.split('\s*,\s*', branches)
66 Task.CheckBranchSpecs(specs)
67 return keyword, Task(suite, specs, pool)
68
69
70 @staticmethod
71 def CheckBranchSpecs(branch_specs):
72 """Make sure entries in the list branch_specs are correctly formed.
73
Chris Masone67f06d62012-04-12 15:16:56 -070074 We accept any of BARE_BRANCHES in |branch_specs|, as
Chris Masone96f16632012-04-04 18:36:03 -070075 well as _one_ string of the form '>=RXX', where 'RXX' is a
76 CrOS milestone number.
77
78 @param branch_specs: an iterable of branch specifiers.
79 @raise MalformedConfigEntry if there's a problem parsing |branch_specs|.
80 """
81 have_seen_numeric_constraint = False
82 for branch in branch_specs:
Chris Masone67f06d62012-04-12 15:16:56 -070083 if branch in BARE_BRANCHES:
Chris Masone96f16632012-04-04 18:36:03 -070084 continue
85 if branch.startswith('>=R') and not have_seen_numeric_constraint:
86 have_seen_numeric_constraint = True
87 continue
88 raise MalformedConfigEntry('%s is not a valid branch spec.', branch)
89
90
91 def __init__(self, suite, branch_specs, pool=None):
Chris Masonefad911a2012-03-29 12:30:26 -070092 """Constructor
93
Chris Masone96f16632012-04-04 18:36:03 -070094 Given an iterable in |branch_specs|, pre-vetted using CheckBranchSpecs,
95 we'll store them such that _FitsSpec() can be used to check whether a
96 given branch 'fits' with the specifications passed in here.
97 For example, given branch_specs = ['factory', '>=R18'], we'd set things
98 up so that _FitsSpec() would return True for 'factory', or 'RXX'
99 where XX is a number >= 18.
100
101 Given branch_specs = ['factory', 'firmware'], _FitsSpec()
102 would pass only those two specific strings.
103
104 Example usage:
105 t = Task('suite', ['factory', '>=R18'])
106 t._FitsSpec('factory') # True
107 t._FitsSpec('R19') # True
108 t._FitsSpec('R17') # False
109 t._FitsSpec('firmware') # False
110 t._FitsSpec('goober') # False
111
Chris Masonefad911a2012-03-29 12:30:26 -0700112 @param suite: the name of the suite to run, e.g. 'bvt'
Chris Masone96f16632012-04-04 18:36:03 -0700113 @param branch_specs: a pre-vetted iterable of branch specifiers,
114 e.g. ['>=R18', 'factory']
Chris Masonefad911a2012-03-29 12:30:26 -0700115 @param pool: the pool of machines to use for scheduling purposes.
116 Default: None
117 """
118 self._suite = suite
Chris Masone96f16632012-04-04 18:36:03 -0700119 self._branch_specs = branch_specs
Chris Masonefad911a2012-03-29 12:30:26 -0700120 self._pool = pool
Chris Masone96f16632012-04-04 18:36:03 -0700121
122 self._bare_branches = []
Chris Masone67f06d62012-04-12 15:16:56 -0700123 if not branch_specs:
124 # Any milestone is OK.
125 self._numeric_constraint = version.LooseVersion('0')
126 else:
127 self._numeric_constraint = None
128 for spec in branch_specs:
129 if spec.startswith('>='):
130 self._numeric_constraint = version.LooseVersion(
131 spec.lstrip('>=R'))
132 else:
133 self._bare_branches.append(spec)
Chris Masone96f16632012-04-04 18:36:03 -0700134
Chris Masonefad911a2012-03-29 12:30:26 -0700135 # Since we expect __hash__() and other comparitor methods to be used
136 # frequently by set operations, and they use str() a lot, pre-compute
137 # the string representation of this object.
Chris Masone3fba86f2012-04-03 10:06:56 -0700138 self._str = '%s: %s on %s with pool %s' % (self.__class__.__name__,
Chris Masone96f16632012-04-04 18:36:03 -0700139 suite, branch_specs, pool)
140
141
142 def _FitsSpec(self, branch):
143 """Checks if a branch is deemed OK by this instance's branch specs.
144
145 When called on a branch name, will return whether that branch
146 'fits' the specifications stored in self._bare_branches and
147 self._numeric_constraint.
148
149 @param branch: the branch to check.
150 @return True if b 'fits' with stored specs, False otherwise.
151 """
152 return (branch in self._bare_branches or
Chris Masone67f06d62012-04-12 15:16:56 -0700153 (self._numeric_constraint and
154 version.LooseVersion(branch) >= self._numeric_constraint))
Chris Masonefad911a2012-03-29 12:30:26 -0700155
156
157 @property
158 def suite(self):
159 return self._suite
160
161
162 @property
Chris Masone96f16632012-04-04 18:36:03 -0700163 def branch_specs(self):
164 return self._branch_specs
Chris Masonefad911a2012-03-29 12:30:26 -0700165
166
167 @property
Chris Masone3fba86f2012-04-03 10:06:56 -0700168 def pool(self):
169 return self._pool
Chris Masonefad911a2012-03-29 12:30:26 -0700170
171
172 def __str__(self):
173 return self._str
174
175
176 def __lt__(self, other):
177 return str(self) < str(other)
178
179
180 def __le__(self, other):
181 return str(self) <= str(other)
182
183
184 def __eq__(self, other):
185 return str(self) == str(other)
186
187
188 def __ne__(self, other):
189 return str(self) != str(other)
190
191
192 def __gt__(self, other):
193 return str(self) > str(other)
194
195
196 def __ge__(self, other):
197 return str(self) >= str(other)
198
199
200 def __hash__(self):
201 """Allows instances to be correctly deduped when used in a set."""
202 return hash(str(self))
203
204
Chris Masone96f16632012-04-04 18:36:03 -0700205 def Run(self, scheduler, branch_builds, board, force=False):
Chris Masone013859b2012-04-01 13:45:26 -0700206 """Run this task. Returns False if it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -0700207
Chris Masone013859b2012-04-01 13:45:26 -0700208 Execute this task. Attempt to schedule the associated suite.
209 Return True if this task should be kept around, False if it
210 should be destroyed. This allows for one-shot Tasks.
Chris Masonefad911a2012-03-29 12:30:26 -0700211
212 @param scheduler: an instance of DedupingScheduler, as defined in
213 deduping_scheduler.py
Chris Masone05b19442012-04-17 13:37:55 -0700214 @param branch_builds: a dict mapping branch name to the build(s) to
Chris Masone96f16632012-04-04 18:36:03 -0700215 install for that branch, e.g.
Chris Masone05b19442012-04-17 13:37:55 -0700216 {'R18': ['x86-alex-release/R18-1655.0.0'],
217 'R19': ['x86-alex-release/R19-2077.0.0']}
Chris Masone96f16632012-04-04 18:36:03 -0700218 @param board: the board against which to run self._suite.
Chris Masonefad911a2012-03-29 12:30:26 -0700219 @param force: Always schedule the suite.
Chris Masone013859b2012-04-01 13:45:26 -0700220 @return True if the task should be kept, False if not
Chris Masonefad911a2012-03-29 12:30:26 -0700221 """
Chris Masone96f16632012-04-04 18:36:03 -0700222 builds = []
Chris Masonefe5a5092012-04-11 18:29:07 -0700223 for branch, build in branch_builds.iteritems():
Chris Masone67f06d62012-04-12 15:16:56 -0700224 logging.debug('Checking if %s fits spec %r',
225 branch, self.branch_specs)
Chris Masone96f16632012-04-04 18:36:03 -0700226 if self._FitsSpec(branch):
Chris Masone05b19442012-04-17 13:37:55 -0700227 builds.extend(build)
Chris Masone96f16632012-04-04 18:36:03 -0700228 for build in builds:
Chris Masone3fba86f2012-04-03 10:06:56 -0700229 try:
Chris Masone96f16632012-04-04 18:36:03 -0700230 if not scheduler.ScheduleSuite(self._suite, board, build,
Chris Masone3fba86f2012-04-03 10:06:56 -0700231 self._pool, force):
Chris Masone96f16632012-04-04 18:36:03 -0700232 logging.info('Skipping scheduling %s on %s for %s',
233 self._suite, build, board)
Chris Masone3fba86f2012-04-03 10:06:56 -0700234 except deduping_scheduler.DedupingSchedulerException as e:
235 logging.error(e)
Chris Masonefad911a2012-03-29 12:30:26 -0700236 return True
237
238
Chris Masone013859b2012-04-01 13:45:26 -0700239class OneShotTask(Task):
240 """A Task that can be run only once. Can schedule itself."""
Chris Masonefad911a2012-03-29 12:30:26 -0700241
242
Chris Masone96f16632012-04-04 18:36:03 -0700243 def Run(self, scheduler, branch_builds, board, force=False):
Chris Masone013859b2012-04-01 13:45:26 -0700244 """Run this task. Returns False, indicating it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -0700245
Chris Masone013859b2012-04-01 13:45:26 -0700246 Run this task. Attempt to schedule the associated suite.
247 Return False, indicating to the caller that it should discard this task.
Chris Masonefad911a2012-03-29 12:30:26 -0700248
249 @param scheduler: an instance of DedupingScheduler, as defined in
250 deduping_scheduler.py
Chris Masone05b19442012-04-17 13:37:55 -0700251 @param branch_builds: a dict mapping branch name to the build(s) to
Chris Masone96f16632012-04-04 18:36:03 -0700252 install for that branch, e.g.
Chris Masone05b19442012-04-17 13:37:55 -0700253 {'R18': ['x86-alex-release/R18-1655.0.0'],
254 'R19': ['x86-alex-release/R19-2077.0.0']}
Chris Masone96f16632012-04-04 18:36:03 -0700255 @param board: the board against which to run self._suite.
Chris Masonefad911a2012-03-29 12:30:26 -0700256 @param force: Always schedule the suite.
257 @return False
258 """
Chris Masone96f16632012-04-04 18:36:03 -0700259 super(OneShotTask, self).Run(scheduler, branch_builds, board, force)
Chris Masonefad911a2012-03-29 12:30:26 -0700260 return False