A subclass of the list object for managing files system objects (files, directories, etc.)
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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | # FileSystemList -- manage a list of files, directories and other
# file system objects. Optionally recurse directories
# and ensure that each item in the list is a regular file.
#
# Version 0.1
# Jan. 1, 2004: initial working version
#
# by: Eamonn Sullivan (eamonn_sullivan@blueyonder.co.uk)
#
import sys, os, os.path, glob
from stat import *
from types import StringType, ListType
# we lose all the tar*() methods on python2.2
try:
import tarfile
COMPAT23 = True
except ImportError:
# we're running on something less than python2.3
COMPAT23 = False
class FSList(list):
"""
File System List class: a subclass of list designed for
manipulating objects in the file system.
flist = FSList([pattern = '', [recursive = False, [file_only = False]]])
- pattern can be a string or a list of files and subdirectories. It can
also contain wildcards (*, ? or []), in which case the pattern
will be expanded automatically using glob.glob()
- recursive controls whether files matching pattern in subdirectories
are added to the list.
- file_only is a convenience option for when you just want a list
of the regular files matching the patter (i.e., no directories,
special files like devices).
- It doesn't follow links or symlinks. At least I don't think it does.
- Computed properties: size (total size of all regular files, in bytes)
smallest (smallest item)
largest
newest (most recent modification time)
oldest (oldest modification time)
The same init options are also available on extend and append methods.
flist.extend('./*.c', recursive=True, file_only=True)
Extends the list with all C files in the current directory and
any subdirectories.
Note, append works like it would on a normal list. For example:
flist.append('./*.c', recursive=True)
would add a list to the list (i.e., flist = [..., ..., [..., ...]]
Attempts to add items that aren't in the filesystem raise ValueError:
>>> test = fslist.FSList()
>>> test[0] = 'This isn't a file.'
Traceback ...
ValueError: Item not found on file system: This isn't a file.
>>> test.append('fslist.py')
>>> test.extend('wx*.py')
>>> print test
['fslist.py', 'wxEdit.py', 'wxMailitEd.py', 'wxSendIt.py', 'wxtestcode.py']
>>>
FSList doesn't keep track of underlying filesystem changes, but you can
test if all the files are still there by testing the instance in a
boolean context.
>>> import fslist
>>> test = fslist.FSList('*.py')
>>> print test
['email-unpack.py', 'FileSystemList.py', 'fslist.py', ...]
>>> if test:
print 'Everything's Still there!'
else:
print 'Something has changed!'
Also included are methods for compressing all of the files on the list
in tar, tar.gz, tar.bz2 (bzip2) and zip formats. The methods return the
data as a stream, for use in file operations. For example:
>>> import fslist
>>> flist = fslist.FSList('*.py')
>>> f = file('testing.tar', 'w+b')
>>> f.write(flist.tar())
>>> f.close
>>> f = file('testing.tar.gz', 'w+b')
>>> f.write(flist.targz())
>>> f.close
>>> f = file('testing.tar.bz2', 'w+b')
>>> f.write(flist.tarbz2())
>>> f.close
>>> f = file('testing.zip', 'w+b')
>>> f.write(flist.zip())
>>> f.close
On Python 2.2, only the zip() is defined.
"""
def __init__(self, pattern = '', recursive = False, file_only = False):
"""
I do ugly type testing in here because I haven't quite
grasped the concepts, I guess. I assume any user of this won't
want to end up with a list containing a list when an instance is
created.
They just want a list, at least at first, so I type test to append()
or extend().
"""
list.__init__(self)
# if pattern is empty, we don't have to do any more
if pattern != '':
# assume at first that it's a single string.
try:
temp = self._populate(pattern, recursive, file_only)
# if wildcards, need to extend the list
self._extendOrAppend(temp)
except TypeError:
# we may have been passed a list. Process
# each separately, any of which can have wildcards.
for fname in pattern:
if isinstance(fname, StringType):
temp = self._populate(fname, recursive, file_only)
self._extendOrAppend(temp)
else:
# Even lists can have lists!
# I'll break, though, if there are lists in lists in
# lists.
for f in fname:
temp = self._populate(f, recursive, file_only)
self._extendOrAppend(temp)
def getSize(self):
"""
Return the total size of the regular files in the list. Used for
the read-only property 'size'.
"""
totsize = 0
try:
for name in self:
if os.path.isfile(name):
totsize += os.path.getsize(name)
except TypeError:
# there may be lists buried in this list, unwind first
temp = self._unwind()
for name in temp:
if os.path.isfile(name):
totsize += os.path.getsize(name)
return totsize
def getSmallest(self):
"""
Returns the smallest file. Yes, this is ugly.
"""
flist_copy = []
try:
flist_copy = zip(map(os.path.getsize, self), self)
except TypeError:
# there may be lists of lists
temp = self._unwind()
flist_copy = zip(map(os.path.getsize, temp), temp)
flist_copy.sort()
try:
size, fname = flist_copy[0]
except IndexError:
# probably an empty list
return None
return fname
def getLargest(self):
"""
Returns the largest file. Yes, this is ugly.
"""
flist_copy = []
try:
flist_copy = zip(map(os.path.getsize, self), self)
except TypeError:
# there may be lists of lists
temp = self._unwind()
flist_copy = zip(map(os.path.getsize, temp), temp)
flist_copy.sort()
try:
size, fname = flist_copy[(len(flist_copy)-1)]
except IndexError:
# probably an empty list
return None
return fname
def getOldest(self):
"""
Returns the file earliest last-modified time.
"""
flist_copy = []
try:
flist_copy = zip(map(os.path.getmtime, self), self)
except TypeError:
# there may be lists of lists
temp = self._unwind()
flist_copy = zip(map(os.path.getmtime, temp), temp)
flist_copy.sort()
try:
time, fname = flist_copy[0]
except IndexError:
# probably an empty list
return None
return fname
def getNewest(self):
"""
Returns the file modified most recently.
"""
flist_copy = []
try:
flist_copy = zip(map(os.path.getmtime, self), self)
except TypeError:
# there may be lists of lists
temp = self._unwind()
flist_copy = zip(map(os.path.getmtime, temp), temp)
flist_copy.sort()
try:
time, fname = flist_copy[(len(flist_copy)-1)]
except IndexError:
# probably an empty list
return None
return fname
# some read-only properties
size = property(getSize, \
doc='Total size of all the regular files in the list.')
smallest = property(getSmallest, \
doc='The smallest item in the list, by bytes.')
largest = property(getLargest, \
doc='The largest item in the list, by bytes.')
oldest = property(getOldest, \
doc='The item with the oldest last-modification time.')
newest = property(getNewest, \
doc='The item with the most recent last-modification time.')
def sort_by_size(self):
"""
Sort the list in by number of bytes
"""
flist_copy = []
temp = []
try:
flist_copy = zip(map(os.path.getsize, self), self)
except TypeError:
# there may be lists of lists
temp = self._unwind()
flist_copy = zip(map(os.path.getsize, temp), temp)
flist_copy.sort()
self[:] = [item for size, item in flist_copy]
def sort_by_mtime(self):
"""
Sort the last modification time
"""
flist_copy = []
temp = []
try:
flist_copy = zip(map(os.path.getmtime, self), self)
except TypeError:
# there may be lists of lists
temp = self._unwind()
flist_copy = zip(map(os.path.getmtime, temp), temp)
flist_copy.sort()
self[:] = [item for time, item in flist_copy]
def append(self, pattern, recursive = False, file_only = False):
"""
subclass the append method to handle wildcards, recursing
and file filter.
"""
# expand any wildcards, recurse if asked, filter on files, etc.
temp = self._populate(pattern, recursive, file_only)
# then just append.
super(FSList, self).append(temp)
def extend(self, pattern, recursive = False, file_only = False):
"""
subclass the extend method to handle wildcards, recursing
and file filter.
"""
# expand any wildcards, recurse if asked, filter on files, etc.
temp = self._populate(pattern, recursive, file_only)
# then extend.
super(FSList, self).extend(temp)
# methods used only internally
def _unwind(self):
"""
Unwinds lists that have embedded lists inside them.
The os.path functions used in newest(), largest(), etc.
choke on lists of files. I am sure there is an easier way.
"""
result = str(self).replace('[','')
result = result.replace(']','')
unwoundlist = result.split(',')
x = 0
while x < len(unwoundlist):
unwoundlist[x] = unwoundlist[x].strip(" \'\"")
x += 1
return unwoundlist
def _extendOrAppend(self, data):
"""
A kludge to ensure that the list is created correctly in __init__
This extends or appends the list, depending on whether we are
adding a list or a string.
"""
if isinstance(data, StringType):
super(FSList, self).append(data)
elif isinstance(data, ListType):
super(FSList, self).extend(data)
def _populate(self, pattern, recursive = False, file_only = False):
"""
The main workhorse function for adding files to the list.
Returns the pattern, if it's just a filesystem item and
it exists in the filesystem. Returns a list if pattern contains
wildcards, or None, if the wildcards do not match anything.
"""
temp = []
wildcard = False
# look for wildcard characters in the pattern
if ('*' in pattern) or ('?' in pattern)\
or ('[' in pattern):
wildcard = True
# items added to the list must exist on the filesystem.
if not wildcard and not os.path.exists(pattern):
raise ValueError, ("Item not found on file system: %s"% pattern)
# if it's a directory, assume they want every file there.
if os.path.isdir(pattern):
pattern = os.path.join(pattern, '*')
wildcard = True
# expand any wildcards
if wildcard:
temp.extend(glob.glob(pattern))
# walk through any directories, if requested,
# using the same pattern, if any.
if recursive:
path, filter = os.path.split(pattern)
# Go through the path, finding all the directories
if wildcard:
# try os.walk first, which is in python2.3
try:
for root, dirs, files in os.walk(path):
for dir in dirs:
temp.extend(glob.glob(\
os.path.join(root, dir, filter)))
except AttributeError:
# probably running on something less than python2.3
dirlist = self._walktree_compat(path)
for dir in dirlist:
temp.extend(glob.glob(os.path.join(dir, filter)))
# Finally, kill any links that got through from glob
# and remove anything that's not a file, if requested.
temp2 = []
name = ''
while True:
try:
# use pop() on the theory that it saves memory
# (one list is reduced while the other grows).
# That's probably not true, but I wouldn't know.
name = temp.pop()
try:
mode = os.stat(name)[ST_MODE]
except OSError, err:
import errno
if err.errno != errno.ENOENT: raise
# file not found. Most likely a broken symlink
continue
if file_only and S_ISREG(mode) and not S_ISLNK(mode):
temp2.append(name)
elif not S_ISLNK(mode):
temp2.append(name)
except IndexError:
break
temp = temp2
# Return the list found
if len(temp) > 0:
return temp
elif len(temp) == 0 and wildcard:
# the wildcard search didn't find anything
return None
else:
return pattern
def _walktree_compat(self, top):
'''
recursively descend the directory tree rooted at top.
Return all of the directories as a list. This should work
on python2.2
'''
dirlist = []
templist = []
try:
templist = os.listdir(top)
except OSError, err:
import errno
if err.errno == errno.ENOENT:
return []
for f in templist:
pathname = os.path.join(top, f)
try:
mode = os.stat(pathname)[ST_MODE]
except OSError, err:
import errno
if err.errno == errno.ENOENT:
continue
if S_ISDIR(mode):
# It's a directory, recurse into it
dirlist.append(pathname)
dirlist.extend(self._walktree_compat(pathname))
return dirlist
def __setitem__(self, key, value):
"""
Override this method to ensure that only valid filesystem
objects are added. Raises ValueError if not.
"""
if not os.path.exists(value):
raise ValueError, ("Item not found on file system: %s"% value)
else:
super(FSList, self).__setitem__(key, value)
def __nonzero__(self):
"""
Return True if all the items still exist on the filesystem,
false otherwise. Also returns false if there are no items
yet (i.e., it's an empty list).
"""
if len(self) > 0:
for name in self:
if not os.path.exists(name):
return False
return True
else:
return False
def zip(self):
"""
return a data stream of the files compressed in zip format.
Returns None if the list is empty.
"""
import cStringIO, zipfile
fobj = cStringIO.StringIO()
# make sure the list contains something
if len(self) >= 0:
try:
zip = zipfile.ZipFile(fobj, "w", zipfile.ZIP_DEFLATED)
except:
sys.stderr.write("Unable to open zip file.")
raise
try:
for name in self:
# only add files
if os.path.isfile(name):
zip.write(name)
zip.close()
return fobj.getvalue()
finally:
fobj.close()
else:
# return None if the list is empty.
return None
if COMPAT23:
def targz(self):
"""
return a data stream of the files in gzipped tar format.
Returns None if the list is empty.
"""
import tarfile, cStringIO
# Create file in memory. I use cStringIO for speed.
fobj = cStringIO.StringIO()
# if the list contains anything
if len(self) >= 0:
# create the tarfile using the cStringIO buffer
try:
tar = tarfile.open('', "w:gz", fobj)
except:
sys.stderr.write("Unable to open tar.gz file.")
raise
try:
for name in self:
# only add files
if os.path.isfile(name):
try:
tar.add(name)
except ValueError:
# it can't store extremely long names
# so tell which ones and continue
sys.stderr.write('Warning: Unable to store: ')
sys.stderr.write(name)
sys.stderr.write('\n')
tar.close()
return fobj.getvalue()
finally:
fobj.close()
else:
# return None if the list is empty
return None
def tarbz2(self):
"""
return a data stream of the files in tar format, compressed
with bzip2.
Returns None if the list is empty.
"""
import tarfile, cStringIO, bz2
fobj = cStringIO.StringIO()
# make sure the list contains something
if len(self) >= 0:
try:
tar = tarfile.open('', "w", fobj)
except:
sys.stderr.write("Can't open file for bzip2 compression.")
raise
try:
for name in self:
# only add files
if os.path.isfile(name):
try:
tar.add(name)
except ValueError:
# it can't store extremely long names
# so tell which ones and continue
# TODO: MAYBE TRY TRUNCATING NAMES HERE
sys.stderr.write('Warning: Unable to store: ')
sys.stderr.write(name)
sys.stderr.write('\n')
tar.close()
# I compress using bzip2 only at this point
# because I get an error if I try to do it with
# tarfile.open. It says file-like objects aren't
# supported.
return bz2.compress(fobj.getvalue())
finally:
fobj.close()
else:
# return None if the list is empty.
return None
def tar(self):
"""
return a data stream of the files in uncompressed tar format.
Returns None if the list is empty.
"""
import cStringIO, tarfile
fobj = cStringIO.StringIO()
# check the list contains something
if len(self) >= 0:
try:
tar = tarfile.open('', "w", fobj)
except:
sys.stderr.write("Unable to open tar file.")
raise
try:
for name in self:
# only add files
if os.path.isfile(name):
try:
tar.add(name)
except ValueError:
# it can't store extremely long names
# so tell which ones and continue
sys.stderr.write('Warning: Unable to store: ')
sys.stderr.write(name)
sys.stderr.write('\n')
tar.close()
return fobj.getvalue()
finally:
fobj.close()
else:
# return None if the list is empty
return None
if __name__ == '__main__':
import time
# change these to suit your system
# single wildcard pattern
pattern1 = ['../../*.doc', False, False, 'singlewild']
# a list of wildcard patterns
pattern2 = [['../*.py', '*'], True, False, 'wildlist']
# a list with an embedded list (tests _unwind)
pattern3 = [['../*.jpg', '../*.gif', ['../*.py', '../*.txt']], \
True, True, 'listlist']
# a straight filename
pattern4 = ['fslist.py', False, True, 'singlefile']
# a list of patterns, some of which probably don't exist
pattern5 = [['../*.py','*.xxx','*.not'], True, True, 'brokenlist']
for arg in (pattern1, pattern2, pattern3, pattern4, pattern5):
print 'Testing pattern:', arg[3]
print 'Pattern:', arg[0]
test = FSList(arg[0], recursive=arg[1], file_only=arg[2])
if len(test) > 0:
print "Number of files:", len(test)
print "Total size:", test.size
print "The smallest file is %s: %i bytes."%\
(test.smallest, os.path.getsize(test.smallest))
print "The largest file is %s: %i bytes."%\
(test.largest, os.path.getsize(test.largest))
print "The oldest file is %s: %s."%\
(test.oldest, time.ctime(os.path.getmtime(test.oldest)))
print "The newest file is %s: %s."%\
(test.newest, time.ctime(os.path.getmtime(test.newest)))
if len(test) > 3:
test.sort_by_size()
print "Three smallest files:"
print "Name: Size:"
print "%s %i"%(test[0], os.path.getsize(test[0]))
print "%s %i"%(test[1], os.path.getsize(test[1]))
print "%s %i"%(test[2], os.path.getsize(test[2]))
test.sort_by_mtime()
print "Three oldest files:"
print "Name: Size:"
print "%s %s"%(test[0], \
time.ctime(os.path.getmtime(test[0])))
print "%s %s"%(test[1], \
time.ctime(os.path.getmtime(test[1])))
print "%s %s"%(test[2], \
time.ctime(os.path.getmtime(test[2])))
try:
if not COMPAT23:
f = file(('testresults/'+arg[3]+'P22'+'.zip'), "w+b")
else:
f = file(('testresults/'+arg[3]+'P23'+'.zip'), "w+b")
f.write(test.zip())
finally:
f.close()
if COMPAT23:
try:
f = file(('testresults/'+arg[3]+'.tar'), "w+b")
f.write(test.tar())
finally:
f.close()
try:
f = file(('testresults/'+arg[3]+'.tgz'), "w+b")
f.write(test.targz())
finally:
f.close()
try:
f = file(('testresults/'+arg[3]+'.tbz'), "w+b")
f.write(test.tarbz2())
finally:
f.close()
|
This was an experiment to see how fast an old geek who hasn't coded anything in years can pick up Python, using nothing but the docs online and the "Python in a Nutshell" book. For my "simple" experiment, I subclassed the built-in list object to handle files and directories. It quickly grew, but I learned about special methods, class properties, tarfile and zipfile and loads else. Usage is mostly explained on top. I've tested it on Windows NT, Windows XP, and Linux. It's not designed for speed, but works well enough up to a few thousand files. I've tried it on lists of more than 100,000 (/usr/*, c:*). It didn't break, but performance was poor.
Obvious enhancements would be a __contains__ that lets you use wildcards and a subclass that does the same thing with files in archives (tar, zip, etc.).