1# Copyright 2017 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import time 6import os 7import shutil 8 9try: 10 from urllib.parse import quote 11except ImportError: 12 from urllib import quote 13 14from pylib.base import output_manager 15 16 17class LocalOutputManager(output_manager.OutputManager): 18 """Saves and manages test output files locally in output directory. 19 20 Location files will be saved in {output_dir}/TEST_RESULTS_{timestamp}. 21 """ 22 23 def __init__(self, output_dir): 24 super().__init__() 25 timestamp = time.strftime( 26 '%Y_%m_%dT%H_%M_%S', time.localtime()) 27 self._output_root = os.path.abspath(os.path.join( 28 output_dir, 'TEST_RESULTS_%s' % timestamp)) 29 30 #override 31 def _CreateArchivedFile(self, out_filename, out_subdir, datatype): 32 return LocalArchivedFile( 33 out_filename, out_subdir, datatype, self._output_root) 34 35 36class LocalArchivedFile(output_manager.ArchivedFile): 37 38 def __init__(self, out_filename, out_subdir, datatype, out_root): 39 super().__init__(out_filename, out_subdir, datatype) 40 self._output_path = os.path.join(out_root, out_subdir, out_filename) 41 42 def _Link(self): 43 return 'file://%s' % quote(self._output_path) 44 45 def _Archive(self): 46 if not os.path.exists(os.path.dirname(self._output_path)): 47 os.makedirs(os.path.dirname(self._output_path)) 48 shutil.copy(self.name, self._output_path) 49