From f8f939634396158de53fb26fa7f9a539a92fb219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Fri, 11 Aug 2017 04:42:19 +0200 Subject: New upstream version 1.8.2 --- mwc.py | 574 ++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 287 insertions(+), 287 deletions(-) diff --git a/mwc.py b/mwc.py index c420a74..4df4799 100755 --- a/mwc.py +++ b/mwc.py @@ -1,4 +1,5 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- # Copyright: (2013-2015) Michael Till Beck # License: GPL-2.0+ @@ -9,6 +10,7 @@ from lxml import etree from cssselect import GenericTranslator import re import io +import hashlib import smtplib from email.mime.text import MIMEText @@ -51,324 +53,322 @@ mailsession = None # translates all relative URIs found in trees to absolute URIs def toAbsoluteURIs(trees, baseuri): - for tree in trees: - if isinstance(tree, str): - continue - for uriAttribute in uriAttributes: - tags = tree.xpath(uriAttribute[0]) - for tag in tags: - if tag.attrib.get(uriAttribute[1]) != None: - if urllib.parse.urlparse(tag.attrib[uriAttribute[1]]).scheme == '': - tag.attrib[uriAttribute[1]] = urllib.parse.urljoin(baseuri, tag.attrib[uriAttribute[1]]) + for tree in trees: + if isinstance(tree, str): + continue + for uriAttribute in uriAttributes: + tags = tree.xpath(uriAttribute[0]) + for tag in tags: + if tag.attrib.get(uriAttribute[1]) != None: + if urllib.parse.urlparse(tag.attrib[uriAttribute[1]]).scheme == '': + tag.attrib[uriAttribute[1]] = urllib.parse.urljoin(baseuri, tag.attrib[uriAttribute[1]]) def parseSite(site): - file, content, titles, warning = None, None, None, None - - uri = site['uri'] - contenttype = site.get('type', 'html') - contentregex = site.get('contentregex', '') - titleregex = site.get('titleregex', '') - splitregex = site.get('splitregex', '') - enc = site.get('encoding', defaultEncoding) - - contentxpath = site.get('contentxpath', '') - if contentxpath == '' and site.get('contentcss', '') != '': - # CSS - contentxpath = GenericTranslator().css_to_xpath(site.get('contentcss')) - titlexpath = site.get('titlexpath', '') - if titlexpath == '' and site.get('titlecss', '') != '': - titlexpath = GenericTranslator().css_to_xpath(site.get('titlecss')) - - try: - - if uri.startswith(cmdscheme): - # run command and retrieve output - process = subprocess.Popen(uri[len(cmdscheme):], stdout=subprocess.PIPE, shell=True, close_fds=True) - file = process.stdout - else: - # open website - req = urllib.request.Request(uri) - if 'user-agent' in site: - req.add_header('User-Agent', site['user-agent']) - if 'accept' in site: - req.add_header('Accept', site['accept']) - file = urllib.request.urlopen(req) - - - if contenttype == 'text' or (contentxpath == '' and titlexpath == ''): - thefullcontent = file.read().decode(enc) - contents = [thefullcontent] - if splitregex != '': - contents = thefullcontent.split(splitregex) - titles = [] - else: - baseuri = uri - if contenttype == 'html': - parser = etree.HTMLParser(encoding=enc) - else: - parser = etree.XMLParser(recover=True, encoding=enc) - - tree = etree.parse(file, parser) - - # xpath - contentresult = tree.xpath(contentxpath) if contentxpath else [] - titleresult = tree.xpath(titlexpath) if titlexpath else [] - - # translate relative URIs to absolute URIs - if contenttype == 'html': - basetaglist = tree.xpath('/html/head/base') - if len(basetaglist) != 0: - baseuri = basetaglist[0].attrib['href'] - if len(contentresult) != 0: - toAbsoluteURIs(contentresult, baseuri) - if len(titleresult) != 0: - toAbsoluteURIs(titleresult, baseuri) - - if contentxpath != '' and titlexpath != '' and len(contentresult) != len(titleresult): - warning = 'WARNING: number of title blocks (' + str(len(titleresult)) + ') does not match number of content blocks (' + str(len(contentresult)) + ')' - elif contentxpath and len(contentresult) == 0: - warning = 'WARNING: content selector became invalid!' - elif titlexpath and len(titleresult) == 0: - warning = 'WARNING: title selector became invalid!' - else: - if len(contentresult) == 0: - contentresult = titleresult - if len(titleresult) == 0: - titleresult = contentresult - - if isinstance(contentresult, str): - contents = [contentresult] - else: - contents = [etree.tostring(s, encoding=defaultEncoding, pretty_print=True).decode(defaultEncoding) for s in contentresult] - if isinstance(titleresult, str): - titles = [getSubject(titleresult)] - else: - titles = [getSubject(' '.join(s.xpath('.//text()'))) for s in titleresult] - - except IOError as e: - warning = 'WARNING: could not open URL; maybe content was moved?\n\n' + str(e) - - if file is not None: - file.close() - - if uri.startswith(cmdscheme) and process.wait() != 0: - warning = 'WARNING: process terminated with an error' - - if warning: - return {'content': content, 'titles': titles, 'warning': warning} - - # parse regex - if contentregex: - contents = [x for y in [re.findall(r'' + contentregex, c, re.S) for c in contents] for x in y] - if titleregex: - titles = [x for y in [re.findall(r'' + titleregex, c, re.S) for c in titles] for x in y] - - if contentregex and titleregex and len(contents) != len(titles): - warning = 'WARNING: number of title blocks (' + str(len(titles)) + ') does not match number of content blocks (' + str(len(contents)) + ') after regex' - elif contentregex and len(contents) == 0: - warning = 'WARNING: content regex became invalid!' - elif titleregex and len(titles) == 0: - warning = 'WARNING: title regex became invalid!' + global defaultEncoding + file, content, titles, warning = None, None, None, None + + uri = site['uri'] + contenttype = site.get('type', 'html') + contentregex = site.get('contentregex', '') + titleregex = site.get('titleregex', '') + splitregex = site.get('splitregex', '') + enc = site.get('encoding', defaultEncoding) + + contentxpath = site.get('contentxpath', '') + if contentxpath == '' and site.get('contentcss', '') != '': + # CSS + contentxpath = GenericTranslator().css_to_xpath(site.get('contentcss')) + titlexpath = site.get('titlexpath', '') + if titlexpath == '' and site.get('titlecss', '') != '': + titlexpath = GenericTranslator().css_to_xpath(site.get('titlecss')) + + try: + + if uri.startswith(cmdscheme): + # run command and retrieve output + process = subprocess.Popen(uri[len(cmdscheme):], stdout=subprocess.PIPE, shell=True, close_fds=True) + file = process.stdout else: - if len(contents) == 0: - contents = titles - if len(titles) == 0: - titles = [getSubject(c) for c in contents] - - return {'contents': contents, 'titles': titles, 'warning': warning} + # open website + req = urllib.request.Request(uri) + if 'user-agent' in site: + req.add_header('User-Agent', site['user-agent']) + if 'accept' in site: + req.add_header('Accept', site['accept']) + file = urllib.request.urlopen(req) + + + if contenttype == 'text' or (contentxpath == '' and titlexpath == ''): + thefullcontent = file.read().decode(enc, errors='ignore') + contents = [thefullcontent] + if splitregex != '': + contents = thefullcontent.split(splitregex) + titles = [] + else: + baseuri = uri + if contenttype == 'html': + parser = etree.HTMLParser(encoding=enc) + else: + parser = etree.XMLParser(recover=True, encoding=enc) + + tree = etree.parse(file, parser) + + # xpath + contentresult = tree.xpath(contentxpath) if contentxpath else [] + titleresult = tree.xpath(titlexpath) if titlexpath else [] + + # translate relative URIs to absolute URIs + if contenttype == 'html': + basetaglist = tree.xpath('/html/head/base') + if len(basetaglist) != 0: + baseuri = basetaglist[0].attrib['href'] + if len(contentresult) != 0: + toAbsoluteURIs(contentresult, baseuri) + if len(titleresult) != 0: + toAbsoluteURIs(titleresult, baseuri) + + if contentxpath != '' and titlexpath != '' and len(contentresult) != len(titleresult): + warning = 'WARNING: number of title blocks (' + str(len(titleresult)) + ') does not match number of content blocks (' + str(len(contentresult)) + ')' + elif contentxpath and len(contentresult) == 0: + warning = 'WARNING: content selector became invalid!' + elif titlexpath and len(titleresult) == 0: + warning = 'WARNING: title selector became invalid!' + else: + if len(contentresult) == 0: + contentresult = titleresult + if len(titleresult) == 0: + titleresult = contentresult + + if isinstance(contentresult, str): + contents = [contentresult] + else: + contents = [etree.tostring(s, encoding=enc, pretty_print=True).decode(enc, errors='ignore') for s in contentresult] + if isinstance(titleresult, str): + titles = [getSubject(titleresult)] + else: + titles = [getSubject(etree.tostring(s, method='text', encoding=enc).decode(enc, errors='ignore')) for s in titleresult] + + except IOError as e: + warning = 'WARNING: could not open URL; maybe content was moved?\n\n' + str(e) + + if file is not None: + file.close() + + if uri.startswith(cmdscheme) and process.wait() != 0: + warning = 'WARNING: process terminated with an error' + + if warning: + return {'content': content, 'titles': titles, 'warning': warning} + + # parse regex + if contentregex: + contents = [x for y in [re.findall(r'' + contentregex, c, re.S) for c in contents] for x in y] + if titleregex: + titles = [x for y in [re.findall(r'' + titleregex, c, re.S) for c in titles] for x in y] + + if contentregex and titleregex and len(contents) != len(titles): + warning = 'WARNING: number of title blocks (' + str(len(titles)) + ') does not match number of content blocks (' + str(len(contents)) + ') after regex' + elif contentregex and len(contents) == 0: + warning = 'WARNING: content regex became invalid!' + elif titleregex and len(titles) == 0: + warning = 'WARNING: title regex became invalid!' + else: + if len(contents) == 0: + contents = titles + if len(titles) == 0: + titles = [getSubject(c) for c in contents] + + return {'contents': contents, 'titles': titles, 'warning': warning} # returns a short subject line def getSubject(textContent): - if textContent == None or textContent == '': - return config.subjectPostfix - textContent = re.sub(' +', ' ', re.sub('\s', ' ', textContent)).strip() - return (textContent[:maxTitleLength] + ' [..]') if len(textContent) > maxTitleLength else textContent + if textContent == None or textContent == '': + return config.subjectPostfix + textContent = re.sub(' +', ' ', re.sub('\s', ' ', textContent)).strip() + return (textContent[:maxTitleLength] + ' [..]') if len(textContent) > maxTitleLength else textContent # generates a new RSS feed item def genFeedItem(subject, content, link, change): - feeditem = etree.Element('item') - titleitem = etree.Element('title') - titleitem.text = subject + ' #' + str(change) - feeditem.append(titleitem) - linkitem = etree.Element('link') - linkitem.text = link - feeditem.append(linkitem) - descriptionitem = etree.Element('description') - descriptionitem.text = content - feeditem.append(descriptionitem) - guiditem = etree.Element('guid') - guiditem.text = str(random.getrandbits(32)) - feeditem.append(guiditem) - dateitem = etree.Element('pubDate') - dateitem.text = strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()) - feeditem.append(dateitem) - - return feeditem + feeditem = etree.Element('item') + titleitem = etree.Element('title') + titleitem.text = subject + ' #' + str(change) + feeditem.append(titleitem) + linkitem = etree.Element('link') + linkitem.text = link + feeditem.append(linkitem) + descriptionitem = etree.Element('description') + descriptionitem.text = content + feeditem.append(descriptionitem) + guiditem = etree.Element('guid') + guiditem.text = str(random.getrandbits(32)) + feeditem.append(guiditem) + dateitem = etree.Element('pubDate') + dateitem.text = strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()) + feeditem.append(dateitem) + + return feeditem # sends mail notification def sendmail(receiver, subject, content, sendAsHtml, link): - global mailsession - - if sendAsHtml: - baseurl = None - if link != None: - content = '

' + subject + '

\n' + content - baseurl = urljoin(link, '/') - mail = MIMEText('' + subject + '' + ('' if baseurl else '') + '' + content + '', 'html', defaultEncoding) - else: - if link != None: - content = link + '\n\n' + content - mail = MIMEText(content, 'text', defaultEncoding) + global mailsession, defaultEncoding + + if sendAsHtml: + baseurl = None + if link != None: + content = '

' + subject + '

\n' + content + baseurl = urljoin(link, '/') + mail = MIMEText('' + subject + '' + ('' if baseurl else '') + '' + content + '', 'html', defaultEncoding) + else: + if link != None: + content = link + '\n\n' + content + mail = MIMEText(content, 'text', defaultEncoding) + + mail['From'] = config.sender + mail['To'] = receiver + mail['Subject'] = Header(subject, defaultEncoding) + + # initialize session once, not each time this method gets called + if mailsession is None: + mailsession = smtplib.SMTP(config.smtphost, config.smtpport) + if config.useTLS: + mailsession.ehlo() + mailsession.starttls() + if config.smtpusername is not None: + mailsession.login(config.smtpusername, config.smtppwd) + + mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) - mail['From'] = config.sender - mail['To'] = receiver - mail['Subject'] = Header(subject, defaultEncoding) - # initialize session once, not each time this method gets called - if mailsession is None: - mailsession = smtplib.SMTP(config.smtphost, config.smtpport) - if config.useTLS: - mailsession.ehlo() - mailsession.starttls() - if config.smtpusername is not None: - mailsession.login(config.smtpusername, config.smtppwd) - - mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) +# returns a list of all content that is stored locally for a specific site +def getStoredHashes(shortname): + result = [] + filename = shortname + ".txt" + if os.path.exists(filename): + with open(filename, 'r') as file: + for line in file: + result.append(line.rstrip()) -# returns a list of all content that is stored locally for a specific site -def getFileContents(shortname): - result = [] - for f in os.listdir('.'): - if f.startswith(shortname + '.') and f.endswith('.txt'): - file = open(f, 'rb') - result.append(file.read().decode('utf-8')) - file.close() - return result + return result # updates list of content that is stored locally for a specific site -def storeFileContents(shortname, contents): - for f in os.listdir('.'): - if f.startswith(shortname + '.') and f.endswith('.txt'): - os.remove(f) - - i = 0 - for c in contents: - file = open(shortname + '.' + str(i) + '.txt', 'wb') - file.write(c.encode('utf-8')) - file.close() - i += 1 +def storeHashes(shortname, contentHashes): + with open(shortname + '.txt', 'w') as file: + for h in contentHashes: + file.write(h + "\n") -def pollWebsites(): - # parse existing feed or create a new one - if config.enableRSSFeed: - if os.path.isfile(config.rssfile): - feedXML = etree.parse(config.rssfile) - else: - feedXML = etree.parse(io.StringIO(emptyfeed)) +def pollWebsites(): + global defaultEncoding - # start polling sites - sessionContents = [] - mailsSent = 0 - for site in config.sites: - if config.maxMailsPerSession != -1 and mailsSent >= config.maxMailsPerSession: - break + # parse existing feed or create a new one + if config.enableRSSFeed: + if os.path.isfile(config.rssfile): + feedXML = etree.parse(config.rssfile) + else: + feedXML = etree.parse(io.StringIO(emptyfeed)) + + # start polling sites + mailsSent = 0 + for site in config.sites: + print('polling site [' + site['shortname'] + '] ...') + sessionHashes = [] + parseResult = parseSite(site) + receiver = site.get('receiver', config.receiver) + + # if something went wrong, notify the user + if parseResult['warning']: + subject = '[' + site['shortname'] + '] WARNING' + print('WARNING: ' + parseResult['warning']) + if config.enableMailNotifications: + if config.maxMailsPerSession == -1 or mailsSent < config.maxMailsPerSession: + sendmail(receiver, subject, parseResult['warning'], False, None) + mailsSent = mailsSent + 1 + if config.enableRSSFeed: + feedXML.xpath('//channel')[0].append(genFeedItem(subject, parseResult['warning'], site['uri'], 0)) + else: + # otherwise, check which parts of the site were updated + changes = 0 + fileHashes = getStoredHashes(site['shortname']) + i = 0 + for content in parseResult['contents']: + + contenthash = hashlib.md5(content.encode(defaultEncoding)).hexdigest() + if contenthash not in fileHashes: + if config.maxMailsPerSession == -1 or mailsSent < config.maxMailsPerSession: + changes += 1 + sessionHashes.append(contenthash) + + subject = '[' + site['shortname'] + '] ' + parseResult['titles'][i] + print(' ' + subject) + if config.enableMailNotifications and len(fileHashes) > 0: + sendmail(receiver, subject, content, (site.get('type', 'html') == 'html'), site['uri']) + mailsSent = mailsSent + 1 - print('polling site [' + site['shortname'] + '] ...') - parseResult = parseSite(site) - receiver = site.get('receiver', config.receiver) - - # if something went wrong, notify the user - if parseResult['warning']: - subject = '[' + site['shortname'] + '] WARNING' - print('WARNING: ' + parseResult['warning']) - if config.enableMailNotifications: - sendmail(receiver, subject, parseResult['warning'], False, None) - mailsSent = mailsSent + 1 if config.enableRSSFeed: - feedXML.xpath('//channel')[0].append(genFeedItem(subject, parseResult['warning'], site['uri'], 0)) + feedXML.xpath('//channel')[0].append(genFeedItem(subject, content, site['uri'], changes)) else: - # otherwise, check which parts of the site were updated - changes = 0 - fileContents = getFileContents(site['shortname']) - i = 0 - for content in parseResult['contents']: - if config.maxMailsPerSession != -1 and mailsSent >= config.maxMailsPerSession: - break - - if content not in fileContents: - changes += 1 - sessionContents.append(content) - - subject = '[' + site['shortname'] + '] ' + parseResult['titles'][i] - print(' ' + subject) - if config.enableMailNotifications and len(fileContents) > 0: - sendmail(receiver, subject, content, (site.get('type', 'html') == 'html'), site['uri']) - mailsSent = mailsSent + 1 - - if config.enableRSSFeed: - feedXML.xpath('//channel')[0].append(genFeedItem(subject, content, site['uri'], changes)) - i += 1 - - - if changes > 0: - storeFileContents(site['shortname'], sessionContents) - print(' ' + str(changes) + ' updates') - - # store feed - if config.enableRSSFeed: - for o in feedXML.xpath('//channel/item[position() 0: + storeHashes(site['shortname'], sessionHashes) + print(' ' + str(changes) + ' updates') - try: - opts, args = getopt.getopt(sys.argv[1:], 'hc:d:', ['help', 'config=', 'dry-run=']) - except getopt.GetoptError: - print('Usage: mwc.py --config=config --dry-run=shortname') - sys.exit(1) - for opt, arg in opts: - if opt == '-h': - print('Usage: mwc.py --config=config') - exit() - elif opt in ('-c', '--config'): - configMod = arg - elif opt in ('-d', '--dry-run'): - dryrun = arg - - config = importlib.import_module(configMod) - - if dryrun: - for site in config.sites: - if site['shortname'] == dryrun: - parseResult = parseSite(site) - print(parseResult) - print(str(len(parseResult['contents'])) + " results") - break - else: - try: - pollWebsites() - except: - msg = str(sys.exc_info()[0]) + '\n\n' + traceback.format_exc() - print(msg) - if config.receiver != '': - sendmail(config.receiver, '[mwc] Something went wrong ...', msg, False, None) - - if mailsession: - mailsession.quit() - mailsession = None + # store feed + if config.enableRSSFeed: + for o in feedXML.xpath('//channel/item[position() Date: Fri, 11 Aug 2017 05:41:14 +0200 Subject: New upstream release; refresh patches --- debian/changelog | 5 +- debian/patches/0100-config.diff | 81 +++++++++++++------------- debian/patches/0105-try_mail.diff | 68 +++++++++++----------- debian/patches/0110-syslog.diff | 118 ++++++++++++++------------------------ 4 files changed, 119 insertions(+), 153 deletions(-) diff --git a/debian/changelog b/debian/changelog index 18767a8..63449fb 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,7 @@ -mwc (1.7.6-1) UNRELEASED; urgency=medium +mwc (1.8.2-1) UNRELEASED; urgency=medium - * New upstream release. + * New upstream release (Closes: #862004). + + Refresh patches. * Renumbering patches. * debian/patches/0105-try_mail.diff: - Replace undefined printf with print (Closes: #860494). diff --git a/debian/patches/0100-config.diff b/debian/patches/0100-config.diff index ce4dba7..8529874 100644 --- a/debian/patches/0100-config.diff +++ b/debian/patches/0100-config.diff @@ -5,47 +5,6 @@ Author: Jörg Frings-Fürst Last-Update: 2014-05-12 --- This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ -Index: trunk/mwc.py -=================================================================== ---- trunk.orig/mwc.py -+++ trunk/mwc.py -@@ -319,7 +319,7 @@ def pollWebsites(): - - if __name__ == "__main__": - -- configMod = 'config' -+ configMod = '/etc/mwc/mwc-config' - dryrun = None - - try: -@@ -335,9 +335,24 @@ if __name__ == "__main__": - configMod = arg - elif opt in ('-d', '--dry-run'): - dryrun = arg -- -- config = importlib.import_module(configMod) -- -+ # -+ # add code to load config from nonsystem path -+ # and change to datadir -+ # -+ try: -+ path = os.path.dirname(configMod) -+ fullname = os.path.basename(configMod) -+ sys.path.append(path) -+ config = importlib.import_module(fullname) -+ except: -+ print('Error: loading config') -+ sys.exit(2) -+ try: -+ os.chdir(config.datadir) -+ except: -+ print('Error: datadir not found') -+ sys.exit(3) -+ - if dryrun: - for site in config.sites: - if site['shortname'] == dryrun: Index: trunk/config_template.py =================================================================== --- trunk.orig/config_template.py @@ -56,7 +15,7 @@ Index: trunk/config_template.py # Copyright: (2013-2014) Michael Till Beck # License: GPL-2.0+ -@@ -46,11 +44,11 @@ sender = 'me@mymail.com' +@@ -47,11 +45,11 @@ sender = 'me@mymail.com' smtphost = 'mysmtpprovider.com' useTLS = True smtpport = 587 @@ -113,3 +72,41 @@ Index: trunk/mwcfeedserver.py +except KeyboardInterrupt: + pass +httpd.server_close() +Index: trunk/mwc.py +=================================================================== +--- trunk.orig/mwc.py ++++ trunk/mwc.py +@@ -334,7 +334,7 @@ def pollWebsites(): + + if __name__ == "__main__": + +- configMod = 'config' ++ configMod = '/etc/mwc/mwc-config' + dryrun = None + + try: +@@ -351,7 +351,23 @@ if __name__ == "__main__": + elif opt in ('-d', '--dry-run'): + dryrun = arg + +- config = importlib.import_module(configMod) ++ # ++ # add code to load config from nonsystem path ++ # and change to datadir ++ # ++ try: ++ path = os.path.dirname(configMod) ++ fullname = os.path.basename(configMod) ++ sys.path.append(path) ++ config = importlib.import_module(fullname) ++ except: ++ print('Error: loading config') ++ sys.exit(2) ++ try: ++ os.chdir(config.datadir) ++ except: ++ print('Error: datadir not found') ++ sys.exit(3) + + if dryrun: + for site in config.sites: diff --git a/debian/patches/0105-try_mail.diff b/debian/patches/0105-try_mail.diff index d390b6d..bc62ef1 100644 --- a/debian/patches/0105-try_mail.diff +++ b/debian/patches/0105-try_mail.diff @@ -12,41 +12,41 @@ Index: trunk/mwc.py =================================================================== --- trunk.orig/mwc.py +++ trunk/mwc.py -@@ -225,16 +225,27 @@ def sendmail(receiver, subject, content, - mail['Subject'] = Header(subject, defaultEncoding) +@@ -232,16 +232,28 @@ def sendmail(receiver, subject, content, + mail['Subject'] = Header(subject, defaultEncoding) - # initialize session once, not each time this method gets called -- if mailsession is None: -- mailsession = smtplib.SMTP(config.smtphost, config.smtpport) -- if config.useTLS: -- mailsession.ehlo() -- mailsession.starttls() -- if config.smtpusername is not None: -- mailsession.login(config.smtpusername, config.smtppwd) + # initialize session once, not each time this method gets called +- if mailsession is None: +- mailsession = smtplib.SMTP(config.smtphost, config.smtpport) +- if config.useTLS: +- mailsession.ehlo() +- mailsession.starttls() +- if config.smtpusername is not None: +- mailsession.login(config.smtpusername, config.smtppwd) - -- mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) -- -+ # -+ # add try / except to open mailsession -+ # -+ try: -+ if mailsession is None: -+ mailsession = smtplib.SMTP(config.smtphost, config.smtpport) -+ if config.useTLS: -+ mailsession.ehlo() -+ mailsession.starttls() -+ mailsession.login(config.smtpusername, config.smtppwd) -+ # -+ # add try / except to send mail -+ # -+ except: -+ print('Error: Open smtp-session') -+ exit(4) -+ try: -+ mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) -+ except: -+ print('Error: sendmail') -+ exit(5) +- mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) + ++ # ++ # add try / except to open mailsession ++ # ++ try: ++ if mailsession is None: ++ mailsession = smtplib.SMTP(config.smtphost, config.smtpport) ++ if config.useTLS: ++ mailsession.ehlo() ++ mailsession.starttls() ++ mailsession.login(config.smtpusername, config.smtppwd) ++ # ++ # add try / except to send mail ++ # ++ except: ++ print('Error: Open smtp-session') ++ exit(4) ++ try: ++ mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) ++ except: ++ print('Error: sendmail') ++ exit(5) # returns a list of all content that is stored locally for a specific site - def getFileContents(shortname): + def getStoredHashes(shortname): diff --git a/debian/patches/0110-syslog.diff b/debian/patches/0110-syslog.diff index 12d629d..bd61d81 100644 --- a/debian/patches/0110-syslog.diff +++ b/debian/patches/0110-syslog.diff @@ -8,7 +8,7 @@ Index: trunk/mwc.py =================================================================== --- trunk.orig/mwc.py +++ trunk/mwc.py -@@ -19,6 +19,7 @@ import os +@@ -21,6 +21,7 @@ import os import sys import getopt import traceback @@ -16,81 +16,49 @@ Index: trunk/mwc.py import subprocess -@@ -227,25 +228,28 @@ def sendmail(receiver, subject, content, - # initialize session once, not each time this method gets called - # - # add try / except to open mailsession -- # -+ # -+ - try: -- if mailsession is None: -- mailsession = smtplib.SMTP(config.smtphost, config.smtpport) -- if config.useTLS: -- mailsession.ehlo() -- mailsession.starttls() -- mailsession.login(config.smtpusername, config.smtppwd) -- # -+ if mailsession is None: -+ mailsession = smtplib.SMTP(config.smtphost, config.smtpport) -+ if config.useTLS: -+ mailsession.ehlo() -+ mailsession.starttls() -+ mailsession.login(config.smtpusername, config.smtppwd) -+ except: -+ print('Error: Open smtp-session') -+ syslog.syslog(syslog.LOG_ERR, 'can not open smtp session') -+ exit(4) -+ # - # add try / except to send mail - # -+ try: -+ mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) - except: -- print('Error: Open smtp-session') -- exit(4) -- try: -- mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) -- except: -- print('Error: sendmail') -- exit(5) -+ print('Error: sendmail') -+ syslog.syslog(syslog.LOG_ERR, 'error on sendmail') -+ exit(5) +@@ -248,11 +249,13 @@ def sendmail(receiver, subject, content, + # + except: + print('Error: Open smtp-session') ++ syslog.syslog(syslog.LOG_ERR, 'can not open smtp session') + exit(4) + try: + mailsession.sendmail(config.sender, receiver.split(','), mail.as_string()) + except: + print('Error: sendmail') ++ syslog.syslog(syslog.LOG_ERR, 'error on sendmail') + exit(5) # returns a list of all content that is stored locally for a specific site - def getFileContents(shortname): -@@ -332,7 +336,11 @@ if __name__ == "__main__": +@@ -349,6 +352,11 @@ if __name__ == "__main__": + configMod = '/etc/mwc/mwc-config' + dryrun = None - configMod = '/etc/mwc/mwc-config' - dryrun = None -- -+ -+ # -+ # add syslog open -+ # -+ syslog.openlog() - try: - opts, args = getopt.getopt(sys.argv[1:], 'hc:d:', ['help', 'config=', 'dry-run=']) - except getopt.GetoptError: -@@ -357,11 +365,13 @@ if __name__ == "__main__": - config = importlib.import_module(fullname) - except: - print('Error: loading config') -+ syslog.syslog(syslog.LOG_ERR, 'can not found / load mwc-config') - sys.exit(2) - try: - os.chdir(config.datadir) - except: - print('Error: datadir not found') -+ syslog.syslog(syslog.LOG_ERR, 'datadir not found') - sys.exit(3) - - if dryrun: -@@ -383,3 +393,5 @@ if __name__ == "__main__": - mailsession.quit() - mailsession = None ++ # ++ # add syslog open ++ # ++ syslog.openlog() ++ + try: + opts, args = getopt.getopt(sys.argv[1:], 'hc:d:', ['help', 'config=', 'dry-run=']) + except getopt.GetoptError: +@@ -374,11 +382,13 @@ if __name__ == "__main__": + config = importlib.import_module(fullname) + except: + print('Error: loading config') ++ syslog.syslog(syslog.LOG_ERR, 'can not found / load mwc-config') + sys.exit(2) + try: + os.chdir(config.datadir) + except: + print('Error: datadir not found') ++ syslog.syslog(syslog.LOG_ERR, 'datadir not found') + sys.exit(3) -+ syslog.closelog() -+ -\ No newline at end of file + if dryrun: +@@ -400,3 +410,5 @@ if __name__ == "__main__": + if mailsession: + mailsession.quit() + mailsession = None ++ ++ syslog.closelog() -- cgit v1.2.3