Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/git-gmail
blob: 68ae9a27365cbc9db35bc80b21437cb7adec35fe (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
#!/usr/bin/env python
#
# Grab email from a gmail label and write it in a local dir
#
# Author: tomeu@sugarlabs.org
# Based on code by: ted@tedkulp.com
# Based on code by: follower@myrealbox.com
#
# License: GPL 2.0
#
import os
import sys
from getpass import getpass
import re
import email
import subprocess

import libgmail

PATCHES_LABEL = 'Patches'
PATCHES_DIR = os.path.expanduser('~/patches')

def _may_be_patch(message):
    found_minuses = False
    for line in message.source.split('\n'):
        if found_minuses and line.startswith('+++'):
            return True
        if line.startswith('---'):
            found_minuses = True
        else:
            found_minuses = False
    return False 


def _write_patch(message):
    if not os.path.exists(PATCHES_DIR):
        os.mkdir(PATCHES_DIR)

    full_message = message.source.replace('\r', '').strip().encode('utf-8')
    email_message = email.message_from_string(full_message)
    subject = email_message['Subject'].replace('\n\t', ' ')

    # TODO: Probably should remove all tags between [], what does git-am?
    subject = subject.replace('[Sugar-devel] ', '')
    subject = subject.replace('[PATCH] ', '')

    file_path = subject.replace(' ', '-')
    file_path += '.patch'
    file_path = os.path.join(PATCHES_DIR, file_path)

    print "Writing patch: %s" % file_path

    patch_file = open(file_path, 'w+')
    patch_file.write(full_message)
    patch_file.close()

if __name__ == "__main__":

    call_args = ['git', 'config', 'user.gmail']
    pipe = subprocess.Popen(call_args, stdout=subprocess.PIPE)
    user_name = pipe.communicate()[0].strip()
    if not user_name:
        print 'Need a user name configured in .gitconfig: ' \
              'git config --global user.gmail <name>@gmail.com'
        exit(0)

    password = getpass('Password: ')
    ga = libgmail.GmailAccount(user_name, password)
     
    print "Please wait, logging in as %s..." % user_name
     
    try:
        ga.login()
    except libgmail.GmailLoginFailure:
        print "Login failed. (Wrong username/password?)"
        sys.exit(2)
    else:
        print "Log in successful"
     
    query = 'label: %s' % PATCHES_LABEL
    if len(sys.argv) > 1:
        query += ' %s' % sys.argv[1]
    result = ga.getMessagesByQuery(query, True)
     
    if len(result):
        for thread in reversed(result):
            for msg in reversed(thread):
                if _may_be_patch(msg):
                    _write_patch(msg)
    else:
        print "No threads found in `%s`." % new_label