~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/dir_exporter.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

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