1*1fa4b3daSHector Dearman# Copyright 2014 The Chromium Authors. All rights reserved. 2*1fa4b3daSHector Dearman# Use of this source code is governed by a BSD-style license that can be 3*1fa4b3daSHector Dearman# found in the LICENSE file. 4*1fa4b3daSHector Dearman 5*1fa4b3daSHector Dearmanimport gzip 6*1fa4b3daSHector Dearmanimport os 7*1fa4b3daSHector Dearmanimport time 8*1fa4b3daSHector Dearmanimport zipfile 9*1fa4b3daSHector Dearman 10*1fa4b3daSHector Dearman 11*1fa4b3daSHector Dearmandef ArchiveFiles(host_files, output): 12*1fa4b3daSHector Dearman with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z: 13*1fa4b3daSHector Dearman for host_file in host_files: 14*1fa4b3daSHector Dearman z.write(host_file) 15*1fa4b3daSHector Dearman os.unlink(host_file) 16*1fa4b3daSHector Dearman 17*1fa4b3daSHector Dearmandef CompressFile(host_file, output): 18*1fa4b3daSHector Dearman with gzip.open(output, 'wb') as out, open(host_file, 'rb') as input_file: 19*1fa4b3daSHector Dearman out.write(input_file.read()) 20*1fa4b3daSHector Dearman os.unlink(host_file) 21*1fa4b3daSHector Dearman 22*1fa4b3daSHector Dearmandef ArchiveData(trace_results, output): 23*1fa4b3daSHector Dearman with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z: 24*1fa4b3daSHector Dearman for result in trace_results: 25*1fa4b3daSHector Dearman trace_file = result.source_name + GetTraceTimestamp() 26*1fa4b3daSHector Dearman WriteDataToCompressedFile(result.raw_data, trace_file) 27*1fa4b3daSHector Dearman z.write(trace_file) 28*1fa4b3daSHector Dearman os.unlink(trace_file) 29*1fa4b3daSHector Dearman 30*1fa4b3daSHector Dearmandef WriteDataToCompressedFile(data, output): 31*1fa4b3daSHector Dearman with gzip.open(output, 'wb') as out: 32*1fa4b3daSHector Dearman out.write(data) 33*1fa4b3daSHector Dearman 34*1fa4b3daSHector Dearmandef GetTraceTimestamp(): 35*1fa4b3daSHector Dearman return time.strftime('%Y-%m-%d-%H%M%S', time.localtime()) 36