summaryrefslogtreecommitdiff
path: root/parabola_repolint/notify.py
blob: 7c122cb90291426596b887b023d48ba9a587d3ae (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
'''
repolint results publishing helpers
'''

import os
import time
import lzma
import tempfile
import smtplib

import selenium
import splinter

from parabola_repolint.config import CONFIG


def etherpad_replace(content):
    ''' replace the pads content with the given data '''
    pad = CONFIG.notify.etherpad_url

    browser = splinter.Browser(headless=True)
    browser.visit(pad)

    with tempfile.NamedTemporaryFile('w') as tmp:
        tmp.write(content)
        tmp.flush()

        for attempt in range(20):
            try:
                btn = browser.driver.find_element_by_class_name('buttonicon-import_export')
                btn.click()
                break
            except (selenium.common.exceptions.NoSuchElementException,
                    selenium.common.exceptions.ElementNotInteractableException):
                if attempt >= 19:
                    raise
                time.sleep(0.2)

        for attempt in range(20):
            try:
                fileinput = browser.driver.find_element_by_id('importfileinput')
                fileinput.send_keys(tmp.name)
                break
            except (selenium.common.exceptions.NoSuchElementException,
                    selenium.common.exceptions.ElementNotInteractableException):
                if attempt >= 19:
                    raise
                time.sleep(0.2)

        for attempt in range(20):
            try:
                submit = browser.driver.find_element_by_id('importsubmitinput')
                submit.click()
                break
            except (selenium.common.exceptions.NoSuchElementException,
                    selenium.common.exceptions.ElementNotInteractableException):
                if attempt >= 19:
                    raise
                time.sleep(0.2)

        browser.driver.switch_to_alert().accept()
        time.sleep(1)
        browser.quit()


def send_mail(subject, body):
    ''' send a mail to the maintenance list '''
    host = CONFIG.notify.smtp_host
    port = int(CONFIG.notify.smtp_port)

    sender = CONFIG.notify.smtp_sender
    receiver = CONFIG.notify.smtp_receiver

    login = CONFIG.notify.smtp_login
    password = CONFIG.notify.smtp_password

    message = '''\
From: %s
To: %s
Subject: %s

%s''' % (sender, receiver, subject, body)

    with smtplib.SMTP(host, port) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.login(login, password)
        smtp.sendmail(sender, [receiver], message)


def write_log(filename, contents):
    ''' produce a logfile with linter results '''
    dst = os.path.expanduser(CONFIG.notify.logfile_dest)
    os.makedirs(dst, exist_ok=True)

    with lzma.open(os.path.join(dst, filename) + '.xz', 'wt') as logfile:
        logfile.write(contents)