|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
epoger
2013/07/24 21:32:25
Tested as follows:
./show_results.py --output /ho
| |
3 ''' | |
4 Copyright 2013 Google Inc. | |
epoger
2013/07/24 21:32:25
Once we get this committed, we could make it part
epoger
2013/07/24 21:36:50
Actually, Zach, you might have some insight on thi
| |
5 | |
6 Use of this source code is governed by a BSD-style license that can be | |
7 found in the LICENSE file. | |
8 ''' | |
9 | |
10 ''' | |
11 Generate an HTML file with links to view/download currently generated GM results | |
12 ''' | |
13 | |
14 # System-level imports | |
15 import argparse | |
16 import re | |
17 import urllib2 | |
18 | |
19 # Imports from local directory | |
20 import gm_json | |
21 | |
22 IMAGE_FILENAME_RE = re.compile(gm_json.IMAGE_FILENAME_PATTERN) | |
23 | |
24 class _InternalException(Exception): | |
25 pass | |
26 | |
27 class Generator(object): | |
28 """ Generates an HTML file with links to view/download currently generated | |
29 GM results. """ | |
30 | |
31 def __init__(self, input_path, output_path=None): | |
32 """ | |
33 params: | |
34 input_path: filepath (or URL) pointing at JSON summary of actual results | |
35 output_path: filepath to write HTML file to, or None to write to stdout | |
36 """ | |
37 self._input_path = input_path | |
38 self._output_path = output_path | |
39 self._output_file = None | |
40 | |
41 def _GetFileContents(self, filepath): | |
42 """ Returns the full contents of filepath, as a single string. | |
43 If filepath looks like a URL, try to read it that way instead of as | |
44 a path on local storage. | |
45 Raises _InternalException if there is a problem. | |
46 """ | |
47 if filepath.startswith('http:') or filepath.startswith('https:'): | |
48 try: | |
49 return urllib2.urlopen(filepath).read() | |
50 except urllib2.HTTPError as e: | |
51 raise _InternalException('unable to read URL %s: %s' % (filepath, e)) | |
52 else: | |
53 return open(filepath, 'r').read() | |
54 | |
55 def _OpenOutput(self): | |
56 """Prepare to write output to either stdout or a file. | |
57 """ | |
58 if self._output_path: | |
59 self._output_file = open(self._output_path, 'w') | |
60 | |
61 def _CloseOutput(self): | |
62 """Close the output file, if necessary. | |
63 """ | |
64 if self._output_file: | |
65 self._output_file.close() | |
66 self._output_file = None | |
67 | |
68 def _Output(self, string): | |
69 """Write a string to the appropriate output destination (file or stdout). | |
70 """ | |
71 if self._output_file: | |
72 print >> self._output_file, string | |
73 else: | |
74 print string | |
75 | |
76 def Run(self): | |
77 self._OpenOutput() | |
78 self._Output('<html><head><title>%s</title></head><body>' % self._input_path ) | |
79 self._Output('Actual GM results from <a href="%s">%s</a><br>' % ( | |
80 self._input_path, self._input_path)) | |
81 results = self._GetFileContents(self._input_path) | |
82 results_dict = gm_json.LoadFromString(results) | |
83 actual_results = results_dict[gm_json.JSONKEY_ACTUALRESULTS] | |
84 sections = actual_results.keys() | |
85 for section in sections: | |
86 self._Output('\n<br><br><br>\nSection: %s<br>' % section) | |
87 section_results = actual_results[section] | |
88 if section_results: | |
89 image_names = sorted(section_results.keys()) | |
90 for image_name in image_names: | |
91 image_results = section_results[image_name] | |
92 image_url = gm_json.CreateGmActualUrl( | |
93 test_name=IMAGE_FILENAME_RE.match(image_name).group(1), | |
94 hash_type=image_results[0], | |
95 hash_digest=image_results[1]) | |
96 self._Output('<a href="%s">%s</a><br>' % (image_url, image_name)) | |
97 self._Output('</body></html>') | |
98 self._CloseOutput() | |
99 | |
100 def Main(): | |
101 parser = argparse.ArgumentParser() | |
102 parser.add_argument( | |
103 'input', | |
104 help='filepath or URL containing a JSON summary of actual GM results') | |
105 parser.add_argument( | |
106 '--output', | |
107 help='filepath to write HTML output to; by default, writes to stdout') | |
108 args = parser.parse_args() | |
109 generator = Generator(input_path=args.input, output_path=args.output) | |
110 generator.Run() | |
111 | |
112 if __name__ == '__main__': | |
113 Main() | |
OLD | NEW |