OLD | NEW |
| (Empty) |
1 # Copyright (c) 2010 The Chromium OS 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, os, re | |
6 from autotest_lib.client.bin import site_log_reader, site_utils, test | |
7 from autotest_lib.client.common_lib import error, utils | |
8 from autotest_lib.client.cros.constants import CLEANUP_LOGS_PAUSED_FILE | |
9 | |
10 class LogRotationPauser(object): | |
11 """ | |
12 Class to control when logs are rotated from either server or client. | |
13 | |
14 Assumes all setting of CLEANUP_LOGS_PAUSED_FILE is done by this class | |
15 and that all calls to begin and end are properly | |
16 nested. For instance, [ a.begin(), b.begin(), b.end(), a.end() ] is | |
17 supported, but [ a.begin(), b.begin(), a.end(), b.end() ] is not. | |
18 We do support redundant calls to the same class, such as | |
19 [ a.begin(), a.begin(), a.end() ]. | |
20 """ | |
21 def __init__(self, host=None): | |
22 self._host = host | |
23 self._begun = False | |
24 self._is_nested = True | |
25 | |
26 | |
27 def _run(self, command, *args, **dargs): | |
28 if self._host: | |
29 return self._host.run(command, *args, **dargs).exit_status | |
30 else: | |
31 return utils.system(command, *args, **dargs) | |
32 | |
33 | |
34 def begin(self): | |
35 """Make sure that log rotation is disabled.""" | |
36 if self._begun: | |
37 return | |
38 self._is_nested = (self._run(('[ -r %s ]' % | |
39 CLEANUP_LOGS_PAUSED_FILE), | |
40 ignore_status=True) == 0) | |
41 if self._is_nested: | |
42 logging.info('File %s was already present' % | |
43 CLEANUP_LOGS_PAUSED_FILE) | |
44 else: | |
45 self._run('touch ' + CLEANUP_LOGS_PAUSED_FILE) | |
46 self._begun = True | |
47 | |
48 | |
49 def end(self): | |
50 assert self._begun | |
51 if not self._is_nested: | |
52 self._run('rm -f ' + CLEANUP_LOGS_PAUSED_FILE) | |
53 else: | |
54 logging.info('Leaving existing %s file' % CLEANUP_LOGS_PAUSED_FILE) | |
55 self._begun = False | |
OLD | NEW |