~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/dir_exporter.py

  • Committer: Andrew Bennetts
  • Date: 2010-01-04 02:25:11 UTC
  • mfrom: (4634.108.8 2.0)
  • mto: This revision was merged to the branch mainline in revision 4928.
  • Revision ID: andrew.bennetts@canonical.com-20100104022511-2tq2r9w2te84wzgs
Merge lp:bzr/2.0 into lp:bzr, including fixes for #343218, #495000, #495023, #494406 and #498378.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2009 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
52
52
                raise errors.BzrError("Can't export tree to non-empty directory.")
53
53
        else:
54
54
            raise
 
55
    # Iterate everything, building up the files we will want to export, and
 
56
    # creating the directories and symlinks that we need.
 
57
    # This tracks (file_id, (destination_path, executable))
 
58
    # This matches the api that tree.iter_files_bytes() wants
 
59
    # Note in the case of revision trees, this does trigger a double inventory
 
60
    # lookup, hopefully it isn't too expensive.
 
61
    to_fetch = []
55
62
    for dp, ie in _export_iter_entries(tree, subdir):
56
63
        fullpath = osutils.pathjoin(dest, dp)
57
64
        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)
 
65
            to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id))))
70
66
        elif ie.kind == "directory":
71
67
            os.mkdir(fullpath)
72
68
        elif ie.kind == "symlink":
80
76
        else:
81
77
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
82
78
               (ie.file_id, ie.kind))
 
79
    # The data returned here can be in any order, but we've already created all
 
80
    # the directories
 
81
    flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
 
82
    for (relpath, executable), chunks in tree.iter_files_bytes(to_fetch):
 
83
        if filtered:
 
84
            filters = tree._content_filter_stack(relpath)
 
85
            context = ContentFilterContext(relpath, tree, ie)
 
86
            chunks = filtered_output_bytes(chunks, filters, context)
 
87
        fullpath = osutils.pathjoin(dest, relpath)
 
88
        # We set the mode and let the umask sort out the file info
 
89
        mode = 0666
 
90
        if executable:
 
91
            mode = 0777
 
92
        out = os.fdopen(os.open(fullpath, flags, mode), 'wb')
 
93
        try:
 
94
            out.writelines(chunks)
 
95
        finally:
 
96
            out.close()