OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 import json | 7 import json |
8 | 8 |
| 9 def _TestDictOfStrings(key, value): |
| 10 if not isinstance(value, dict): |
| 11 raise Exception('Wrong type for key "%s", expecting dict, got %s.' % |
| 12 (key, type(value))) |
| 13 for k, v in value.iteritems(): |
| 14 if not isinstance(k, basestring): |
| 15 raise Exception("In %s the decription %s was not a string but %s" % |
| 16 (key, k, type(k))) |
| 17 if not isinstance(v, basestring): |
| 18 raise Exception("In %s the command %s was not a string but %s" % |
| 19 (key, v, type(v))) |
| 20 |
| 21 def _TestString(key, value): |
| 22 if not isinstance(value, basestring): |
| 23 raise Exception('Wrong type for key "%s", expecting string, got %s.' % |
| 24 (key, type(value))) |
| 25 |
9 VALID_TYPES = { | 26 VALID_TYPES = { |
10 # Hooks mapping names (as displayed by the buildbot) to commands to execute. | 27 # Hooks mapping names (as displayed by the buildbot) to commands to execute. |
11 'pre_pub_upgrade_hooks' : dict, | 28 'pre_pub_upgrade_hooks' : _TestDictOfStrings, |
12 'pre_pub_build_hooks' : dict, | 29 'pre_pub_build_hooks' : _TestDictOfStrings, |
13 'post_pub_build_hooks' : dict, | 30 'post_pub_build_hooks' : _TestDictOfStrings, |
14 'pre_test_hooks' : dict, | 31 'pre_test_hooks' : _TestDictOfStrings, |
15 'post_test_hooks' : dict | 32 'post_test_hooks' : _TestDictOfStrings, |
| 33 # Using a custom script to run steps. |
| 34 'use_custom_script' : _TestString, |
16 } | 35 } |
17 | 36 |
18 """ | 37 """ |
19 Example config: | 38 Example config: |
20 { | 39 { |
21 "pre_pub_build_hooks" : { | 40 "pre_pub_build_hooks" : { |
22 "Fixing up something": "$dart $project_root/test.dart first" | 41 "Fixing up something": "$dart $project_root/test.dart first" |
23 }, | 42 }, |
24 "post_pub_build_hooks" : { | 43 "post_pub_build_hooks" : { |
25 "Fixing up something": "$dart $project_root/test.dart first" | 44 "Fixing up something": "$dart $project_root/test.dart first" |
26 }, | 45 }, |
27 "pre_pub_upgrade_hooks" : { | 46 "pre_pub_upgrade_hooks" : { |
28 "Mess up": "$dart $project_root -la" | 47 "Mess up": "$dart $project_root -la" |
29 }, | 48 }, |
30 "pre_test_hooks" : { | 49 "pre_test_hooks" : { |
31 "Fix tests": "$dart $project_root/test.dart foo", | 50 "Fix tests": "$dart $project_root/test.dart foo", |
32 "Fix tests some more": "$dart $project_root/test.dart bar" | 51 "Fix tests some more": "$dart $project_root/test.dart bar" |
33 }, | 52 }, |
34 "post_test_hooks" : { | 53 "post_test_hooks" : { |
35 "Code coverage": "$dart $project_root/test.dart coverage" | 54 "Code coverage": "$dart $project_root/test.dart coverage" |
36 } | 55 } |
37 } | 56 } |
| 57 |
| 58 Alternatively you can give a custom script to run: |
| 59 { |
| 60 "use_custom_script" : "$python tools/annotated_scripts.py" |
| 61 } |
38 """ | 62 """ |
39 | 63 |
40 | 64 |
41 class ConfigParser(object): | 65 class ConfigParser(object): |
42 """ | 66 """ |
43 Encapsulation of package testing config. | 67 Encapsulation of package testing config. |
44 Hooks, which are lists of commands, are read from a JSON file. | 68 Hooks, which are lists of commands, are read from a JSON file. |
45 The objects are simply instantiated with a file parameter. | 69 The objects are simply instantiated with a file parameter. |
46 - file: The file to get the config from | 70 - file: The file to get the config from |
47 There are a number of magic markers that can be used in the config: | 71 There are a number of magic markers that can be used in the config: |
48 $dart: the full path to the Dart vm | 72 $dart: the full path to the Dart vm |
49 $project_root: path to the package being tested | 73 $project_root: path to the package being tested |
| 74 $python: path to a python executable |
50 """ | 75 """ |
51 def __init__(self, file, dart_binary, project_root): | 76 def __init__(self, file): |
52 self.config = self._get_config(file) | 77 self.config = self._get_config(file) |
53 self.dart_binary = dart_binary | 78 self._validate_config_file() |
54 self.project_root = project_root | |
55 | 79 |
56 def _validate_config_value(self, value, key): | 80 def _validate_config_file(self): |
57 valid_types = VALID_TYPES[key] | 81 for (key, value) in self.config.iteritems(): |
58 if value: | 82 if not (key in VALID_TYPES): |
59 if isinstance(value, valid_types): | 83 raise Exception("Unknown configuration key %s" % key) |
60 return value | 84 type_test = VALID_TYPES[key] |
61 else: | 85 type_test(key, value) |
62 error = 'Wrong type for key "%s", expecting %s, got %s' % (key, | 86 if "use_custom_script" in self.config and len(self.config) > 1: |
63 valid_types, | 87 raise Exception("Cannot use 'use_custom_script' combined with hooks.") |
64 type(value)) | |
65 raise Exception(error) | |
66 | |
67 def _is_string(self, value): | |
68 return isinstance(value, basestring) | |
69 | |
70 def _guarantee_dict_of_strings(self, hooks): | |
71 if len(hooks) == 0: | |
72 return | |
73 if not all([self._is_string(v) for v in hooks.keys()]): | |
74 raise Exception("Command names must be strings, was %s" % hooks) | |
75 if not all([self._is_string(v) for v in hooks.values()]): | |
76 raise Exception("All commands must be strings, was %s" % hooks) | |
77 | |
78 def _fill_magic_markers(self, hooks): | |
79 for k, v in hooks.iteritems(): | |
80 v = v.replace('$dart', self.dart_binary) | |
81 v = v.replace('$project_root', self.project_root) | |
82 hooks[k] = v | |
83 | 88 |
84 def _get_hooks(self, hook_kind): | 89 def _get_hooks(self, hook_kind): |
85 hooks = self._validate_config_value(self.config.get(hook_kind), | 90 return self.config.get(hook_kind) or {} |
86 hook_kind) or {} | |
87 self._guarantee_dict_of_strings(hooks) | |
88 self._fill_magic_markers(hooks) | |
89 return hooks | |
90 | 91 |
91 def get_pre_pub_upgrade_hooks(self): | 92 def get_pre_pub_upgrade_hooks(self): |
92 return self._get_hooks('pre_pub_upgrade_hooks') | 93 return self._get_hooks('pre_pub_upgrade_hooks') |
93 | 94 |
94 def get_pre_pub_build_hooks(self): | 95 def get_pre_pub_build_hooks(self): |
95 return self._get_hooks('pre_pub_build_hooks') | 96 return self._get_hooks('pre_pub_build_hooks') |
96 | 97 |
97 def get_post_pub_build_hooks(self): | 98 def get_post_pub_build_hooks(self): |
98 return self._get_hooks('post_pub_build_hooks') | 99 return self._get_hooks('post_pub_build_hooks') |
99 | 100 |
100 def get_pre_test_hooks(self): | 101 def get_pre_test_hooks(self): |
101 return self._get_hooks('pre_test_hooks') | 102 return self._get_hooks('pre_test_hooks') |
102 | 103 |
103 def get_post_test_hooks(self): | 104 def get_post_test_hooks(self): |
104 return self._get_hooks('post_test_hooks') | 105 return self._get_hooks('post_test_hooks') |
105 | 106 |
| 107 def get_custom_script(self): |
| 108 return self.config.get('use_custom_script') or None |
| 109 |
106 def _get_config(self, file): | 110 def _get_config(self, file): |
107 if os.path.isfile(file): | 111 if os.path.isfile(file): |
108 return json.loads(open(file).read()) | 112 return json.loads(open(file).read()) |
109 else: | 113 else: |
110 print("No config test file in package") | 114 print("No config test file in package") |
111 return {} | 115 return {} |
112 | 116 |
113 def __str__(self): | 117 def __str__(self): |
114 config_string = json.dumps(self.config, indent=2) | 118 config_string = json.dumps(self.config, indent=2) |
115 return 'dart: %s\nproject_root: %s\nconfig: \n%s' % (self.dart_binary, | 119 return 'dart: %s\nproject_root: %s\nconfig: \n%s' % (self.dart_binary, |
116 self.project_root, | 120 self.project_root, |
117 config_string) | 121 config_string) |
118 | 122 |
119 if __name__ == '__main__': | 123 if __name__ == '__main__': |
120 parser = ConfigParser('.test_config', 'daaaaaart', 'foobar') | 124 parser = ConfigParser('.test_config', 'daaaaaart', 'foobar') |
121 parser.get_pre_pub_build_hooks() | 125 parser.get_pre_pub_build_hooks() |
122 parser.get_pre_pub_upgrade_hooks() | 126 parser.get_pre_pub_upgrade_hooks() |
123 parser.get_pre_test_hooks() | 127 parser.get_pre_test_hooks() |
124 parser.get_post_test_hooks() | 128 parser.get_post_test_hooks() |
125 print parser | 129 print parser |
126 | 130 |
127 | 131 |
OLD | NEW |