root/snippets/commit-notifier.py

Revision 12, 4.0 kB (checked in by hey, 1 year ago)

msnlib을 사용한 커밋 알리미.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2 # *-* coding: utf-8 -*-
3
4 """
5 This is a very simple bot to show how automation using msnlib could be done.
6 It's not quite useful as-is, but provides a good example.
7
8 If you play with it, please let me know.
9 """
10
11
12 # sys, for getting the parameters
13 import sys, os
14
15 # time, for sleeping
16 import time
17
18 # select to wait for events
19 import select
20
21 # socket, to catch errors
22 import socket
23
24 # thread, for creating the worker thread
25 import thread
26
27 # and, of course, msnlib
28 import msnlib
29 import msncb
30
31 os.environ["LANG"] = "ko_KR.utf-8"
32
33 def null(s):
34         "Null function, useful to void debug ones"
35         pass
36
37 msnlib.debug = null
38 msncb.debug = null
39
40 m = msnlib.msnd()
41 m.cb = msncb.cb()
42
43 def get_grouped_list(md):
44         contacts = []
45         db = {}
46         for gid in md.groups.keys():
47                 db[gid] = []
48         for gid in md.groups.keys():
49                 for e in md.users.keys():
50                         if md.users[e].gid == gid:
51                                 db[gid].append(e)
52         gids = db.keys()
53         gids.sort()
54         for gid in gids:
55                 ul = db[gid]
56                 ul.sort()
57                 for email in ul:
58                         u = m.users[email]
59                         if u.status == 'FLN':
60                                 continue
61                         contacts.append(email)
62         return contacts
63
64 def lookSvn(command, repos, rev):
65         result = os.popen("/usr/bin/svnlook %s %s -r %s"%(command, repos, rev))
66         log = ""
67         for i in result.readlines():
68                 if i[-1:] == "\n":
69                         i = i[:-1]
70                 log += i + "\r\n"
71         if log[-4:] == "\r\n\r\n":
72                 log = log[:-2]
73         return log
74
75 def getAuthor(repos, rev):
76         return lookSvn("author", repos, rev)
77
78 def getLog(repos, rev):
79         return lookSvn("log", repos, rev)
80
81 def getChanged(repos, rev):
82         return lookSvn("changed", repos, rev)
83
84 def getContacts():
85         return get_grouped_list(m)
86
87 def sendToAll(log):
88         contacts = getContacts()
89         for contact in contacts:
90                 m.sendmsg(contact, log)
91                 time.sleep(1)
92         time.sleep(1)
93
94 def appendLog(log, string):
95         log += string
96         if 1400 <= len(log):
97                 sendToAll(log[:1500])
98                 print 'Send with overflow: '+log[:1500]
99                 log = log[1500:]
100         return log
101
102 def do_work():
103         """
104         Here you do your stuff and send messages using m.sendmsg()
105         This is the only place your code lives
106         """
107        
108         # wait a bit for everything to settle down (sync taking efect
109         # basically)
110         print 'Wait to login'
111         time.sleep(3)
112         print 'Making log..'
113        
114         repos = sys.argv[1]
115         rev = sys.argv[2]
116
117         m.encoding="UTF-8"
118
119         log = ""
120         log = appendLog(log, "Author: " + getAuthor(repos, rev))
121         log = appendLog(log, "Revision: " + rev + "\r\n")
122         log = appendLog(log, "----\r\n")
123         log = appendLog(log, getChanged(repos, rev))
124         log = appendLog(log, "----\r\n")
125         log = appendLog(log, getLog(repos, rev))
126
127         print 'Made a log'
128         sendToAll(log)
129
130         print 'Send messages: ', log
131
132         # give time to send the messages
133         print 'Wait to send'
134         time.sleep(10)
135         print 'Quit'
136
137         # and then quit
138         quit()
139        
140
141 # you shouldn't need to touch anything past here
142
143
144 # get the login email and password from the parameters
145 try:
146         m.email = "PUT YOUR MSN E-MAIL"
147         m.pwd = "PUT YOUR MSN PASSWORD"
148 except:
149         print "Use: msnbot email password"
150         sys.exit(1)
151
152
153 m.login()
154
155 # this makes the server send you the contact list, and it's recommended that
156 # you do it because you can get in trouble when getting certain events from
157 # people that are not on your list; and it's not that expensive anyway
158 m.sync()
159
160 # any non-offline status will do, otherwise we'll get an error from msn when
161 # sending a message
162 m.change_status("away")
163
164 def quit():
165         try:
166                 m.disconnect()
167         except:
168                 pass
169         print "Exit"
170         sys.exit(0)
171
172 # we start a thread to do the work. it's a thread because we want to share
173 # everything, and fork cow semantics cause problems here
174 thread.start_new_thread(do_work, ())
175
176
177 # we loop over the network socket to get events
178 while 1:
179         # we get pollable fds
180         t = m.pollable()
181         infd = t[0]
182         outfd = t[1]
183
184         # we select, waiting for events
185         try:
186                 fds = select.select(infd, outfd, [], 0)
187         except:
188                 quit()
189        
190         for i in fds[0] + fds[1]:       # see msnlib.msnd.pollable.__doc__
191                 try:
192                         m.read(i)
193                 except ('SocketError', socket.error), err:
194                         print 'Socket Error'
195                         if i != m:
196                                 # user closed a connection
197                                 # note that messages can be lost here
198                                 m.close(i)
199                         else:
200                                 # main socket closed
201                                 quit()
202
203         # sleep a bit so we don't take over the cpu
204         time.sleep(0.01)
205
206
207
Note: See TracBrowser for help on using the browser.