From 44245e959f972bef0bcbc45319fd5f4b039ece32 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 31 Jan 2017 21:15:02 +0200 Subject: Refactor: get rid of some common code --- 900-implements.yarn | 46 +++++++++++----------------------------------- yarnhelper.py | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/900-implements.yarn b/900-implements.yarn index fd9ea26..2abb5ee 100644 --- a/900-implements.yarn +++ b/900-implements.yarn @@ -123,7 +123,7 @@ ## Check for a mail in a mailbox IMPLEMENTS THEN mailbox of (\S+) has a mail with subject \$(\S+) - import email, imaplib, os, subprocess, yarnhelper + import os, yarnhelper h = yarnhelper.YarnHelper() user = h.get_next_match() subject_key = h.get_next_match() @@ -132,30 +132,19 @@ address = os.environ['ADDRESS'] pass_name = 'server-yarns/imap/{}@{}'.format(user, address) pass_home = os.environ['PASS_HOME'] - p = subprocess.Popen( - ['env', 'HOME={}'.format(pass_home), 'pass', 'show', pass_name], - stdout=subprocess.PIPE) - stdout, stderr = p.communicate() - password = stdout.rstrip() - m = imaplib.IMAP4_SSL(address) - m.login(user, password) - m.select('INBOX', True) - typ, data = m.search(None, 'ALL') + password = h.get_password_with_pass(pass_home, pass_name) got_it = False - for num in data[0].split(): - typ, data = m.fetch(num, '(RFC822)') - typ, text = data[0] - msg = email.message_from_string(text) + def match(imapobj, msgid, msg): + global got_it print 'Subject:', msg['Subject'] got_it = got_it or (msg['Subject'] == subject) - m.close() - m.logout() + h.iterate_mails_in_imap_mailbox(address, user, password, match, False) h.assertEqual(got_it, True) ## Delete mails IMPLEMENTS THEN mails for (\S+) with subject \$(\S+) get deleted - import email, imaplib, os, subprocess, yarnhelper + import os, yarnhelper h = yarnhelper.YarnHelper() user = h.get_next_match() subject_key = h.get_next_match() @@ -164,22 +153,9 @@ address = os.environ['ADDRESS'] pass_name = 'server-yarns/imap/{}@{}'.format(user, address) pass_home = os.environ['PASS_HOME'] - p = subprocess.Popen( - ['env', 'HOME={}'.format(pass_home), 'pass', 'show', pass_name], - stdout=subprocess.PIPE) - stdout, stderr = p.communicate() - password = stdout.rstrip() - m = imaplib.IMAP4_SSL(address) - m.login(user, password) - m.select('INBOX', False) - typ, data = m.search(None, 'ALL') - for num in data[0].split(): - typ, data = m.fetch(num, '(RFC822)') - typ, text = data[0] - msg = email.message_from_string(text) - print 'Subject:', msg['Subject'] + password = h.get_password_with_pass(pass_home, pass_name) + def delete_matching(imapobj, msgid, msg): if msg['Subject'] == subject: - m.store(num, '+FLAGS', '(\\Deleted)') - m.expunge() - m.close() - m.logout() + imapobj.store(msgid, '+FLAGS', '(\\Deleted)') + h.iterate_mails_in_imap_mailbox( + address, user, password, delete_matching, True) diff --git a/yarnhelper.py b/yarnhelper.py index ee2a901..8f9da19 100644 --- a/yarnhelper.py +++ b/yarnhelper.py @@ -16,7 +16,10 @@ # =*= License: GPL-3+ =*= +import email +import imaplib import os +import subprocess import urlparse import requests @@ -94,6 +97,30 @@ class YarnHelper(object): if a == b: raise Error('assertion {!r} != {!r} failed'.format(a, b)) + def get_password_with_pass(self, pass_home, pass_name): # pragma: no cover + p = subprocess.Popen( + ['env', 'HOME={}'.format(pass_home), 'pass', 'show', pass_name], + stdout=subprocess.PIPE) + stdout, stderr = p.communicate() + password = stdout.rstrip() + return password + + def iterate_mails_in_imap_mailbox( + self, address, user, password, callback, exp): # pragma: no cover + m = imaplib.IMAP4_SSL(address) + m.login(user, password) + m.select('INBOX', False) + typ, data = m.search(None, 'ALL') + for num in data[0].split(): + typ, data = m.fetch(num, '(RFC822)') + typ, text = data[0] + msg = email.message_from_string(text) + callback(m, num, msg) + if exp: + m.expunge() + m.close() + m.logout() + class Error(Exception): -- cgit v1.2.1