97
98
'cached_kernel_like_tree')
98
99
os.mkdir(cache_dir)
99
100
self._make_kernel_files(root=cache_dir)
101
self._protect_files(cache_dir)
100
102
Benchmark._cached_kernel_like_tree = cache_dir
102
104
# Hardlinking the target directory is *much* faster (7s => <1s).
103
105
osutils.copy_tree(Benchmark._cached_kernel_like_tree, root,
104
106
handlers={'file':os.link})
108
def _clone_tree(self, source, dest, link_bzr=False, link_working=True):
109
"""Copy the contents from a given location to another location.
110
Optionally hardlink certain pieces of the tree.
112
:param source: The directory to copy
113
:param dest: The destination
114
:param link_bzr: Should the .bzr/ files be hardlinked?
115
:param link_working: Should the working tree be hardlinked?
117
# We use shutil.copyfile so that we don't copy permissions
118
# because most of our source trees are marked readonly to
119
# prevent modifying in the case of hardlinks
120
handlers = {'file':shutil.copyfile}
121
if osutils.hardlinks_good():
124
handlers = {'file':os.link}
126
# Don't hardlink files inside bzr
127
def file_handler(source, dest):
128
if '.bzr/' in source:
129
shutil.copyfile(source, dest)
131
os.link(source, dest)
132
handlers = {'file':file_handler}
134
# Only link files inside .bzr/
135
def file_handler(source, dest):
136
if '.bzr/' in source:
137
os.link(source, dest)
139
shutil.copyfile(source, dest)
140
handlers = {'file':file_handler}
141
osutils.copy_tree(source, dest, handlers=handlers)
143
def _protect_files(self, root):
144
"""Chmod all files underneath 'root' to prevent writing
146
:param root: The base directory to modify
148
for dirinfo, entries in osutils.walkdirs(root):
149
for relpath, name, kind, st, abspath in entries:
151
os.chmod(abspath, 0440)
106
153
def make_kernel_like_added_tree(self, root='.',
107
154
hardlink_working=True):
108
155
"""Make a kernel like tree, with all files added
112
159
files, just hardlink them to the cached files. Tests can unlink
113
160
files that they will change.
162
# There isn't much underneath .bzr, so we don't support hardlinking
163
# it. Testing showed there wasn't much gain, and there is potentially
164
# a problem if someone modifies something underneath us.
115
165
if Benchmark._cached_kernel_like_added_tree is None:
116
166
cache_dir = osutils.pathjoin(self.TEST_ROOT,
117
167
'cached_kernel_like_added_tree')
120
170
hardlink_working=True)
121
171
# Add everything to it
122
172
add.smart_add_tree(tree, [cache_dir], recurse=True, save=True)
174
self._protect_files(cache_dir+'/.bzr')
123
175
Benchmark._cached_kernel_like_added_tree = cache_dir
177
self._clone_tree(Benchmark._cached_kernel_like_added_tree, root,
178
link_working=hardlink_working)
179
return workingtree.WorkingTree.open(root)
181
def make_kernel_like_committed_tree(self, root='.',
182
hardlink_working=True,
184
"""Make a kernel like tree, with all files added and committed
186
:param root: Where to create the files
187
:param hardlink_working: Instead of copying all of the working tree
188
files, just hardlink them to the cached files. Tests can unlink
189
files that they will change.
190
:param hardlink_bzr: Hardlink the .bzr directory. For readonly
191
operations this is safe, and shaves off a lot of setup time
193
if Benchmark._cached_kernel_like_committed_tree is None:
194
cache_dir = osutils.pathjoin(self.TEST_ROOT,
195
'cached_kernel_like_committed_tree')
196
# Get a basic tree with working files
197
tree = self.make_kernel_like_added_tree(root=cache_dir,
198
hardlink_working=True)
199
tree.commit('first post', rev_id='r1')
201
self._protect_files(cache_dir+'/.bzr')
202
Benchmark._cached_kernel_like_committed_tree = cache_dir
125
204
# Now we have a cached tree, just copy it
126
handlers = {} # Copy all files
127
if osutils.hardlinks_good() and hardlink_working:
128
# Hardlink only working files (not files underneath .bzr)
129
def file_handler(source, dest):
130
if '.bzr/' in source:
131
shutil.copy2(source, dest)
133
os.link(source, dest)
134
handlers = {'file':file_handler}
136
osutils.copy_tree(Benchmark._cached_kernel_like_added_tree, root,
205
self._clone_tree(Benchmark._cached_kernel_like_committed_tree, root,
206
link_bzr=hardlink_bzr,
207
link_working=hardlink_working)
138
208
return workingtree.WorkingTree.open(root)
140
210
def make_many_commit_tree(self, directory_name='.'):