• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import os
19import sys
20import time
21
22LOG_DIR = '/tmp'
23
24
25def GetLatestLog():
26    '''Find the latest vts runner log folder in log directory and return its content.
27
28    Returns:
29        (string, string), returns the latest log file path and log content
30    '''
31    folders = [os.path.join(LOG_DIR, folder_name)
32               for folder_name in os.listdir(LOG_DIR)
33               if os.path.isdir(os.path.join(LOG_DIR, folder_name)) and
34               folder_name.startswith('vts-runner-log')]
35
36    try:
37        folders.sort(
38            lambda folder1, folder2: int(os.path.getmtime(folder1) - os.path.getmtime(folder2)))
39        folder_latest = folders[-1]
40        log_path_latest = os.path.join(folder_latest,
41                                       os.listdir(folder_latest)[0], 'latest',
42                                       'test_run_details.txt')
43        with open(log_path_latest, 'r') as log_latest:
44            return (log_path_latest, log_latest.read())
45    except Exception as e:
46        return (None, None)
47
48
49def StartMonitoring(path_only):
50    '''Pull the latest vts runner log in a loop, and print out any new contents.
51
52    Args:
53        path_only: bool, only print out the latest log path in temporary directory.
54    '''
55    is_first_time = True
56    last_log_path = None
57    last_text = ''
58    while True:
59        log_path_latest, text_latest = GetLatestLog()
60
61        if path_only:
62            print log_path_latest
63            return
64
65        if log_path_latest is None:  # Case: cannot find any runner log
66            time.sleep(2)
67            continue
68
69        if last_log_path == log_path_latest:
70            text_new = text_latest[len(last_text):]
71            last_text = text_latest
72            if text_new:  # Case: runner log file changed
73                if is_first_time:
74                    is_first_time = False
75                    print text_new
76                    continue
77                lines = text_new.split('\n')
78                for l in lines:
79                    print l
80                    time.sleep(0.6 / len(lines))
81            else:  # Case: runner log file didn't change
82                time.sleep(1)
83        else:  # Case: found a new runner log file
84            last_text = ''
85            last_log_path = log_path_latest
86            print '\n' * 6 + '=' * 24 + 'new file' + '=' * 24 + '\n' * 6
87            time.sleep(1)
88
89
90def PrintUsage():
91    print 'A script to read VTS Runner\'s log from temporary directory.'
92    print 'Usage:'
93    print '  -h, --help: print usage.'
94    print '  -p, --path-only: print path to the latest runner file only.'
95    print '                   You may pipe the result to vim for searching.'
96    print '                   Example: script/monitor-runner-output.py --path-only | xargs gedit'
97    print '  -m, --monitor: print VTS runner\'s output in close to real time'
98    print '  If no argument is provided, this script will keep pulling the latest log and print it out.'
99
100
101if __name__ == "__main__":
102    argv = sys.argv
103    path_only = False
104    if len(argv) == 1 or argv[1] == '-h' or argv[1] == '--help':
105        PrintUsage()
106        exit()
107    elif argv[1] == '-p' or argv[1] == '--path-only':
108        path_only = True
109    elif argv[1] == '-m' or argv[1] == '--monitor':
110        path_only = False
111    StartMonitoring(path_only)
112