forked from lisaglendenning/google-apps-email-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbox2gdata.py
226 lines (185 loc) · 8.46 KB
/
mbox2gdata.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python2.6
# Copyright (c) 2009 by Joseph Devietti (devietti@cs.washington.edu).
# This file is part of the tbird2gmail program.
# Tbird2gmail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Tbird2gmail is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Tbird2gmail. If not, see <http://www.gnu.org/licenses/>.
r"""Sample script to upload mbox files to Google Apps.
Requires migration.py from http://github.com/lisaglendenning/google-apps-email-migrate
History:
- April 29, 2010 Lisa Glendenning (lglenden@cs.washington.edu):
Modified from http://www.cs.washington.edu/homes/devietti/tbird2gmail/tbird2gmail.py
"""
##############################################################################
##############################################################################
import os, os.path, sys, socket, time, optparse, email, mailbox
import migration
##############################################################################
##############################################################################
OUTPUT_TAG = "mbox2gdata"
FOLDER_DELIM = '-'
FAILED_PREFIX = "%s%sfailed" % (OUTPUT_TAG,FOLDER_DELIM)
FAILURE_HEADER = 'X-%s-Failure' % OUTPUT_TAG
UNLABELED_FOLDER = 'all'
FLAGS = { 'INBOX' : migration.MAIL_INBOX,
'UNREAD' : migration.MAIL_UNREAD,
'STARRED' : migration.MAIL_STARRED,
'TRASH' : migration.MAIL_TRASH,
'DRAFT' : migration.MAIL_DRAFT,
'SENT' : migration.MAIL_SENT }
##############################################################################
##############################################################################
def upload_test(service, options):
message = str(migration.SAMPLE_EMAIL)
properties = migration.MAIL_UNREAD | migration.MAIL_INBOX | migration.MAIL_STARRED
labels = [OUTPUT_TAG]
messages = message,
if options.verbose:
print "Uploading sample message:"
print message
if not options.dryrun:
for failed, why in service.upload_messages(messages, properties, labels):
sys.stderr.write('Error:%s\n%s\n' % (why, failed))
##############################################################################
def upload_folder(path, service, options):
sys.stdout.write("Opening mbox file: %s\n" % path)
# extract properties and labels from file name
folder = os.path.basename(path).rsplit('.', 1)[0]
labels = None
properties = 0
if folder != UNLABELED_FOLDER:
labels = folder.split(FOLDER_DELIM)
i = 0
while i < len(labels):
label = labels[i]
if label in FLAGS:
flag = FLAGS[label]
properties |= flag
if options.verbose:
print "Flag:", migration.MAIL_PROPERTIES[flag]
del labels[i]
else:
if options.verbose:
print "Label:", label
i += 1
failed_file = '%s%s%s.mbox' % (FAILED_PREFIX,
FOLDER_DELIM,
folder)
if options.verbose:
print 'Messages that fail to upload will be written to:', failed_file
failed_mbox = None
mbox = mailbox.mbox(path)
total = len(mbox)
progress_format = 'Message %d/' + str(total) + ': (%d kB) ...'
count = 0
total_size = 0
failed = None
for mailkey in mbox.iterkeys():
count += 1
message = mbox.get_string(mailkey)
size = len(message) / 1000
if options.verbose:
print progress_format % (count, size)
if not options.dryrun:
messages = message,
failed = service.upload_messages(messages, properties, labels)
if failed and failed_mbox is None:
failed_mbox = mailbox.mbox(failed_file, create=True)
for msg, why in failed:
if options.verbose:
print 'ERROR:', str(why)
obj = email.message_from_string(msg)
obj[FAILURE_HEADER] = str(why)
failed_mbox.add(obj)
failed_mbox.flush()
if not failed:
total_size += size
if options.verbose:
print 'Successful data upload: %d kB' % total_size
##############################################################################
def upload(options):
service = migration.EmailMigrationService(options.email, options.password)
service.authenticate()
if options.test:
upload_test(service, options)
return
path = options.input
folders = []
if os.path.isdir(path):
for file in os.listdir(path):
if not file.startswith(FAILED_PREFIX) and file.endswith('.mbox'):
folders.append(os.path.join(path, file))
elif os.path.isfile(path):
folders.append(path)
else:
raise RuntimeError('Input path must be a directory or file: %s' % path)
for path in folders:
upload_folder(path, service, options)
##############################################################################
##############################################################################
def main(argv):
usage = "%prog [options] GOOGLE_EMAIL GOOGLE_PASSWORD..."
description="""Uploads emails from a set of mbox files to Google Apps. The '-i' option is
used to specify either the filename of an mbox file, or a directory containing
mbox files. The default behavior is to look for .mbox files in the
current working directory.
mbox2gdata uses a naming convention for .mbox files. The name of the file
must be either the keyword 'all', or one or more labels
separated by the character '-'. Some labels
are reserved for Google Mail Properties:
INBOX, STARRED, UNREAD, TRASH, DRAFT, SENT. The keyword 'all' will result
in those messages being uploaded without any labels or properties.
For example: All messages in the file 'INBOX-STARRED-priority.mbox' will
be uploaded to your Inbox, starred, and labeled with 'priority'.
"""
optparser = optparse.OptionParser(usage=usage, description=description)
optparser.add_option('-i',
'--input',
metavar='PATH',
dest="input",
default = os.getcwd(),
help='mbox file, or directory containing mbox files')
# testing/debugging
optparser.add_option('-t',
"--test",
dest="test",
default=False,
action="store_true",
help="Upload a single test email to your Inbox. The email will be starred and unread." )
optparser.add_option('-v',
"--verbose",
dest="verbose",
default=False,
action="store_true",
help="Turn on debugging output" )
optparser.add_option('-d',
"--dry-run",
dest="dryrun",
default=False,
action="store_true",
help="Execute the program without side effects." )
options, args = optparser.parse_args(argv)
if len(args) != 3:
optparser.error("Incorrect number of required arguments")
prog, email, password = args
assert 1 == email.count('@'), 'Malformed email address: %s' % email
options.email = email
options.password = password
if not os.path.isabs(options.input):
options.input = os.path.abspath(options.input)
if not os.path.exists(options.input):
optparser.error("Nonexistent path: %s" % options.input)
upload(options)
##############################################################################
##############################################################################
if __name__ == '__main__':
main(sys.argv)
##############################################################################
##############################################################################