~ubuntu-branches/ubuntu/jaunty/pida/jaunty

« back to all changes in this revision

Viewing changes to pida/services/versioncontrol.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
 
4
#Copyright (c) 2005-2006 The PIDA Project
 
5
 
 
6
#Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
#of this software and associated documentation files (the "Software"), to deal
 
8
#in the Software without restriction, including without limitation the rights
 
9
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
#copies of the Software, and to permit persons to whom the Software is
 
11
#furnished to do so, subject to the following conditions:
 
12
 
 
13
#The above copyright notice and this permission notice shall be included in
 
14
#all copies or substantial portions of the Software.
 
15
 
 
16
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
22
#SOFTWARE.
 
23
 
 
24
import os
 
25
 
 
26
import gobject
 
27
 
 
28
import pida.core.service as service
 
29
import pida.core.actions as actions
 
30
 
 
31
import pida.utils.vc as vc
 
32
 
 
33
defs = service.definitions
 
34
types = service.types
 
35
 
 
36
class version_control(service.service):
 
37
 
 
38
    display_name = 'Version Control Integration'
 
39
 
 
40
    class meld_integration(defs.optiongroup):
 
41
        """How much meld will be used."""
 
42
        class use_meld_for_statuses(defs.option):
 
43
            """Whether Meld will be used for file listings."""
 
44
            rtype = types.boolean
 
45
            default = False
 
46
        class use_meld_for_diff(defs.option):
 
47
            """Whether Meld (visual diff) will be used for file diffs."""
 
48
            rtype = types.boolean
 
49
            default = False
 
50
 
 
51
    def init(self):
 
52
        self.__currentfile = None
 
53
        self.action_group.set_sensitive(False)
 
54
        self.__cached_vcs = {}
 
55
 
 
56
    def bnd_buffermanager_document_changed(self, document):
 
57
        self.__currentfile = document.filename
 
58
        self.action_group.set_sensitive(True)
 
59
 
 
60
    def cmd_get_vcs_for_directory(self, directory):
 
61
        vcs = vc.Vc(directory)
 
62
        workdir = vcs.get_working_directory(directory)
 
63
        if workdir in self.__cached_vcs:
 
64
            vcs = self.__cached_vcs[workdir]
 
65
        else:
 
66
            self.__cached_vcs[workdir] = vcs
 
67
        return vcs
 
68
 
 
69
    def cmd_forget_directory(self, directory):
 
70
        vcs = vc.Vc(directory)
 
71
        workdir = vcs.get_working_directory(directory)
 
72
        if workdir in self.__cached_vcs:
 
73
            del self.__cached_vcs[workdir]
 
74
 
 
75
    def cmd_statuses(self, directory):
 
76
        if self.opt('meld_integration', 'use_meld_for_statuses'):
 
77
            self.boss.call_command('meldembed', 'browse',
 
78
                                    directory=directory)
 
79
        else:
 
80
            print 'not using meld'
 
81
 
 
82
    def cmd_get_statuses(self, directory):
 
83
        vcs = self.call('get_vcs_for_directory', directory=directory)
 
84
        if vcs.NAME == 'Null':
 
85
            self.log.info('"%s" is not version controlled', directory)
 
86
        else:
 
87
            try:
 
88
                statuses = vcs.listdir(directory)
 
89
                return statuses
 
90
            except NotImplementedError:
 
91
                self.log.info('"%s" is not version controlled', directory)
 
92
 
 
93
    def cmd_diff_file(self, filename):
 
94
        if self.opt('meld_integration', 'use_meld_for_diff'):
 
95
            self.boss.call_command('meldembed', 'diff',
 
96
                                    filename=filename)
 
97
        else:
 
98
            directory = os.path.dirname(filename)
 
99
            basename = os.path.basename(filename)
 
100
            vcs = self.call('get_vcs_for_directory', directory=directory)
 
101
            if vcs.NAME == 'Null':
 
102
                self.log.info('"%s" is not version controlled', directory)
 
103
            else:
 
104
                try:
 
105
                    commandargs = vcs.diff_command() + [basename]
 
106
                    self.boss.call_command('terminal', 'execute',
 
107
                                        command_args=commandargs,
 
108
                                        icon_name='vcs_diff',
 
109
                                        term_type='dumb',
 
110
                                        kwdict = {'directory':
 
111
                                                   directory},
 
112
                                        short_title='Differences')
 
113
                except NotImplementedError:
 
114
                    self.log.info('Not implemented for %s' % vcs.NAME)
 
115
 
 
116
    def cmd_update(self, directory):
 
117
        vcs = self.call('get_vcs_for_directory', directory=directory)
 
118
        if vcs.NAME == 'Null':
 
119
            self.log.info('"%s" is not version controlled', directory)
 
120
        else:
 
121
            try:
 
122
                commandargs = vcs.update_command()
 
123
                self.boss.call_command('terminal', 'execute',
 
124
                                        command_args=commandargs,
 
125
                                        icon_name='vcs_update',
 
126
                                        kwdict = {'directory':
 
127
                                                   directory},
 
128
                                        short_title='Update')
 
129
                self._update_filemanager(directory)
 
130
            except NotImplementedError:
 
131
                self.log.info('"%s" is not version controlled', directory)
 
132
            return vcs
 
133
 
 
134
    def cmd_commit(self, directory):
 
135
        vcs = self.call('get_vcs_for_directory', directory=directory)
 
136
        if vcs.NAME == 'Null':
 
137
            self.log.info('"%s" is not version controlled', directory)
 
138
        else:
 
139
            def commit(message):
 
140
                commandargs = vcs.commit_command(message)
 
141
                self.boss.call_command('terminal', 'execute',
 
142
                                        command_args=commandargs,
 
143
                                        icon_name='vcs_commit',
 
144
                                        kwdict = {'directory':
 
145
                                                   directory},
 
146
                                        short_title='Commit')
 
147
                self._update_filemanager(directory)
 
148
            self.boss.call_command('window', 'input',
 
149
                                   callback_function=commit,
 
150
                                   prompt='%s commit message' % vcs.NAME)
 
151
 
 
152
    def cmd_add_file(self, filename):
 
153
        directory = os.path.dirname(filename)
 
154
        basename = os.path.basename(filename)
 
155
        vcs = self.call('get_vcs_for_directory', directory=directory)
 
156
        if vcs.NAME == 'Null':
 
157
            self.log.info('"%s" is not version controlled', directory)
 
158
        else:
 
159
            try:
 
160
                commandargs = vcs.add_command() + [basename]
 
161
                self.boss.call_command('terminal', 'execute',
 
162
                                    command_args=commandargs,
 
163
                                    icon_name='vcs_add',
 
164
                                    kwdict = {'directory':
 
165
                                               directory},
 
166
                                    short_title='Add')
 
167
                self._update_filemanager(directory)
 
168
            except NotImplementedError:
 
169
                self.log.info('Not implemented for %s' % vcs.NAME)
 
170
 
 
171
    def cmd_remove_file(self, filename):
 
172
        directory = os.path.dirname(filename)
 
173
        basename = os.path.basename(filename)
 
174
        vcs = self.call('get_vcs_for_directory', directory=directory)
 
175
        if vcs.NAME == 'Null':
 
176
            self.log.info('"%s" is not version controlled', directory)
 
177
        else:
 
178
            try:
 
179
                commandargs = vcs.remove_command() + [basename]
 
180
                self.boss.call_command('terminal', 'execute',
 
181
                                    command_args=commandargs,
 
182
                                    icon_name='vcs_add',
 
183
                                    kwdict = {'directory':
 
184
                                               directory},
 
185
                                    short_title='Remove')
 
186
                self._update_filemanager(directory)
 
187
            except NotImplementedError:
 
188
                self.log.info('Not implemented for %s' % vcs.NAME)
 
189
 
 
190
    def cmd_revert_file(self, filename):
 
191
        directory = os.path.dirname(filename)
 
192
        basename = os.path.basename(filename)
 
193
        vcs = self.call('get_vcs_for_directory', directory=directory)
 
194
        if vcs.NAME == 'Null':
 
195
            self.log.info('"%s" is not version controlled', directory)
 
196
        else:
 
197
            try:
 
198
                commandargs = vcs.revert_command() + [basename]
 
199
                self.boss.call_command('terminal', 'execute',
 
200
                                    command_args=commandargs,
 
201
                                    icon_name='undo',
 
202
                                    kwdict = {'directory':
 
203
                                               directory},
 
204
                                    short_title='Revert')
 
205
                self._update_filemanager(directory)
 
206
            except NotImplementedError:
 
207
                self.log.info('Not implemented for %s' % vcs.NAME)
 
208
        
 
209
    def _update_filemanager(self, directory):
 
210
        fmdir = self.boss.call_command('filemanager', 'get_current_directory')
 
211
        print fmdir, directory
 
212
        if directory == fmdir:
 
213
            self.call('forget_directory', directory=directory)
 
214
            def browse():
 
215
                self.boss.call_command('filemanager', 'browse',
 
216
                                       directory=directory)
 
217
            gobject.timeout_add(200, browse)
 
218
 
 
219
    @actions.action(label='Differences',
 
220
                    stock_id='vcs_diff',
 
221
                    default_accel='<Shift><Control>d',
 
222
                    is_important=False)
 
223
    def act_diff_file(self, action):
 
224
        self.call('diff_file', filename=self.__currentfile)
 
225
 
 
226
    def get_menu_definition(self):
 
227
        return """
 
228
            <toolbar>
 
229
            </toolbar>
 
230
            <menubar>
 
231
            <menu name="base_file" action="base_file_menu">
 
232
            <placeholder name="SubSaveFileMenu" >
 
233
            <menuitem name="diff_file" action="versioncontrol+diff_file" />
 
234
            </placeholder>
 
235
            </menu>
 
236
            </menubar>
 
237
            """
 
238
        
 
239
        
 
240
 
 
241
Service = version_control