~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

Changed model again.  Now iterator is used immediately.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
        self._tree_id_paths = {}
37
37
        self._new_root = self.get_id_tree(tree.get_root_id())
38
38
        self.__done = False
 
39
        self._limbodir = self._tree.branch.controlfilename('limbo')
 
40
        os.mkdir(self._limbodir)
39
41
 
40
42
    def finalize(self):
41
43
        """Release the working tree lock, if held."""
42
44
        if self._tree is None:
43
45
            return
 
46
        for trans_id, kind in self._new_contents.iteritems():
 
47
            path = self._limbo_name(trans_id)
 
48
            if kind == "directory":
 
49
                os.rmdir(path)
 
50
            else:
 
51
                os.unlink(path)
 
52
        os.rmdir(self._limbodir)
44
53
        self._tree.unlock()
45
54
        self._tree = None
46
55
 
134
143
        Contents is an iterator of strings, all of which will be written
135
144
        to the target destination.
136
145
        """
137
 
        unique_add(self._new_contents, trans_id, ('file', contents))
 
146
        f = file(self._limbo_name(trans_id), 'wb')
 
147
        for segment in contents:
 
148
            f.write(segment)
 
149
        f.close()
 
150
        unique_add(self._new_contents, trans_id, 'file')
138
151
 
139
152
    def create_directory(self, trans_id):
140
153
        """Schedule creation of a new directory.
141
154
        
142
155
        See also new_directory.
143
156
        """
144
 
        unique_add(self._new_contents, trans_id, ('directory',))
 
157
        os.mkdir(self._limbo_name(trans_id))
 
158
        unique_add(self._new_contents, trans_id, 'directory')
145
159
 
146
160
    def create_symlink(self, target, trans_id):
147
161
        """Schedule creation of a new symbolic link.
149
163
        target is a bytestring.
150
164
        See also new_symlink.
151
165
        """
152
 
        unique_add(self._new_contents, trans_id, ('symlink', target))
 
166
        os.symlink(target, self._limbo_name(trans_id))
 
167
        unique_add(self._new_contents, trans_id, 'symlink')
153
168
 
154
169
    def delete_contents(self, trans_id):
155
170
        """Schedule the contents of a path entry for deletion"""
215
230
        corresponding contents insertion command)
216
231
        """
217
232
        if trans_id in self._new_contents:
218
 
            return self._new_contents[trans_id][0]
 
233
            return self._new_contents[trans_id]
219
234
        elif trans_id in self._removed_contents:
220
235
            raise NoSuchFile(None)
221
236
        else:
479
494
        conflicts = self.find_conflicts()
480
495
        if len(conflicts) != 0:
481
496
            raise MalformedTransform(conflicts=conflicts)
482
 
        os.mkdir(self._tree.branch.controlfilename('limbo'))
483
497
        limbo_inv = {}
484
498
        inv = self._tree.inventory
485
 
        self._gen_new_contents()
486
499
        self._apply_removals(inv, limbo_inv)
487
500
        self._apply_insertions(inv, limbo_inv)
488
 
        os.rmdir(self._tree.branch.controlfilename('limbo'))
489
501
        self._tree._write_inventory(inv)
490
502
        self.__done = True
491
503
        self.finalize()
495
507
        limbo = self._tree.branch.controlfilename('limbo')
496
508
        return os.path.join(limbo, trans_id)
497
509
 
498
 
    def _gen_new_contents(self):
499
 
        """\
500
 
        Store new file contents in limbo.
501
 
        This is to avoid problems when the working tree is also one of the
502
 
        input trees.
503
 
        """
504
 
        for trans_id, content in self._new_contents.iteritems():
505
 
            kind = content[0]
506
 
            path = self._limbo_name(trans_id)
507
 
            if kind == 'file':
508
 
                f = file(path, 'wb')
509
 
                for segment in content[1]:
510
 
                    f.write(segment)
511
 
                f.close()
512
 
            elif kind == 'directory':
513
 
                os.mkdir(self._tree.abspath(path))
514
 
            elif kind == 'symlink':
515
 
                target = self._new_contents[trans_id][1]
516
 
                os.symlink(target, self._tree.abspath(path))
517
 
        
518
 
 
519
510
    def _apply_removals(self, inv, limbo_inv):
520
511
        """Perform tree operations that remove directory/inventory names.
521
512
        
564
555
        limbo = self._tree.branch.controlfilename('limbo')
565
556
        for path, trans_id in self.new_paths():
566
557
            try:
567
 
                kind = self._new_contents[trans_id][0]
 
558
                kind = self._new_contents[trans_id]
568
559
            except KeyError:
569
560
                kind = contents = None
570
561
            if trans_id in self._new_contents or self.path_changed(trans_id):
575
566
                    # We may be renaming a dangling inventory id
576
567
                    if e.errno != errno.ENOENT:
577
568
                        raise
 
569
                if trans_id in self._new_contents:
 
570
                    del self._new_contents[trans_id]
578
571
 
579
572
            if trans_id in self._new_id:
580
573
                if kind is None: