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.
11 from lib
.subcommand
import SubCommand
12 from lib
.symbol
import SymbolDataSources
15 LOGGER
= logging
.getLogger('dmprof')
18 class UploadCommand(SubCommand
):
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')
26 def do(self
, sys_argv
):
27 options
, args
= self
._parse
_args
(sys_argv
, 2)
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()
38 handle_zip
, filename_zip
= tempfile
.mkstemp('.zip', 'dmprof')
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
)))
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
))))
56 returncode
= UploadCommand
._run
_gsutil
(
57 options
.gsutil
, 'cp', '-a', 'public-read', filename_zip
, gs_path
)
59 os
.remove(filename_zip
)
64 def _run_gsutil(gsutil
, *args
):
65 """Run gsutil as a subprocess.
68 *args: Arguments to pass to gsutil. The first argument should be an
69 operation such as ls, cp or cat.
71 The return code from the process.
73 command
= [gsutil
] + list(args
)
74 LOGGER
.info("Running: %s", command
)
77 return subprocess
.call(command
)
79 LOGGER
.error('Error to run gsutil: %s', e
)