-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaa.py
executable file
·693 lines (604 loc) · 23.4 KB
/
aa.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# Copyright 2011 Lab Macambira
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#-----------------------------------------------------------------------------
import sys, os, time, atexit, urllib, urllib2, json, io, ConfigParser, csv
from signal import SIGTERM
from datetime import datetime, timedelta
try:
import pynotify
if not pynotify.init("AA"):
print("there was a problem initializing the pynotify module")
except:
print("you don't seem to have pynotify installed")
guide = """
_.__o_oOoOo[ AA ]oOoOo_o__._
Usage:
aa config <config> <value> ... sets the config value
aa start ... starts the work session of the day
aa stop ... stops the work session
aa shout <message> ... alerts what he is doing now (online)
aa post <message> ... alerts what he is doing now (offline)
aa push ... pushes the log to the server
aa status ... checks daemon status
aa logview ... shows your current log
aa time ... shows time and timeslot info
aa revive ... revives the session after a crash
aa tickets ... shows your active tickets (if you are a Macambira)
aa showticket <ticket number> ... shows a ticket on trac
Remember, you have to configure your nickname before start:
aa config user.nickname <YOUR NICKNAME HERE>
Workflow:
.-.
v |
start -> shout -> stop -> push
or
post
"""
configuration = ConfigParser.RawConfigParser()
__init = """
[user]
nickname=
email=
tick=900
[server]
url=http://www.pulapirata.com/skills/aa/parser.php
"""
def init_config():
"""Checks if configuration file exists, case not creates one with initial layout"""
try:
open(__get_config_file())
except IOError:
configuration.readfp(io.BytesIO(__init))
#FIXME implement with dictionaries maybe
__save()
def __save():
"""Saves configuration options to file"""
with open(__get_config_file(), "wb") as f:
configuration.write(f)
def __get_config_file():
"""Gets configuration file name"""
return os.getenv('HOME')+'/.aaconfig'
def config(params):
"""Receives a list with attribute and value.
Parses attribute for section information and saves value associated."""
configuration.read(__get_config_file())
#if section user not present, then create config file with initial layout
if not configuration.has_section('user'):
init_config()
#checks if params is the right size
if len(params) == 2:
attribute, value = params
#if attribute is like section.attribute parses it
if attribute.count('.') == 1:
section, attribute = attribute.split('.')
#if section does not exist creates it
if not configuration.has_section(section):
configuration.add_section(section)
#else set correspondent value
configuration.set(section, attribute, value)
else:
#if no section is specified, add attribute to user section
configuration.set('user', attribute, value)
__save()
def get(params):
"""Receives a list with section and attribute name and returns the correspondent value"""
configuration.read(__get_config_file())
if (len(params)) == 2:
section, attribute = params
try:
return configuration.get(section, attribute)
except ConfigParser.NoOptionError:
pass
return None
#
# Calcula timeslots dos logs
#
class Slotador():
"""Classe para calcular timeslots dos logs
Para usar, instancie:
s = Slotador()
E pegue a lista dos numeros de slots ja respondidos
s.respondidos
Ex. dessa lista:
[1, 2, 4, 5]
Isso quer dizer que vc respondeu corretamente nos timeslots 1,2,4 e 5
Mas perdeu o 3
"""
def __init__(self):
self.atualizar()
def atualizar(self):
"""Recarrega logs e recalcula os timeslots"""
home = os.getenv('HOME')
f = open(home + '/.aa.log', 'r')
linhas = f.read().splitlines()
f.close()
mensagens = []
self.fim = None
for linha in linhas:
data, texto = linha.split(',', 1)
if texto != 'notify':
data = self.interpretar_data(data)
if texto == 'start':
self.inicio = data
elif texto == 'stop':
self.fim = data
else:
mensagens.append((data, texto))
self.respondidos = []
for m in mensagens:
slot = self.timeslotar(m[0])
if slot != -1:
if slot not in self.respondidos:
self.respondidos.append(slot)
if self.fim == None:
agora = datetime.now()
else:
agora = self.fim
agora = agora - self.inicio
minu = agora.total_seconds()/60.0
atual = int((minu-5)/15)
self.perdidos = []
for i in range(1,atual+1):
if i not in self.respondidos:
self.perdidos.append(i)
self.fim_teorico = self.inicio + timedelta(minutes=120)
self.faltam = self.fim_teorico - datetime.now()
self.faltam_m = int(self.faltam.total_seconds()/60+1)
self.prox_time = (self.faltam_m-5) % 15
self.decorridos_m = int(minu)
def atual_respondido(self):
"""Retorna se o timeslot atual ja foi respondido"""
atual = self.timeslotar(datetime.now())
if atual != -1:
if atual not in self.respondidos:
return False
return True
def interpretar_data(self, texto):
"""Converte de string para date"""
return datetime.strptime(texto, '%Y-%m-%d %H-%M-%S')
def timeslotar(self, tempo):
"""Dado um tempo qualquer, calcula em que timeslot eles caem"""
tempo = tempo - self.inicio
minu = tempo.total_seconds()/60.0
resto = minu%15
# A mensagem nao caiu em nenhum timeslot valido
if resto > 5.0 and resto < 10.0:
return -1
else:
return int(round((minu+0.1)/15,0))
#
# Generic Double-fork based Daemon
#
class Daemon:
"""
A generic daemon class. From Sander Marechal
<http://www.jejik.com/authors/sander_marechal/>
Usage: subclass the Daemon class and override the run() method
"""
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
self.pidfile = pidfile
def daemonize(self):
"""
Do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
or http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/
"""
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError as e:
sys.stderr.write("[AA] Fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError as e:
sys.stderr.write("[AA] Fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(self.stdin, 'r')
so = file(self.stdout, 'a+')
se = file(self.stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile
atexit.register(self.delpid)
pid = str(os.getpid())
file(self.pidfile,'w+').write("%s\n" % pid)
def delpid(self):
os.remove(self.pidfile)
def start(self):
"""
Start the daemon
"""
# Check for a pidfile to see if the daemon already runs
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if pid:
message = "[AA] pidfile %s already exists. Daemon already running?\n"
sys.stderr.write(message % self.pidfile)
sys.exit(1)
# Start the daemon
self.daemonize()
self.run()
def stop(self):
"""
Stop the daemon
"""
pid = self.getpid()
if not pid:
message = "[AA] pidfile %s does not exist. Daemon not running?\n"
sys.stderr.write(message % self.pidfile)
return # not an error in a restart
# Try killing the daemon process
try:
while 1:
os.kill(pid, SIGTERM)
time.sleep(0.1)
except OSError as err:
err = str(err)
if err.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print(str(err))
sys.exit(1)
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
def restart(self):
"""
Restart the daemon
"""
self.stop()
self.start()
def run(self):
"""
You should override this method when you subclass Daemon. It will be called after the process has been
daemonized by start() or restart().
"""
pass
def getpid(self):
# Get the pid from the pidfile
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
return pid
#
# AA Daemon
#
class AADaemon(Daemon):
"""
The AA daemon. The function run() runs forever notifying the user about the remaining time of
his session.
"""
def run(self):
"""
This method runs forever (note the while true loop), notifying the user every N seconds.
"""
self.logger = AALogger()
avisos_padroes = 0
avisos_tick = 0
inicio = time.time()
atual = time.time()
# self.notify('Your session has started, %s. Programming, modafoca! :-)' %
self.notify('Your session has started, %s.' % get(['user','nickname']))
s = Slotador()
while True:
tick = int(get(['user','tick']))
prox_aviso_padrao = 10*60 + avisos_padroes * 15*60
prox_aviso_tick = avisos_tick * tick
if prox_aviso_padrao < prox_aviso_tick:
prox_aviso = prox_aviso_padrao
avisos_padroes += 1
else:
prox_aviso = prox_aviso_tick
avisos_tick += 1
dormir = prox_aviso + inicio - atual
if dormir > 0:
time.sleep(float(dormir))
atual = time.time()
minutos = int((atual-inicio)/60)
#segundos = int((atual-inicio)%60)
s.atualizar()
if not s.atual_respondido():
self.notify('Tic-tac... ' + str(minutos) + ' minutos' )
self.logger.log('notify') # precisamos notificar isso no log?
# FIXME: notificar a cada X minutos e informar quanto tempo falta
# FIXME: como verificar que o usuario logou? fica a cargo do servidor?
def notify(self, msg, lang='pt'):
"""
A simple wrapper to Ubuntu's notify-send.
"""
#os.system('notify-send "AA [%s]: " "%s"' % (time.strftime("%Y-%m-%d %H-%M-%S"), msg))
pynotify.Notification("AA [%s]" % time.strftime("%Y-%m-%d %H-%M-%S"),
msg, 'face-monkey').show()
if lang != '':
lang = '-v ' + lang
retcode = os.system('espeak {0} "{1}" >/dev/null 2>&1 &'.format(lang, msg))
#FIXME generally retcode won't work with async exec
if retcode != 0:
sys.stderr.write("some problem occurred with espeak.")
sys.stderr.write("trying again with error messages enabled.")
os.system('espeak {0} "{1}" >/dev/null 2>&1'.format(lang, msg))
#
# AA HTTP Sender
#
class AAHTTPSender:
"""
The AA HTTP Sender module. It sends the HTTP messages to the server.
"""
def send(self, msg):
"""
Sends the msg to the server, encoding it apropriatelly.
"""
dic = {'json': msg}
data = urllib.urlencode(dic)
req = urllib2.Request(get(['server', 'url']), data.encode('ascii'))
res = urllib2.urlopen(req)
res.close()
def send_log(self):
"""
Uses the send() method to send every line of the ~/.aa.log to the server.
"""
home = os.getenv('HOME')
f = open(home + '/.aa.log', 'r')
alerts = f.read().splitlines()
f.close()
d = [{'user': get(['user','nickname']), 'date': a.split(',')[:2][0], 'log': a.split(',')[:2][1]} for a in alerts]
for linha in d:
ret = linha['log'].split(' ', 1)
if len(ret) == 2:
for tipo in ['scream', 'say', 'oalert', 'shout']:
ret[0] = ret[0].replace(tipo, 'alert')
linha['log'] = ret[0] + ' ' + ret[1]
j = json.dumps(d)
self.send(j)
#
# AA Logger
#
class AALogger:
"""
The AA Logger module. It writes every msg to the ~/.aa.log file.
"""
def __init__(self):
"""
Creates the logger and set the log file path to ~/.aa.log.
"""
self.log_filename = os.getenv('HOME') + '/.aa.log'
def write(self, msg):
"""
A wrapper to append msg to ~/.aa.log.
"""
self.log_file = open(self.log_filename, 'a')
self.log_file.write(msg)
self.log_file.close()
def log(self, msg):
"""
A wrapper to log msg to ~/.aa.log.
"""
self.write(time.strftime("%Y-%m-%d %H-%M-%S") + ',' + msg + '\n')
def start(self):
"""
Starts the logger by creating/overwriting the ~/.aa.log temp file.
"""
self.log_file = open(self.log_filename, 'w')
class Console():
def __init__(self):
# Creating the AA modules
# Here we create a logger obj to log every msg to the ~/.aa.log
self.logger = AALogger()
# And this module deals with the HTTP server
self.http_sender = AAHTTPSender()
# Here the daemon that notifies the user every N seconds
# /tmp/aad.pid has the PID of the forked daemon
self.daemon = AADaemon('/tmp/aad.pid','/dev/null','/tmp/aa.stdout','/tmp/aa.stderr')
def send_scream(self, msg):
# log a scream action
self.logger.log('shout ' + msg)
# send the msg to the HTTP server, so it'll be online imediatelly!
j = json.dumps([{'user': get(['user','nickname']), 'date': time.strftime("%Y-%m-%d %H-%M-%S"), 'log': 'shout ' + msg}])
self.http_sender.send(j)
def stop(self):
if not self.daemon_running():
print('[AA] Daemon not running, so no point in trying to stop it.')
sys.exit(0)
# log a stop session action
self.logger.log('stop')
# the daemon notifies that the session is finished
self.daemon.notify('Your session has finished. Dont forget to record your screencast.','')
# kill the daemon
self.daemon.stop()
def daemon_running(self):
if self.daemon.getpid() is not None:
return True
else:
return False
def start(self):
init_config()
# checks if the user nickname is defined
if get(['user','nickname']) is '':
print('[AA] Please, set your nickname before start hacking. Use: aa config user.nickname <YOUR NICKNAME>.')
sys.exit(0)
# start the logger (overwrite or create the ~/.aa.log file)
self.logger.start()
# log a start session action
self.logger.log('start')
# fork the daemon and exit
self.daemon.start()
def push(self):
# log a push action
self.logger.log('push')
# send all the lines at ~/.aa.log file
self.http_sender.send_log()
# notify to the user the push action
# self.daemon.notify('Session pushed to the server. Now get away of this fucking laptop and go fuck.')
self.daemon.notify('Session pushed to the server.','')
# FIXME: I'm tired now, but this would be more interesting in a proper class
def get_trac_tickets(self, sf_user=None):
if sf_user is not None:
nick = sf_user
else:
nick = get(['user','nickname'])
req = urllib2.Request('http://sourceforge.net/apps/trac/labmacambira/report/1?format=tab')
res = urllib2.urlopen(req)
tab = csv.DictReader(res, delimiter='\t')
c = 0
print('\n')
for line in tab:
if (nick in line['owner']) or ('everyone' in line['owner']):
print(" #%-10s %-20s %s" % (line['ticket'], line['component'], line['summary']))
c += 1
if c is not 0:
print('\n[AA] You have %s active tickets. Lets hack modafoca!' % c)
else:
print('\n[AA] You dont have active tickets actually.')
def process_args(self):
# Parsing console arguments
# FIXME: talvez usar o argparse?
args = sys.argv[1:]
if len(sys.argv) > 1:
# START
if args[0] in ['start', 'inicio', 'inicia', 'início', 'begin']:
if not self.daemon_running():
self.start()
# inform to the user at console
print('[AA] Your session has started. Happy hacking!')
else:
print('[AA] Error: Daemon seems to be already running. Try to stop and start again')
# REVIVE
elif args[0] in ['revive','ressuscitar', 'resurrect', 'reviver']:
if not self.daemon_running():
print('[AA] What have you done? Run to the hills! The daemon has been'+\
' ressurected and will unleash hell upon us ALL!')
self.daemon.start()
# inform to the user at console
else:
print('[AA] These dark magics are not needed now. The daemon '+\
'is already alive and spreading chaos upon the world.')
# STOP
elif args[0] in ['stop','fim', 'finaliza', 'termina', 'end']:
self.stop()
# inform to the user at console
print('[AA] Your session has finished. Dont forget to record your screencast.')
# ALERT
elif args[0] in ['alert', 'informa', 'marca', 'anota', 'msg', 'post']:
if len(args) < 2:
print('[AA] Please specify a message to post. Use: aa %s <message>' % args[0])
sys.exit(0)
# no matter if we use quotes or not after the "aa alert"
msg = ''.join([pal+" " for pal in sys.argv[2:]])
msg = msg.strip()
# log a alert action
self.logger.log('alert ' + msg)
# inform the user
print('[AA] New alert: "%s" logged.' % msg)
# SCREAM
# Esses nomes estao sendo usados no PUSH! Se alterar aqui, altere
# lá!
elif args[0] in ['scream', 'say', 'oalert', 'shout']:
if len(args) < 2:
print('[AA] Please specify a message to say. Use: aa %s <message>' % args[0])
sys.exit(0)
msg = ''.join([pal+" " for pal in sys.argv[2:]])
msg = msg.strip()
self.send_scream(msg)
# inform the user
print('[AA] New shout: "%s" logged.' % msg)
# CONFIG
elif args[0] in ['config', 'configura', 'seta']:
if len(args) < 3:
print('[AA] Missing arguments. Use: aa %s <config> <value>' % args[0])
sys.exit(0)
config(sys.argv[2:])
# STATUS
elif args[0] in ['status', 'st']:
if self.daemon_running():
print('[AA] daemon is up and running... (pid: %s)' % self.daemon.getpid())
else:
print('[AA] Oh nooo! daemon is not running... Get back to work!!!')
# LOGVIEW
elif args[0] in ['logview', 'viewlog']:
os.system('less ' + os.getenv('HOME') + '/.aa.log')
# TIMESLOTS
elif args[0] in ['timeslots', 'ts', 'slots', 'time']:
s = Slotador()
print("Trabalhando ha: "+str(s.decorridos_m)+" minutos")
print("Faltam: "+str(s.faltam_m)+" minutos")
print("Tempo até proximo timeslot: "+str(s.prox_time)+" minutos")
print("")
print("Timeslots:")
print("respondidos: "+str(s.respondidos))
print("perdidos: "+str(s.perdidos))
if len(s.perdidos) == 0:
print("Muito bem! Agora continue trabalhando!")
else:
print("NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!")
# PUSH
elif args[0] in ['push']:
self.push()
print('[AA] Session pushed to the server.')
# TICKETS
elif args[0] in ['tickets']:
if len(args) >= 2:
self.get_trac_tickets(args[1])
else:
self.get_trac_tickets()
# VIEWTICKET
elif args[0] in ['viewticket', 'showticket']:
if len(args) < 2:
print('[AA] Missing arguments. Use: aa %s <ticket number>' % args[0])
sys.exit(0)
print('[AA] Openning ticket #%s in your browser...' % args[1])
os.system('firefox http://sourceforge.net/apps/trac/labmacambira/ticket/%s' % args[1])
# UNKNOWN OPTION
else:
print('[AA] Unknown option: "%s". Please, try again!' % args[0])
sys.exit(2)
sys.exit(0)
else:
print(guide)
sys.exit(2)
#
# Main Function (start here!)
#
if __name__ == "__main__":
c = Console()
c.process_args()