xattr - Python extended filesystem attributes
xattr is a Python wrapper for the extended attributes APIs available in Darwin 8.0+ (Mac OS X 10.4) and Linux 2.6+. 0.2 added Linux compatibility, and API compatibility with the GPL pyxattr module. Unlike pyxattr, xattr supports both Darwin and Linux, has a dict-like API, and it is MIT licensed.
Usage looks like this:
>>> import xattr >>> x = xattr.xattr('tiger_8a428_userdvd.dmg') >>> x <xattr file='tiger_8a428_userdvd.dmg'> >>> x.keys() [u'com.apple.diskimages.recentcksum'] >>> dict(x) {u'com.apple.diskimages.recentcksum': 'i:969254 ...more stuff'} >>> x['com.apple.diskimages.recentcksum'] 'i:969254 ...more stuff' >>> x['com.apple.adc'] = 'select' >>> dict(x) {u'com.apple.adc': 'select', u'com.apple.diskimages.recentcksum': 'i:969254 ...more stuff'} >>> del x['com.apple.adc'] >>> dict(x) {u'com.apple.diskimages.recentcksum': 'i:969254 ...more stuff'}
You can download xattr 0.2 from http://pythonmac.org/packages/ for Tiger's Python 2.3.5 or Python 2.4.1 (installed on Tiger).
- svn trunk:
- http://svn.red-bean.com/bob/xattr/trunk/
- 0.2 source:
- http://svn.red-bean.com/bob/xattr/releases/xattr-0.2/
Nice!
I am not all that found of resource forks, so I am happy to see xattr. And real happy to see this module.
On OSX, I did notice the enforcement of the 4K value limits with:
IOError: [Errno 7] Argument list too long
But when I make the key longer than 128 bytes the error is not as descriptive:
IOError: [Errno 1] Operation not permitted
I wish this was more descriptive, but probably Apples fault?
Regards
Comment by Brian Ray — 2005-10-29 @ 4:09 pm
I made a small script that helps me with making cd’s and dvd’s. I always try to put md5sums on the dvd’s I make. Using the xattr keeps me from having to do the same thing more that once. You may place this as an example, since there are none in the package. It’s public domain. I call it generate-md5sums
Here it is.
—————————————-
#!/usr/bin/python import os,sys import os.path from md5 import md5 from xattr import xattr from optparse import OptionParser # you may want to set these variables BLOCK_SIZE = 1024 attribute_name = 'user.md5sum' MD5SUMS_FILENAME = 'md5sums.txt' # there is no use for the option parser now parser = OptionParser() opts, args = parser.parse_args(sys.argv[1:]) def md5sum(afile): “”"returns the standard md5sum hexdigest for a file object”"” m = md5() block = afile.read(BLOCK_SIZE) while block: m.update(block) block = afile.read(BLOCK_SIZE) return m.hexdigest() def append_md5sum(mainfile, filename, md5digest): mainfile.write(’%s %s\n’ % (md5digest, filename)) if len(args) == 0: top = ‘.’ else: print “can’t handle arguments right now” sys.exit(1) main_md5sums_filename = os.path.join(top, MD5SUMS_FILENAME) main_md5sums = file(main_md5sums_filename, ‘w’) for root, dirs, files in os.walk(top, topdown=False): for name in files: filename = os.path.join(root, name) if filename != main_md5sums_filename: xf = xattr(filename) if xf.has_key(attribute_name): md = xf.get(attribute_name) else: md = md5sum(file(filename)) xf.set(attribute_name, md) append_md5sum(main_md5sums, filename, md) main_md5sums.close()————————————
btw, thanks for making my life a little easier now. :)
Comment by Joseph Rawson — 2006-12-04 @ 1:05 pm
i found this page while looking at the requirements for apple’s calendar server. from http://trac.macosforge.org/projects/calendarserver/wiki/Portability: “File system extended attributes are available on all file systems in Mac OS X, and on some file systems on Linux and FreeBSD.”
I was hoping to find a list of filesystems under linux which a) have extended attributes and b) are supported by xattr. I could not find such info on the xattr home page. Could this information be added?
Comment by Don Park — 2007-03-28 @ 8:55 am
I don’t know nor care which Linux filesystems support extended attributes… I would imagine ext3 has them. I don’t use Linux with any regularity.
Comment by bob — 2007-03-28 @ 10:55 am