~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/dir_exporter.py

  • Committer: Jelmer Vernooij
  • Date: 2011-08-04 13:30:30 UTC
  • mfrom: (6050 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6052.
  • Revision ID: jelmer@samba.org-20110804133030-uwo00unp8b0n782c
merge bzr.dev.

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-2011 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
21
21
 
22
22
from bzrlib import errors, osutils
23
23
from bzrlib.export import _export_iter_entries
24
 
from bzrlib.filters import (
25
 
    ContentFilterContext,
26
 
    filtered_output_bytes,
27
 
    )
28
 
 
29
 
 
30
 
def dir_exporter(tree, dest, root, subdir=None, filtered=False, force_mtime=None):
31
 
    """Export this tree to a new directory.
 
24
 
 
25
 
 
26
def dir_exporter_generator(tree, dest, root, subdir=None,
 
27
                           force_mtime=None, fileobj=None):
 
28
    """Return a generator that exports this tree to a new directory.
32
29
 
33
30
    `dest` should either not exist or should be empty. If it does not exist it
34
31
    will be created holding the contents of this tree.
35
32
 
 
33
    :param fileobj: Is not used in this exporter
 
34
 
36
35
    :note: If the export fails, the destination directory will be
37
36
           left in an incompletely exported state: export is not transactional.
38
37
    """
42
41
        if e.errno == errno.EEXIST:
43
42
            # check if directory empty
44
43
            if os.listdir(dest) != []:
45
 
                raise errors.BzrError("Can't export tree to non-empty directory.")
 
44
                raise errors.BzrError(
 
45
                    "Can't export tree to non-empty directory.")
46
46
        else:
47
47
            raise
48
48
    # Iterate everything, building up the files we will want to export, and
62
62
            try:
63
63
                symlink_target = tree.get_symlink_target(ie.file_id)
64
64
                os.symlink(symlink_target, fullpath)
65
 
            except OSError,e:
 
65
            except OSError, e:
66
66
                raise errors.BzrError(
67
67
                    "Failed to create symlink %r -> %r, error: %s"
68
68
                    % (fullpath, symlink_target, e))
69
69
        else:
70
70
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
71
71
               (ie.file_id, ie.kind))
 
72
 
 
73
        yield
72
74
    # The data returned here can be in any order, but we've already created all
73
75
    # the directories
74
76
    flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
75
77
    for (relpath, executable), chunks in tree.iter_files_bytes(to_fetch):
76
 
        if filtered:
77
 
            filters = tree._content_filter_stack(relpath)
78
 
            context = ContentFilterContext(relpath, tree, ie)
79
 
            chunks = filtered_output_bytes(chunks, filters, context)
80
78
        fullpath = osutils.pathjoin(dest, relpath)
81
79
        # We set the mode and let the umask sort out the file info
82
80
        mode = 0666
90
88
        if force_mtime is not None:
91
89
            mtime = force_mtime
92
90
        else:
93
 
            mtime = tree.get_file_mtime(tree.path2id(relpath), relpath)
 
91
            if subdir is None:
 
92
                file_id = tree.path2id(relpath)
 
93
            else:
 
94
                file_id = tree.path2id(osutils.pathjoin(subdir, relpath))
 
95
            mtime = tree.get_file_mtime(file_id, relpath)
94
96
        os.utime(fullpath, (mtime, mtime))
 
97
 
 
98
        yield