Picking up resource forks and extended attributes on Mac OS X ?!

M

Mitrokhin

Hello

(First off this is my first python (semi-) script so bear with me :).

What is the most efficient way to walk a directory structure and
list files with resource forks ?. Reusing bits of code from here and
there the script below is what I've come up with, but can't it be
done in a better and quicker way ?

import os, os.path

for dir, subdir, files in os.walk(os.getcwd()):
for file in files:
temp = os.path.join(dir,file,'..namedfork', 'rsrc')
if os.path.isfile(temp) and os.path.getsize(temp) > 0:
print os.path.join(dir,file)


By comparison the perl script below (which needless to say isn't of my
creation) seems lightningly fast.


Also (dare I ask for this too) If some nice soul could point me in the
right direction for picking up files with extended attributes under OS
X I'd be really gratefull to. Should it be done the same way, ie. via
os. calls or perhaps by way of a popen or ... ?

Suggestions anyone


============
#!/usr/bin/perl -w

# Perl script to find files with Mac resource forks. Files are found
# recursively from the specified directory.

use strict;
use File::Find ();

if ($#ARGV != 0) {
print "Usage: $0 <directory>\n";
exit(-1);
}

my $rootDir = $ARGV[0];
use vars qw/*name/;
*name = *File::Find::name;

#File::Find::find({wanted => \&checkForResourceFork, no_chdir =>0,
File::Find::find({wanted => \&checkForResourceFork, no_chdir =>1,
follow=>1}, $rootDir);

sub checkForResourceFork {
my $rsrcNamePath = "$name/..namedfork/rsrc";
if ((-e $rsrcNamePath) && ((-s "$name/..namedfork/rsrc") > 0)) {
print "$name\n";
}
}
============
 
M

Miles

Mitrokhin said:
Also (dare I ask for this too) If some nice soul could point me in the
right direction for picking up files with extended attributes under OS
X I'd be really gratefull to. Should it be done the same way, ie. via
os. calls or perhaps by way of a popen or ... ?

Mac OS X 10.5 comes with a Python "xattr" package pre-installed, which
the xattr command-line utility is built on top of. Furthermore, the
resource fork can be accessed as an extended attribute
(xattr.XATTR_RESOURCEFORK_NAME). This might also be true of Mac OS X
10.4, but I don't recall for sure and I don't have access to it to
check.

-Miles
 
M

Mitrokhin

i comp.lang.python skrev Miles
lørdag 30. august 2008 12:24:13 (0400), i en artikel med
ID <[email protected]>
og titlen "Re: Picking up resource forks and ...",
følgende:

: Mitrokhin wrote:
: > Also (dare I ask for this too) If some nice soul could point me in the
: > right direction for picking up files with extended attributes under OS
: > X I'd be really gratefull to. Should it be done the same way, ie. via
: > os. calls or perhaps by way of a popen or ... ?
:
: Mac OS X 10.5 comes with a Python "xattr" package pre-installed, which
: the xattr command-line utility is built on top of. Furthermore, the
: resource fork can be accessed as an extended attribute
: (xattr.XATTR_RESOURCEFORK_NAME). This might also be true of Mac OS X
: 10.4, but I don't recall for sure and I don't have access to it to
: check.
:
: -Miles
:

Yes I should have said that I'm on Leopard.

I did know beforehand about xattr (cp. my effort below, I still
have the annoying feeling that there's a smarter and faster way
of doing this too though) but I hadn't heard about
XATTR_RESOURCEFORK_NAME

Thanks for the input !


===========
import os, os.path, xattr, pprint
l = []
for dir, subdir, files in os.walk(os.getcwd()):
for file in files:
if file.find('._') == 0 : continue # generates an error otherwise
temp = xattr.xattr(os.path.join(dir,file))
if temp.keys() != []:
# better leave these rez files alone
if "com.apple.ResourceFork" in temp.keys(): continue
l.append(os.path.join(dir,file))
l.append(temp.keys())
while temp.keys():
temp.remove(temp.keys()[0])

pprint.pprint(l)
==========
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top