/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 bzrlib/export/dir_exporter.py

  • Committer: Martin Pool
  • Date: 2009-12-14 06:06:59 UTC
  • mfrom: (4889 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4891.
  • Revision ID: mbp@sourcefrog.net-20091214060659-1ucv8hpnky0cbnaj
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Export a Tree to a non-versioned directory.
18
18
"""
19
19
 
 
20
import errno
20
21
import os
 
22
import StringIO
 
23
 
 
24
from bzrlib import errors, osutils
 
25
from bzrlib.export import _export_iter_entries
 
26
from bzrlib.filters import (
 
27
    ContentFilterContext,
 
28
    filtered_output_bytes,
 
29
    )
21
30
from bzrlib.trace import mutter
22
31
 
23
 
def dir_exporter(tree, dest, root):
 
32
 
 
33
def dir_exporter(tree, dest, root, subdir, filtered=False):
24
34
    """Export this tree to a new directory.
25
35
 
26
36
    `dest` should not exist, and will be created holding the
32
42
    :note: If the export fails, the destination directory will be
33
43
           left in a half-assed state.
34
44
    """
35
 
    import os
36
 
    os.mkdir(dest)
37
45
    mutter('export version %r', tree)
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
 
 
 
46
    try:
 
47
        os.mkdir(dest)
 
48
    except OSError, e:
 
49
        if e.errno == errno.EEXIST:
 
50
            # check if directory empty
 
51
            if os.listdir(dest) != []:
 
52
                raise errors.BzrError("Can't export tree to non-empty directory.")
 
53
        else:
 
54
            raise
 
55
    for dp, ie in _export_iter_entries(tree, subdir):
 
56
        fullpath = osutils.pathjoin(dest, dp)
 
57
        if ie.kind == "file":
 
58
            if filtered:
 
59
                chunks = tree.get_file_lines(ie.file_id)
 
60
                filters = tree._content_filter_stack(dp)
 
61
                context = ContentFilterContext(dp, tree, ie)
 
62
                contents = filtered_output_bytes(chunks, filters, context)
 
63
                content = ''.join(contents)
 
64
                fileobj = StringIO.StringIO(content)
 
65
            else:
 
66
                fileobj = tree.get_file(ie.file_id)
 
67
            osutils.pumpfile(fileobj, file(fullpath, 'wb'))
 
68
            if tree.is_executable(ie.file_id):
 
69
                os.chmod(fullpath, 0755)
 
70
        elif ie.kind == "directory":
 
71
            os.mkdir(fullpath)
 
72
        elif ie.kind == "symlink":
 
73
            try:
 
74
                symlink_target = tree.get_symlink_target(ie.file_id)
 
75
                os.symlink(symlink_target, fullpath)
 
76
            except OSError,e:
 
77
                raise errors.BzrError(
 
78
                    "Failed to create symlink %r -> %r, error: %s"
 
79
                    % (fullpath, symlink_target, e))
 
80
        else:
 
81
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
 
82
               (ie.file_id, ie.kind))