/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/export/dir_exporter.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-20 15:54:59 UTC
  • mfrom: (6968.2.10 archive)
  • mto: This revision was merged to the branch mainline in revision 6973.
  • Revision ID: jelmer@jelmer.uk-20180520155459-4u1tpealx8jj3sy3
Merge archive branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Export a breezy.tree.Tree to a new or empty directory."""
18
 
 
19
 
from __future__ import absolute_import
20
 
 
21
 
import errno
22
 
import os
23
 
 
24
 
from .. import errors, osutils
25
 
from ..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.
31
 
 
32
 
    `dest` should either not exist or should be empty. If it does not exist it
33
 
    will be created holding the contents of this tree.
34
 
 
35
 
    :param fileobj: Is not used in this exporter
36
 
 
37
 
    :note: If the export fails, the destination directory will be
38
 
           left in an incompletely exported state: export is not transactional.
39
 
    """
40
 
    try:
41
 
        os.mkdir(dest)
42
 
    except OSError as e:
43
 
        if e.errno == errno.EEXIST:
44
 
            # check if directory empty
45
 
            if os.listdir(dest) != []:
46
 
                raise errors.BzrError(
47
 
                    "Can't export tree to non-empty directory.")
48
 
        else:
49
 
            raise
50
 
    # Iterate everything, building up the files we will want to export, and
51
 
    # creating the directories and symlinks that we need.
52
 
    # This tracks (file_id, (destination_path, executable))
53
 
    # This matches the api that tree.iter_files_bytes() wants
54
 
    # Note in the case of revision trees, this does trigger a double inventory
55
 
    # lookup, hopefully it isn't too expensive.
56
 
    to_fetch = []
57
 
    for dp, tp, ie in _export_iter_entries(tree, subdir):
58
 
        fullpath = osutils.pathjoin(dest, dp)
59
 
        if ie.kind == "file":
60
 
            to_fetch.append((tp, (dp, tp, ie.file_id)))
61
 
        elif ie.kind in ("directory", "tree-reference"):
62
 
            os.mkdir(fullpath)
63
 
        elif ie.kind == "symlink":
64
 
            try:
65
 
                symlink_target = tree.get_symlink_target(tp, ie.file_id)
66
 
                os.symlink(symlink_target, fullpath)
67
 
            except OSError as e:
68
 
                raise errors.BzrError(
69
 
                    "Failed to create symlink %r -> %r, error: %s"
70
 
                    % (fullpath, symlink_target, e))
71
 
        else:
72
 
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
73
 
               (tp, ie.kind))
74
 
 
75
 
        yield
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
 
    for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
80
 
        fullpath = osutils.pathjoin(dest, relpath)
81
 
        # We set the mode and let the umask sort out the file info
82
 
        mode = 0o666
83
 
        if tree.is_executable(treepath, file_id):
84
 
            mode = 0o777
85
 
        with os.fdopen(os.open(fullpath, flags, mode), 'wb') as out:
86
 
            out.writelines(chunks)
87
 
        if force_mtime is not None:
88
 
            mtime = force_mtime
89
 
        else:
90
 
            mtime = tree.get_file_mtime(treepath, file_id)
91
 
        os.utime(fullpath, (mtime, mtime))
92
 
 
93
 
        yield