chiark / gitweb /
Fix the exception class name in export.py
[stgit] / stgit / commands / export.py
1 """Export command
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20
21 import sys, os
22 from optparse import OptionParser, make_option
23
24 from stgit.commands.common import *
25 from stgit.utils import *
26 from stgit import stack, git
27
28
29 help = 'exports a series of patches to <dir> (or patches)'
30 usage = """%prog [options] [<dir>]"""
31
32 options = [make_option('-n', '--numbered',
33                        help = 'number the patch names',
34                        action = 'store_true'),
35            make_option('-d', '--diff',
36                        help = 'append .diff to the patch names',
37                        action = 'store_true'),
38            make_option('-t', '--template', metavar = 'FILE',
39                        help = 'Use FILE as a template'),
40            make_option('-r', '--range',
41                        metavar = '[PATCH1][:[PATCH2]]',
42                        help = 'export patches between PATCH1 and PATCH2')]
43
44
45 def func(parser, options, args):
46     if len(args) == 0:
47         dirname = 'patches'
48     elif len(args) == 1:
49         dirname = args[0]
50     else:
51         parser.error('incorrect number of arguments')
52
53     if git.local_changes():
54         print 'Warning: local changes in the tree. ' \
55               'You might want to commit them first'
56
57     if not os.path.isdir(dirname):
58         os.makedirs(dirname)
59     series = file(os.path.join(dirname, 'series'), 'w+')
60
61     applied = crt_series.get_applied()
62
63     if options.range:
64         boundaries = options.range.split(':')
65         if len(boundaries) == 1:
66             start = boundaries[0]
67             stop = boundaries[0]
68         elif len(boundaries) == 2:
69             if boundaries[0] == '':
70                 start = applied[0]
71             else:
72                 start = boundaries[0]
73             if boundaries[1] == '':
74                 stop = applied[-1]
75             else:
76                 stop = boundaries[1]
77         else:
78             raise CmdException, 'incorrect parameters to "--range"'
79
80         if start in applied:
81             start_idx = applied.index(start)
82         else:
83             raise CmdException, 'Patch "%s" not applied' % start
84         if stop in applied:
85             stop_idx = applied.index(stop) + 1
86         else:
87             raise CmdException, 'Patch "%s" not applied' % stop
88
89         if start_idx >= stop_idx:
90             raise CmdException, 'Incorrect patch range order'
91     else:
92         start_idx = 0
93         stop_idx = len(applied)
94
95     patches = applied[start_idx:stop_idx]
96
97     num = len(patches)
98     zpadding = len(str(num))
99     if zpadding < 2:
100         zpadding = 2
101
102     patch_no = 1;
103     for p in patches:
104         pname = p
105         if options.diff:
106             pname = '%s.diff' % pname
107         if options.numbered:
108             pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
109         pfile = os.path.join(dirname, pname)
110         print >> series, pname
111
112         # get the template
113         if options.template:
114             patch_tmpl = options.template
115         else:
116             patch_tmpl = os.path.join(git.base_dir, 'patchexport.tmpl')
117         if os.path.isfile(patch_tmpl):
118             tmpl = file(patch_tmpl).read()
119         else:
120             tmpl = ''
121
122         # get the patch description
123         patch = crt_series.get_patch(p)
124
125         tmpl_dict = {'description': patch.get_description().rstrip(),
126                      'diffstat': git.diffstat(rev1 = git_id('%s/bottom' % p),
127                                               rev2 = git_id('%s/top' % p)),
128                      'authname': patch.get_authname(),
129                      'authemail': patch.get_authemail(),
130                      'authdate': patch.get_authdate(),
131                      'commname': patch.get_commname(),
132                      'commemail': patch.get_commemail()}
133         for key in tmpl_dict:
134             if not tmpl_dict[key]:
135                 tmpl_dict[key] = ''
136
137         try:
138             descr = tmpl % tmpl_dict
139         except KeyError, err:
140             raise CmdException, 'Unknown patch template variable: %s' \
141                   % err
142         except TypeError:
143             raise CmdException, 'Only "%(name)s" variables are ' \
144                   'supported in the patch template'
145         f = open(pfile, 'w+')
146         f.write(descr)
147
148         # write the diff
149         git.diff(rev1 = git_id('%s/bottom' % p),
150                  rev2 = git_id('%s/top' % p),
151                  out_fd = f)
152         f.close()
153         patch_no += 1
154
155     series.close()