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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
import os
from bzrlib.errors import DuplicateKey, MalformedTransform, NoSuchFile
from bzrlib.osutils import file_kind
from bzrlib.inventory import InventoryEntry
import errno
def unique_add(map, key, value):
if key in map:
raise DuplicateKey(key=key)
map[key] = value
class TreeTransform(object):
"""Represent a tree transformation."""
def __init__(self, tree):
"""Note: a write lock is taken on the tree.
Use TreeTransform.finalize() to release the lock
"""
object.__init__(self)
self._tree = tree
self._tree.lock_write()
self._id_number = 0
self._new_name = {}
self._new_parent = {}
self._new_contents = {}
self._new_id = {}
self._tree_path_ids = {}
self._tree_id_paths = {}
self._new_root = self.get_id_tree(tree.get_root_id())
def finalize(self):
if self._tree is None:
return
self._tree.unlock()
self._tree = None
def _assign_id(self):
"""Produce a new tranform id"""
new_id = "new-%s" % self._id_number
self._id_number +=1
return new_id
def create_path(self, name, parent):
"""Assign a transaction id to a new path"""
trans_id = self._assign_id()
unique_add(self._new_name, trans_id, name)
unique_add(self._new_parent, trans_id, parent)
return trans_id
def adjust_path(self, name, parent, trans_id):
"""Change the path that is assigned to a transaction id."""
self._new_name[trans_id] = name
self._new_parent[trans_id] = parent
def get_id_tree(self, inventory_id):
"""Determine the transaction id of a working tree file.
This reflects only files that already exist, not ones that will be
added by transactions.
"""
return self.get_tree_path_id(self._tree.id2path(inventory_id))
def canonical_path(self, path):
"""Get the canonical tree-relative path"""
# don't follow final symlinks
dirname, basename = os.path.split(self._tree.abspath(path))
dirname = os.path.realpath(dirname)
return self._tree.relpath(os.path.join(dirname, basename))
def get_tree_path_id(self, path):
"""Determine (and maybe set) the transaction ID for a tree path."""
path = self.canonical_path(path)
if path not in self._tree_path_ids:
self._tree_path_ids[path] = self._assign_id()
self._tree_id_paths[self._tree_path_ids[path]] = path
return self._tree_path_ids[path]
def get_tree_parent(self, trans_id):
"""Determine id of the parent in the tree, or None for tree root."""
path = self._tree_id_paths[trans_id]
if path == "":
return None
return self.get_tree_path_id(os.path.dirname(path))
def create_file(self, contents, trans_id):
"""Schedule creation of a new file.
See also new_file.
Contents is an iterator of strings, all of which will be written
to the target destination.
"""
unique_add(self._new_contents, trans_id, ('file', contents))
def create_directory(self, trans_id):
"""Schedule creation of a new directory.
See also new_directory.
"""
unique_add(self._new_contents, trans_id, ('directory',))
def create_symlink(self, target, trans_id):
"""Schedule creation of a new symbolic link.
target is a bytestring.
See also new_symlink.
"""
unique_add(self._new_contents, trans_id, ('symlink', target))
def version_file(self, file_id, trans_id):
"""Schedule a file to become versioned."""
unique_add(self._new_id, trans_id, file_id)
def new_paths(self):
"""Determine the paths of all new and changed files"""
new_ids = set()
fp = FinalPaths(self._new_root, self._new_name, self)
for id_set in (self._new_name, self._new_parent, self._new_contents,
self._new_id):
new_ids.update(id_set)
new_paths = [(fp.get_path(t), t) for t in new_ids]
new_paths.sort()
return new_paths
def final_kind(self, trans_id):
"""\
Determine the final file kind, after any changes applied.
Raises NoSuchFile if the file does not exist/has no contents.
(It is conceivable that a path would be created without the
corresponding contents insertion command)
"""
if trans_id in self._new_contents:
return self._new_contents[trans_id][0]
else:
path = self._tree_id_paths.get(trans_id)
if path is None:
raise NoSuchFile(None)
try:
return file_kind(self._tree.abspath(path))
except OSError, e:
if e.errno != errno.ENOENT:
raise
else:
raise NoSuchFile(path)
def final_file_id(self, trans_id):
"""\
Determine the file id after any changes are applied, or None.
None indicates that the file will not be versioned after changes are
applied.
"""
try:
# there is a new id for this file
return self._new_id[trans_id]
except KeyError:
try:
path = self._tree_id_paths[trans_id]
except KeyError:
# the file is a new, unversioned file, or invalid trans_id
return None
# the file is old; the old id is still valid
return self._tree.path2id(path)
def final_parent(self, trans_id):
"""\
Determine the parent file_id, after any changes are applied.
None is returned for the tree root.
"""
try:
return self._new_parent[trans_id]
except KeyError:
return self.get_tree_parent(trans_id)
def find_conflicts(self):
"""Find any violations of inventory of filesystem invariants"""
by_parent = {}
conflicts = []
for trans_id, parent_id in self._new_parent.iteritems():
if parent_id not in by_parent:
by_parent[parent_id] = set()
by_parent[parent_id].add(trans_id)
conflicts.extend(self._unversioned_parents(by_parent))
conflicts.extend(self._parent_loops())
conflicts.extend(self._duplicate_entries(by_parent))
conflicts.extend(self._parent_type_conflicts(by_parent))
conflicts.extend(self._improper_versioning())
return conflicts
def _parent_loops(self):
"""No entry should be its own ancestor"""
conflicts = []
for trans_id in self._new_parent:
seen = set()
parent_id = trans_id
while parent_id is not None:
seen.add(parent_id)
parent_id = self.final_parent(parent_id)
if parent_id == trans_id:
conflicts.append(('parent loop', trans_id))
if parent_id in seen:
break
return conflicts
def _unversioned_parents(self, by_parent):
"""If parent directories are versioned, children must be versioned."""
conflicts = []
for parent_id, children in by_parent.iteritems():
if self.final_file_id(parent_id) is not None:
continue
for child_id in children:
if self.final_file_id(child_id) is not None:
conflicts.append(('unversioned parent', parent_id))
break;
return conflicts
def _improper_versioning(self):
"""\
Cannot version a file with no contents, or a bad type.
However, existing entries with no contents are okay.
"""
conflicts = []
for trans_id in self._new_id.iterkeys():
try:
kind = self.final_kind(trans_id)
except NoSuchFile:
conflicts.append(('versioning no contents', trans_id))
continue
if not InventoryEntry.versionable_kind(kind):
conflicts.append(('versioning bad kind', trans_id, kind))
return conflicts
def _duplicate_entries(self, by_parent):
"""No directory may have two entries with the same name."""
conflicts = []
for children in by_parent.itervalues():
name_ids = [(self._new_name[t], t) for t in children]
name_ids.sort()
last_name = None
last_trans_id = None
for name, trans_id in name_ids:
if name == last_name:
conflicts.append(('duplicate', last_trans_id, trans_id))
last_name = name
last_trans_id = trans_id
return conflicts
def _parent_type_conflicts(self, by_parent):
"""parents must have directory 'contents'."""
conflicts = []
for parent_id in by_parent.iterkeys():
try:
kind = self.final_kind(parent_id)
except NoSuchFile:
kind = None
if kind is None:
conflicts.append(('missing parent', parent_id))
elif kind != "directory":
conflicts.append(('non-directory parent', parent_id))
return conflicts
def apply(self):
"""\
Apply all changes to the inventory and filesystem.
If filesystem or inventory conflicts are present, MalformedTransform
will be thrown.
"""
if len(self.find_conflicts()) != 0:
raise MalformedTransform()
inv = self._tree.inventory
for path, trans_id in self.new_paths():
try:
kind = self._new_contents[trans_id][0]
except KeyError:
kind = contents = None
if kind == 'file':
contents = self._new_contents[trans_id][1]
f = file(self._tree.abspath(path), 'wb')
for segment in contents:
f.write(segment)
f.close()
elif kind == 'directory':
os.mkdir(self._tree.abspath(path))
elif kind == 'symlink':
target = self._new_contents[trans_id][1]
os.symlink(target, path)
if trans_id in self._new_id:
if kind is None:
kind = file_kind(self._tree.abspath(path))
inv.add_path(path, kind, self._new_id[trans_id])
self._tree._write_inventory(inv)
def new_entry(self, name, parent_id, file_id):
"""Helper function to create a new filesystem entry."""
trans_id = self.create_path(name, parent_id)
if file_id is not None:
self.version_file(file_id, trans_id)
return trans_id
def new_file(self, name, parent_id, contents, file_id=None):
"""\
Convenience method to create files.
name is the name of the file to create.
parent_id is the transaction id of the parent directory of the file.
contents is an iterator of bytestrings, which will be used to produce
the file.
file_id is the inventory ID of the file, if it is to be versioned.
"""
trans_id = self.new_entry(name, parent_id, file_id)
self.create_file(contents, trans_id)
return trans_id
def new_directory(self, name, parent_id, file_id=None):
"""\
Convenience method to create directories.
name is the name of the directory to create.
parent_id is the transaction id of the parent directory of the
directory.
file_id is the inventory ID of the directory, if it is to be versioned.
"""
trans_id = self.new_entry(name, parent_id, file_id)
self.create_directory(trans_id)
return trans_id
def new_symlink(self, name, parent_id, target, file_id=None):
"""\
Convenience method to create symbolic link.
name is the name of the symlink to create.
parent_id is the transaction id of the parent directory of the symlink.
target is a bytestring of the target of the symlink.
file_id is the inventory ID of the file, if it is to be versioned.
"""
trans_id = self.new_entry(name, parent_id, file_id)
self.create_symlink(target, trans_id)
return trans_id
class FinalPaths(object):
"""\
Make path calculation cheap by memoizing paths.
The underlying tree must not be manipulated between calls, or else
the results will likely be incorrect.
"""
def __init__(self, root, names, tree):
object.__init__(self)
self.root = root
self._new_name = names
self._known_paths = {}
self.tree = tree
def _determine_path(self, trans_id):
if trans_id == self.root:
return ""
name = self._new_name[trans_id]
parent_id = self.tree.final_parent(trans_id)
if parent_id == self.root:
return name
else:
return os.path.join(self.get_path(parent_id), name)
def get_path(self, trans_id):
if trans_id not in self._known_paths:
self._known_paths[trans_id] = self._determine_path(trans_id)
return self._known_paths[trans_id]
|