~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/dir_exporter.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 16:43:12 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730164312-b025fd3ff0cee59e
rename  gpl.txt => COPYING.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2005 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Export a bzrlib.tree.Tree to a new or empty directory."""
18
 
 
19
 
import errno
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Export a Tree to a non-versioned directory.
 
18
"""
 
19
 
20
20
import os
21
 
import time
22
 
 
23
 
from bzrlib import errors, osutils
24
 
from bzrlib.export import _export_iter_entries
25
 
from bzrlib.filters import (
26
 
    ContentFilterContext,
27
 
    filtered_output_bytes,
28
 
    )
29
21
from bzrlib.trace import mutter
30
22
 
31
 
 
32
 
def dir_exporter(tree, dest, root, subdir, filtered=False,
33
 
                 per_file_timestamps=False):
 
23
def dir_exporter(tree, dest, root):
34
24
    """Export this tree to a new directory.
35
25
 
36
 
    `dest` should either not exist or should be empty. If it does not exist it
37
 
    will be created holding the contents of this tree.
 
26
    `dest` should not exist, and will be created holding the
 
27
    contents of this tree.
 
28
 
 
29
    TODO: To handle subdirectories we need to create the
 
30
           directories first.
38
31
 
39
32
    :note: If the export fails, the destination directory will be
40
 
           left in an incompletely exported state: export is not transactional.
 
33
           left in a half-assed state.
41
34
    """
 
35
    import os
 
36
    os.mkdir(dest)
42
37
    mutter('export version %r', tree)
43
 
    try:
44
 
        os.mkdir(dest)
45
 
    except OSError, e:
46
 
        if e.errno == errno.EEXIST:
47
 
            # check if directory empty
48
 
            if os.listdir(dest) != []:
49
 
                raise errors.BzrError("Can't export tree to non-empty directory.")
50
 
        else:
51
 
            raise
52
 
    # Iterate everything, building up the files we will want to export, and
53
 
    # creating the directories and symlinks that we need.
54
 
    # This tracks (file_id, (destination_path, executable))
55
 
    # This matches the api that tree.iter_files_bytes() wants
56
 
    # Note in the case of revision trees, this does trigger a double inventory
57
 
    # lookup, hopefully it isn't too expensive.
58
 
    to_fetch = []
59
 
    for dp, ie in _export_iter_entries(tree, subdir):
60
 
        fullpath = osutils.pathjoin(dest, dp)
61
 
        if ie.kind == "file":
62
 
            to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
63
 
        elif ie.kind == "directory":
64
 
            os.mkdir(fullpath)
65
 
        elif ie.kind == "symlink":
66
 
            try:
67
 
                symlink_target = tree.get_symlink_target(ie.file_id)
68
 
                os.symlink(symlink_target, fullpath)
69
 
            except OSError,e:
70
 
                raise errors.BzrError(
71
 
                    "Failed to create symlink %r -> %r, error: %s"
72
 
                    % (fullpath, symlink_target, e))
73
 
        else:
74
 
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
75
 
               (ie.file_id, ie.kind))
76
 
    # The data returned here can be in any order, but we've already created all
77
 
    # the directories
78
 
    flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
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)
85
 
        fullpath = osutils.pathjoin(dest, relpath)
86
 
        # We set the mode and let the umask sort out the file info
87
 
        mode = 0666
88
 
        if executable:
89
 
            mode = 0777
90
 
        out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
91
 
        try:
92
 
            out.writelines(chunks)
93
 
        finally:
94
 
            out.close()
95
 
        if per_file_timestamps:
96
 
            mtime = tree.get_file_mtime(tree.path2id(relpath), relpath)
97
 
        else:
98
 
            mtime = now
99
 
        os.utime(fullpath, (mtime, mtime))
 
38
    inv = tree.inventory
 
39
    entries = inv.iter_entries()
 
40
    entries.next() # skip root
 
41
    for dp, ie in entries:
 
42
        # .bzrignore has no meaning outside of a working tree
 
43
        # so do not export it
 
44
        if dp == ".bzrignore":
 
45
            continue
 
46
        
 
47
        ie.put_on_disk(dest, dp, tree)
 
48