-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep_until_modified.py
executable file
·111 lines (88 loc) · 2.89 KB
/
sleep_until_modified.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# vi:ts=4 sw=4 et
# TODO list:
# * Add support for pyinotify (falling back to dumb polling if it's not
# available)
# * Add support for monitoring multiple files, instead of just one.
import getopt
import os
import os.path
import sys
import time
available_parameters = [
("h", "help", "Print help"),
("i:","interval=", "Defines the polling interval, in seconds (default=1.0)"),
]
class ProgramOptions(object):
"""Holds the program options, after they are parsed by parse_options()"""
def __init__(self):
self.poll_interval = 1
self.args = []
def print_help():
scriptname = os.path.basename(sys.argv[0])
print "Usage: {0} [options] filename".format(scriptname)
print "Sleeps until 'filename' has been modified."
print ""
print "Options:"
long_length = 2 + max(len(long) for x,long,y in available_parameters)
for short, long, desc in available_parameters:
if short and long:
comma = ", "
else:
comma = " "
if short == "":
short = " "
else:
short = "-" + short[0]
if long:
long = "--" + long
print " {0}{1}{2:{3}} {4}".format(short,comma,long,long_length, desc)
print ""
print "Currently, it is implemented using polling. In future, support for pyinotify might be added."
print ""
print "Sample usage command-line:"
print " while sleep_until_modified.py myfile.tex || sleep 1; do make ; done "
def parse_options(argv, opt):
"""argv should be sys.argv[1:]
opt should be an instance of ProgramOptions()"""
try:
opts, args = getopt.getopt(
argv,
"".join(short for short,x,y in available_parameters),
[long for x,long,y in available_parameters]
)
except getopt.GetoptError as e:
print str(e)
print "Use --help for usage instructions."
sys.exit(2)
for o,v in opts:
if o in ("-h", "--help"):
print_help()
sys.exit(0)
elif o in ("-i", "--interval"):
opt.poll_interval = float(v)
else:
print "Invalid parameter: {0}".format(o)
print "Use --help for usage instructions."
sys.exit(2)
opt.args = args
if len(args) == 0:
print "Missing filename"
print "Use --help for usage instructions."
sys.exit(2)
if len(args) > 1:
print "Currently, this script monitors only one file, but {0} files were given. Aborting.".format(len(args))
sys.exit(2)
def main():
opt = ProgramOptions()
parse_options(sys.argv[1:], opt)
file = opt.args[0]
prev_time = os.stat(file).st_mtime
while True:
time.sleep(opt.poll_interval)
new_time = os.stat(file).st_mtime
if new_time != prev_time:
break
if __name__ == "__main__":
main()