ラベル Python の投稿を表示しています。 すべての投稿を表示
ラベル Python の投稿を表示しています。 すべての投稿を表示

2012-04-09

bashとpythonとの連携スクリプトで「du」を観易くする

最近、「python shell」でpythonの練習をしていると、ちょっと複雑なスクリプトをチューニングしようとすると、Aquamacsで.pyとして書き出し、terminalで動作確認する方が楽である事に気がついた。

慣れるにつれて、bashもpythonもターミナルから見ればスクリプトである事を実感して来た。数値処理は、若い頃にFortranやHP-BASICなどを使っていた事も在り、bashでやるよりはpythonの方が使い易い事にも気がついた。

そんな折り、「Time machine」によるバックアップに並行して、ディレクトリごとのバックアップ(rsync)をしたいと思うようになった。ついては以前、iMac間で、データの転送する時に、「du -h -d 1」をして、印刷用紙上で、転送の有無と、サイズの確認をしたものの見辛かったのを思い出した。

今回は、このコマンドをサイズの大きい順にソートして、「-h」に近い形式で見易くする事を考えた。大部分は下記の参考書を見ると、bashでできる;
  •  UNIXシェルスクリプト 逆引き大全 333の極意
    • 中橋 一郎・著
    • 秀和システム、2009-08.
    • ISBN-10: 4-7980-0884-2
が、計算処理はpythonの方が楽そうだ。そこで、bashスクリプトからpythonスクリプトを「バックコートで括る」形式で読みだす事にトライした。

まず、bashスクリプト本体は;

u1@div-mm:~/bin$ cat myDU
#!/bin/bash 

# Finderで、ディレクトリ右クリックで「情報」を見るのは面倒だ。そこで
# 現在のディレクトリ中の全てのディレクトリのサイズを求め、
# ブロックの順にソートし、
# バイト単位(kB=1000B, MB=1000kB, GB=1000MB)で表示する。
#    2012-04-08  (日) 15:44 by mNeji/irt
#    on Lion/10.7.3 at Mac mini(mid 2011)

# 参考: 「UNIXシェルスクリプト 逆引き大全 333極意」
#          中橋 一郎 著
#          秀和システム、2009-08
#          ISBN-10:4-7980-0886-2
# 参考箇所:unix333, (p223, Tips#194), (p112, Tips#084)

# 説明の表示
echo "+ Script($0) at `myDate` start +++++"
echo "+ action: 'du -d 1 | sort -gr' for dir:'`pwd`' " 
printf '+ Size \t\t Directory Name ----------------------\n'

# 現在のディレクトリと直下のディレクトリのサイズを求め、ブロックの順にソートする
du -d 1 |sort -gr > temp_du

# unix333, (p223, Tips#194): ファイルを行に分割
IFS=$'\n'               #改行区切り 
file=(`cat temp_du`)    #fileはアレイ構造

rm -f temp_du           # 中間ファイルを削除

# アレイの第一列にあるブロック数を適切なバイト数に変換して、
# 表として再度、表示する。

for line in "${file[@]}";do

    # unix333, (p112, Tips#084): 1行をタブで分割
    IFS=$'\t'       #タブ区切り
    set -- $line    #$1はブロック数(512Byte) , $2は相対ディレクトリ 
 
    #ブロック数からkByte, MByte, GByteへの変換には、
    #「Python script: myBlock2Byte.py」を作成して、用いた。
    ByteSize=`myBlock2Byte.py "$1"`  
    
    # 行表示
    printf '%s\t %s\n' "$ByteSize" "'$2'"

done

echo "+ Script($0) at `myDate` stop +++++"
u1@div-mm:~/bin$ 


ここで、「pythonスクリプトの呼び出し」は第42行目の;

  • ByteSize=`myBlock2Byte.py "$1"`

です。

 そのpythonスクリプト「myBlock2Byte.py」は;

u1@div-mm:~/bin$ cat myBlock2Byte.py 
#! /usr/bin/python
# coding: utf-8

# usage: $ myBloch2Byte.py block_number
#         The parameter, block_number indicates in the unit of 1block = 512Byte

# input: Size in the unit of Block
# output: Size in the unit of kB, MG, GB
# 2012-04-06 by mNeji
#   Python/2.7.1 on Lion/10.7.2 at Mac mini(mid 2011)


# 引数の受け入れ
import sys
myArg= sys.argv

# ブロック数を整数型に変換し、
blockSize=int(myArg[1])

# module math
import math

# バイト数に変換
byteSize=math.floor(blockSize*512)  # 1block = 512Byte
kSize=byteSize/1000.    # 1kB = 1000Byte for Lion/10.7.3
MSize=kSize/1000.       # 1MB = 1000kB for Lion/10.7.3
GSize=MSize/1000.       # 1GB = 1000MB for Lion/10.7.3



# 使用サイズの補助単位選択

if GSize >= 1.:
    print '%7.2f%s' % (GSize,"GB")
elif MSize >= 1.:
    print '%6.1f%s' % (MSize,"MB")
else:
    print '%5.0f%s\t' % (kSize,"kB")


    
u1@div-mm:~/bin$ 



です。

このスクリプトで自分のホーム・ディレクトリのディレクトリ容量を出した結果は;

u1@div-mm:~$ myDU
+ Script(/Users/u1/bin/myDU) at 2012-04-09(月)14:02:28 start +++++
+ action: 'du -d 1 | sort -gr' for dir:'/Users/u1' 
+ Size    Directory Name ----------------------
 129.61GB  '.'
  64.22GB  './Library'
  57.51GB  './Music'
   5.76GB  './VirtualBox VMs'
 714.6MB  './Sites'
 564.1MB  './Documents'
 465.9MB  './Pictures'
 143.2MB  './myLocal'
 111.6MB  './Kabe-Gamis'
  88.3MB  './tmp'
  11.5MB  './bin'
   8.3MB  './backup'
   4.6MB  './.npm'
   2.9MB  './.gem'
   1.4MB  './myBash'
   1.3MB  './RRwork'
   1.1MB  './solarized'
  569kB   './Public'
  172kB   './.zsh.d'
  164kB   './DLしたプログラム資料'
   66kB   './function'
   53kB   './zsh_back0'
   20kB   './.ssh'
   16kB   './mytest'
   16kB   './Downloads'
   16kB   './Desktop'
    4kB   './.pip'
    4kB   './.idlerc'
    4kB   './.cups'
    0kB   './NTT_PR-S300SE'
    0kB   './Movies'
    0kB   './.emacs.d'
    0kB   './.bundler'
    0kB   './.Trash'
+ Script(/Users/u1/bin/myDU) at 2012-04-09(月)14:02:30 stop +++++
u1@div-mm:~$ 

です。なお、上記の容量値を、Finderで該当するディレクトリを右クリックして情報を見てみますと;




サイズの項にでている「129.61 GB」 と一致します。それぞれ小さい場合も完全に一致しています。今後、気楽にバックアップを検討する事ができそうです。


慣れてしまえば、完全にpythonスクリプトで処理する方が楽そうである。いよいよ、Calcのマクロをpythonで作る事も可能な気がして来た。



ーーーー このポストの履歴
  1. 開始 2012-04-09 (月) 13:53


2012-01-28

「floating menu」のサイト

Python関係の検索していて「floating menu」を実施しているサイトを拝見した。<frame>を用いていると思ったが違うようだ:その記事を見ていたら;
と言う事らしい。このサイトは部分的にも「Pyhton-Django」を用いているらしい。

自分も取りあえず「Python-WSGI」から手を出してみたいものだ。

「floating menu」が出来れば、昔のPerlのWikiが再現できるかもしれない。


ーーーー このポストの履歴
  1. 開始 2012-01-28 (土) 20:39



2012-01-23

「Lion 10.7.2」での「Pythonの複数ヴァージョン」の確認

以前の記事、図書紹介#004:「Go言語プログラミング入門 on Google App Engine」を見ていると、Lionには複数のヴァージョンのPythonがプリンストールされている様です。

ネット上で検索するも、意外と情報がありません。常道として「man python」を眺めていると、まずプリインストールされているのは;
  • Python 2.5
  • Python 2.6
  • Python 2.7
のようです。これのヴァージョンを直接インタプリタとして動作確認が出来る様です。

div-mm:~ u1$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**10
1024
>>> exit()
div-mm:~ u1$ python2.5
Python 2.5.6 (r256:88840, Jul 31 2011, 19:30:45) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**5
32
>>> ^D
div-mm:~ u1$ python2.6
Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**6+0.7
64.700000000000003
>>> ^D
div-mm:~ u1$ python2.7
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**7
128
>>> ^D
div-mm:~ u1$ 

少なくとも、ヴァージョン番号をつければ、該当するインタプリタが呼び出される様です。その他の方法でも出来るらしい;

div-mm:~ u1$ defaults write com.apple.versioner.python Version 2.5
div-mm:~ u1$ python
Python 2.5.6 (r256:88840, Jul 31 2011, 19:30:45) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**5+.6
32.600000000000001
>>> ^D
div-mm:~ u1$ python
Python 2.5.6 (r256:88840, Jul 31 2011, 19:30:45) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**5+.06
32.060000000000002
>>> ^D
div-mm:~ u1$ defaults write com.apple.versioner.python Version 2.6
div-mm:~ u1$ python
Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**6+.007
64.007000000000005
>>> ^D
div-mm:~ u1$ defaults write com.apple.versioner.python Version 2.7
div-mm:~ u1$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**7+.0001
128.0001
>>> ^D
div-mm:~ u1$ echo "-----------------------------"
-----------------------------
div-mm:~ u1$ export VERSIONER_PYTHON_VERSION=2.5
div-mm:~ u1$ python
Python 2.5.6 (r256:88840, Jul 31 2011, 19:30:45) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**5+0.6
32.600000000000001
>>> ^D
div-mm:~ u1$ 

で、実際にGAEに対応させる場合、ディフォールトでpython2.5.6をコールしたいわけである。そこで実際のコマンドを調べてみた;

div-mm:~ u1$ 
div-mm:~ u1$ which python
/usr/bin/python
div-mm:~ u1$ ll /usr/bin/py*
-rwxr-xr-x  6 root  wheel    925  9 18 15:47 /usr/bin/pydoc*
lrwxr-xr-x  1 root  wheel     74  9 18 15:47 /usr/bin/pydoc2.5@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pydoc2.5
lrwxr-xr-x  1 root  wheel     74  9 18 15:47 /usr/bin/pydoc2.6@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pydoc2.6
lrwxr-xr-x  1 root  wheel     74  9 18 15:47 /usr/bin/pydoc2.7@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7
-rwxr-xr-x  2 root  wheel  62752  9 18 15:47 /usr/bin/python*
-rwxr-xr-x  6 root  wheel    925  9 18 15:47 /usr/bin/python-config*
lrwxr-xr-x  1 root  wheel     75  9 18 15:47 /usr/bin/python2.5@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5
lrwxr-xr-x  1 root  wheel     82  9 18 15:47 /usr/bin/python2.5-config@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5-config
lrwxr-xr-x  1 root  wheel     75  9 18 15:47 /usr/bin/python2.6@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
lrwxr-xr-x  1 root  wheel     82  9 18 15:47 /usr/bin/python2.6-config@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6-config
lrwxr-xr-x  1 root  wheel     75  9 18 15:47 /usr/bin/python2.7@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel     82  9 18 15:47 /usr/bin/python2.7-config@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x  2 root  wheel  62752  9 18 15:47 /usr/bin/pythonw*
lrwxr-xr-x  1 root  wheel     76  9 18 15:47 /usr/bin/pythonw2.5@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pythonw2.5
lrwxr-xr-x  1 root  wheel     76  9 18 15:47 /usr/bin/pythonw2.6@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6
lrwxr-xr-x  1 root  wheel     76  9 18 15:47 /usr/bin/pythonw2.7@ -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
div-mm:~ u1$ ll /System/Library/Frameworks/Python.framework/Versions/
total 8
drwxr-xr-x   7 root  wheel  238  9 18 15:48 2.3/
drwxr-xr-x  11 root  wheel  374 10 13 10:55 2.5/
drwxr-xr-x  11 root  wheel  374 10 13 10:55 2.6/
drwxr-xr-x  11 root  wheel  374 10  1 11:57 2.7/
lrwxr-xr-x   1 root  wheel    3  9 18 15:47 Current@ -> 2.7
div-mm:~ u1$ 

だから、少なくとも
  • /System/Library/Frameworks/Python.framework/Versions/Current@ -> 2.5
とする必要がありそうだ。まだ外にも必要な設定がありそうな気がする。その後、
という論議の最後で、Alexさんが「Apr 22 '11」のコメントで;
cd /Library/Frameworks/Python.framework/Versions
sudo rm Current
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.0 Current
と指摘されている。

ーーーー このポストの履歴
  1. 開始 2012-01-23 (月) 20:36
  2. 追加 2012-01-25  (水) 13:19  Alexさんが「Apr 22 '11」のコメント


2011-10-14

LionでのJava

どうやらAppleはJavaから離れつつあるようだが、
これを使ってみるかな。Javaといえば、GWT(Google Web Toolkit 2)もJavaか。

他方、AppleのXcodeでは、JavaでなくPythonが主役だろう。

素人としては、何を選択すれいいんだろう。AppleはDeveloperに会員として参加しても、英語の資料しか手に入りそうになさそうだが。GWTに関してはどうかな?


ーーーー このポストの履歴
  1. 開始 2011-10-14 (金) 21:09

注目の投稿

Terminalでの、なんちゃってViモドキ

近頃、ようやくKarabiner-Elementsに慣れてきたので、 Terminalで動作する「擬似Vi-Mode」を作って見たので、ご紹介します。 『概要』 「擬似Vi-Mode」の所以は、方向キー「←↓↑→」を通常の「hjkl」ではなくて「jkil」としました。これ...