- add sources.
[platform/framework/web/crosswalk.git] / src / tools / deep_memory_profiler / subcommands / upload.py
1 # Copyright 2013 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 logging
6 import os
7 import subprocess
8 import tempfile
9 import zipfile
10
11 from lib.subcommand import SubCommand
12 from lib.symbol import SymbolDataSources
13
14
15 LOGGER = logging.getLogger('dmprof')
16
17
18 class UploadCommand(SubCommand):
19   def __init__(self):
20     super(UploadCommand, self).__init__(
21         'Usage: %prog upload [--gsutil path/to/gsutil] '
22         '<first-dump> <destination-gs-path>')
23     self._parser.add_option('--gsutil', default='gsutil',
24                             help='path to GSUTIL', metavar='GSUTIL')
25
26   def do(self, sys_argv):
27     options, args = self._parse_args(sys_argv, 2)
28     dump_path = args[1]
29     gs_path = args[2]
30
31     dump_files = SubCommand._find_all_dumps(dump_path)
32     bucket_files = SubCommand._find_all_buckets(dump_path)
33     prefix = SubCommand._find_prefix(dump_path)
34     symbol_data_sources = SymbolDataSources(prefix)
35     symbol_data_sources.prepare()
36     symbol_path = symbol_data_sources.path()
37
38     handle_zip, filename_zip = tempfile.mkstemp('.zip', 'dmprof')
39     os.close(handle_zip)
40
41     try:
42       file_zip = zipfile.ZipFile(filename_zip, 'w', zipfile.ZIP_DEFLATED)
43       for filename in dump_files:
44         file_zip.write(filename, os.path.basename(os.path.abspath(filename)))
45       for filename in bucket_files:
46         file_zip.write(filename, os.path.basename(os.path.abspath(filename)))
47
48       symbol_basename = os.path.basename(os.path.abspath(symbol_path))
49       for filename in os.listdir(symbol_path):
50         if not filename.startswith('.'):
51           file_zip.write(os.path.join(symbol_path, filename),
52                          os.path.join(symbol_basename, os.path.basename(
53                              os.path.abspath(filename))))
54       file_zip.close()
55
56       returncode = UploadCommand._run_gsutil(
57           options.gsutil, 'cp', '-a', 'public-read', filename_zip, gs_path)
58     finally:
59       os.remove(filename_zip)
60
61     return returncode
62
63   @staticmethod
64   def _run_gsutil(gsutil, *args):
65     """Run gsutil as a subprocess.
66
67     Args:
68         *args: Arguments to pass to gsutil. The first argument should be an
69             operation such as ls, cp or cat.
70     Returns:
71         The return code from the process.
72     """
73     command = [gsutil] + list(args)
74     LOGGER.info("Running: %s", command)
75
76     try:
77       return subprocess.call(command)
78     except OSError, e:
79       LOGGER.error('Error to run gsutil: %s', e)