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))) | |
ricow1
2015/05/19 09:58:05
indentation
sigurdm
2015/05/19 13:32:16
Done.
| |
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" : "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 |
50 """ | 74 """ |
51 def __init__(self, file, dart_binary, project_root): | 75 def __init__(self, file): |
52 self.config = self._get_config(file) | 76 self.config = self._get_config(file) |
53 self.dart_binary = dart_binary | 77 self._validate_config_file() |
54 self.project_root = project_root | |
55 | 78 |
56 def _validate_config_value(self, value, key): | 79 def _validate_config_file(self): |
57 valid_types = VALID_TYPES[key] | 80 for (key, value) in self.config.iteritems(): |
58 if value: | 81 if not (key in VALID_TYPES): |
59 if isinstance(value, valid_types): | 82 raise Exception("Unknown configuration key %s" % key) |
60 return value | 83 type_test = VALID_TYPES[key] |
61 else: | 84 type_test(key, value) |
62 error = 'Wrong type for key "%s", expecting %s, got %s' % (key, | 85 if "use_custom_script" in self.config and len(self.config) > 1: |
63 valid_types, | 86 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 | 87 |
84 def _get_hooks(self, hook_kind): | 88 def _get_hooks(self, hook_kind): |
85 hooks = self._validate_config_value(self.config.get(hook_kind), | 89 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 | 90 |
91 def get_pre_pub_upgrade_hooks(self): | 91 def get_pre_pub_upgrade_hooks(self): |
92 return self._get_hooks('pre_pub_upgrade_hooks') | 92 return self._get_hooks('pre_pub_upgrade_hooks') |
93 | 93 |
94 def get_pre_pub_build_hooks(self): | 94 def get_pre_pub_build_hooks(self): |
95 return self._get_hooks('pre_pub_build_hooks') | 95 return self._get_hooks('pre_pub_build_hooks') |
96 | 96 |
97 def get_post_pub_build_hooks(self): | 97 def get_post_pub_build_hooks(self): |
98 return self._get_hooks('post_pub_build_hooks') | 98 return self._get_hooks('post_pub_build_hooks') |
99 | 99 |
100 def get_pre_test_hooks(self): | 100 def get_pre_test_hooks(self): |
101 return self._get_hooks('pre_test_hooks') | 101 return self._get_hooks('pre_test_hooks') |
102 | 102 |
103 def get_post_test_hooks(self): | 103 def get_post_test_hooks(self): |
104 return self._get_hooks('post_test_hooks') | 104 return self._get_hooks('post_test_hooks') |
105 | 105 |
106 def get_custom_script(self): | |
107 return self.config.get('use_custom_script') or None | |
108 | |
106 def _get_config(self, file): | 109 def _get_config(self, file): |
107 if os.path.isfile(file): | 110 if os.path.isfile(file): |
108 return json.loads(open(file).read()) | 111 return json.loads(open(file).read()) |
109 else: | 112 else: |
110 print("No config test file in package") | 113 print("No config test file in package") |
111 return {} | 114 return {} |
112 | 115 |
113 def __str__(self): | 116 def __str__(self): |
114 config_string = json.dumps(self.config, indent=2) | 117 config_string = json.dumps(self.config, indent=2) |
115 return 'dart: %s\nproject_root: %s\nconfig: \n%s' % (self.dart_binary, | 118 return 'dart: %s\nproject_root: %s\nconfig: \n%s' % (self.dart_binary, |
116 self.project_root, | 119 self.project_root, |
117 config_string) | 120 config_string) |
118 | 121 |
119 if __name__ == '__main__': | 122 if __name__ == '__main__': |
120 parser = ConfigParser('.test_config', 'daaaaaart', 'foobar') | 123 parser = ConfigParser('.test_config', 'daaaaaart', 'foobar') |
121 parser.get_pre_pub_build_hooks() | 124 parser.get_pre_pub_build_hooks() |
122 parser.get_pre_pub_upgrade_hooks() | 125 parser.get_pre_pub_upgrade_hooks() |
123 parser.get_pre_test_hooks() | 126 parser.get_pre_test_hooks() |
124 parser.get_post_test_hooks() | 127 parser.get_post_test_hooks() |
125 print parser | 128 print parser |
126 | 129 |
127 | 130 |
OLD | NEW |