~bzr-pqm/bzr/bzr.dev

5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
1
# Copyright (C) 2005-2011 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Export a bzrlib.tree.Tree to a new or empty directory."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
21
import errno
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
22
import os
3368.2.32 by Ian Clatworthy
add --filters to export command
23
24
from bzrlib import errors, osutils
3613.2.2 by Robert Collins
Refactor exporters to remove obvious duplication to a helper function.
25
from bzrlib.export import _export_iter_entries
6006.3.7 by Martin Pool
Remove duplicated content-filtering code from exporters
26
27
28
def dir_exporter_generator(tree, dest, root, subdir=None,
5952.1.20 by geoffreyfishing at gmail
Fixed compatibility issues with directory exporter.
29
                           force_mtime=None, fileobj=None):
5952.1.15 by geoffreyfishing at gmail
Major code cleanup.
30
    """Return a generator that exports this tree to a new directory.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
31
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
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.
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
34
5952.1.9 by geoffreyfishing at gmail
Updated directory exporter.
35
    :param fileobj: Is not used in this exporter
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
36
37
    :note: If the export fails, the destination directory will be
4996.1.1 by Robert Collins
Set the file mtime when exporting to a directory to prevent triggering make rebuilds unnecessarily.
38
           left in an incompletely exported state: export is not transactional.
1185.31.12 by John Arbash Meinel
Refactored the export code to make it easier to add new export formats.
39
    """
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
40
    try:
41
        os.mkdir(dest)
42
    except OSError, e:
43
        if e.errno == errno.EEXIST:
44
            # check if directory empty
45
            if os.listdir(dest) != []:
5952.2.1 by Vincent Ladeuil
PEP8 tweaks, lines too long, spaces at end of lines.
46
                raise errors.BzrError(
47
                    "Can't export tree to non-empty directory.")
4576.1.1 by Alexander Belchenko
Fixed export to existing empty directory.
48
        else:
49
            raise
4634.111.1 by John Arbash Meinel
First cut at a possible fix for bug #343218
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 = []
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
57
    for dp, tp, ie in _export_iter_entries(tree, subdir):
3368.2.32 by Ian Clatworthy
add --filters to export command
58
        fullpath = osutils.pathjoin(dest, dp)
59
        if ie.kind == "file":
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
60
            to_fetch.append((ie.file_id, (dp, tp, ie.file_id)))
3368.2.32 by Ian Clatworthy
add --filters to export command
61
        elif ie.kind == "directory":
62
            os.mkdir(fullpath)
63
        elif ie.kind == "symlink":
64
            try:
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
65
                symlink_target = tree.get_symlink_target(ie.file_id, tp)
4562.1.1 by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree.
66
                os.symlink(symlink_target, fullpath)
5967.6.2 by Martin Pool
Delete fairly useless and repetitive per-format export single-call functions.
67
            except OSError, e:
3368.2.32 by Ian Clatworthy
add --filters to export command
68
                raise errors.BzrError(
69
                    "Failed to create symlink %r -> %r, error: %s"
4562.1.1 by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree.
70
                    % (fullpath, symlink_target, e))
3368.2.32 by Ian Clatworthy
add --filters to export command
71
        else:
72
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
73
               (ie.file_id, ie.kind))
5952.1.15 by geoffreyfishing at gmail
Major code cleanup.
74
5952.1.9 by geoffreyfishing at gmail
Updated directory exporter.
75
        yield
4634.111.1 by John Arbash Meinel
First cut at a possible fix for bug #343218
76
    # The data returned here can be in any order, but we've already created all
77
    # the directories
4634.111.5 by John Arbash Meinel
Change to using os.open() allowing the users umask to handle perms.
78
    flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
79
    for (relpath, treepath, file_id), chunks in tree.iter_files_bytes(to_fetch):
4634.111.1 by John Arbash Meinel
First cut at a possible fix for bug #343218
80
        fullpath = osutils.pathjoin(dest, relpath)
4634.111.5 by John Arbash Meinel
Change to using os.open() allowing the users umask to handle perms.
81
        # We set the mode and let the umask sort out the file info
82
        mode = 0666
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
83
        if tree.is_executable(file_id, treepath):
4634.111.5 by John Arbash Meinel
Change to using os.open() allowing the users umask to handle perms.
84
            mode = 0777
4634.111.6 by John Arbash Meinel
Of course, it helps if you tell the buffered file that it is writable.
85
        out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
4634.111.1 by John Arbash Meinel
First cut at a possible fix for bug #343218
86
        try:
87
            out.writelines(chunks)
88
        finally:
89
            out.close()
5718.5.1 by Jelmer Vernooij
per_file_timestamp -> force_mtime.
90
        if force_mtime is not None:
91
            mtime = force_mtime
92
        else:
6331.2.1 by Jelmer Vernooij
Consistently pass tree path when exporting.
93
            mtime = tree.get_file_mtime(file_id, treepath)
5076.2.1 by Jelmer Vernooij
Add use_tree_timestamp argument to exporters.
94
        os.utime(fullpath, (mtime, mtime))
5952.1.15 by geoffreyfishing at gmail
Major code cleanup.
95
5952.1.9 by geoffreyfishing at gmail
Updated directory exporter.
96
        yield