/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 (
26
    commands,
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
27
    errors,
0.152.3 by v.ladeuil+lp at free
Make the tests fail not error out.
28
    option,
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
29
    revisionspec,
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
30
    transport,
31
    workingtree,
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.3 by v.ladeuil+lp at free
Make the tests fail not error out.
43
        option.Option('full', 'Upload the full working tree.'),
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
44
       ]
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
45
0.152.8 by Vincent Ladeuil
Handle uploading directories.
46
    def run(self, location, full=False, revision=None,
47
            working_dir=None, # For tests
48
            ):
49
        if working_dir is None:
50
            working_dir = u'.'
51
        wt = workingtree.WorkingTree.open_containing(working_dir)[0]
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
52
        b = wt.branch
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
53
0.152.7 by Vincent Ladeuil
Slight refactoring.
54
        if location is None:
55
            # XXX: use the remembered location
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
56
            raise NotImplementedError
57
        else:
0.152.7 by Vincent Ladeuil
Slight refactoring.
58
            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.
59
        if revision is None:
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
60
            rev_id = wt.last_revision()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
61
        else:
62
            if len(revision) != 1:
63
                raise errors.BzrCommandError(
64
                    'bzr upload --revision takes exactly 1 argument')
65
            rev_id = revision[0].in_history(b).rev_id
66
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
67
        self.rev_id = rev_id
68
        self.tree = b.repository.revision_tree(rev_id)
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
69
        if full:
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
70
            self.upload_full_tree()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
71
        else:
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
72
            rev1 = revisionspec.RevisionSpec.from_string('1')
73
            rev1_id = rev1.in_history(b).rev_id
74
            from_tree = b.repository.revision_tree(rev1_id)
75
            self.upload_tree(from_tree)
76
77
    def set_uploaded_revid(self, rev_id):
78
        # XXX: Rename to .bzr/uploaded ? Add tests for concurrent updates, etc.
0.152.7 by Vincent Ladeuil
Slight refactoring.
79
        self.to_transport.put_bytes('.bzr.uploaded', rev_id)
80
81
    def upload_file(self, relpath, id):
82
        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.
83
0.152.8 by Vincent Ladeuil
Handle uploading directories.
84
    def make_remote_dir(self, relpath):
85
        # XXX: handle mode
86
        self.to_transport.mkdir(relpath)
87
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
88
    def upload_full_tree(self):
0.152.7 by Vincent Ladeuil
Slight refactoring.
89
        self.to_transport.ensure_base() # XXX: Handle errors
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
90
        self.tree.lock_read()
91
        try:
92
            inv = self.tree.inventory
93
            entries = inv.iter_entries()
94
            entries.next() # skip root
95
            for dp, ie in entries:
96
                # .bzrignore has no meaning outside of a working tree
0.152.9 by Vincent Ladeuil
Recfactoring to simplify tests writing.
97
                # so do not upload it
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
98
                if dp == ".bzrignore":
99
                    continue
100
0.152.8 by Vincent Ladeuil
Handle uploading directories.
101
                if ie.kind == 'file':
102
                    self.upload_file(dp, ie.file_id)
103
                elif ie.kind == 'directory':
104
                    try:
105
                        self.make_remote_dir(dp)
106
                    except errors.FileExists:
107
                        # The directory existed before the upload
108
                        pass
109
                else:
110
                    raise NotImplementedError
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
111
            self.set_uploaded_revid(self.rev_id)
112
        finally:
113
            self.tree.unlock()
114
115
    def upload_tree(self, from_tree):
0.152.7 by Vincent Ladeuil
Slight refactoring.
116
        self.to_transport.ensure_base() # XXX: Handle errors
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
117
        changes = self.tree.changes_from(from_tree)
118
        self.tree.lock_read()
119
        try:
0.152.8 by Vincent Ladeuil
Handle uploading directories.
120
            # XXX: handle removed, renamed, kind_changed
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
121
            for (path, id, kind) in changes.added:
122
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
123
                    self.upload_file(path, id)
0.152.8 by Vincent Ladeuil
Handle uploading directories.
124
                elif kind is 'directory':
125
                    self.make_remote_dir(path)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
126
                else:
127
                    raise NotImplementedError
128
            # XXX: Add a test for exec_change
129
            for (path, id, kind,
130
                 content_change, exec_change) in changes.modified:
131
                if kind is 'file':
0.152.7 by Vincent Ladeuil
Slight refactoring.
132
                    self.upload_file(path, id)
0.152.5 by v.ladeuil+lp at free
Partial incremental upload implementationm tests pass.
133
                else:
134
                    raise NotImplementedError
135
            self.set_uploaded_revid(self.rev_id)
136
        finally:
137
            self.tree.unlock()
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
138
0.152.1 by Vincent Ladeuil
Empty shell
139
140
commands.register_command(cmd_upload)
141
0.152.4 by v.ladeuil+lp at free
Implement a trivial implementation to make one test pass.
142
0.152.1 by Vincent Ladeuil
Empty shell
143
def test_suite():
144
    from bzrlib.tests import TestUtil
145
146
    suite = TestUtil.TestSuite()
147
    loader = TestUtil.TestLoader()
148
    testmod_names = [
149
        'test_upload',
150
        ]
151
152
    suite.addTest(loader.loadTestsFromModuleNames(
153
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
154
    return suite