/
peon.py
138 lines (111 loc) · 4.18 KB
/
peon.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Original By Jeff Winkler, http://jeffwinkler.net
# Got the code at http://jeffwinkler.net/2006/04/27/keeping-your-nose-green/
# Copyright Bernardo Heynemann <heynemann@gmail.com>
# Licensed under the Open Software License ("OSL") v. 3.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.opensource.org/licenses/osl-3.0.php
# 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.
import sys
import glob
import os
import stat
import time
import optparse
from os.path import abspath, dirname, join
_checksum = 0
_pattern = None
class Urgency(object):
low = 0
normal = 1
critical = 2
def _get_stats_from_filename(filename):
stats = os.stat(filename)
return stats[stat.ST_SIZE] + stats[stat.ST_MTIME]
def _get_checksum_from_dir(dirpath, pattern):
result = 0
for file in glob.glob(os.path.join(dirpath, pattern)):
absolute_filename = os.path.abspath(file)
result += _get_stats_from_filename(absolute_filename)
return result
def checksum_recursively(directory, pattern):
result = 0
for dirpath, dirs, files in os.walk(directory):
result += _get_checksum_from_dir(dirpath, pattern)
return result
def something_has_changed(dir, pattern):
global _checksum
global _pattern
if _pattern != pattern:
_pattern = pattern
_checksum = checksum_recursively(dir, _pattern)
new_checksum = checksum_recursively(dir, _pattern)
if new_checksum != _checksum:
_checksum = new_checksum
return True
return False
def main():
'''
Watch for changes in files that match the pattern in a directory.
Default dir is '.' and default pattern is '*.py'.
Whenever a change to any matched file in directory happens, peon runs
the command specified or nosetests by default
'''
parser = optparse.OptionParser()
parser.add_option('-d', '--dir', default='.', dest='directory',
help='the directory peon will watch for changes')
parser.add_option('-p', '--pattern', default='*.py', dest='pattern',
help='the glob pattern to watch for changes. '\
'(default is "*.py)"')
options, args = parser.parse_args()
directory = options.directory
pattern = options.pattern
command = ' '.join(args) or 'nosetests'
is_build_broken = False
try:
while True:
if something_has_changed(directory, pattern):
os.system('reset')
status = os.system(command)
if status != 0:
is_build_broken = True
notify("Broken build",
"Your command of '%s' returned exit"\
"code '%s'. Please verify the console output for"\
"more info." % (command, status),
"stop.png",
urgency=Urgency.critical)
elif is_build_broken:
is_build_broken = False
notify("Build fixed",
"Your build with command '%s' IS FIXED!" % command,
"tick.png")
time.sleep(1)
except KeyboardInterrupt:
return
def notify(title, message, image, urgency=Urgency.normal):
try:
import pynotify
except:
return
urgencies = {
Urgency.low: pynotify.URGENCY_LOW,
Urgency.normal: pynotify.URGENCY_NORMAL,
Urgency.critical: pynotify.URGENCY_CRITICAL,
}
if pynotify.init("Nosy"):
n = pynotify.Notification(title,
message,
abspath(join(dirname(__file__), image)))
n.set_urgency(urgencies[urgency])
n.show()
time.sleep(2)
n.close()
if __name__ == '__main__':
sys.exit(main())