Chromium Code Reviews| Index: build/android/pylib/utils/test_output_saver_factory.py |
| diff --git a/build/android/pylib/utils/test_output_saver_factory.py b/build/android/pylib/utils/test_output_saver_factory.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bc7d227e0d671a627752540fa82f7c3b6f6b74da |
| --- /dev/null |
| +++ b/build/android/pylib/utils/test_output_saver_factory.py |
| @@ -0,0 +1,122 @@ |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os |
| +import shutil |
| +import urllib |
| + |
| +from pylib import constants |
| +from pylib.utils import logdog_helper |
| +from pylib.utils import google_storage_helper |
| +from pylib.utils import time_helper |
| + |
| + |
| +class Datatype(object): |
|
jbudorick
2017/06/20 14:12:55
_TestOutputSaver, its subclasses, and this class s
|
| + HTML = 'html' |
| + IMAGE = 'image' |
| + TEXT = 'text' |
| + |
| + |
| +def CreateTestOutputSaver(args): |
| + if args.local_output: |
| + return _LocalTestOutputSaver( |
| + # TODO(mikecase): Find a generic way of getting the suite name. |
| + suite=args.command, |
| + output_dir=constants.GetOutDirectory()) |
| + elif args.gs_results_bucket: |
| + return _RemoteTestOutputSaver( |
| + bucket=args.gs_results_bucket) |
| + else: |
| + return _NoopTestOutputSaver() |
| + |
| + |
| +class _TestOutputSaver(object): |
|
jbudorick
2017/06/20 14:12:55
TestOutputDelegate?
|
| + |
| + def __init__(self): |
| + pass |
| + |
| + def Save(self, in_file, out_file, out_subdir, datatype): |
| + """Saves file to specified location. |
| + |
| + Args |
| + file: Path to file you want to save. |
| + output_relpath: Relative path to save file to, |
| + |
| + Returns |
| + Link or path to saved file. |
| + """ |
| + raise NotImplementedError |
| + |
| + |
| +class _LocalTestOutputSaver(_TestOutputSaver): |
| + |
| + def __init__(self, suite, output_dir): |
| + """Saves files locally in output directory. |
| + |
| + Location files will be saved is {output_dir}/{suitename}_{timestamp}. |
| + """ |
| + super(_LocalTestOutputSaver, self).__init__() |
| + timestamp = time_helper.timestamp(local=True) |
|
jbudorick
2017/06/20 14:12:54
Get rid of time_helper and just write the timestam
|
| + self._output_root = os.path.join( |
| + output_dir, '%s_%s' % (suite, timestamp)) |
| + |
| + #override |
| + def Save(self, in_file, out_file, out_subdir, datatype): |
| + assert datatype in (Datatype.TEXT, Datatype.IMAGE, Datatype.HTML) |
|
jbudorick
2017/06/20 14:12:55
Handle this w/ a regular exception rather than an
|
| + |
| + output_dir = os.path.join(self._output_root, out_subdir) |
| + if not os.path.exists(output_dir): |
| + os.makedirs(output_dir) |
| + shutil.copy(in_file, os.path.join(output_dir, out_file)) |
| + return 'file://%s' % urllib.quote(os.path.join(output_dir, out_file)) |
| + |
| + |
| +class _RemoteTestOutputSaver(_TestOutputSaver): |
| + |
| + def __init__(self, bucket): |
| + """Uploads output files to Google Storage or LogDog. |
| + |
| + Files will either be uploaded directly to Google Storage or LogDog |
| + depending on the datatype. |
| + |
| + Args |
| + bucket: Bucket to use when saving to Google Storage. |
| + """ |
| + super(_RemoteTestOutputSaver, self).__init__() |
| + self._bucket = bucket |
| + |
| + #override |
| + def Save(self, in_file, out_file, out_subdir, datatype): |
| + """ |
| + Args |
| + in_filepath: Path to file you want to save. |
| + out_filename: Name you want to save file as. Unused for |Datatype.image| |
| + which uses content addressed naming scheme. |
| + out_subdir: Subdirectory to save file to. When saving |Datatype.text| this |
| + is converted to part of the stream name. |
| + """ |
| + assert datatype in (Datatype.TEXT, Datatype.IMAGE, Datatype.HTML) |
|
jbudorick
2017/06/20 14:12:55
same
|
| + |
| + link = '' |
| + if datatype == Datatype.TEXT: |
| + with open(in_file, 'r') as f: |
| + link = logdog_helper.text('%s_%s' % (out_subdir, out_file), f.read()) |
| + elif datatype == Datatype.IMAGE: |
| + link = google_storage_helper.upload_content_addressed( |
| + in_file, self._bucket) |
| + elif datatype == Datatype.HTML: |
| + link = google_storage_helper.upload_content_addressed( |
| + in_file, self._bucket, content_type='text/html') |
| + |
| + return link |
| + |
| + |
| +class _NoopTestOutputSaver(_TestOutputSaver): |
| + |
| + def __init__(self): |
| + super(_NoopTestOutputSaver, self).__init__() |
| + |
| + #override |
| + def Save(self, *args, **kwargs): |
| + pass |