|
1 #!/usr/bin/env python |
|
2 # |
|
3 # Copyright 2007 Google Inc. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 # |
|
17 |
|
18 """CronInfo tools. |
|
19 |
|
20 A library for working with CronInfo records, describing cron entries for an |
|
21 application. Supports loading the records from yaml. |
|
22 """ |
|
23 |
|
24 |
|
25 |
|
26 from google.appengine.cron import groc |
|
27 from google.appengine.api import validation |
|
28 from google.appengine.api import yaml_builder |
|
29 from google.appengine.api import yaml_listener |
|
30 from google.appengine.api import yaml_object |
|
31 |
|
32 _URL_REGEX = r'^/.*$' |
|
33 |
|
34 |
|
35 _TIMEZONE_REGEX = r'^.{0,100}$' |
|
36 |
|
37 _DESCRIPTION_REGEX = r'^.{0,499}$' |
|
38 |
|
39 |
|
40 class GrocValidator(validation.Validator): |
|
41 """Checks that a schedule is in valid groc format.""" |
|
42 |
|
43 def Validate(self, value): |
|
44 """Validates a schedule.""" |
|
45 if value is None: |
|
46 raise validation.MissingAttribute('schedule must be specified') |
|
47 if not isinstance(value, basestring): |
|
48 raise TypeError('schedule must be a string, not \'%r\''%type(value)) |
|
49 schedule = groc.CreateParser(value) |
|
50 try: |
|
51 schedule.timespec() |
|
52 except groc.GrocException, e: |
|
53 raise validation.ValidationError('schedule \'%s\' failed to parse: %s'%( |
|
54 value, e.args[0])) |
|
55 return value |
|
56 |
|
57 |
|
58 CRON = 'cron' |
|
59 |
|
60 URL = 'url' |
|
61 SCHEDULE = 'schedule' |
|
62 TIMEZONE = 'timezone' |
|
63 DESCRIPTION = 'description' |
|
64 |
|
65 |
|
66 class MalformedCronfigurationFile(Exception): |
|
67 """Configuration file for Cron is malformed.""" |
|
68 pass |
|
69 |
|
70 |
|
71 class CronEntry(validation.Validated): |
|
72 """A cron entry describes a single cron job.""" |
|
73 ATTRIBUTES = { |
|
74 URL: _URL_REGEX, |
|
75 SCHEDULE: GrocValidator(), |
|
76 TIMEZONE: validation.Optional(_TIMEZONE_REGEX), |
|
77 DESCRIPTION: validation.Optional(_DESCRIPTION_REGEX) |
|
78 } |
|
79 |
|
80 |
|
81 class CronInfoExternal(validation.Validated): |
|
82 """CronInfoExternal describes all cron entries for an application.""" |
|
83 ATTRIBUTES = { |
|
84 CRON: validation.Optional(validation.Repeated(CronEntry)) |
|
85 } |
|
86 |
|
87 |
|
88 def LoadSingleCron(cron_info): |
|
89 """Load a cron.yaml file or string and return a CronInfoExternal object.""" |
|
90 builder = yaml_object.ObjectBuilder(CronInfoExternal) |
|
91 handler = yaml_builder.BuilderHandler(builder) |
|
92 listener = yaml_listener.EventListener(handler) |
|
93 listener.Parse(cron_info) |
|
94 |
|
95 cron_info = handler.GetResults() |
|
96 if len(cron_info) < 1: |
|
97 raise MalformedCronfigurationFile('Empty cron configuration.') |
|
98 if len(cron_info) > 1: |
|
99 raise MalformedCronfigurationFile('Multiple cron sections ' |
|
100 'in configuration.') |
|
101 return cron_info[0] |