1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# Copyright (C) 2005 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Test helper for constructing and testing directories.
This module transforms filesystem directories to and from Python lists.
As a Python list the descriptions can be stored in test cases, compared,
etc.
"""
# TODO: Script to write a description of a directory for testing
# TODO: Helper that compares two structures and raises a helpful error
# where they differ. Option to ignore some files or directories in the
# comparison.
import os
import stat
from bzrlib.trace import warning
from bzrlib.osutils import pathjoin
def build_tree_contents(template):
"""Reconstitute some files from a text description.
Each element of template is a tuple. The first element is a filename,
with an optional ending character indicating the type.
The template is built relative to the Python process's current
working directory.
('foo/',) will build a directory.
('foo', 'bar') will write 'bar' to 'foo'
('foo@', 'linktarget') will raise an error
"""
for tt in template:
name = tt[0]
if name[-1] == '/':
os.mkdir(name)
elif name[-1] == '@':
raise NotImplementedError('symlinks not handled yet')
else:
f = file(name, 'wb')
try:
f.write(tt[1])
finally:
f.close()
def capture_tree_contents(top):
"""Make a Python datastructure description of a tree.
If top is an absolute path the descriptions will be absolute."""
for dirpath, dirnames, filenames in os.walk(top):
yield (dirpath + '/', )
filenames.sort()
for fn in filenames:
fullpath = pathjoin(dirpath, fn)
if (fullpath[-1] in '@/'):
raise AssertionError(fullpath)
info = os.lstat(fullpath)
if stat.S_ISLNK(info.st_mode):
yield (fullpath + '@', os.readlink(fullpath))
elif stat.S_ISREG(info.st_mode):
yield (fullpath, file(fullpath, 'rb').read())
else:
warning("can't capture file %s with mode %#o",
fullpath, info.st_mode)
pass
|