summaryrefslogtreecommitdiffstats
path: root/loginfo-producer
blob: 556aaf8c493dd351173d813e0ac674e1d5ebc1a5 (plain)
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
#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-

# Copyright © 2008 Jeffrey C. Ollie

# 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
import re
import os
import time
import pwd
import socket

from qpid.util import connect
from qpid.connection import Connection
from qpid.datatypes import Message, RangedSet, uuid4
from qpid.queue import Empty

from lxml import etree

host = '127.0.0.1'
port = 5672

root_element = etree.Element('loginfo')

timestamp = time.time()
timestamp_element = etree.SubElement(root_element, 'timestamp')
timestamp_element.text = '%17.6f' % timestamp

uid = os.getuid()
pwd_entry = pwd.getpwuid(uid)

username = pwd_entry[0].decode('utf-8')
username_element = etree.SubElement(root_element, 'username')
username_element.text = username

fullname = pwd_entry[4].decode('utf-8')
fullname_element = etree.SubElement(root_element, 'fullname')
fullname_element.text = fullname


cvsserver_element = etree.SubElement(root_element, 'cvsserver')
cvsserver_element.text = socket.gethostname()

cvsroot = os.environ['CVSROOT']

cvsroot_element = etree.SubElement(root_element, 'cvsroot')
cvsroot_element.text = cvsroot

directory_re = re.compile(r'^Update of %s/(.*)$' % (cvsroot,), re.M)

message = sys.stdin.read().decode('utf-8', 'replace')

directory_match = directory_re.search(message)
assert(directory_match)
directory = directory_match.group(1)

directory_element = etree.SubElement(root_element, 'directory')
directory_element.text = directory

filespecs = sys.argv[1].decode('utf-8')
assert(filespecs.startswith(directory + ' '))

filespecs = filespecs[len(directory)+1:]

new_dir_re = re.compile('(.*) - New directory')

filespec_re = re.compile(r'^(.*?),(\d+(?:\.\d+)+|NONE),(\d+(?:\.\d+)+|NONE),(\S*)\s*')

files_element = etree.SubElement(root_element, 'files')

while len(filespecs) > 0:
    filespec_match = filespec_re.match(filespecs)
    assert(filespec_match)
    file_element = etree.SubElement(files_element, 'file')
    filename_element = etree.SubElement(file_element, 'filename')
    filename_element.text = filespec_match.group(1)
    old_version_element = etree.SubElement(file_element, 'old-version')
    old_version_element.text = filespec_match.group(2)
    new_version_element = etree.SubElement(file_element, 'new-version')
    new_version_element.text = filespec_match.group(3)
    if filespec_match.group(4):
        tag_element = etree.SubElement(file_element, 'tag')
        tag_element.text = filespec_match.group(4)

    filespecs = filespecs[filespec_match.end():]

message_element = etree.SubElement(root_element, 'message')
message_element.text = message

socket = connect(host, port)
connection = Connection(sock = socket)
connection.start()
session = connection.session(str(uuid4()))

properties = session.delivery_properties(routing_key = 'loginfo')
session.message_transfer(destination='cvs_exchange',
                         message = Message(properties,
                                           etree.tostring(root_element,
                                                          encoding = 'utf-8',
                                                          xml_declaration = True)))

session.close(timeout=10)