t-nissieの日記: 【電脳】Mac OS X 10.8.2 (Mountain Lion) の/usr/bin/xattr (Python script) 3
Mac OS X 10.8.2 (Mountain Lion) の/usr/bin/xattr は Python script なんだけど
$ ls -l /usr/bin/xattr*
-rwxr-xr-x 2 root wheel 925 9 6 18:39 /usr/bin/xattr
-rwxr-xr-x 1 root wheel 7786 9 6 18:39 /usr/bin/xattr-2.5
-rwxr-xr-x 1 root wheel 9442 9 6 18:39 /usr/bin/xattr-2.6
-rwxr-xr-x 1 root wheel 9442 9 6 18:39 /usr/bin/xattr-2.7
とPythonのバージョン毎に起動する本体を換えるディスパッチャみたい.
Mac OS X 10.5 (Leopard) の /usr/bin/xattr よりずっと速くなったみたい.
gemをインストールしてdelxattr.rbってのを書いて使っていた
んだけど,Pythonで書き直すかなぁ.Python勉強するのめんどうだなぁ.
(とここに書いておけばだれかがdelxattr.pyを書いてくれるかもしれない.)
手もとのdelxattr.rbはnext if File.ftype(f)=="link"って行を書き加えてあった.
#!/usr/bin/env ruby
# delxattr.rb removes all extended attributes (xattr) of all files and directories recursively under the current or given directory.
# Example1$ delxattr.rb
# Example2$ delxattr.rb ~/foo
# Copying: delxattr.rb is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY. You can copy, modify and
# redistribute delxattr.rb, but only under the conditions described
# in the GNU General Public License (the "GPL"). For more detail,
# see http://www.gnu.org/licenses/gpl.html .
##
require "rubygems"
require "find"
require "xattr"
if ARGV[0]
dir = ARGV[0]
else
dir = "."
end
Find.find(dir){|f|
next if File.ftype(f)=="link"
xattr = Xattr.new(f)
xattr.list.each{|a|
xattr.remove(a)
}
}
しょうがないから自分で書いてみたよ.
ぼくの初Pythonスクリプトだよ.
こんなもんでよいのかね.
どなたか採点して下さ〜い.
インデントが崩れないとよいけど.
#!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# delxattr.py removes all extended attributes (xattr) of all files and directories recursively under the current or given directory.
# Example1$ delxattr.py
# Example2$ delxattr.py ~/foo
# Copying: delxattr.py is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY. You can copy, modify and
# redistribute delxattr.py, but only under the conditions described
# in the GNU General Public License (the "GPL"). For more detail,
# see http://www.gnu.org/licenses/gpl.html .
##
import sys
import os
import xattr
try:
dir = sys.argv[1]
except IndexError:
dir = "."
for dpath,dnames,fnames in os.walk(dir):
for fname in fnames:
f = dpath+"/"+fname
if os.path.islink(f):
continue
x = xattr.xattr(f)
for a in x.keys():
del x[a]
おおむねよろしいんじゃないかと思いますが (スコア:1)
あえて指摘するならパス名の連結はスラッシュをくっつけるんじゃなくos.pathモジュールをインストールしてos.path.join()すべきかな、と。
あと、お使いのxattrモジュールがhttps://github.com/xattr [github.com]で公開されているxattrモジュールだとすると、x = xattr.xattr(f); x.clear()で拡張属性を全部消せるようです。縮めて書くとxattr.xattr(f).clear()ですね。
Re:おおむねよろしいんじゃないかと思いますが (スコア:1)
> os.pathモジュールをインストールしてos.path.join()すべきかな、と。
すいません、インストールじゃなくてインポートですorz。
Re:おおむねよろしいんじゃないかと思いますが (スコア:1)
採点,ありがとうございます!
os.path.join()すべき
OS依存性をなくすためですね.
縮めて書くとxattr.xattr(f).clear()
なるほど.Mac OS X にはxattrがはじめから入っているようです.
現状はこんなかんじです.
love && peace && free_software
t-nissie