~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/dir_exporter.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Export a bzrlib.tree.Tree to a new or empty directory."""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
19
import errno
22
20
import os
 
21
import time
23
22
 
24
23
from bzrlib import errors, osutils
25
24
from bzrlib.export import _export_iter_entries
26
 
 
27
 
 
28
 
def dir_exporter_generator(tree, dest, root, subdir=None,
29
 
                           force_mtime=None, fileobj=None):
30
 
    """Return a generator that exports this tree to a new directory.
 
25
from bzrlib.filters import (
 
26
    ContentFilterContext,
 
27
    filtered_output_bytes,
 
28
    )
 
29
from bzrlib.trace import mutter
 
30
 
 
31
 
 
32
def dir_exporter(tree, dest, root, subdir, filtered=False,
 
33
                 per_file_timestamps=False):
 
34
    """Export this tree to a new directory.
31
35
 
32
36
    `dest` should either not exist or should be empty. If it does not exist it
33
37
    will be created holding the contents of this tree.
34
38
 
35
 
    :param fileobj: Is not used in this exporter
36
 
 
37
39
    :note: If the export fails, the destination directory will be
38
40
           left in an incompletely exported state: export is not transactional.
39
41
    """
 
42
    mutter('export version %r', tree)
40
43
    try:
41
44
        os.mkdir(dest)
42
45
    except OSError, e:
43
46
        if e.errno == errno.EEXIST:
44
47
            # check if directory empty
45
48
            if os.listdir(dest) != []:
46
 
                raise errors.BzrError(
47
 
                    "Can't export tree to non-empty directory.")
 
49
                raise errors.BzrError("Can't export tree to non-empty directory.")
48
50
        else:
49
51
            raise
50
52
    # Iterate everything, building up the files we will want to export, and
54
56
    # Note in the case of revision trees, this does trigger a double inventory
55
57
    # lookup, hopefully it isn't too expensive.
56
58
    to_fetch = []
57
 
    for dp, tp, ie in _export_iter_entries(tree, subdir):
 
59
    for dp, ie in _export_iter_entries(tree, subdir):
58
60
        fullpath = osutils.pathjoin(dest, dp)
59
61
        if ie.kind == "file":
60
 
            to_fetch.append((ie.file_id, (dp, tp, ie.file_id)))
 
62
            to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
61
63
        elif ie.kind == "directory":
62
64
            os.mkdir(fullpath)
63
65
        elif ie.kind == "symlink":
64
66
            try:
65
 
                symlink_target = tree.get_symlink_target(ie.file_id, tp)
 
67
                symlink_target = tree.get_symlink_target(ie.file_id)
66
68
                os.symlink(symlink_target, fullpath)
67
 
            except OSError, e:
 
69
            except OSError,e:
68
70
                raise errors.BzrError(
69
71
                    "Failed to create symlink %r -> %r, error: %s"
70
72
                    % (fullpath, symlink_target, e))
71
73
        else:
72
74
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
73
75
               (ie.file_id, ie.kind))
74
 
 
75
 
        yield
76
76
    # The data returned here can be in any order, but we've already created all
77
77
    # the directories
78
78
    flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
79
 
    for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
 
79
    now = time.time()
 
80
    for (relpath, executable), chunks in tree.iter_files_bytes(to_fetch):
 
81
        if filtered:
 
82
            filters = tree._content_filter_stack(relpath)
 
83
            context = ContentFilterContext(relpath, tree, ie)
 
84
            chunks = filtered_output_bytes(chunks, filters, context)
80
85
        fullpath = osutils.pathjoin(dest, relpath)
81
86
        # We set the mode and let the umask sort out the file info
82
87
        mode = 0666
83
 
        if tree.is_executable(file_id, treepath):
 
88
        if executable:
84
89
            mode = 0777
85
90
        out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
86
91
        try:
87
92
            out.writelines(chunks)
88
93
        finally:
89
94
            out.close()
90
 
        if force_mtime is not None:
91
 
            mtime = force_mtime
 
95
        if per_file_timestamps:
 
96
            mtime = tree.get_file_mtime(tree.path2id(relpath), relpath)
92
97
        else:
93
 
            mtime = tree.get_file_mtime(file_id, treepath)
 
98
            mtime = now
94
99
        os.utime(fullpath, (mtime, mtime))
95
 
 
96
 
        yield