-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
119 lines (90 loc) · 3.25 KB
/
monitor.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
#-*-coding:utf-8
'''
PUG-PE Arduino Tools
Por Marcel Pinheiro Caraciolo - caraciol@gmail.com
Victor Case - victor.case@hotmail.com
Gabriel Liber - lsl.gabriel@gmail.com
Diego Liber - diegoliber@gmail.com
Semaforo PUG-PE
'''
from optparse import OptionParser,SUPPRESS_USAGE
from arduino_handler import *
import commands
import os
from time import ctime
import subprocess
import pygame
from pygame.locals import *
__version__ = 0.1
event_happened = False
arduino = None
def git_commit_all(directory):
"""
Adds all files and commits them using git
Code fetched from Flavio Amieiro
https://github.com/flavioamieiro/dojotools/blob/master/dojotools.py
"""
msg = ctime()
process = subprocess.Popen(
"git add . && git commit -m '%s'" % msg,
shell=True,
cwd= directory,
)
#if git returns 128 it means 'command not found' or 'not a git repo'
if process.wait() == 128:
error = ('Impossible to commit to repository. '
'Make sure git is installed an this is a valid repository')
raise OSError(error)
def key_handler():
global event_happened
for event in pygame.event.get():
if not hasattr(event,'key'): continue
if event.key == K_ESCAPE:
sys.exit(0)
elif event.key == K_SPACE and event.type == KEYDOWN:
event_happened = True
def main(options):
global event_happened
#Execute the test and get the output
#Send the ouput to arduino
test_file = options.test_file
test_file = test_file + '.py' if not test_file.endswith('.py') else test_file
output = commands.getoutput('python %s/%s' % (options.directory,test_file))
if 'FAILED' in output or 'Traceback' in output or 'Error' in output:
arduino.send_message(FAIL)
print output
print 'Test has failed.'
else:
arduino.send_message(PASS)
print output
print 'Test has passed.'
#if -c active, add, commit the files.
if options.commit:
git_commit_all(options.directory)
event_happened = False
def run_main(options):
global arduino
#Connect to Arduino
arduino = Arduino()
#Initialize the main program
pygame.init()
windowsSurface = pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption('Semaforo')
while True:
key_handler()
if event_happened:
main(options)
if __name__ == '__main__':
parser = OptionParser(usage=SUPPRESS_USAGE)
print 'PUG-PE Semaforo Monitor v.%s\n' % __version__
print 'Type --help parameter for help.\n'
parser.add_option('-t', '--test-file', dest='test_file',
help='Test File that will be executed and monitored.')
parser.add_option('-c', '--commit', action= 'store_true', dest= 'commit', default= False,
help = 'if this flag is used, a git commit will be issued whenever the files change')
parser.add_option('-d', '--directory', dest= 'directory',
help='Watch Directory', default= os.path.abspath(os.path.curdir))
(options,args) = parser.parse_args()
if not options.test_file:
parser.error('You must specifiy a valid test_file .py (-t parameter) !')
run_main(options)