/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
1
# Copyright (C) 2008, 2009 Canonical Ltd
0.152.1 by Vincent Ladeuil
Empty shell
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
17
"""Upload a working tree, incrementally.
18
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
19
Quickstart
20
----------
21
22
To get started, it's as simple as running::
23
24
    bzr upload sftp://user@host/location/on/webserver
25
26
This will initially upload the whole working tree, and leave a file on the
27
remote location indicating the last revision that was uploaded
28
(.bzr-upload.revid), in order to avoid uploading unnecessary information the
29
next time.
30
31
If you would like to upload a specific revision, you just do:
32
33
    bzr upload -r X  sftp://user@host/location/on/webserver
34
35
bzr-upload, just as bzr does, will remember the location where you upload the 
36
first time, so you don't need to specify it every time.
37
38
If you need to re-upload the whole working tree for some reason, you can:
39
40
    bzr upload --full sftp://user@host/location/on/webserver
41
42
43
Automatically Uploading
44
-----------------------
45
46
bzr-upload comes with a hook that can be used to trigger an upload whenever
47
the tip of the branch changes, including on commit, push, uncommit etc. This
48
would allow you to keep the code on the target up to date automatically.
49
50
The easiest way to enable this is to run upload with the --auto option.
51
52
     bzr upload --auto
53
54
will enable the hook for this branch. If you were to do a commit in this branch
55
now you would see it trigger the upload automatically.
56
57
If you wish to disable this for a branch again then you can use the --no-auto
58
option.
59
60
     bzr upload --no-auto
61
62
will disable the feature for that branch.
63
64
Since the auto hook is triggered automatically, you can't use the --quiet
65
option available for the upload command. Instead, you can set the
66
'upload_auto_quiet' configuration variable to True or False in either
67
bazaar.conf, locations.conf or branch.conf.
68
69
70
Storing the '.bzr-upload.revid' file
71
------------------------------------
72
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
73
The only bzr-related info uploaded with the working tree is the corresponding
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
74
revision id. The uploaded working tree is not linked to any other bzr data.
75
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
76
If the layout of your remote server is such that you can't write in the
77
root directory but only in the directories inside that root, you will need
78
to use the 'upload_revid_location' configuration variable to specify the
79
relative path to be used. That configuration variable can be specified in
80
locations.conf or branch.conf.
81
82
For example, given the following layout:
83
84
  Project/
85
    private/
86
    public/
87
88
you may have write access in 'private' and 'public' but in 'Project'
89
itself. In that case, you can add the following in your locations.conf or
90
branch.conf file:
91
92
  upload_revid_location = private/.bzr-upload.revid
93
94
95
Upload from Remote Location
96
---------------------------
97
98
It is possible to upload to a remote location from another remote location by
99
specifying it with the --directory option:
100
101
    bzr upload ftp://public.example.com --directory sftp://private.example.com 
102
103
This, together with --auto, can be used to upload when you push to your
104
central branch, rather than when you commit to your local branch.
105
106
Note that you will consume more bandwith this way than uploading from a local
107
branch.
108
109
Collaborating
110
-------------
111
112
While we don't have any platform setup, you can branch from trunk:
113
114
    bzr branch lp:bzr-upload
115
116
And change anything you'd like, and get in touch with any of the authors to 
117
review and add the changes.
118
119
120
Known Issues
121
------------
122
123
 * Symlinks are not supported
124
125
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
126
"""
0.152.1 by Vincent Ladeuil
Empty shell
127
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
128
# TODO: the chmod bits *can* be supported via the upload protocols
129
# (i.e. poorly), but since the web developers use these protocols to upload
130
# manually, it is expected that the associated web server is coherent with
131
# their presence/absence. In other words, if a web hosting provider requires
132
# chmod bits but don't provide an ftp server that support them, well, better
133
# find another provider ;-)
134
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
135
# TODO: The message emitted in verbose mode displays local paths. That may be
136
# scary for the user when we say 'Deleting <path>' and are referring to
137
# remote files...
0.152.19 by Vincent Ladeuil
Handle kind_change. Trivial implementation, blocked by bug #205636.
138
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
139
from bzrlib import (
0.155.2 by James Westby
Add a post_change_branch_tip hook to upload.
140
    branch,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
141
    commands,
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
142
    lazy_import,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
143
    option,
144
    )
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
145
lazy_import.lazy_import(globals(), """
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
146
import stat
147
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
148
from bzrlib import (
0.158.1 by Gary van der Merwe
Don't require a working tree.
149
    bzrdir,
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
150
    errors,
151
    revisionspec,
152
    transport,
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
153
    osutils,
0.152.40 by Martin Albisetti
We need to import urlutils if we are going to use it
154
    urlutils,
0.152.27 by Martin Albisetti
* Added error message if the working tree has uncommited changes
155
    workingtree,
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
156
    )
157
""")
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
158
0.152.54 by Vincent Ladeuil
Let's start dev for a 1.0.
159
version_info = (1, 0, 0, 'dev', 0)
0.152.23 by Martin Albisetti
Added version_info and plugin_name
160
plugin_name = 'upload'
161
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
162
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
163
def _get_branch_option(branch, option):
164
    return branch.get_config().get_user_option(option)
165
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
166
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
167
def _set_branch_option(branch, option, value):
168
    branch.get_config().set_user_option(option, value)
169
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
170
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
171
def get_upload_location(branch):
172
    return _get_branch_option(branch, 'upload_location')
173
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
174
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
175
def set_upload_location(branch, location):
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
176
    _set_branch_option(branch, 'upload_location', location)
177
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
178
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
179
# FIXME: Add more tests around invalid paths used here or relative paths that
180
# doesn't exist on remote (if only to get proper error messages)
181
def get_upload_revid_location(branch):
182
    loc =  _get_branch_option(branch, 'upload_revid_location')
183
    if loc is None:
184
        loc = '.bzr-upload.revid'
185
    return loc
186
187
188
def set_upload_revid_location(branch, location):
189
    _set_branch_option(branch, 'upload_revid_location', location)
190
191
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
192
def get_upload_auto(branch):
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
193
    auto = branch.get_config().get_user_option_as_bool('upload_auto')
194
    if auto is None:
195
        auto = False # Default to False if not specified
196
    return auto
197
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
198
199
def set_upload_auto(branch, auto):
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
200
    # FIXME: What's the point in allowing a boolean here instead of requiring
201
    # the callers to use strings instead ?
0.155.3 by James Westby
Add some tests for the hook, rename the option to "upload_auto"
202
    if auto:
203
        auto_str = "True"
204
    else:
205
        auto_str = "False"
206
    _set_branch_option(branch, 'upload_auto', auto_str)
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
207
208
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
209
def get_upload_auto_quiet(branch):
210
    quiet = branch.get_config().get_user_option_as_bool('upload_auto_quiet')
211
    if quiet is None:
212
        quiet = False # Default to False if not specified
213
    return quiet
214
215
216
def set_upload_auto_quiet(branch, quiet):
217
    _set_branch_option(branch, 'upload_auto_quiet', quiet)
218
219
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
220
class BzrUploader(object):
221
222
    def __init__(self, branch, to_transport, outf, tree, rev_id,
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
223
                 quiet=False):
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
224
        self.branch = branch
225
        self.to_transport = to_transport
226
        self.outf = outf
227
        self.tree = tree
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
228
        self.rev_id = rev_id
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
229
        self.quiet = quiet
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
230
        self._pending_deletions = []
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
231
        self._pending_renames = []
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
232
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
233
    def set_uploaded_revid(self, rev_id):
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
234
        # XXX: Add tests for concurrent updates, etc.
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
235
        revid_path = get_upload_revid_location(self.branch)
236
        self.to_transport.put_bytes(revid_path, rev_id)
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
237
238
    def get_uploaded_revid(self):
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
239
        revid_path = get_upload_revid_location(self.branch)
240
        return self.to_transport.get_bytes(revid_path)
0.152.7 by Vincent Ladeuil
Slight refactoring.
241
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
242
    def upload_file(self, relpath, id, mode=None):
243
        if mode is None:
244
            if self.tree.is_executable(id):
245
                mode = 0775
246
            else:
247
                mode = 0664
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
248
        if not self.quiet:
249
            self.outf.write('Uploading %s\n' % relpath)
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
250
        self.to_transport.put_bytes(relpath, self.tree.get_file_text(id), mode)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
251
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
252
    def upload_file_robustly(self, relpath, id, mode=None):
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
253
        """Upload a file, clearing the way on the remote side.
254
255
        When doing a full upload, it may happen that a directory exists where
256
        we want to put our file.
257
        """
258
        try:
259
            st = self.to_transport.stat(relpath)
260
            if stat.S_ISDIR(st.st_mode):
261
                # A simple rmdir may not be enough
262
                if not self.quiet:
263
                    self.outf.write('Clearing %s/%s\n' % (
264
                            self.to_transport.external_url(), relpath))
265
                self.to_transport.delete_tree(relpath)
266
        except errors.PathError:
267
            pass
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
268
        self.upload_file(relpath, id, mode)
269
270
    def make_remote_dir(self, relpath, mode=None):
271
        if mode is None:
272
            mode = 0775
273
        self.to_transport.mkdir(relpath, mode)
274
275
    def make_remote_dir_robustly(self, relpath, mode=None):
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
276
        """Create a remote directory, clearing the way on the remote side.
277
278
        When doing a full upload, it may happen that a file exists where we
279
        want to create our directory.
280
        """
281
        try:
282
            st = self.to_transport.stat(relpath)
283
            if not stat.S_ISDIR(st.st_mode):
284
                if not self.quiet:
285
                    self.outf.write('Deleting %s/%s\n' % (
286
                            self.to_transport.external_url(), relpath))
287
                self.to_transport.delete(relpath)
0.152.47 by Vincent Ladeuil
Don't fail a full upload on an already existing dir.
288
            else:
289
                # Ok the remote dir already exists, nothing to do
290
                return
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
291
        except errors.PathError:
292
            pass
0.152.46 by Vincent Ladeuil
Handle x mode bit for files and provides default mode bits for
293
        self.make_remote_dir(relpath, mode)
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
294
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
295
    def delete_remote_file(self, relpath):
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
296
        if not self.quiet:
0.152.30 by Vincent Ladeuil
Comply to verbose.
297
            self.outf.write('Deleting %s\n' % relpath)
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
298
        self.to_transport.delete(relpath)
299
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
300
    def delete_remote_dir(self, relpath):
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
301
        if not self.quiet:
0.152.30 by Vincent Ladeuil
Comply to verbose.
302
            self.outf.write('Deleting %s\n' % relpath)
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
303
        self.to_transport.rmdir(relpath)
304
305
    def delete_remote_dir_maybe(self, relpath):
306
        """Try to delete relpath, keeping failures to retry later."""
307
        try:
308
            self.to_transport.rmdir(relpath)
309
        # any kind of PathError would be OK, though we normally expect
310
        # DirectoryNotEmpty
311
        except errors.PathError:
312
            self._pending_deletions.append(relpath)
313
314
    def finish_deletions(self):
315
        if self._pending_deletions:
316
            # Process the previously failed deletions in reverse order to
317
            # delete children before parents
318
            for relpath in reversed(self._pending_deletions):
319
                self.to_transport.rmdir(relpath)
320
            # The following shouldn't be needed since we use it once per
321
            # upload, but better safe than sorry ;-)
322
            self._pending_deletions = []
323
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
324
    def rename_remote(self, old_relpath, new_relpath):
325
        """Rename a remote file or directory taking care of collisions.
326
327
        To avoid collisions during bulk renames, each renamed target is
328
        temporarily assigned a unique name. When all renames have been done,
329
        each target get its proper name.
330
        """
331
        # We generate a sufficiently random name to *assume* that
332
        # no collisions will occur and don't worry about it (nor
333
        # handle it).
334
        import os
335
        import random
336
        import time
337
338
        stamp = '.tmp.%.9f.%d.%d' % (time.time(),
339
                                     os.getpid(),
340
                                     random.randint(0,0x7FFFFFFF))
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
341
        if not self.quiet:
0.152.30 by Vincent Ladeuil
Comply to verbose.
342
            self.outf.write('Renaming %s to %s\n' % (old_relpath, new_relpath))
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
343
        self.to_transport.rename(old_relpath, stamp)
344
        self._pending_renames.append((stamp, new_relpath))
345
346
    def finish_renames(self):
347
        for (stamp, new_path) in self._pending_renames:
348
            self.to_transport.rename(stamp, new_path)
349
        # The following shouldn't be needed since we use it once per upload,
350
        # but better safe than sorry ;-)
351
        self._pending_renames = []
352
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
353
    def upload_full_tree(self):
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
354
        self.to_transport.ensure_base() # XXX: Handle errors (add
355
                                        # --create-prefix option ?)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
356
        self.tree.lock_read()
357
        try:
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
358
            for relpath, ie in self.tree.inventory.iter_entries():
359
                if relpath in ('', '.bzrignore'):
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
360
                    # skip root ('')
361
                    # .bzrignore has no meaning outside of a working tree
362
                    # so do not upload it
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
363
                    continue
0.152.8 by Vincent Ladeuil
Handle uploading directories.
364
                if ie.kind == 'file':
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
365
                    self.upload_file_robustly(relpath, ie.file_id)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
366
                elif ie.kind == 'directory':
0.152.44 by Vincent Ladeuil
More robust full upload (at least regarding files changed to dirs and vice-versa).
367
                    self.make_remote_dir_robustly(relpath)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
368
                else:
369
                    raise NotImplementedError
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
370
            self.set_uploaded_revid(self.rev_id)
371
        finally:
372
            self.tree.unlock()
373
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
374
    def upload_tree(self):
0.152.24 by Martin Albisetti
Changed the way we upload the full tree if its never been uploaded
375
        # If we can't find the revid file on the remote location, upload the
376
        # full tree instead
377
        try:
378
            rev_id = self.get_uploaded_revid()
379
        except errors.NoSuchFile:
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
380
            if not self.quiet:
0.152.29 by Vincent Ladeuil
Work around test fixture limitation regarding self.outf (cough). All tests passing again.
381
                self.outf.write('No uploaded revision id found,'
0.152.34 by Martin Albisetti
* Change the default behaviour to be more verbose
382
                                ' switching to full upload\n')
0.152.24 by Martin Albisetti
Changed the way we upload the full tree if its never been uploaded
383
            self.upload_full_tree()
0.152.29 by Vincent Ladeuil
Work around test fixture limitation regarding self.outf (cough). All tests passing again.
384
            # We're done
385
            return
0.152.24 by Martin Albisetti
Changed the way we upload the full tree if its never been uploaded
386
0.152.35 by Martin Albisetti
* Tell the user if the remote location is already up to date
387
        # Check if the revision hasn't already been uploaded
388
        if rev_id == self.rev_id:
389
            if not self.quiet:
390
                self.outf.write('Remote location already up to date\n')
391
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
392
        # XXX: errors out if rev_id not in branch history (probably someone
393
        # uploaded from a different branch).
394
        from_tree = self.branch.repository.revision_tree(rev_id)
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
395
        self.to_transport.ensure_base() # XXX: Handle errors (add
396
                                        # --create-prefix option ?)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
397
        changes = self.tree.changes_from(from_tree)
398
        self.tree.lock_read()
399
        try:
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
400
            for (path, id, kind) in changes.removed:
401
                if kind is 'file':
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
402
                    self.delete_remote_file(path)
403
                elif kind is  'directory':
404
                    self.delete_remote_dir_maybe(path)
0.152.17 by Vincent Ladeuil
Handle deletes (trivial implementation).
405
                else:
406
                    raise NotImplementedError
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
407
0.152.14 by Vincent Ladeuil
Handle renames (trivial implementation).
408
            for (old_path, new_path, id, kind,
409
                 content_change, exec_change) in changes.renamed:
0.152.52 by Vincent Ladeuil
Fix bug #270219 by handling content changes during renames.
410
                if content_change:
411
                    # We update the old_path content because renames and
412
                    # deletions are differed.
413
                    self.upload_file(old_path, id)
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
414
                self.rename_remote(old_path, new_path)
415
            self.finish_renames()
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
416
            self.finish_deletions()
0.152.16 by Vincent Ladeuil
Handle renames. Robust implementation.
417
0.152.19 by Vincent Ladeuil
Handle kind_change. Trivial implementation, blocked by bug #205636.
418
            for (path, id, old_kind, new_kind) in changes.kind_changed:
419
                if old_kind is 'file':
420
                    self.delete_remote_file(path)
421
                elif old_kind is  'directory':
422
                    self.delete_remote_dir(path)
423
                else:
424
                    raise NotImplementedError
425
426
                if new_kind is 'file':
427
                    self.upload_file(path, id)
428
                elif new_kind is 'directory':
429
                    self.make_remote_dir(path)
430
                else:
431
                    raise NotImplementedError
432
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
433
            for (path, id, kind) in changes.added:
434
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
435
                    self.upload_file(path, id)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
436
                elif kind is 'directory':
437
                    self.make_remote_dir(path)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
438
                else:
439
                    raise NotImplementedError
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
440
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
441
            # XXX: Add a test for exec_change
442
            for (path, id, kind,
443
                 content_change, exec_change) in changes.modified:
444
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
445
                    self.upload_file(path, id)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
446
                else:
447
                    raise NotImplementedError
0.152.18 by Vincent Ladeuil
Handle deletions. Robust implementation.
448
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
449
            self.set_uploaded_revid(self.rev_id)
450
        finally:
451
            self.tree.unlock()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
452
0.152.57 by Vincent Ladeuil
Fix minor 2.4 compatibility bug.
453
0.158.19 by Vincent Ladeuil
Bzr has facilities for exceptions, let's use them.
454
class CannotUploadToWorkingTreeError(errors.BzrCommandError):
455
456
    _fmt = 'Cannot upload to a bzr managed working tree: %(url)s".'
457
0.152.1 by Vincent Ladeuil
Empty shell
458
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
459
class cmd_upload(commands.Command):
460
    """Upload a working tree, as a whole or incrementally.
461
462
    If no destination is specified use the last one used.
463
    If no revision is specified upload the changes since the last upload.
0.152.56 by Vincent Ladeuil
Small tweaks including doc fix (#275538).
464
465
    Changes include files added, renamed, modified or removed.
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
466
    """
0.152.63 by Vincent Ladeuil
Make the doc more easily discoverable.
467
    _see_also = ['plugins/upload']
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
468
    takes_args = ['location?']
469
    takes_options = [
470
        'revision',
471
        'remember',
472
        option.Option('full', 'Upload the full working tree.'),
473
        option.Option('quiet', 'Do not output what is being done.',
474
                       short_name='q'),
475
        option.Option('directory',
476
                      help='Branch to upload from, '
477
                      'rather than the one containing the working directory.',
478
                      short_name='d',
479
                      type=unicode,
480
                      ),
0.155.5 by James Westby
Hook up the --upload option and document it.
481
        option.Option('auto',
482
                      'Trigger an upload from this branch whenever the tip '
483
                      'revision changes.')
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
484
       ]
485
486
    def run(self, location=None, full=False, revision=None, remember=None,
0.155.4 by James Westby
Add the groundwork for --auto that enables the hook for a branch.
487
            directory=None, quiet=False, auto=None
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
488
            ):
489
        if directory is None:
490
            directory = u'.'
0.158.7 by Gary van der Merwe
Clean up white space.
491
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
492
        if auto and not auto_hook_available:
493
            raise BzrCommandError("Your version of bzr does not have the "
494
                    "hooks necessary for --auto to work")
495
0.158.19 by Vincent Ladeuil
Bzr has facilities for exceptions, let's use them.
496
        (wt, branch,
497
         relpath) = bzrdir.BzrDir.open_containing_tree_or_branch(directory)
0.158.7 by Gary van der Merwe
Clean up white space.
498
0.158.1 by Gary van der Merwe
Don't require a working tree.
499
        if wt:
500
            changes = wt.changes_from(wt.basis_tree())
0.158.7 by Gary van der Merwe
Clean up white space.
501
0.158.1 by Gary van der Merwe
Don't require a working tree.
502
            if revision is None and  changes.has_changed():
503
                raise errors.UncommittedChanges(wt)
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
504
505
        if location is None:
506
            stored_loc = get_upload_location(branch)
507
            if stored_loc is None:
508
                raise errors.BzrCommandError('No upload location'
509
                                             ' known or specified.')
510
            else:
511
                # FIXME: Not currently tested
512
                display_url = urlutils.unescape_for_display(stored_loc,
513
                        self.outf.encoding)
514
                self.outf.write("Using saved location: %s\n" % display_url)
515
                location = stored_loc
516
517
        to_transport = transport.get_transport(location)
0.158.9 by Gary van der Merwe
Add a check to make sure we are not uploading to an existing wt.
518
519
        # Check that we are not uploading to a existing working tree.
520
        try:
521
            to_bzr_dir = bzrdir.BzrDir.open_from_transport(to_transport)
522
            has_wt = to_bzr_dir.has_workingtree()
523
        except errors.NotBranchError:
524
            has_wt = False
525
        except errors.NotLocalUrl:
0.158.19 by Vincent Ladeuil
Bzr has facilities for exceptions, let's use them.
526
            # The exception raised is a bit weird... but that's life.
0.158.9 by Gary van der Merwe
Add a check to make sure we are not uploading to an existing wt.
527
            has_wt = True
528
529
        if has_wt:
0.152.57 by Vincent Ladeuil
Fix minor 2.4 compatibility bug.
530
            raise CannotUploadToWorkingTreeError(url=location)
0.158.9 by Gary van der Merwe
Add a check to make sure we are not uploading to an existing wt.
531
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
532
        if revision is None:
533
            rev_id = branch.last_revision()
534
        else:
535
            if len(revision) != 1:
536
                raise errors.BzrCommandError(
537
                    'bzr upload --revision takes exactly 1 argument')
538
            rev_id = revision[0].in_history(branch).rev_id
539
540
        tree = branch.repository.revision_tree(rev_id)
541
542
        uploader = BzrUploader(branch, to_transport, self.outf, tree,
0.152.62 by Vincent Ladeuil
Fix bug #423331 by adding a way to configure the path used to
543
                               rev_id, quiet=quiet)
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
544
545
        if full:
546
            uploader.upload_full_tree()
547
        else:
548
            uploader.upload_tree()
549
550
        # We uploaded successfully, remember it
551
        if get_upload_location(branch) is None or remember:
552
            set_upload_location(branch, to_transport.base)
0.155.4 by James Westby
Add the groundwork for --auto that enables the hook for a branch.
553
        if auto is not None:
554
            set_upload_auto(branch, auto)
0.155.1 by James Westby
Switch most of the logic to a class outside of the command class.
555
556
0.152.1 by Vincent Ladeuil
Empty shell
557
commands.register_command(cmd_upload)
558
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
559
def install_auto_upload_hook():
560
    from bzrlib.plugins.upload import auto_upload_hook
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
561
    branch.Branch.hooks.install_named_hook('post_change_branch_tip',
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
562
            auto_upload_hook.auto_upload_hook,
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
563
            'Auto upload code from a branch when it is changed.')
0.152.61 by Vincent Ladeuil
Fix bug #312686 and add an 'upload_auto_quiet' config variable.
564
565
566
if hasattr(branch.Branch.hooks, "install_named_hook"):
567
    install_auto_upload_hook()
0.157.1 by James Westby
Don't try and register the hook if install_named_hook is not available
568
    auto_hook_available = True
569
else:
570
    auto_hook_available = False
0.155.2 by James Westby
Add a post_change_branch_tip hook to upload.
571
572
0.152.29 by Vincent Ladeuil
Work around test fixture limitation regarding self.outf (cough). All tests passing again.
573
def load_tests(basic_tests, module, loader):
0.153.1 by Vincent Ladeuil
Clean up references to verbose.
574
    # This module shouldn't define any tests but I don't know how to report
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
575
    # that. I prefer to update basic_tests with the other tests to detect
576
    # unwanted tests and I think that's sufficient.
0.152.1 by Vincent Ladeuil
Empty shell
577
578
    testmod_names = [
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
579
        'tests',
0.152.1 by Vincent Ladeuil
Empty shell
580
        ]
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
581
    basic_tests.addTest(loader.loadTestsFromModuleNames(
0.152.1 by Vincent Ladeuil
Empty shell
582
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
0.154.1 by Vincent Ladeuil
Create a simple setup.py and rework tests modules accordingly.
583
    return basic_tests