/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.152.6 by Vincent Ladeuil
Really use the transports and test against all targeted protocols.
1
# Copyright (C) 2008 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
19
The only bzr-related info uploaded with the working tree is the corrseponding
20
revision id. The uploaded working tree is not linked to any other bzr data.
21
22
The intended use is for web sites.
23
"""
0.152.1 by Vincent Ladeuil
Empty shell
24
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
25
from bzrlib import (
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
26
    branch,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
27
    commands,
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
28
    errors,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
29
    option,
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
30
    revisionspec,
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
31
    transport,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
32
    )
0.152.1 by Vincent Ladeuil
Empty shell
33
34
class cmd_upload(commands.Command):
0.152.2 by v.ladeuil+lp at free
Add failing tests for basic usage.
35
    """Upload a working tree, as a whole or incrementally.
36
37
    If no destination is specified use the last one used.
38
    If no revision is specified upload the changes since the last upload.
39
    """
0.152.7 by Vincent Ladeuil
Slight refactoring.
40
    takes_args = ['location?']
0.152.2 by v.ladeuil+lp at free
Add failing tests for basic usage.
41
    takes_options = [
42
        'revision',
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
43
        'remember',
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
44
        option.Option('full', 'Upload the full working tree.'),
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
45
        option.Option('directory',
46
                      help='Branch to upload from, '
47
                      'rather than the one containing the working directory.',
48
                      short_name='d',
49
                      type=unicode,
50
                      ),
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
51
       ]
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
52
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
53
    def run(self, location, full=False, revision=None, remember=None,
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
54
            directory=None,
0.152.8 by Vincent Ladeuil
Handle uploading directories.
55
            ):
0.152.11 by Vincent Ladeuil
Be coherent with bzr push.
56
        if directory is None:
57
            directory = u'.'
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
58
        self.branch = branch.Branch.open_containing(directory)[0]
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
59
0.152.7 by Vincent Ladeuil
Slight refactoring.
60
        if location is None:
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
61
            stored_loc = self.get_upload_location()
62
            if stored_loc is None:
63
                raise errors.BzrCommandError('No upload location'
64
                                             ' known or specified.')
65
            else:
66
                display_url = urlutils.unescape_for_display(stored_loc,
67
                        self.outf.encoding)
68
                self.outf.write("Using saved location: %s\n" % display_url)
69
                location = stored_loc
70
71
        self.to_transport = transport.get_transport(location)
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
72
        if revision is None:
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
73
            rev_id = self.branch.last_revision()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
74
        else:
75
            if len(revision) != 1:
76
                raise errors.BzrCommandError(
77
                    'bzr upload --revision takes exactly 1 argument')
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
78
            rev_id = revision[0].in_history(self.branch).rev_id
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
79
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
80
        self.tree = self.branch.repository.revision_tree(rev_id)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
81
        self.rev_id = rev_id
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
82
        if full:
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
83
            self.upload_full_tree()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
84
        else:
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
85
            self.upload_tree()
86
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
87
        # We uploaded successfully, remember it
88
        if self.get_upload_location() is None or remember:
89
            self.set_upload_location(self.to_transport.base)
90
91
    def set_upload_location(self, location):
92
        self.branch.get_config().set_user_option('upload_location', location)
93
94
    def get_upload_location(self):
95
        return self.branch.get_config().get_user_option('upload_location')
96
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
97
    bzr_upload_revid_file_name = '.bzr-upload.revid'
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
98
99
    def set_uploaded_revid(self, rev_id):
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
100
        # XXX: Add tests for concurrent updates, etc.
101
        self.to_transport.put_bytes(self.bzr_upload_revid_file_name, rev_id)
102
103
    def get_uploaded_revid(self):
104
        return self.to_transport.get_bytes(self.bzr_upload_revid_file_name)
0.152.7 by Vincent Ladeuil
Slight refactoring.
105
106
    def upload_file(self, relpath, id):
107
        self.to_transport.put_bytes(relpath, self.tree.get_file_text(id))
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
108
0.152.8 by Vincent Ladeuil
Handle uploading directories.
109
    def make_remote_dir(self, relpath):
110
        # XXX: handle mode
111
        self.to_transport.mkdir(relpath)
112
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
113
    def upload_full_tree(self):
0.152.7 by Vincent Ladeuil
Slight refactoring.
114
        self.to_transport.ensure_base() # XXX: Handle errors
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
115
        self.tree.lock_read()
116
        try:
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
117
            for dp, ie in self.tree.inventory.iter_entries():
118
                # XXX: add an explicit test with an empty tree.
119
                if dp in ('', '.bzrignore'):
120
                    # skip root ('')
121
                    # .bzrignore has no meaning outside of a working tree
122
                    # so do not upload it
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
123
                    continue
124
0.152.8 by Vincent Ladeuil
Handle uploading directories.
125
                if ie.kind == 'file':
126
                    self.upload_file(dp, ie.file_id)
127
                elif ie.kind == 'directory':
128
                    try:
129
                        self.make_remote_dir(dp)
130
                    except errors.FileExists:
131
                        # The directory existed before the upload
132
                        pass
133
                else:
134
                    raise NotImplementedError
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
135
            self.set_uploaded_revid(self.rev_id)
136
        finally:
137
            self.tree.unlock()
138
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
139
    def upload_tree(self):
0.152.12 by Vincent Ladeuil
Implement 'upload_location' in config files.
140
        # XXX: errors out if NoSuchFile and recommand --full on first upload ?
141
        # Add tests.
0.152.10 by Vincent Ladeuil
Fix incremental upload cheat.
142
        rev_id = self.get_uploaded_revid()
143
        # XXX: errors out if rev_id not in branch history (probably someone
144
        # uploaded from a different branch).
145
        from_tree = self.branch.repository.revision_tree(rev_id)
0.152.7 by Vincent Ladeuil
Slight refactoring.
146
        self.to_transport.ensure_base() # XXX: Handle errors
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
147
        changes = self.tree.changes_from(from_tree)
148
        self.tree.lock_read()
149
        try:
0.152.8 by Vincent Ladeuil
Handle uploading directories.
150
            # XXX: handle removed, renamed, kind_changed
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
151
            for (path, id, kind) in changes.added:
152
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
153
                    self.upload_file(path, id)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
154
                elif kind is 'directory':
155
                    self.make_remote_dir(path)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
156
                else:
157
                    raise NotImplementedError
158
            # XXX: Add a test for exec_change
159
            for (path, id, kind,
160
                 content_change, exec_change) in changes.modified:
161
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
162
                    self.upload_file(path, id)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
163
                else:
164
                    raise NotImplementedError
165
            self.set_uploaded_revid(self.rev_id)
166
        finally:
167
            self.tree.unlock()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
168
0.152.1 by Vincent Ladeuil
Empty shell
169
170
commands.register_command(cmd_upload)
171
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
172
0.152.1 by Vincent Ladeuil
Empty shell
173
def test_suite():
174
    from bzrlib.tests import TestUtil
175
176
    suite = TestUtil.TestSuite()
177
    loader = TestUtil.TestLoader()
178
    testmod_names = [
179
        'test_upload',
180
        ]
181
182
    suite.addTest(loader.loadTestsFromModuleNames(
183
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
184
    return suite