Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 # Copyright 2017 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 os | |
| 6 import shutil | |
| 7 import urllib | |
| 8 | |
| 9 from pylib import constants | |
| 10 from pylib.utils import logdog_helper | |
| 11 from pylib.utils import google_storage_helper | |
| 12 from pylib.utils import time_helper | |
| 13 | |
| 14 | |
| 15 class Datatype(object): | |
| 
 
jbudorick
2017/06/20 14:12:55
_TestOutputSaver, its subclasses, and this class s
 
 | |
| 16 HTML = 'html' | |
| 17 IMAGE = 'image' | |
| 18 TEXT = 'text' | |
| 19 | |
| 20 | |
| 21 def CreateTestOutputSaver(args): | |
| 22 if args.local_output: | |
| 23 return _LocalTestOutputSaver( | |
| 24 # TODO(mikecase): Find a generic way of getting the suite name. | |
| 25 suite=args.command, | |
| 26 output_dir=constants.GetOutDirectory()) | |
| 27 elif args.gs_results_bucket: | |
| 28 return _RemoteTestOutputSaver( | |
| 29 bucket=args.gs_results_bucket) | |
| 30 else: | |
| 31 return _NoopTestOutputSaver() | |
| 32 | |
| 33 | |
| 34 class _TestOutputSaver(object): | |
| 
 
jbudorick
2017/06/20 14:12:55
TestOutputDelegate?
 
 | |
| 35 | |
| 36 def __init__(self): | |
| 37 pass | |
| 38 | |
| 39 def Save(self, in_file, out_file, out_subdir, datatype): | |
| 40 """Saves file to specified location. | |
| 41 | |
| 42 Args | |
| 43 file: Path to file you want to save. | |
| 44 output_relpath: Relative path to save file to, | |
| 45 | |
| 46 Returns | |
| 47 Link or path to saved file. | |
| 48 """ | |
| 49 raise NotImplementedError | |
| 50 | |
| 51 | |
| 52 class _LocalTestOutputSaver(_TestOutputSaver): | |
| 53 | |
| 54 def __init__(self, suite, output_dir): | |
| 55 """Saves files locally in output directory. | |
| 56 | |
| 57 Location files will be saved is {output_dir}/{suitename}_{timestamp}. | |
| 58 """ | |
| 59 super(_LocalTestOutputSaver, self).__init__() | |
| 60 timestamp = time_helper.timestamp(local=True) | |
| 
 
jbudorick
2017/06/20 14:12:54
Get rid of time_helper and just write the timestam
 
 | |
| 61 self._output_root = os.path.join( | |
| 62 output_dir, '%s_%s' % (suite, timestamp)) | |
| 63 | |
| 64 #override | |
| 65 def Save(self, in_file, out_file, out_subdir, datatype): | |
| 66 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
 
 | |
| 67 | |
| 68 output_dir = os.path.join(self._output_root, out_subdir) | |
| 69 if not os.path.exists(output_dir): | |
| 70 os.makedirs(output_dir) | |
| 71 shutil.copy(in_file, os.path.join(output_dir, out_file)) | |
| 72 return 'file://%s' % urllib.quote(os.path.join(output_dir, out_file)) | |
| 73 | |
| 74 | |
| 75 class _RemoteTestOutputSaver(_TestOutputSaver): | |
| 76 | |
| 77 def __init__(self, bucket): | |
| 78 """Uploads output files to Google Storage or LogDog. | |
| 79 | |
| 80 Files will either be uploaded directly to Google Storage or LogDog | |
| 81 depending on the datatype. | |
| 82 | |
| 83 Args | |
| 84 bucket: Bucket to use when saving to Google Storage. | |
| 85 """ | |
| 86 super(_RemoteTestOutputSaver, self).__init__() | |
| 87 self._bucket = bucket | |
| 88 | |
| 89 #override | |
| 90 def Save(self, in_file, out_file, out_subdir, datatype): | |
| 91 """ | |
| 92 Args | |
| 93 in_filepath: Path to file you want to save. | |
| 94 out_filename: Name you want to save file as. Unused for |Datatype.image| | |
| 95 which uses content addressed naming scheme. | |
| 96 out_subdir: Subdirectory to save file to. When saving |Datatype.text| this | |
| 97 is converted to part of the stream name. | |
| 98 """ | |
| 99 assert datatype in (Datatype.TEXT, Datatype.IMAGE, Datatype.HTML) | |
| 
 
jbudorick
2017/06/20 14:12:55
same
 
 | |
| 100 | |
| 101 link = '' | |
| 102 if datatype == Datatype.TEXT: | |
| 103 with open(in_file, 'r') as f: | |
| 104 link = logdog_helper.text('%s_%s' % (out_subdir, out_file), f.read()) | |
| 105 elif datatype == Datatype.IMAGE: | |
| 106 link = google_storage_helper.upload_content_addressed( | |
| 107 in_file, self._bucket) | |
| 108 elif datatype == Datatype.HTML: | |
| 109 link = google_storage_helper.upload_content_addressed( | |
| 110 in_file, self._bucket, content_type='text/html') | |
| 111 | |
| 112 return link | |
| 113 | |
| 114 | |
| 115 class _NoopTestOutputSaver(_TestOutputSaver): | |
| 116 | |
| 117 def __init__(self): | |
| 118 super(_NoopTestOutputSaver, self).__init__() | |
| 119 | |
| 120 #override | |
| 121 def Save(self, *args, **kwargs): | |
| 122 pass | |
| OLD | NEW |