~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

Use global osutils, otherwise it creates a local var.

Which works, but causes us to run the import on every call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from stat import S_ISREG, S_IEXEC
20
20
import time
21
21
 
 
22
import bzrlib
22
23
from bzrlib import (
23
24
    errors,
24
25
    lazy_import,
25
26
    registry,
26
 
    trace,
27
27
    tree,
28
28
    )
29
29
lazy_import.lazy_import(globals(), """
38
38
    multiparent,
39
39
    osutils,
40
40
    revision as _mod_revision,
 
41
    trace,
41
42
    ui,
42
43
    urlutils,
43
44
    )
47
48
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
48
49
                           UnableCreateSymlink)
49
50
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
 
51
from bzrlib.inventory import InventoryEntry
50
52
from bzrlib.osutils import (
51
53
    delete_any,
52
54
    file_kind,
62
64
    deprecated_in,
63
65
    deprecated_method,
64
66
    )
 
67
from bzrlib.trace import warning
65
68
 
66
69
 
67
70
ROOT_PARENT = "root-parent"
102
105
        self._new_parent = {}
103
106
        # mapping of trans_id with new contents -> new file_kind
104
107
        self._new_contents = {}
105
 
        # mapping of trans_id => (sha1 of content, stat_value)
106
 
        self._observed_sha1s = {}
107
108
        # Set of trans_ids whose contents will be removed
108
109
        self._removed_contents = set()
109
110
        # Mapping of trans_id -> new execute-bit value
628
629
            if kind is None:
629
630
                conflicts.append(('versioning no contents', trans_id))
630
631
                continue
631
 
            if not inventory.InventoryEntry.versionable_kind(kind):
 
632
            if not InventoryEntry.versionable_kind(kind):
632
633
                conflicts.append(('versioning bad kind', trans_id, kind))
633
634
        return conflicts
634
635
 
753
754
        return trans_id
754
755
 
755
756
    def new_file(self, name, parent_id, contents, file_id=None,
756
 
                 executable=None, sha1=None):
 
757
                 executable=None):
757
758
        """Convenience method to create files.
758
759
 
759
760
        name is the name of the file to create.
766
767
        trans_id = self._new_entry(name, parent_id, file_id)
767
768
        # TODO: rather than scheduling a set_executable call,
768
769
        # have create_file create the file with the right mode.
769
 
        self.create_file(contents, trans_id, sha1=sha1)
 
770
        self.create_file(contents, trans_id)
770
771
        if executable is not None:
771
772
            self.set_executability(executable, trans_id)
772
773
        return trans_id
1248
1249
            descendants.update(self._limbo_descendants(descendant))
1249
1250
        return descendants
1250
1251
 
1251
 
    def create_file(self, contents, trans_id, mode_id=None, sha1=None):
 
1252
    def create_file(self, contents, trans_id, mode_id=None):
1252
1253
        """Schedule creation of a new file.
1253
1254
 
1254
 
        :seealso: new_file.
1255
 
 
1256
 
        :param contents: an iterator of strings, all of which will be written
1257
 
            to the target destination.
1258
 
        :param trans_id: TreeTransform handle
1259
 
        :param mode_id: If not None, force the mode of the target file to match
1260
 
            the mode of the object referenced by mode_id.
1261
 
            Otherwise, we will try to preserve mode bits of an existing file.
1262
 
        :param sha1: If the sha1 of this content is already known, pass it in.
1263
 
            We can use it to prevent future sha1 computations.
 
1255
        See also new_file.
 
1256
 
 
1257
        Contents is an iterator of strings, all of which will be written
 
1258
        to the target destination.
 
1259
 
 
1260
        New file takes the permissions of any existing file with that id,
 
1261
        unless mode_id is specified.
1264
1262
        """
1265
1263
        name = self._limbo_name(trans_id)
1266
1264
        f = open(name, 'wb')
1273
1271
                f.close()
1274
1272
                os.unlink(name)
1275
1273
                raise
 
1274
 
1276
1275
            f.writelines(contents)
1277
1276
        finally:
1278
1277
            f.close()
1279
1278
        self._set_mtime(name)
1280
1279
        self._set_mode(trans_id, mode_id, S_ISREG)
1281
 
        # It is unfortunate we have to use lstat instead of fstat, but we just
1282
 
        # used utime and chmod on the file, so we need the accurate final
1283
 
        # details.
1284
 
        if sha1 is not None:
1285
 
            self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1286
1280
 
1287
1281
    def _read_file_chunks(self, trans_id):
1288
1282
        cur_file = open(self._limbo_name(trans_id), 'rb')
1347
1341
    def cancel_creation(self, trans_id):
1348
1342
        """Cancel the creation of new file contents."""
1349
1343
        del self._new_contents[trans_id]
1350
 
        if trans_id in self._observed_sha1s:
1351
 
            del self._observed_sha1s[trans_id]
1352
1344
        children = self._limbo_children.get(trans_id)
1353
1345
        # if this is a limbo directory with children, move them before removing
1354
1346
        # the directory
1370
1362
        if orphan_policy is None:
1371
1363
            orphan_policy = default_policy
1372
1364
        if orphan_policy not in orphaning_registry:
1373
 
            trace.warning('%s (from %s) is not a known policy, defaulting '
1374
 
                'to %s' % (orphan_policy, conf_var_name, default_policy))
 
1365
            trace.warning('%s (from %s) is not a known policy, defaulting to %s'
 
1366
                          % (orphan_policy, conf_var_name, default_policy))
1375
1367
            orphan_policy = default_policy
1376
1368
        handle_orphan = orphaning_registry.get(orphan_policy)
1377
1369
        handle_orphan(self, trans_id, parent_id)
1710
1702
        finally:
1711
1703
            child_pb.finished()
1712
1704
        self._tree.apply_inventory_delta(inventory_delta)
1713
 
        self._apply_observed_sha1s()
1714
1705
        self._done = True
1715
1706
        self.finalize()
1716
1707
        return _TransformResults(modified_paths, self.rename_count)
1836
1827
                            raise
1837
1828
                    else:
1838
1829
                        self.rename_count += 1
1839
 
                    # TODO: if trans_id in self._observed_sha1s, we should
1840
 
                    #       re-stat the final target, since ctime will be
1841
 
                    #       updated by the change.
1842
1830
                if (trans_id in self._new_contents or
1843
1831
                    self.path_changed(trans_id)):
1844
1832
                    if trans_id in self._new_contents:
1845
1833
                        modified_paths.append(full_path)
1846
1834
                if trans_id in self._new_executability:
1847
1835
                    self._set_executability(path, trans_id)
1848
 
                if trans_id in self._observed_sha1s:
1849
 
                    o_sha1, o_st_val = self._observed_sha1s[trans_id]
1850
 
                    st = osutils.lstat(full_path)
1851
 
                    self._observed_sha1s[trans_id] = (o_sha1, st)
1852
1836
        finally:
1853
1837
            child_pb.finished()
1854
1838
        self._new_contents.clear()
1855
1839
        return modified_paths
1856
1840
 
1857
 
    def _apply_observed_sha1s(self):
1858
 
        """After we have finished renaming everything, update observed sha1s
1859
 
 
1860
 
        This has to be done after self._tree.apply_inventory_delta, otherwise
1861
 
        it doesn't know anything about the files we are updating. Also, we want
1862
 
        to do this as late as possible, so that most entries end up cached.
1863
 
        """
1864
 
        # TODO: this doesn't update the stat information for directories. So
1865
 
        #       the first 'bzr status' will still need to rewrite
1866
 
        #       .bzr/checkout/dirstate. However, we at least don't need to
1867
 
        #       re-read all of the files.
1868
 
        # TODO: If the operation took a while, we could do a time.sleep(3) here
1869
 
        #       to allow the clock to tick over and ensure we won't have any
1870
 
        #       problems. (we could observe start time, and finish time, and if
1871
 
        #       it is less than eg 10% overhead, add a sleep call.)
1872
 
        paths = FinalPaths(self)
1873
 
        for trans_id, observed in self._observed_sha1s.iteritems():
1874
 
            path = paths.get_path(trans_id)
1875
 
            # We could get the file_id, but dirstate prefers to use the path
1876
 
            # anyway, and it is 'cheaper' to determine.
1877
 
            # file_id = self._new_id[trans_id]
1878
 
            self._tree._observed_sha1(None, path, observed)
1879
 
 
1880
1841
 
1881
1842
class TransformPreview(DiskTreeTransform):
1882
1843
    """A TreeTransform for generating preview trees.
1932
1893
        raise NotImplementedError(self.new_orphan)
1933
1894
 
1934
1895
 
1935
 
class _PreviewTree(tree.InventoryTree):
 
1896
class _PreviewTree(tree.Tree):
1936
1897
    """Partial implementation of Tree to support show_diff_trees"""
1937
1898
 
1938
1899
    def __init__(self, transform):
1967
1928
                yield self._get_repository().revision_tree(revision_id)
1968
1929
 
1969
1930
    def _get_file_revision(self, file_id, vf, tree_revision):
1970
 
        parent_keys = [(file_id, t.get_file_revision(file_id)) for t in
 
1931
        parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
1971
1932
                       self._iter_parent_trees()]
1972
1933
        vf.add_lines((file_id, tree_revision), parent_keys,
1973
1934
                     self.get_file_lines(file_id))
2541
2502
                    executable = tree.is_executable(file_id, tree_path)
2542
2503
                    if executable:
2543
2504
                        tt.set_executability(executable, trans_id)
2544
 
                    trans_data = (trans_id, tree_path, entry.text_sha1)
 
2505
                    trans_data = (trans_id, tree_path)
2545
2506
                    deferred_contents.append((file_id, trans_data))
2546
2507
                else:
2547
2508
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2563
2524
            precomputed_delta = None
2564
2525
        conflicts = cook_conflicts(raw_conflicts, tt)
2565
2526
        for conflict in conflicts:
2566
 
            trace.warning(conflict)
 
2527
            warning(conflict)
2567
2528
        try:
2568
2529
            wt.add_conflicts(conflicts)
2569
2530
        except errors.UnsupportedOperation:
2592
2553
        unchanged = dict(unchanged)
2593
2554
        new_desired_files = []
2594
2555
        count = 0
2595
 
        for file_id, (trans_id, tree_path, text_sha1) in desired_files:
 
2556
        for file_id, (trans_id, tree_path) in desired_files:
2596
2557
            accelerator_path = unchanged.get(file_id)
2597
2558
            if accelerator_path is None:
2598
 
                new_desired_files.append((file_id,
2599
 
                    (trans_id, tree_path, text_sha1)))
 
2559
                new_desired_files.append((file_id, (trans_id, tree_path)))
2600
2560
                continue
2601
2561
            pb.update('Adding file contents', count + offset, total)
2602
2562
            if hardlink:
2609
2569
                    contents = filtered_output_bytes(contents, filters,
2610
2570
                        ContentFilterContext(tree_path, tree))
2611
2571
                try:
2612
 
                    tt.create_file(contents, trans_id, sha1=text_sha1)
 
2572
                    tt.create_file(contents, trans_id)
2613
2573
                finally:
2614
2574
                    try:
2615
2575
                        contents.close()
2618
2578
                        pass
2619
2579
            count += 1
2620
2580
        offset += count
2621
 
    for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
 
2581
    for count, ((trans_id, tree_path), contents) in enumerate(
2622
2582
            tree.iter_files_bytes(new_desired_files)):
2623
2583
        if wt.supports_content_filtering():
2624
2584
            filters = wt._content_filter_stack(tree_path)
2625
2585
            contents = filtered_output_bytes(contents, filters,
2626
2586
                ContentFilterContext(tree_path, tree))
2627
 
        tt.create_file(contents, trans_id, sha1=text_sha1)
 
2587
        tt.create_file(contents, trans_id)
2628
2588
        pb.update('Adding file contents', count + offset, total)
2629
2589
 
2630
2590
 
2805
2765
                unversioned_filter=working_tree.is_ignored)
2806
2766
            delta.report_changes(tt.iter_changes(), change_reporter)
2807
2767
        for conflict in conflicts:
2808
 
            trace.warning(conflict)
 
2768
            warning(conflict)
2809
2769
        pp.next_phase()
2810
2770
        tt.apply()
2811
2771
        working_tree.set_merge_modified(merge_modified)
3039
2999
                        file_id = tt.final_file_id(trans_id)
3040
3000
                        if file_id is None:
3041
3001
                            file_id = tt.inactive_file_id(trans_id)
3042
 
                        _, entry = path_tree.iter_entries_by_dir(
3043
 
                            [file_id]).next()
 
3002
                        entry = path_tree.inventory[file_id]
3044
3003
                        # special-case the other tree root (move its
3045
3004
                        # children to current root)
3046
3005
                        if entry.parent_id is None: