homebrew と pip で sphinx-blockdiag Install

PILのインストール

PILのインストールでの失敗

sphinx のもろもろと blockdiag をインストールしたとき

$pip install sphinxcontrib-blockdiag sphinxcontrib-seqdiag \
sphinxcontrib-nwdiag sphinxjp.themes.s6 sphinxjp.themecore \
sphinxjp.themes.sphinxjp sphinxcontrib-googleanalytics cloud_sptheme

あら、PILでエラー

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.6/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _imaging.c -o build/temp.macosx-10.6-universal-2.6/_imaging.o

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed

Installed assemblers are:

/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64

/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

_imaging.c:3281: fatal error: error writing to -: Broken pipe

compilation terminated.

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

lipo: can't open input file: /var/folders/c2/c232utXKFae8IIi13U1tyk+++TI/-Tmp-//ccBD0atA.out (No such file or directory)

error: command 'gcc-4.2' failed with exit status 1

----------------------------------------
Command /usr/bin/python -c "import setuptools;__file__='/Users/{myname}/build/PIL/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/c2/c232utXKFae8IIi13U1tyk+++TI/-Tmp-/pip-5LNZcb-record/install-record.txt failed with error code 1
Storing complete log in /Users/{myname}/.pip/pip.log

だめなので、 LeopardにPILをpipでインストール を参考に downloads.effbot.org から最新の imaging の Source の URL を探して imaging からインストールすることにした。が..

$ pip install http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
--- using frameworks at /System/Library/Frameworks

building '_imaging' extension

creating build/temp.macosx-10.6-universal-2.6

creating build/temp.macosx-10.6-universal-2.6/libImaging

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.6/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _imaging.c -o build/temp.macosx-10.6-universal-2.6/_imaging.o

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed

Installed assemblers are:

/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64

/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

_imaging.c:3281: fatal error: error writing to -: Broken pipe

compilation terminated.

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

lipo: can't open input file: /var/folders/c2/c232utXKFae8IIi13U1tyk+++TI/-Tmp-//cc4td21O.out (No such file or directory)

error: command 'gcc-4.2' failed with exit status 1

----------------------------------------
Command /usr/bin/python -c "import setuptools;__file__='/var/folders/c2/c232utXKFae8IIi13U1tyk+++TI/-Tmp-/pip-bwSJGa-build/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/c2/c232utXKFae8IIi13U1tyk+++TI/-Tmp-/pip-aiYVUZ-record/install-record.txt failed with error code 1
Storing complete log in /Users/{myname}/.pip/pip.log

ハイ .. ごめんなさい orz

Mac Book Pro / Snow Leopard での PILインストール 方法

pip をあきらめて Homebrew でインストールすることにしました。

$ brew info pil
pil 1.1.7
http://www.pythonware.com/products/pil/
Depends on: jpeg, little-cms
Not installed

This formula installs PIL against whatever Python is first in your path.
This Python needs to have either setuptools or distribute installed or
the build will fail.

http://github.com/mxcl/homebrew/commits/master/Library/Formula/pil.rb

$ brew install pil
Also installing dependencies: jpeg, little-cms
==> Downloading http://www.ijg.org/files/jpegsrc.v8c.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/jpeg/8c --disable-dependency-tracking
==> make install
/usr/local/Cellar/jpeg/8c: 17 files, 1.4M, built in 10 seconds
==> Downloading http://downloads.sourceforge.net/project/lcms/lcms/2.1/lcms2-2.1
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/little-cms/2.1 --disable-debug
==> make install
/usr/local/Cellar/little-cms/2.1: 14 files, 1004K, built in 8 seconds
==> Downloading http://effbot.org/downloads/Imaging-1.1.7.tar.gz
######################################################################## 100.0%
==> python setup.py build_ext
==> python setup.py install --prefix=/usr/local/Cellar/pil/1.1.7
==> Caveats
This formula installs PIL against whatever Python is first in your path.
This Python needs to have either setuptools or distribute installed or
the build will fail.
==> Summary
/usr/local/Cellar/pil/1.1.7: 172 files, 2.0M, built in 12 seconds

$ cd /Library/Python/2.6/site-packages
$ pip freeze | grep PIL

$ ln -s /usr/local/Cellar/pil/1.1.7/lib/python2.6/site-packages/PIL ./PIL
$ ln -s /usr/local/Cellar/pil/1.1.7/lib/python2.6/site-packages/PIL.pth ./PIL.pth
$ ln -s /usr/local/Cellar/pil/1.1.7/lib/python2.6/site-packages/PIL/PIL-1.1.7-py2.6.egg-info ./PIL-1.1.7-py2.6.egg-info
$ pip freeze | grep PIL
PIL==1.1.7

$ pip install sphinxcontrib-blockdiag
Successfully installed funcparserlib
Cleaning up...

ようやく通りました。

blockdiag の upgrade での注意点 ( 2011/07/03 追記)

後日、blockdiag の upgrade をしようとするとやはりPILをインストールしようとして以下のようにエラーが発生する。

$ sudo pip install blockdiag -U
Downloading/unpacking blockdiag
  Downloading blockdiag-0.8.2.tar.gz (113Kb): 113Kb downloaded
  Running setup.py egg_info for package blockdiag
Downloading/unpacking setuptools (from blockdiag)
  Running setup.py egg_info for package setuptools
Downloading/unpacking PIL (from blockdiag)
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -DHAVE_LIBJPEG -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.6/include -I/usr/local/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _imaging.c -o build/temp.macosx-10.6-universal-2.6/_imaging.o

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed

Installed assemblers are:

/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64

/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

_imaging.c:3281: fatal error: error writing to -: Broken pipe

compilation terminated.

_imaging.c:3017: warning: initialization from incompatible pointer type

_imaging.c:3077: warning: initialization from incompatible pointer type

lipo: can't open input file: /var/tmp//cckDX5O9.out (No such file or directory)

error: command 'gcc-4.2' failed with exit status 1

----------------------------------------
  Rolling back uninstall of PIL
Command /usr/bin/python -c "import setuptools;__file__='/Library/Python/2.6/site-packages/build/PIL/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-X2Qz3C-record/install-record.txt failed with error code 1
Storing complete log in /Users/joey/.pip/pip.log

不思議なことに、すでにある setuptools や PIL を再度インストールしようとしているのである。 いろいろ調べて回ったが、よい解決法が見つからないため、以下のような力技で対応した。

ビルドのためのディレクトリ /Library/Python/2.6/site-packages/build/blockdiag が上記エラー発生時にできているので

$ cd /Library/Python/2.6/site-packages/build/blockdiag/

以下 diff のように、いったん PIL を insatll_requires から除外する。
$ sudo cp setup.py setup.py_org
$ diff -u setup.py_org setup.py
--- setup.py_org       2011-07-03 09:14:43.000000000 +0900
+++ setup.py   2011-07-03 09:14:56.000000000 +0900
@@ -37,7 +37,6 @@
      include_package_data=True,
      install_requires=[
         'setuptools',
-        'PIL',
         'funcparserlib',
          # -*- Extra requirements: -*-
      ],

これで再度アップグレードをすれば、以下のようにPILのinstallを省いてさくっと終わる

$ sudo pip install blockdiag -U
Downloading/unpacking blockdiag
  Running setup.py egg_info for package blockdiag
Downloading/unpacking setuptools (from blockdiag)
  Running setup.py egg_info for package setuptools
Downloading/unpacking funcparserlib (from blockdiag)
  Running setup.py egg_info for package funcparserlib
    warning: no files found matching '*' under directory 'tests'
Installing collected packages: blockdiag, setuptools, funcparserlib
  Found existing installation: blockdiag 0.8.1
    Uninstalling blockdiag:
      Successfully uninstalled blockdiag
  Running setup.py install for blockdiag
    Installing blockdiag script to /usr/local/bin
  Found existing installation: setuptools 0.6c12dev-r88846
    Uninstalling setuptools:
      Successfully uninstalled setuptools
  Running setup.py install for setuptools
    Installing easy_install script to /usr/local/bin
    Installing easy_install-2.6 script to /usr/local/bin
  Found existing installation: funcparserlib 0.3.5
    Uninstalling funcparserlib:
      Successfully uninstalled funcparserlib
  Running setup.py install for funcparserlib
    warning: no files found matching '*' under directory 'tests'
Successfully installed blockdiag setuptools funcparserlib
Cleaning up...

blockdiag 動作試験 ( in bash )

さて、実験してみよう。

$ vi simple.diag
$ cat simple.diag
diagram {
   A -> B -> C -> D;
   A -> E -> F -> G;
}
$ blockdiag simple.diag
_images/simple.png
$ vi simpleJP.diag
$ cat simpleJP.diag
diagram {
   あ -> ほげ -> C -> D;
   あ -> E -> 日本語 -> G;
}
$ blockdiag simpleJP.diag
_images/simpleJP.png

ちゃんとできてますね。

bash での blockdiag のフォントの設定

ところで、blockdiagを使った際に生成されるpngファイルのFontを設定しておきたい場合は blockdiag の概要 フォント設定 を参考し、

$ cat $HOME/.blockdiagrc
[blockdiag]
fontpath = /Library/Fonts/HiraginoKG_W3.otf

しておくと、いつも指定のフォントを利用できます。 フォント指定した状態で先ほどつくった simpleJP.diag を生成しなおしてみると以下のようにきれいになりました。

_images/simpleJP2.png

ところで、本家では、 MacOSX (homebrew) の環境でインストールされる PIL パッケージは freetype2 に対応していないためそのままでは blockdiag を利用することはできません 的な話がありましたが、今回の手順では不要でした。いろいろ試行錯誤してしまっているので、知らない間に freetype2 をインストールしてしまっているのかな?

$ brew list
git            jpeg            little-cms      pil             tree

うーん 入ってない。ま、使えるからいいか。

blockdiag 動作試験 ( in sphinx )

じゃ、Sphinxでも書いてみましょう conf.py に以下の設定を追記します。

# Enabled extensions
extensions = ['sphinxcontrib.blockdiag']

# Fontpath for blockdiag (truetype font)
blockdiag_fontpath = '/Library/Fonts/Osaka.ttf'

そして reST で以下のように記述します。

.. blockdiag::
diagram admin {
       top_page -> config -> config_edit -> config_confirm -> top_page;
}

None

日本語もテストしましょう。

.. blockdiag::

 diagram admin {
       トップページ -> コンフィグ -> 編集画面 -> 確認画面 -> トップページ;
     }

None

Osakaのフォントじゃいまいちきれいじゃないので、ヒラギノを利用するようにしました。

$ cd /Library/Fonts
$ ln -s  ./"ヒラギノ角ゴ Pro W3.otf" HiraginoKG_W3.otf

してから、conf.py を

blockdiag_fontpath = ‘/Library/Fonts/HiraginoKG_W3.otf’

にすると、もう少しきれいになります。

sphinxjp.themes.sphinxjp を使いたい

sphinxjp の theme がきれいめだったので、インストールしてみた。

$ sudo pip install sphinxjp.themes.sphinxjp

ここで一点注意だが、ほかのsphinxのPluginと併用する場合、conf.py に以下のように書いちゃいけない

# include themecore
extensions = ['sphinxjp.themecore']
html_theme = 'sphinxjp'

# Enabled extensions
extensions = ['sphinxcontrib.blockdiag']
blockdiag_fontpath = '/Library/Fonts/HiraginoKG_W3.otf'

正しくはこうである

# Enabled extensions
extensions = ['sphinxjp.themecore','sphinxcontrib.blockdiag']
# Use Sphinxjp Theme
html_theme = 'sphinxjp'
# Set blockdiag Font Path
blockdiag_fontpath = '/Library/Fonts/HiraginoKG_W3.otf'