Commit 1b4aa4cf753833b21f55b0c191e900b9018c5637
1 parent
abc758a9df
Exists in
master
init
Showing 1 changed file with 61 additions and 0 deletions
pytail.py
View file @
1b4aa4c
1 | +#!/usr/bin/env python3 | |
2 | + | |
3 | +import re, os, time, sys | |
4 | +from time import time, sleep | |
5 | +from os.path import join, getsize, getmtime | |
6 | + | |
7 | +if len(sys.argv) == 1: | |
8 | + print("Argument Missing !") | |
9 | + print("Usage:\n {0:s} /path/to/log/file".format(sys.argv[0])) | |
10 | + sys.exit(1) | |
11 | + | |
12 | +startTime = time() | |
13 | +mTimeStart = 0 | |
14 | +fileName = sys.argv[1] | |
15 | + | |
16 | +if not os.path.exists(fileName) or not os.path.isfile(fileName): | |
17 | + print("This file '{0:s}' does not exist !".format(fileName)) | |
18 | + sys.exit(2) | |
19 | + | |
20 | +try: | |
21 | + with open(fileName, 'r') as f: | |
22 | + f.read(1) | |
23 | +except Exception as e: | |
24 | + print("This file '{0:s}' cannot be opened for reading !".format(fileName)) | |
25 | + sys.exit(3) | |
26 | + | |
27 | +folderLogs = os.path.dirname(fileName) | |
28 | + | |
29 | +copyLines = [] | |
30 | + | |
31 | +try: | |
32 | + | |
33 | + while True: | |
34 | + with open(fileName, 'r') as f: | |
35 | + lines = f.readlines() | |
36 | + | |
37 | + show = False | |
38 | + | |
39 | + if len(copyLines) == 0: | |
40 | + showLines = lines[-10:] | |
41 | + show = True | |
42 | + | |
43 | + elif len(copyLines) != len(lines): | |
44 | + showLines = lines[len(copyLines) - len(lines):] | |
45 | + show = True | |
46 | + | |
47 | + if show: | |
48 | + copyLines = lines.copy() | |
49 | + for line in showLines: | |
50 | + print(line, end='') | |
51 | + | |
52 | + sleep(1) | |
53 | + | |
54 | +except KeyboardInterrupt as e: | |
55 | + print("Program stopped by user !") | |
56 | + sys.exit(0) | |
57 | + | |
58 | +except Exception as e: | |
59 | + print("Unknown error during execution !") | |
60 | + print(e) | |
61 | + sys.exit(4) |