File: commitbot.py

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (131 lines) | stat: -rwxr-xr-x 2,992 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
#
# Author: Tobi Vollebregt
#
# Very simple script which is ment to be used as SVN post-commit hook to inform
# users of TASServer of commits.  It does this by logging in as a bot and
# sending a summary of the commit (changed paths and log message) to certain
# channels.  It can be configured in the code below.
#
# Usage: commitbot.py REPOS_PATH REVISION
# Example: /usr/local/bin/commitbot.py /var/svn/spring 3604
#

import os
import socket
import sys
import time



class SvnLook:

	svnlook = "/usr/local/bin/svnlook"

	def __init__(self, repos_path, revision):

		self.repos_path = repos_path
		self.revision = revision

		self.author = self.look('author')[0]
		self.changed = self.look('changed')
		self.date = self.look('date')[0]
		self.log = self.look('log')

		self.added = []
		for line in self.changed:
			if line[0] == 'A':
				self.added += [line[4:]]

		self.deleted = []
		for line in self.changed:
			if line[0] == 'D':
				self.deleted += [line[4:]]

		self.modified = []
		for line in self.changed:
			if line[0] == 'U':
				self.modified += [line[4:]]


	def look(self, command):
		return os.popen("%s %s %s --revision %i" % (self.svnlook, command, self.repos_path, self.revision)).read().strip().split('\n')


	def summary(self):

                #msg = ["r%i | %s | %s" % (self.revision, self.author, self.date)]
		msg = ["/me ====> %s committed revision %d <====" % (self.author, self.revision)]
		indent = "    "

		if len(self.added) > 0:
			msg += ["/me Added:"]
			for line in self.added:
				msg += [indent + line]

		if len(self.deleted) > 0:
			msg += ["/me Deleted:"]
			for line in self.deleted:
				msg += [indent + line]

		if len(self.modified) > 0:
			msg += ["/me Modified:"]
			for line in self.modified:
				msg += [indent + line]

		if len(self.log) > 0:
			msg += ["/me Log:"]
			for line in self.log:
				msg += [indent + line]

		msg += [indent + "."]

		return msg



class TasServer:

	server_address = "lobby.springrts.com"
	server_port = 8200

	username = "CommitBot"
	password = "" # set this to base64 encoded md5 hash of password

	channels = ["commits"]


	def __init__(self):
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.connect((self.server_address, self.server_port))
		self.socket.send("LOGIN %s %s 0 * commitbot.py 0\n" % (self.username, self.password))
		for channel in self.channels:
			self.socket.send("JOIN %s\n" % (channel))
		time.sleep(0.1) #prevent ban for spamming


	def send(self, msg):
		command = "SAY";
		if msg[:4] == "/me ":
			command = "SAYEX"
			msg = msg[4:]
		for channel in self.channels:
			self.socket.send("%s %s %s\n" % (command, channel, msg))
		time.sleep(0.1) #prevent ban for spamming



### main ###

if len(sys.argv) < 3:
	print 'Usage: %s REPOS_PATH REVISION'
	sys.exit(1)

repos_path = sys.argv[1]
revision = int(sys.argv[2])

svnlook = SvnLook(repos_path, revision)
tasserver = TasServer()

for msg in svnlook.summary():
	tasserver.send(msg)