20
21
from sets import Set
21
22
import os.path, os, fnmatch
23
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
24
joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
26
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
28
24
from inventory import Inventory
29
25
from trace import mutter, note
26
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
27
joinpath, splitpath, appendpath, isdir, isfile, file_kind
30
28
from errors import bailout
30
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
75
74
doc="Inventory of this Tree")
77
76
def _check_retrieved(self, ie, f):
78
fp = fingerprint_file(f)
81
if ie.text_size != None:
82
if ie.text_size != fp['size']:
77
# TODO: Test this check by damaging the store?
78
if ie.text_size is not None:
80
if fs != ie.text_size:
83
81
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
84
82
["inventory expects %d bytes" % ie.text_size,
85
"file is actually %d bytes" % fp['size'],
83
"file is actually %d bytes" % fs,
86
84
"store is probably damaged/corrupt"])
88
if ie.text_sha1 != fp['sha1']:
88
if ie.text_sha1 != f_hash:
89
89
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
90
90
["inventory expects %s" % ie.text_sha1,
91
"file is actually %s" % fp['sha1'],
91
"file is actually %s" % f_hash,
92
92
"store is probably damaged/corrupt"])
95
def print_file(self, fileid):
96
"""Print file with id `fileid` to stdout."""
98
pumpfile(self.get_file(fileid), sys.stdout)
101
def export(self, dest):
95
def export(self, dest):
102
96
"""Export this tree to a new directory.
104
98
`dest` should not exist, and will be created holding the
105
99
contents of this tree.
107
TODO: To handle subdirectories we need to create the
101
:todo: To handle subdirectories we need to create the
108
102
directories first.
110
104
:note: If the export fails, the destination directory will be
144
138
return "<%s of %s>" % (self.__class__.__name__,
147
def abspath(self, filename):
141
def _rel(self, filename):
148
142
return os.path.join(self.basedir, filename)
150
144
def has_filename(self, filename):
151
return os.path.exists(self.abspath(filename))
145
return os.path.exists(self._rel(filename))
153
147
def get_file(self, file_id):
154
148
return self.get_file_byname(self.id2path(file_id))
156
150
def get_file_byname(self, filename):
157
return file(self.abspath(filename), 'rb')
151
return file(self._rel(filename), 'rb')
159
153
def _get_store_filename(self, file_id):
160
## XXX: badly named; this isn't in the store at all
161
return self.abspath(self.id2path(file_id))
163
def has_id(self, file_id):
164
# files that have been deleted are excluded
165
if not self.inventory.has_id(file_id):
167
return os.access(self.abspath(self.inventory.id2path(file_id)), os.F_OK)
154
return self._rel(self.id2path(file_id))
169
156
def get_file_size(self, file_id):
170
157
return os.stat(self._get_store_filename(file_id))[ST_SIZE]
196
192
inv = self.inventory
198
def descend(from_dir_relpath, from_dir_id, dp):
194
def descend(from_dir, from_dir_id, dp):
199
195
ls = os.listdir(dp)
202
## TODO: If we find a subdirectory with its own .bzr
203
## directory, then that is a separate tree and we
204
## should exclude it.
205
198
if bzrlib.BZRDIR == f:
208
201
# path within tree
209
fp = appendpath(from_dir_relpath, f)
202
fp = appendpath(from_dir, f)
212
205
fap = appendpath(dp, f)
238
231
for ff in descend(fp, f_ie.file_id, fap):
241
for f in descend('', inv.root.file_id, self.basedir):
234
for f in descend('', None, self.basedir):
247
for subp in self.extras():
248
if not self.is_ignored(subp):
253
"""Yield all unknown files in this WorkingTree.
239
def unknowns(self, path='', dir_id=None):
240
"""Yield names of unknown files in this WorkingTree.
255
242
If there are any unknown directories then only the directory is
256
243
returned, not all its children. But if there are unknown files
259
246
Currently returned depth-first, sorted by name within directories.
261
## TODO: Work from given directory downwards
263
for path, dir_entry in self.inventory.directories():
264
mutter("search for unknowns in %r" % path)
265
dirabs = self.abspath(path)
266
if not isdir(dirabs):
267
# e.g. directory deleted
271
for subf in os.listdir(dirabs):
273
and (subf not in dir_entry.children)):
278
subp = appendpath(path, subf)
248
for fpath, fclass, fkind, fid in self.list_files():
282
253
def ignored_files(self):
283
"""Yield list of PATH, IGNORE_PATTERN"""
284
for subp in self.extras():
285
pat = self.is_ignored(subp)
254
for fpath, fclass, fkind, fid in self.list_files():
290
259
def get_ignore_list(self):
291
"""Return list of ignore patterns.
293
Cached in the Tree object after the first call.
295
if hasattr(self, '_ignorelist'):
296
return self._ignorelist
298
l = bzrlib.DEFAULT_IGNORE[:]
260
"""Return list of ignore patterns."""
299
261
if self.has_filename(bzrlib.IGNORE_FILENAME):
300
262
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
301
l.extend([line.rstrip("\n\r") for line in f.readlines()])
263
return [line.rstrip("\n\r") for line in f.readlines()]
265
return bzrlib.DEFAULT_IGNORE
306
268
def is_ignored(self, filename):
307
r"""Check whether the filename matches an ignore pattern.
309
Patterns containing '/' or '\' need to match the whole path;
310
others match against only the last component.
312
If the file is ignored, returns the pattern which caused it to
313
be ignored, otherwise None. So this can simply be used as a
314
boolean if desired."""
316
# TODO: Use '**' to match directories, and other extended
317
# globbing stuff from cvs/rsync.
319
# XXX: fnmatch is actually not quite what we want: it's only
320
# approximately the same as real Unix fnmatch, and doesn't
321
# treat dotfiles correctly and allows * to match /.
322
# Eventually it should be replaced with something more
269
"""Check whether the filename matches an ignore pattern.
271
Patterns containing '/' need to match the whole path; others
272
match against only the last component."""
273
## TODO: Take them from a file, not hardcoded
274
## TODO: Use extended zsh-style globs maybe?
275
## TODO: Use '**' to match directories?
325
276
for pat in self.get_ignore_list():
326
if '/' in pat or '\\' in pat:
328
# as a special case, you can put ./ at the start of a
329
# pattern; this is good to match in the top-level
332
if (pat[:2] == './') or (pat[:2] == '.\\'):
336
if fnmatch.fnmatchcase(filename, newpat):
278
if fnmatch.fnmatchcase(filename, pat):
339
281
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):