/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/transport/gio_transport.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-16 23:15:15 UTC
  • mfrom: (7180 work)
  • mto: This revision was merged to the branch mainline in revision 7183.
  • Revision ID: jelmer@jelmer.uk-20181116231515-zqd2yn6kj8lfydyp
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
 
104
104
    def write(self, bytes):
105
105
        try:
106
 
            #Using pump_string_file seems to make things crash
 
106
            # Using pump_string_file seems to make things crash
107
107
            osutils.pumpfile(BytesIO(bytes), self.stream)
108
108
        except gio.Error as e:
109
 
            #self.transport._translate_gio_error(e,self.relpath)
 
109
            # self.transport._translate_gio_error(e,self.relpath)
110
110
            raise errors.BzrError(str(e))
111
111
 
112
112
 
132
132
            raise ValueError(base)
133
133
 
134
134
        (scheme, netloc, path, params, query, fragment) = \
135
 
                urlparse(base[len('gio+'):], allow_fragments=False)
 
135
            urlparse(base[len('gio+'):], allow_fragments=False)
136
136
        if '@' in netloc:
137
137
            user, netloc = netloc.rsplit('@', 1)
138
 
        #Seems it is not possible to list supported backends for GIO
139
 
        #so a hardcoded list it is then.
 
138
        # Seems it is not possible to list supported backends for GIO
 
139
        # so a hardcoded list it is then.
140
140
        gio_backends = ['dav', 'file', 'ftp', 'obex', 'sftp', 'ssh', 'smb']
141
141
        if scheme not in gio_backends:
142
142
            raise urlutils.InvalidURL(base,
143
 
                    extra="GIO support is only available for " + \
144
 
                    ', '.join(gio_backends))
 
143
                                      extra="GIO support is only available for " +
 
144
                                      ', '.join(gio_backends))
145
145
 
146
 
        #Remove the username and password from the url we send to GIO
147
 
        #by rebuilding the url again.
 
146
        # Remove the username and password from the url we send to GIO
 
147
        # by rebuilding the url again.
148
148
        u = (scheme, netloc, path, '', '', '')
149
149
        self.url = urlunparse(u)
150
150
 
151
151
        # And finally initialize super
152
152
        super(GioTransport, self).__init__(base,
153
 
            _from_transport=_from_transport)
 
153
                                           _from_transport=_from_transport)
154
154
 
155
155
    def _relpath_to_url(self, relpath):
156
156
        full_url = urlutils.join(self.url, relpath)
171
171
        return file
172
172
 
173
173
    def _auth_cb(self, op, message, default_user, default_domain, flags):
174
 
        #really use breezy.auth get_password for this
175
 
        #or possibly better gnome-keyring?
 
174
        # really use breezy.auth get_password for this
 
175
        # or possibly better gnome-keyring?
176
176
        auth = config.AuthenticationConfig()
177
177
        parsed_url = urlutils.URL.from_string(self.url)
178
178
        user = None
181
181
            prompt = (u'%s' % (parsed_url.scheme.upper(),) +
182
182
                      u' %(host)s DOMAIN\\username')
183
183
            user_and_domain = auth.get_user(parsed_url.scheme,
184
 
                parsed_url.host, port=parsed_url.port, ask=True,
185
 
                prompt=prompt)
 
184
                                            parsed_url.host, port=parsed_url.port, ask=True,
 
185
                                            prompt=prompt)
186
186
            (domain, user) = user_and_domain.split('\\', 1)
187
187
            op.set_username(user)
188
188
            op.set_domain(domain)
189
189
        elif flags & gio.ASK_PASSWORD_NEED_USERNAME:
190
190
            user = auth.get_user(parsed_url.scheme, parsed_url.host,
191
 
                    port=parsed_url.port, ask=True)
 
191
                                 port=parsed_url.port, ask=True)
192
192
            op.set_username(user)
193
193
        elif flags & gio.ASK_PASSWORD_NEED_DOMAIN:
194
 
            #Don't know how common this case is, but anyway
195
 
            #a DOMAIN and a username prompt should be the
196
 
            #same so I will missuse the ui_factory get_username
197
 
            #a little bit here.
 
194
            # Don't know how common this case is, but anyway
 
195
            # a DOMAIN and a username prompt should be the
 
196
            # same so I will missuse the ui_factory get_username
 
197
            # a little bit here.
198
198
            prompt = (u'%s' % (parsed_url.scheme.upper(),) +
199
199
                      u' %(host)s DOMAIN')
200
200
            domain = ui.ui_factory.get_username(prompt=prompt)
204
204
            if user is None:
205
205
                user = op.get_username()
206
206
            password = auth.get_password(parsed_url.scheme, parsed_url.host,
207
 
                    user, port=parsed_url.port)
 
207
                                         user, port=parsed_url.port)
208
208
            op.set_password(password)
209
209
        op.reply(gio.MOUNT_OPERATION_HANDLED)
210
210
 
214
214
            self.loop.quit()
215
215
        except gio.Error as e:
216
216
            self.loop.quit()
217
 
            raise errors.BzrError("Failed to mount the given location: " + str(e));
 
217
            raise errors.BzrError(
 
218
                "Failed to mount the given location: " + str(e))
218
219
 
219
220
    def _create_connection(self, credentials=None):
220
221
        if credentials is None:
230
231
            except gio.Error as e:
231
232
                if (e.code == gio.ERROR_NOT_MOUNTED):
232
233
                    self.loop = glib.MainLoop()
233
 
                    ui.ui_factory.show_message('Mounting %s using GIO' % \
234
 
                            self.url)
 
234
                    ui.ui_factory.show_message('Mounting %s using GIO' %
 
235
                                               self.url)
235
236
                    op = gio.MountOperation()
236
237
                    if user:
237
238
                        op.set_username(user)
239
240
                        op.set_password(password)
240
241
                    op.connect('ask-password', self._auth_cb)
241
242
                    m = connection.mount_enclosing_volume(op,
242
 
                            self._mount_done_cb)
 
243
                                                          self._mount_done_cb)
243
244
                    self.loop.run()
244
245
        except gio.Error as e:
245
246
            raise errors.TransportError(msg="Error setting up connection:"
296
297
            fin.close()
297
298
            return BytesIO(buf)
298
299
        except gio.Error as e:
299
 
            #If we get a not mounted here it might mean
300
 
            #that a bad path has been entered (or that mount failed)
 
300
            # If we get a not mounted here it might mean
 
301
            # that a bad path has been entered (or that mount failed)
301
302
            if (e.code == gio.ERROR_NOT_MOUNTED):
302
303
                raise errors.PathError(relpath,
303
 
                  extra='Failed to get file, make sure the path is correct. ' \
304
 
                  + str(e))
 
304
                                       extra='Failed to get file, make sure the path is correct. '
 
305
                                       + str(e))
305
306
            else:
306
307
                self._translate_gio_error(e, relpath)
307
308
 
314
315
        if 'gio' in debug.debug_flags:
315
316
            mutter("GIO put_file %s" % relpath)
316
317
        tmppath = '%s.tmp.%.9f.%d.%d' % (relpath, time.time(),
317
 
                    os.getpid(), random.randint(0, 0x7FFFFFFF))
 
318
                                         os.getpid(), random.randint(0, 0x7FFFFFFF))
318
319
        f = None
319
320
        fout = None
320
321
        try:
386
387
        except gio.Error as e:
387
388
            self._translate_gio_error(e, relpath)
388
389
        except errors.NotADirectory as e:
389
 
            #just pass it forward
 
390
            # just pass it forward
390
391
            raise e
391
392
        except Exception as e:
392
393
            mutter('failed to rmdir %s: %s' % (relpath, e))
396
397
        """Append the text in the file-like object into the final
397
398
        location.
398
399
        """
399
 
        #GIO append_to seems not to append but to truncate
400
 
        #Work around this.
 
400
        # GIO append_to seems not to append but to truncate
 
401
        # Work around this.
401
402
        if 'gio' in debug.debug_flags:
402
403
            mutter("GIO append_file: %s" % relpath)
403
404
        tmppath = '%s.tmp.%.9f.%d.%d' % (relpath, time.time(),
404
 
                    os.getpid(), random.randint(0, 0x7FFFFFFF))
 
405
                                         os.getpid(), random.randint(0, 0x7FFFFFFF))
405
406
        try:
406
407
            result = 0
407
408
            fo = self._get_GIO(tmppath)
413
414
                fin = fi.read()
414
415
                self._pump(fin, fout)
415
416
                fin.close()
416
 
            #This separate except is to catch and ignore the
417
 
            #gio.ERROR_NOT_FOUND for the already existing file.
418
 
            #It is valid to open a non-existing file for append.
419
 
            #This is caused by the broken gio append_to...
 
417
            # This separate except is to catch and ignore the
 
418
            # gio.ERROR_NOT_FOUND for the already existing file.
 
419
            # It is valid to open a non-existing file for append.
 
420
            # This is caused by the broken gio append_to...
420
421
            except gio.Error as e:
421
422
                if e.code != gio.ERROR_NOT_FOUND:
422
423
                    self._translate_gio_error(e, relpath)
424
425
            fout.close()
425
426
            info = GioStatResult(fo)
426
427
            if info.st_size != result + length:
427
 
                raise errors.BzrError("Failed to append size after " \
428
 
                      "(%d) is not original (%d) + written (%d) total (%d)" % \
429
 
                      (info.st_size, result, length, result + length))
 
428
                raise errors.BzrError("Failed to append size after "
 
429
                                      "(%d) is not original (%d) + written (%d) total (%d)" %
 
430
                                      (info.st_size, result, length, result + length))
430
431
            fo.move(fi, flags=gio.FILE_COPY_OVERWRITE)
431
432
            return result
432
433
        except gio.Error as e:
447
448
                if e.code == gio.ERROR_NOT_SUPPORTED:
448
449
                    # Command probably not available on this server
449
450
                    mutter("GIO Could not set permissions to %s on %s. %s",
450
 
                        oct(mode), self._remote_path(relpath), str(e))
 
451
                           oct(mode), self._remote_path(relpath), str(e))
451
452
                else:
452
453
                    self._translate_gio_error(e, relpath)
453
454
 
589
590
        else:
590
591
            mutter('unable to understand error for path: %s: %s', path, err)
591
592
            raise errors.PathError(path,
592
 
                    extra="Unhandled gio error: " + str(err))
 
593
                                   extra="Unhandled gio error: " + str(err))
593
594
 
594
595
 
595
596
def get_test_permutations():