4634.111.1
by John Arbash Meinel
First cut at a possible fix for bug #343218 |
1 |
# Copyright (C) 2005, 2009 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 |
|
17 |
"""Export a Tree to a non-versioned directory.
|
|
18 |
"""
|
|
19 |
||
4576.1.1
by Alexander Belchenko
Fixed export to existing empty directory. |
20 |
import errno |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
21 |
import os |
3368.2.32
by Ian Clatworthy
add --filters to export command |
22 |
import StringIO |
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 |
3368.2.32
by Ian Clatworthy
add --filters to export command |
26 |
from bzrlib.filters import ( |
27 |
ContentFilterContext, |
|
28 |
filtered_output_bytes, |
|
29 |
)
|
|
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
30 |
from bzrlib.trace import mutter |
31 |
||
3368.2.32
by Ian Clatworthy
add --filters to export command |
32 |
|
3368.2.41
by Ian Clatworthy
1st cut merge of bzr.dev r3907 |
33 |
def dir_exporter(tree, dest, root, subdir, filtered=False): |
1185.31.12
by John Arbash Meinel
Refactored the export code to make it easier to add new export formats. |
34 |
"""Export this tree to a new directory.
|
35 |
||
36 |
`dest` should not exist, and will be created holding the
|
|
37 |
contents of this tree.
|
|
38 |
||
39 |
TODO: To handle subdirectories we need to create the
|
|
40 |
directories first.
|
|
41 |
||
42 |
:note: If the export fails, the destination directory will be
|
|
43 |
left in a half-assed state.
|
|
44 |
"""
|
|
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
45 |
mutter('export version %r', tree) |
4576.1.1
by Alexander Belchenko
Fixed export to existing empty directory. |
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
|
|
4634.111.1
by John Arbash Meinel
First cut at a possible fix for bug #343218 |
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 = [] |
|
3613.2.2
by Robert Collins
Refactor exporters to remove obvious duplication to a helper function. |
62 |
for dp, ie in _export_iter_entries(tree, subdir): |
3368.2.32
by Ian Clatworthy
add --filters to export command |
63 |
fullpath = osutils.pathjoin(dest, dp) |
64 |
if ie.kind == "file": |
|
4634.111.1
by John Arbash Meinel
First cut at a possible fix for bug #343218 |
65 |
to_fetch.append((ie.file_id, (dp, tree.is_executable(ie.file_id)))) |
3368.2.32
by Ian Clatworthy
add --filters to export command |
66 |
elif ie.kind == "directory": |
67 |
os.mkdir(fullpath) |
|
68 |
elif ie.kind == "symlink": |
|
69 |
try: |
|
4562.1.1
by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree. |
70 |
symlink_target = tree.get_symlink_target(ie.file_id) |
71 |
os.symlink(symlink_target, fullpath) |
|
3368.2.32
by Ian Clatworthy
add --filters to export command |
72 |
except OSError,e: |
73 |
raise errors.BzrError( |
|
74 |
"Failed to create symlink %r -> %r, error: %s" |
|
4562.1.1
by Jelmer Vernooij
Support exporting symlinks when exporting from a working tree. |
75 |
% (fullpath, symlink_target, e)) |
3368.2.32
by Ian Clatworthy
add --filters to export command |
76 |
else: |
77 |
raise errors.BzrError("don't know how to export {%s} of kind %r" % |
|
78 |
(ie.file_id, ie.kind)) |
|
4634.111.1
by John Arbash Meinel
First cut at a possible fix for bug #343218 |
79 |
# The data returned here can be in any order, but we've already created all
|
80 |
# the directories
|
|
4634.111.5
by John Arbash Meinel
Change to using os.open() allowing the users umask to handle perms. |
81 |
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | getattr(os, 'O_BINARY', 0) |
4634.111.1
by John Arbash Meinel
First cut at a possible fix for bug #343218 |
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) |
|
4634.111.5
by John Arbash Meinel
Change to using os.open() allowing the users umask to handle perms. |
88 |
# We set the mode and let the umask sort out the file info
|
89 |
mode = 0666 |
|
90 |
if executable: |
|
91 |
mode = 0777 |
|
4634.111.6
by John Arbash Meinel
Of course, it helps if you tell the buffered file that it is writable. |
92 |
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 |
93 |
try: |
94 |
out.writelines(chunks) |
|
95 |
finally: |
|
96 |
out.close() |