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 logging | |
6 import os | |
7 | |
8 from devil.utils import reraiser_thread | |
9 | |
10 | |
11 class Datatype(object): | |
12 HTML = 'html' | |
13 IMAGE = 'image' | |
14 TEXT = 'text' | |
15 | |
16 | |
17 class OutputManager(object): | |
18 | |
19 def __init__(self): | |
20 """OutputManager Constructor. | |
21 | |
22 This class provides a simple interface to save test output. Subclasses | |
23 of this will allow users to save test results in the cloud or locally. | |
24 """ | |
25 self._allow_upload = False | |
26 self._thread_group = None | |
27 | |
28 def ArchiveAndDeleteFile( | |
29 self, in_filename, out_filename, out_subdir, datatype=Datatype.TEXT): | |
30 """Context manager used to archive file contents asynchonously. | |
jbudorick
2017/07/19 22:45:09
This doesn't appear to be a context manager any mo
mikecase (-- gone --)
2017/07/26 21:21:37
Done
| |
31 | |
32 Example Usage. | |
33 | |
34 try: | |
35 // Create non-temporary File. Upload is done asynchonously. | |
36 file = ... | |
37 file.write(...) | |
38 finally: | |
39 output_manager.ArchiveAndDeleteFile() | |
jbudorick
2017/07/19 22:45:09
This example doesn't pass any arguments to Archive
mikecase (-- gone --)
2017/07/26 21:21:37
Done
| |
40 | |
41 Args: | |
jbudorick
2017/07/19 22:45:09
in_filename?
mikecase (-- gone --)
2017/07/26 21:21:37
Done
| |
42 out_filename: Name for saved file. | |
43 out_subdir: Directory to save |out_file| in. | |
44 datatype: Datatype of file. | |
45 | |
46 Yields: | |
jbudorick
2017/07/19 22:45:09
nit: Returns, not Yields
mikecase (-- gone --)
2017/07/26 21:21:37
Yeah, I changed the implementation a bunch. Seems
| |
47 A tuple (filename, link). Filename is a file you write to and link is a | |
48 URL to where the contents of the file will end up. | |
49 """ | |
50 assert self._allow_upload, 'Must run |SetUp| before attempting to upload!' | |
jbudorick
2017/07/19 22:45:09
Do this w/ an explicit exception rather than an as
mikecase (-- gone --)
2017/07/26 21:21:37
Done
| |
51 | |
52 job = self._CreateArchiveJob( | |
53 in_filename, out_filename, out_subdir, datatype) | |
54 | |
55 if job is None: | |
56 return '' | |
57 | |
58 def archive(): | |
59 try: | |
60 job.Archive() | |
61 finally: | |
62 os.remove(in_filename) | |
63 | |
64 thread = reraiser_thread.ReraiserThread(func=archive) | |
65 thread.start() | |
66 self._thread_group.Add(thread) | |
67 return job.Link() | |
68 | |
69 def _CreateArchiveJob(self, in_filepath, out_filename, out_subdir, datatype): | |
70 """Returns an instance of Job that actually uploads/saves the file.""" | |
71 raise NotImplementedError | |
72 | |
73 def SetUp(self): | |
74 self._allow_upload = True | |
75 self._thread_group = reraiser_thread.ReraiserThreadGroup() | |
76 | |
77 def TearDown(self): | |
78 self._allow_upload = False | |
79 logging.info('Finishing archiving output.') | |
80 self._thread_group.JoinAll() | |
81 | |
82 def __enter__(self): | |
83 self.SetUp() | |
84 return self | |
85 | |
86 def __exit__(self, _exc_type, _exc_val, _exc_tb): | |
87 self.TearDown() | |
88 | |
89 | |
90 class Job(object): | |
91 | |
92 def __init__(self, in_filepath, out_filename, out_subdir, datatype): | |
93 self._in_filepath = in_filepath | |
94 self._out_filename = out_filename | |
95 self._out_subdir = out_subdir | |
96 self._datatype = datatype | |
97 | |
98 def Link(self): | |
99 raise NotImplementedError | |
100 | |
101 def Archive(self): | |
102 raise NotImplementedError | |
OLD | NEW |