| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 The Chromium 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 | |
| 5 import codecs | |
| 6 import gzip | |
| 7 import json | |
| 8 import os | |
| 9 import shutil | |
| 10 import sys | |
| 11 import zipfile | |
| 12 | |
| 13 from profile_chrome import util | |
| 14 | |
| 15 _SRC_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) | |
| 16 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'tracing')) | |
| 17 # pylint: disable=F0401 | |
| 18 from tracing_build import trace2html | |
| 19 | |
| 20 | |
| 21 def _PackageTracesAsHtml(trace_files, html_file): | |
| 22 with codecs.open(html_file, mode='w', encoding='utf-8') as f: | |
| 23 trace2html.WriteHTMLForTracesToFile(trace_files, f) | |
| 24 for trace_file in trace_files: | |
| 25 os.unlink(trace_file) | |
| 26 | |
| 27 | |
| 28 def _CompressFile(host_file, output): | |
| 29 with gzip.open(output, 'wb') as out, \ | |
| 30 open(host_file, 'rb') as input_file: | |
| 31 out.write(input_file.read()) | |
| 32 os.unlink(host_file) | |
| 33 | |
| 34 | |
| 35 def _ArchiveFiles(host_files, output): | |
| 36 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z: | |
| 37 for host_file in host_files: | |
| 38 z.write(host_file) | |
| 39 os.unlink(host_file) | |
| 40 | |
| 41 | |
| 42 def _MergeTracesIfNeeded(trace_files): | |
| 43 if len(trace_files) <= 1: | |
| 44 return trace_files | |
| 45 merge_candidates = [] | |
| 46 for trace_file in trace_files: | |
| 47 with open(trace_file) as f: | |
| 48 # Try to detect a JSON file cheaply since that's all we can merge. | |
| 49 if f.read(1) != '{': | |
| 50 continue | |
| 51 f.seek(0) | |
| 52 try: | |
| 53 json_data = json.load(f) | |
| 54 except ValueError: | |
| 55 continue | |
| 56 merge_candidates.append((trace_file, json_data)) | |
| 57 if len(merge_candidates) <= 1: | |
| 58 return trace_files | |
| 59 | |
| 60 other_files = [f for f in trace_files | |
| 61 if not f in [c[0] for c in merge_candidates]] | |
| 62 merged_file, merged_data = merge_candidates[0] | |
| 63 for trace_file, json_data in merge_candidates[1:]: | |
| 64 for key, value in json_data.items(): | |
| 65 if not merged_data.get(key) or json_data[key]: | |
| 66 merged_data[key] = value | |
| 67 os.unlink(trace_file) | |
| 68 | |
| 69 with open(merged_file, 'w') as f: | |
| 70 json.dump(merged_data, f) | |
| 71 return [merged_file] + other_files | |
| 72 | |
| 73 | |
| 74 def PackageTraces(trace_files, output=None, compress=False, write_json=False): | |
| 75 trace_files = _MergeTracesIfNeeded(trace_files) | |
| 76 if not write_json: | |
| 77 html_file = os.path.splitext(trace_files[0])[0] + '.html' | |
| 78 _PackageTracesAsHtml(trace_files, html_file) | |
| 79 trace_files = [html_file] | |
| 80 | |
| 81 if compress and len(trace_files) == 1: | |
| 82 result = output or trace_files[0] + '.gz' | |
| 83 _CompressFile(trace_files[0], result) | |
| 84 elif len(trace_files) > 1: | |
| 85 result = output or 'chrome-combined-trace-%s.zip' % util.GetTraceTimestamp() | |
| 86 _ArchiveFiles(trace_files, result) | |
| 87 elif output: | |
| 88 result = output | |
| 89 shutil.move(trace_files[0], result) | |
| 90 else: | |
| 91 result = trace_files[0] | |
| 92 return result | |
| OLD | NEW |