| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
import os |
|---|
| 4 |
import sys |
|---|
| 5 |
import string |
|---|
| 6 |
import getopt |
|---|
| 7 |
import ConfigParser |
|---|
| 8 |
import smtplib |
|---|
| 9 |
from email.MIMEText import MIMEText |
|---|
| 10 |
import time |
|---|
| 11 |
|
|---|
| 12 |
class TicketReminder(object): |
|---|
| 13 |
|
|---|
| 14 |
env = None |
|---|
| 15 |
|
|---|
| 16 |
def __init__(self, env, parameters): |
|---|
| 17 |
self.env = env |
|---|
| 18 |
self.db = None |
|---|
| 19 |
self.recipients = [] |
|---|
| 20 |
|
|---|
| 21 |
def start(self): |
|---|
| 22 |
self.db = self.env.get_db_cnx() |
|---|
| 23 |
cursor = self.db.cursor() |
|---|
| 24 |
sql = "select time,summary,owner,id from ticket where status = 'new' order by time DESC;" |
|---|
| 25 |
cursor.execute(sql) |
|---|
| 26 |
ntickets = cursor.fetchall() |
|---|
| 27 |
sql = """select time,summary,owner,status,id from ticket where status != 'new' |
|---|
| 28 |
and status != 'closed' order by time DESC;""" |
|---|
| 29 |
cursor.execute(sql) |
|---|
| 30 |
otickets = cursor.fetchall() |
|---|
| 31 |
msg = "Report open Tickets on Trac " + self.env.project_name + ":\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n" |
|---|
| 32 |
msg = msg + "New tickets:\n\n" |
|---|
| 33 |
for nt in ntickets: |
|---|
| 34 |
msg = msg + "Date: " + self.ts2date(nt[0]) + "\n" |
|---|
| 35 |
msg = msg + "Summary: " + nt[1] + "\n" |
|---|
| 36 |
if nt[2] != None: |
|---|
| 37 |
msg = msg + "Owner: " + nt[2] + "\n" |
|---|
| 38 |
msg = msg + "Url: " + self.env.project_url + "ticket/" + str(nt[3]) + "\n" |
|---|
| 39 |
msg = msg + "------------------------------\n" |
|---|
| 40 |
msg = msg + "\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n" |
|---|
| 41 |
msg = msg + "Active Tickets:\n\n" |
|---|
| 42 |
for ot in otickets: |
|---|
| 43 |
msg = msg + "Date: " + self.ts2date(ot[0]) + "\n" |
|---|
| 44 |
msg = msg + "Summary: " + ot[1] + "\n" |
|---|
| 45 |
if ot[2] != None: |
|---|
| 46 |
msg = msg + "Owner: " + ot[2] + "\n" |
|---|
| 47 |
msg = msg + "State: " + ot[3] + "\n" |
|---|
| 48 |
msg = msg + "Url: " + self.env.project_url + "ticket/" + str(ot[4]) + "\n" |
|---|
| 49 |
msg = msg + "------------------------------\n" |
|---|
| 50 |
mailmsg = MIMEText(msg) |
|---|
| 51 |
mailmsg['Subject'] = "Report open Tickets on Trac " + self.env.project_name + " " + self.ts2date(time.time()) |
|---|
| 52 |
mailmsg['From'] = 'advisor@trac.nexlab.it' |
|---|
| 53 |
mailmsg['To'] = settings['sendto'].split(',')[0] |
|---|
| 54 |
cc = None |
|---|
| 55 |
for addr in settings['sendto'].split(',')[1:]: |
|---|
| 56 |
if cc == None: |
|---|
| 57 |
cc = addr.replace(" ", "") |
|---|
| 58 |
else: |
|---|
| 59 |
cc = cc + "," + addr.replace(" ", "") |
|---|
| 60 |
if cc: |
|---|
| 61 |
mailmsg['Cc'] = cc |
|---|
| 62 |
s = smtplib.SMTP() |
|---|
| 63 |
s.connect() |
|---|
| 64 |
s.sendmail(mailmsg['From'], mailmsg['To'], mailmsg.as_string()) |
|---|
| 65 |
s.close() |
|---|
| 66 |
|
|---|
| 67 |
def ts2date(self, ts): |
|---|
| 68 |
return time.strftime('%d-%m-%y %H:%M', time.localtime(ts)) |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
def ReadConfig(name, config): |
|---|
| 74 |
""" |
|---|
| 75 |
Parse the config file |
|---|
| 76 |
""" |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
if name: |
|---|
| 80 |
if not config.has_section(name): |
|---|
| 81 |
print "Not a valid project name: %s" %name |
|---|
| 82 |
print "Valid names: %s" %config.sections() |
|---|
| 83 |
sys.exit(1) |
|---|
| 84 |
|
|---|
| 85 |
project = dict() |
|---|
| 86 |
for option in config.options(name): |
|---|
| 87 |
project[option] = config.get(name, option) |
|---|
| 88 |
|
|---|
| 89 |
else: |
|---|
| 90 |
project = config.defaults() |
|---|
| 91 |
|
|---|
| 92 |
return project |
|---|
| 93 |
|
|---|
| 94 |
def GetConfigSections(file): |
|---|
| 95 |
if not os.path.isfile(file): |
|---|
| 96 |
print 'File %s does not exist' %file |
|---|
| 97 |
sys.exit(1) |
|---|
| 98 |
config = ConfigParser.ConfigParser() |
|---|
| 99 |
try: |
|---|
| 100 |
config.read(file) |
|---|
| 101 |
except ConfigParser.MissingSectionHeaderError,detail: |
|---|
| 102 |
print detail |
|---|
| 103 |
sys.exit(1) |
|---|
| 104 |
|
|---|
| 105 |
return [config, config.sections()] |
|---|
| 106 |
|
|---|
| 107 |
if __name__ == '__main__': |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
configfile = '/etc/tracremind.conf' |
|---|
| 111 |
project = '' |
|---|
| 112 |
component = '' |
|---|
| 113 |
ENABLE_SYSLOG = 0 |
|---|
| 114 |
|
|---|
| 115 |
try: |
|---|
| 116 |
opts, args = getopt.getopt(sys.argv[1:], 'f:p:', ['file=', 'project=']) |
|---|
| 117 |
except getopt.error,detail: |
|---|
| 118 |
print __doc__ |
|---|
| 119 |
print detail |
|---|
| 120 |
sys.exit(1) |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
projects_name = [] |
|---|
| 124 |
for opt,value in opts: |
|---|
| 125 |
if opt in ['-f', '--file']: |
|---|
| 126 |
configfile = value |
|---|
| 127 |
elif opt in ['-p', '--project']: |
|---|
| 128 |
projects_name = [value] |
|---|
| 129 |
if len(projects_name) == 0: |
|---|
| 130 |
projects_name = [None] |
|---|
| 131 |
|
|---|
| 132 |
config, allprojects = GetConfigSections(configfile) |
|---|
| 133 |
|
|---|
| 134 |
if projects_name[0] == 'all': |
|---|
| 135 |
projects_name = allprojects |
|---|
| 136 |
|
|---|
| 137 |
for pname in projects_name: |
|---|
| 138 |
settings = ReadConfig(pname, config) |
|---|
| 139 |
if not settings.has_key('project'): |
|---|
| 140 |
print __doc__ |
|---|
| 141 |
print 'No Trac project is defined in the tracremind config file.' |
|---|
| 142 |
sys.exit(1) |
|---|
| 143 |
print pname |
|---|
| 144 |
try: |
|---|
| 145 |
from trac.env import Environment |
|---|
| 146 |
from trac.ticket import Ticket |
|---|
| 147 |
from trac.web.href import Href |
|---|
| 148 |
from trac import util |
|---|
| 149 |
|
|---|
| 150 |
env = Environment(settings['project'], create=0) |
|---|
| 151 |
tktremind = TicketReminder(env, settings) |
|---|
| 152 |
tktremind.start() |
|---|
| 153 |
|
|---|
| 154 |
except: |
|---|
| 155 |
sys.exit(1) |
|---|