Kiln » TortoiseHg » TortoiseHg
Clone URL:  
thgimport.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# thgimport.py - TortoiseHg's dialog for (q)importing patches # # Copyright 2009 Yuki KODAMA <endflow.net@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import os import gtk from mercurial import hg, ui from tortoisehg.util.i18n import _ from tortoisehg.util import hglib, paths from tortoisehg.hgtk import hgcmd, gtklib, gdialog, cslist MODE_NORMAL = 'normal' MODE_WORKING = 'working' class ImportDialog(gtk.Dialog): """ Dialog to import patches """ def __init__(self, repo=None): """ Initialize the Dialog """ gtk.Dialog.__init__(self) gtklib.set_tortoise_icon(self, 'menuimport.ico') gtklib.set_tortoise_keys(self) self.set_resizable(False) self.set_has_separator(False) # buttons self.importbtn = self.add_button(_('Import'), gtk.RESPONSE_OK) self.closebtn = self.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE) if not repo: try: repo = hg.repository(ui.ui(), path=paths.find_root()) except hglib.RepoError: gtklib.idle_add_single_call(self.destroy) return self.repo = repo self.set_title(_('Import - %s') % hglib.get_reponame(repo)) self.done = False # layout table self.table = table = gtklib.LayoutTable() self.vbox.pack_start(table, True, True, 2) ## source path entry & browse button self.source_entry = gtk.Entry() self.files_btn = gtk.Button(_('Files...')) self.dir_btn = gtk.Button(_('Directory...')) table.add_row(_('Source:'), self.source_entry, 1, self.files_btn, self.dir_btn, expand=0) # copy form thgstrip.py def createlabel(): label = gtk.Label() label.set_alignment(0, 0.5) label.set_size_request(-1, 24) label.size_request() return label ## info label self.infolbl = createlabel() table.add_row(_('Preview:'), self.infolbl, padding=False) ## patch preview self.cslist = cslist.ChangesetList() table.add_row(None, self.cslist, padding=False) self.cslist.set_dnd_enable(True) self.cslist.set_checkbox_enable(True) # signal handlers self.connect('response', self.dialog_response) self.files_btn.connect('clicked', self.files_clicked) self.dir_btn.connect('clicked', self.dir_clicked) self.cslist.connect('list-updated', self.list_updated) self.source_entry.connect('changed', lambda e: self.preview(queue=True)) # prepare to show self.cslist.clear() gtklib.idle_add_single_call(self.after_init) def after_init(self): # CmdWidget self.cmd = hgcmd.CmdWidget() self.cmd.show_all() self.cmd.hide() self.vbox.pack_start(self.cmd, True, True, 6) # abort button self.abortbtn = self.add_button(_('Abort'), gtk.RESPONSE_CANCEL) self.abortbtn.hide() def files_clicked(self, button): initdir = self.get_initial_dir() result = gtklib.NativeSaveFileDialogWrapper(title=_('Select Patches'), initial=initdir, open=True, multi=True).run() if result and result != initdir: if not isinstance(result, basestring): result = os.pathsep.join(result) self.source_entry.set_text(result) self.preview() def dir_clicked(self, button): initdir = self.get_initial_dir() result = gtklib.NativeFolderSelectDialog( title=_('Select Directory contains patches:'), initial=initdir).run() if result and result != initdir: self.source_entry.set_text(result) self.preview() def list_updated(self, cslist, total, sel, *args): self.update_status(sel) def dialog_response(self, dialog, response_id): def abort(): self.cmd.stop() self.cmd.show_log() self.switch_to(MODE_NORMAL, cmd=False) # Import button if response_id == gtk.RESPONSE_OK: self.doimport() # Close button or dialog closing by the user elif response_id in (gtk.RESPONSE_CLOSE, gtk.RESPONSE_DELETE_EVENT): if self.cmd.is_alive(): ret = gdialog.Confirm(_('Confirm Abort'), [], self, _('Do you want to abort?')).run() if ret == gtk.RESPONSE_YES: abort() elif len(self.cslist.get_items()) != 0 and not self.done: ret = gdialog.Confirm(_('Confirm Close'), [], self, _('Do you want to close?')).run() if ret == gtk.RESPONSE_YES: self.destroy() return # close dialog else: self.destroy() return # close dialog # Abort button elif response_id == gtk.RESPONSE_CANCEL: abort() else: raise _('unexpected response id: %s') % response_id self.run() # don't close dialog def get_initial_dir(self): src = self.source_entry.get_text() if src and os.path.exists(src): if os.path.isdir(src): return src parent = os.path.dirname(src) if parent and os.path.exists(parent): return parent return None def update_status(self, count): if count: info = _('<span weight="bold">%s patches</span> will' ' be imported') % count else: info = '<span weight="bold" foreground="#880000">%s</span>' \ % _('Nothing to import') self.infolbl.set_markup(info) self.importbtn.set_sensitive(bool(count)) def get_filepaths(self): src = self.source_entry.get_text() if not src: return [] files = [] for path in src.split(os.pathsep): path = path.strip('\r\n\t ') if not os.path.exists(path): continue if os.path.isdir(path): entries = os.listdir(path) for entry in entries: file = os.path.join(path, entry) if os.path.isfile(file): files.append(file) elif os.path.isfile(path): files.append(path) return files def preview(self, queue=False): files = self.get_filepaths() if files: self.cslist.update(files, self.repo, queue=queue) else: self.cslist.clear() def set_notify_func(self, func, *args, **kargs): self.notify_func = func self.notify_args = args self.notify_kargs = kargs def switch_to(self, mode, cmd=True): if mode == MODE_NORMAL: normal = True self.closebtn.grab_focus() elif mode == MODE_WORKING: normal = False self.abortbtn.grab_focus() else: raise _('unknown mode name: %s') % mode working = not normal self.table.set_sensitive(normal) self.importbtn.set_property('visible', normal) self.closebtn.set_property('visible', normal) if cmd: self.cmd.set_property('visible', working) self.abortbtn.set_property('visible', working) def doimport(self): items = self.cslist.get_items(sel=True) files = [file for file, sel in items if sel] if not files: return cmdline = ['hg', 'import', '--verbose'] cmdline.extend(files) def cmd_done(returncode, useraborted): self.done = True self.switch_to(MODE_NORMAL, cmd=False) if hasattr(self, 'notify_func'): self.notify_func(*self.notify_args, **self.notify_kargs) if returncode == 0: if not self.cmd.is_show_log(): self.response(gtk.RESPONSE_CLOSE) self.cmd.set_result(_('Imported successfully'), style='ok') elif useraborted: self.cmd.set_result(_('Canceled importing'), style='error') else: self.cmd.set_result(_('Failed to import'), style='error') self.switch_to(MODE_WORKING) self.cmd.execute(cmdline, cmd_done)