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
5import gzip
6import os
7import time
8import zipfile
9
10
11def ArchiveFiles(host_files, output):
12  with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z:
13    for host_file in host_files:
14      z.write(host_file)
15      os.unlink(host_file)
16
17def CompressFile(host_file, output):
18  with gzip.open(output, 'wb') as out, open(host_file, 'rb') as input_file:
19    out.write(input_file.read())
20  os.unlink(host_file)
21
22def ArchiveData(trace_results, output):
23  with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as z:
24    for result in trace_results:
25      trace_file = result.source_name + GetTraceTimestamp()
26      WriteDataToCompressedFile(result.raw_data, trace_file)
27      z.write(trace_file)
28      os.unlink(trace_file)
29
30def WriteDataToCompressedFile(data, output):
31  with gzip.open(output, 'wb') as out:
32    out.write(data)
33
34def GetTraceTimestamp():
35  return time.strftime('%Y-%m-%d-%H%M%S', time.localtime())
36