Add the any() and all() utility functions from Python 2.5
[stgit] / stgit / commands / imprt.py
CommitLineData
0d2cd1e4
CM
1__copyright__ = """
2Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
6ef533bc 18import sys, os, re, email
2ac5a14c 19from email.Header import decode_header, make_header
99c52915 20from mailbox import UnixMailbox
457c3093 21from StringIO import StringIO
0d2cd1e4
CM
22from optparse import OptionParser, make_option
23
24from stgit.commands.common import *
25from stgit.utils import *
5e888f30 26from stgit.out import *
0d2cd1e4
CM
27from stgit import stack, git
28
29
30help = 'import a GNU diff file as a new patch'
575c575e 31usage = """%prog [options] [<file>|<url>]
0d2cd1e4 32
b8a0986f
CM
33Create a new patch and apply the given GNU diff file (or the standard
34input). By default, the file name is used as the patch name but this
388f63b6 35can be overridden with the '--name' option. The patch can either be a
b8a0986f
CM
36normal file with the description at the top or it can have standard
37mail format, the Subject, From and Date headers being used for
99c52915
CM
38generating the patch information. The command can also read series and
39mbox files.
40
41If a patch does not apply cleanly, the failed diff is written to the
42.stgit-failed.patch file and an empty StGIT patch is added to the
43stack.
0d2cd1e4 44
b8a0986f 45The patch description has to be separated from the data with a '---'
99e73103 46line."""
0d2cd1e4
CM
47
48options = [make_option('-m', '--mail',
49 help = 'import the patch from a standard e-mail file',
50 action = 'store_true'),
99c52915
CM
51 make_option('-M', '--mbox',
52 help = 'import a series of patches from an mbox file',
53 action = 'store_true'),
54 make_option('-s', '--series',
55 help = 'import a series of patches',
56 action = 'store_true'),
575c575e
CW
57 make_option('-u', '--url',
58 help = 'import a patch from a URL',
59 action = 'store_true'),
0d2cd1e4
CM
60 make_option('-n', '--name',
61 help = 'use NAME as the patch name'),
b0cdad5e
CM
62 make_option('-t', '--strip',
63 help = 'strip numbering and extension from patch name',
64 action = 'store_true'),
9417ece4
CM
65 make_option('-i', '--ignore',
66 help = 'ignore the applied patches in the series',
67 action = 'store_true'),
034db15c
CM
68 make_option('--replace',
69 help = 'replace the unapplied patches in the series',
70 action = 'store_true'),
b21bc8d1 71 make_option('-b', '--base',
35344f86 72 help = 'use BASE instead of HEAD for file importing'),
33e580e0
CM
73 make_option('-e', '--edit',
74 help = 'invoke an editor for the patch description',
75 action = 'store_true'),
9417ece4 76 make_option('-p', '--showpatch',
6ad48e48
PBG
77 help = 'show the patch content in the editor buffer',
78 action = 'store_true'),
0d2cd1e4
CM
79 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
80 help = 'use "NAME <EMAIL>" as the author details'),
81 make_option('--authname',
82 help = 'use AUTHNAME as the author name'),
83 make_option('--authemail',
84 help = 'use AUTHEMAIL as the author e-mail'),
85 make_option('--authdate',
86 help = 'use AUTHDATE as the author date'),
87 make_option('--commname',
88 help = 'use COMMNAME as the committer name'),
89 make_option('--commemail',
90 help = 'use COMMEMAIL as the committer e-mail')]
91
92
d4c43e19
PBG
93def __end_descr(line):
94 return re.match('---\s*$', line) or re.match('diff -', line) or \
95 re.match('Index: ', line)
99e73103 96
b0cdad5e 97def __strip_patch_name(name):
bcb6d890
CM
98 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
99 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
100
101 return stripped
b0cdad5e 102
613a2f16
PBG
103def __replace_slashes_with_dashes(name):
104 stripped = name.replace('/', '-')
105
106 return stripped
107
f21ba536
CM
108def __split_descr_diff(string):
109 """Return the description and the diff from the given string
110 """
111 descr = diff = ''
112 top = True
113
114 for line in string.split('\n'):
115 if top:
116 if not __end_descr(line):
117 descr += line + '\n'
118 continue
119 else:
120 top = False
121 diff += line + '\n'
122
123 return (descr.rstrip(), diff)
124
99e73103
CM
125def __parse_description(descr):
126 """Parse the patch description and return the new description and
127 author information (if any).
128 """
129 subject = body = ''
0543bc5f 130 authname = authemail = authdate = None
99e73103 131
0543bc5f 132 descr_lines = [line.rstrip() for line in descr.split('\n')]
99e73103
CM
133 if not descr_lines:
134 raise CmdException, "Empty patch description"
135
0543bc5f 136 lasthdr = 0
99e73103
CM
137 end = len(descr_lines)
138
0543bc5f 139 # Parse the patch header
61dabd0e 140 for pos in range(0, end):
0543bc5f
TM
141 if not descr_lines[pos]:
142 continue
143 # check for a "From|Author:" line
144 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
145 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
146 authname, authemail = name_email(auth)
147 lasthdr = pos + 1
148 continue
149 # check for a "Date:" line
150 if re.match('\s*date:\s+', descr_lines[pos], re.I):
151 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
152 lasthdr = pos + 1
153 continue
154 if subject:
155 break
156 # get the subject
157 subject = descr_lines[pos]
158 lasthdr = pos + 1
99e73103
CM
159
160 # get the body
0543bc5f
TM
161 if lasthdr < end:
162 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
99e73103 163
0543bc5f 164 return (subject + body, authname, authemail, authdate)
99e73103 165
99c52915
CM
166def __parse_mail(msg):
167 """Parse the message object and return (description, authname,
168 authemail, authdate, diff)
0d2cd1e4 169 """
2ac5a14c
CM
170 def __decode_header(header):
171 """Decode a qp-encoded e-mail header as per rfc2047"""
172 try:
173 words_enc = decode_header(header)
174 hobj = make_header(words_enc)
175 except Exception, ex:
176 raise CmdException, 'header decoding error: %s' % str(ex)
177 return unicode(hobj).encode('utf-8')
178
0d2cd1e4 179 # parse the headers
6ef533bc
CM
180 if msg.has_key('from'):
181 authname, authemail = name_email(__decode_header(msg['from']))
182 else:
183 authname = authemail = None
184
99c52915
CM
185 # '\n\t' can be found on multi-line headers
186 descr = __decode_header(msg['subject']).replace('\n\t', ' ')
6ef533bc 187 authdate = msg['date']
0d2cd1e4 188
186e6b6b 189 # remove the '[*PATCH*]' expression in the subject
0d2cd1e4 190 if descr:
dfeeba67 191 descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
7c02f338 192 descr)[0][1]
0d2cd1e4
CM
193 else:
194 raise CmdException, 'Subject: line not found'
195
6ef533bc 196 # the rest of the message
f21ba536
CM
197 msg_text = ''
198 for part in msg.walk():
199 if part.get_content_type() == 'text/plain':
200 msg_text += part.get_payload(decode = True)
6ef533bc 201
f21ba536
CM
202 rem_descr, diff = __split_descr_diff(msg_text)
203 if rem_descr:
204 descr += '\n\n' + rem_descr
0d2cd1e4 205
99e73103 206 # parse the description for author information
6ef533bc
CM
207 descr, descr_authname, descr_authemail, descr_authdate = \
208 __parse_description(descr)
99e73103
CM
209 if descr_authname:
210 authname = descr_authname
211 if descr_authemail:
212 authemail = descr_authemail
0543bc5f
TM
213 if descr_authdate:
214 authdate = descr_authdate
99e73103 215
6ef533bc 216 return (descr, authname, authemail, authdate, diff)
0d2cd1e4 217
99c52915 218def __parse_patch(fobj):
0d2cd1e4 219 """Parse the input file and return (description, authname,
99c52915 220 authemail, authdate, diff)
0d2cd1e4 221 """
f21ba536 222 descr, diff = __split_descr_diff(fobj.read())
0543bc5f 223 descr, authname, authemail, authdate = __parse_description(descr)
99e73103
CM
224
225 # we don't yet have an agreed place for the creation date.
226 # Just return None
6ef533bc 227 return (descr, authname, authemail, authdate, diff)
0d2cd1e4 228
fd1c0cfc 229def __create_patch(filename, message, author_name, author_email,
99c52915
CM
230 author_date, diff, options):
231 """Create a new patch on the stack
0d2cd1e4 232 """
fd1c0cfc
CM
233 if options.name:
234 patch = options.name
235 elif filename:
236 patch = os.path.basename(filename)
237 else:
238 patch = ''
239 if options.strip:
240 patch = __strip_patch_name(patch)
6ef533bc 241
fff9bce5 242 if not patch:
c4f99b6c
KH
243 if options.ignore or options.replace:
244 unacceptable_name = lambda name: False
245 else:
246 unacceptable_name = crt_series.patch_exists
247 patch = make_patch_name(message, unacceptable_name)
fd1c0cfc
CM
248 else:
249 # fix possible invalid characters in the patch name
250 patch = re.sub('[^\w.]+', '-', patch).strip('-')
251
99c52915 252 if options.ignore and patch in crt_series.get_applied():
27ac2b7e 253 out.info('Ignoring already applied patch "%s"' % patch)
99c52915
CM
254 return
255 if options.replace and patch in crt_series.get_unapplied():
256 crt_series.delete_patch(patch)
fff9bce5 257
95742cfc
PBG
258 # refresh_patch() will invoke the editor in this case, with correct
259 # patch content
9d15ccd8 260 if not message:
95742cfc 261 can_edit = False
9d15ccd8 262
99c52915
CM
263 committer_name = committer_email = None
264
265 if options.author:
266 options.authname, options.authemail = name_email(options.author)
267
0d2cd1e4
CM
268 # override the automatically parsed settings
269 if options.authname:
270 author_name = options.authname
271 if options.authemail:
272 author_email = options.authemail
273 if options.authdate:
274 author_date = options.authdate
275 if options.commname:
276 committer_name = options.commname
277 if options.commemail:
278 committer_email = options.commemail
279
95742cfc 280 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
281 author_name = author_name,
282 author_email = author_email,
283 author_date = author_date,
284 committer_name = committer_name,
285 committer_email = committer_email)
286
5f1629be
CM
287 if not diff:
288 out.warn('No diff found, creating empty patch')
35344f86 289 else:
5f1629be
CM
290 out.start('Importing patch "%s"' % patch)
291 if options.base:
292 git.apply_patch(diff = diff, base = git_id(options.base))
293 else:
294 git.apply_patch(diff = diff)
295 crt_series.refresh_patch(edit = options.edit,
296 show_patch = options.showpatch)
297 out.done()
99c52915 298
fd1c0cfc 299def __import_file(filename, options, patch = None):
99c52915
CM
300 """Import a patch from a file or standard input
301 """
302 if filename:
303 f = file(filename)
304 else:
305 f = sys.stdin
306
307 if options.mail:
308 try:
309 msg = email.message_from_file(f)
310 except Exception, ex:
311 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
312 message, author_name, author_email, author_date, diff = \
313 __parse_mail(msg)
314 else:
315 message, author_name, author_email, author_date, diff = \
316 __parse_patch(f)
317
318 if filename:
319 f.close()
320
fd1c0cfc
CM
321 if patch:
322 pname = patch
323 else:
324 pname = filename
325
326 __create_patch(pname, message, author_name, author_email,
99c52915 327 author_date, diff, options)
9417ece4
CM
328
329def __import_series(filename, options):
330 """Import a series of patches
331 """
332 applied = crt_series.get_applied()
333
334 if filename:
335 f = file(filename)
336 patchdir = os.path.dirname(filename)
337 else:
338 f = sys.stdin
339 patchdir = ''
340
341 for line in f:
342 patch = re.sub('#.*$', '', line).strip()
343 if not patch:
344 continue
bcb6d890 345 patchfile = os.path.join(patchdir, patch)
613a2f16 346 patch = __replace_slashes_with_dashes(patch);
9417ece4 347
fd1c0cfc 348 __import_file(patchfile, options, patch)
99c52915
CM
349
350 if filename:
351 f.close()
352
353def __import_mbox(filename, options):
354 """Import a series from an mbox file
355 """
356 if filename:
357 f = file(filename, 'rb')
358 else:
457c3093 359 f = StringIO(sys.stdin.read())
99c52915
CM
360
361 try:
362 mbox = UnixMailbox(f, email.message_from_file)
363 except Exception, ex:
364 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
365
366 for msg in mbox:
367 message, author_name, author_email, author_date, diff = \
368 __parse_mail(msg)
369 __create_patch(None, message, author_name, author_email,
370 author_date, diff, options)
371
457c3093 372 f.close()
9417ece4 373
575c575e
CW
374def __import_url(url, options):
375 """Import a patch from a URL
376 """
377 import urllib
378 import tempfile
379
380 if not url:
381 parser.error('URL argument required')
382
fd1c0cfc
CM
383 patch = os.path.basename(urllib.unquote(url))
384 filename = os.path.join(tempfile.gettempdir(), patch)
385 urllib.urlretrieve(url, filename)
386 __import_file(filename, options)
575c575e 387
9417ece4
CM
388def func(parser, options, args):
389 """Import a GNU diff file as a new patch
390 """
391 if len(args) > 1:
392 parser.error('incorrect number of arguments')
393
394 check_local_changes()
395 check_conflicts()
396 check_head_top_equal()
397
398 if len(args) == 1:
399 filename = args[0]
400 else:
401 filename = None
402
403 if options.series:
404 __import_series(filename, options)
99c52915
CM
405 elif options.mbox:
406 __import_mbox(filename, options)
575c575e
CW
407 elif options.url:
408 __import_url(filename, options)
9417ece4 409 else:
fd1c0cfc 410 __import_file(filename, options)
9417ece4 411
0d2cd1e4 412 print_crt_patch()