-
Notifications
You must be signed in to change notification settings - Fork 1
/
popTail
executable file
·82 lines (60 loc) · 2.27 KB
/
popTail
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
#!/usr/bin/env python
#
# Copyright (c) 2016, 2017 Timothy Savannah All Rights Reserved
# under terms of the GPL version 2. You should have recieved a copy of this with distribution,
# as LICENSE. You may also find the full text at https://github.com/kata198/popLines/LICENSE
#
# This program turns files into queues. You can pop off the tail of a file to stdout.
# So your file can contain a list of items to process, and you can use this to pop off that queue.
#
#
# You can also peek (not modify the source) with "--peek"
#vim: set ts=4 sw=4 expandtab
import os
import sys
from PopLines import popTail
def printUsage():
sys.stderr.write('''Usage: popTail (Options) [number of lines] [filename]
Options:
--peek Perform the operation, but don't modify the source file
Removes the given number of lines from the bottom of provided file, returning on stdout.
If the "number of lines" is negative ( -N ), popTail will pop lines,
starting at the tail (end), and stopping at the Nth-line.
For example, if "number of lines" is -2 on a 5 line file, the last 3 will be popped.
Use this program to turn a list into a queue.
''')
if __name__ == '__main__':
args = sys.argv[1:]
if '--help' in args:
printUsage()
sys.exit(1)
if '--peek' in args:
saveChanges = False
args.remove('--peek')
else:
saveChanges = True
if len(args) != 2:
sys.stderr.write('Wrong number of arguments.\n\n')
printUsage()
sys.exit(1)
numLines = args[0]
if not numLines.isdigit() and not (numLines.startswith('-') and numLines[1:].isdigit()):
sys.stderr.write('Number of lines (second argument) must be an integer.\n\n')
printUsage()
sys.exit(1)
numLines = int(numLines)
filename = args[1]
if not os.path.exists(filename):
sys.stderr.write('No such file: %s\n' %(filename,))
sys.exit(2)
if not os.path.isfile(filename):
sys.stderr.write('Not a file: %s\n' %(filename,))
sys.exit(2)
try:
output = popTail(numLines, filename, saveChanges)
except Exception as e:
sys.stderr.write('Error: %s\n' %(str(e,)))
sys.exit(2)
if len(output) > 0:
sys.stdout.write('\n'.join(output))
sys.stdout.write('\n')