File: outfilter.py

package info (click to toggle)
python-diskimage-builder 3.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,572 kB
  • sloc: sh: 7,380; python: 6,444; makefile: 37
file content (86 lines) | stat: -rwxr-xr-x 2,885 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#
# Copyright 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# This is an output filter to filter and timestamp the logs from Grenade and
# DevStack. Largely our awk filters got beyond the complexity level which were
# sustainable, so this provides us much more control in a single place.
#
# The overhead of running python should be less than execing `date` a million
# times during a run.

import argparse
import datetime
import re
import sys

IGNORE_LINES = re.compile(r'(set \+o|xtrace)')


def get_options():
    parser = argparse.ArgumentParser(
        description='Filter output by DevStack and friends')
    parser.add_argument('-o', '--outfile',
                        help='Output file for content',
                        default=None)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='Write to stdout',
                        default=False)
    parser.add_argument('-b', '--no-timestamp', action='store_true',
                        help='Do not prefix stdout with timestamp (bare)',
                        default=False)
    return parser.parse_args()


def skip_line(line):
    """Should we skip this line."""
    return IGNORE_LINES.search(line) is not None


def main():
    opts = get_options()
    outfile = None
    if opts.outfile:
        outfile = open(opts.outfile, 'ab', 0)

    # Otherwise fileinput reprocess args as files
    sys.argv = []
    for line in iter(sys.stdin.readline, ''):
        # put skip lines here
        if skip_line(line):
            continue

        now = datetime.datetime.utcnow()
        ts_line = ("%s | %s" % (
            now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3],
            line))

        if opts.verbose:
            sys.stdout.write(line if opts.no_timestamp else ts_line)
            sys.stdout.flush()
        if outfile:
            # We've opened outfile as a binary file to get the
            # non-buffered behaviour.  on python3, sys.stdin was
            # opened with the system encoding and made the line into
            # utf-8, so write the logfile out in utf-8 bytes.
            outfile.write(ts_line.encode('utf-8', errors='surrogateescape'))
            outfile.flush()


if __name__ == '__main__':
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        sys.exit(1)