ふにゃるんv2

もとは、http://d.hatena.ne.jp/Wacky/

pyCDKを入れよう

Linuxなどのような環境を使っていると、リモートからログインして作業する訳で、大体がテキストな操作がほとんど となる。
VNCなどを使えば、一応グラフィカルな操作が可能になるけど、OK/Canelボタンが逆とか、クリップボード機能が使えないとか、キー操作が全く統一されてないとか、ちょっとでかいGUIアプリだと簡単に落っこちるとか、そもそも使い難い訳であって、パイプで小物アプリを繋げて要求事項を満たすってな、Unix系 本来の操作を使うのなら、テキストが中心になるんだよね。


とはいえ、あの呪文のようなコマンドとオプションを頭に格納しておけるほど、容量も大きくない。
ってな訳で、ちょっとGUIっぽい環境を作る為の折衷案として、cursesライブラリを使う事を考えた。


ちなみに、cursesに関する日本語な解説サイトって、あまり見当たらない。
結構歴史があると思うんだが、何でだろ?
ぐーぐるで、ヒットしたと思ったら、「…という訳で、作ってみた」ってな感じでプログラムした後。つぅか出来るのがデフォルトみたいな言い回し。つくづくヘボ甘グラマに 世知辛い世の中よ。


まぁ、それはそれとして、cursesを使っていこうとすると、ちょっとしたエディットボックスとかダイアログが欲しくなる訳だわさ。
何か良いのが無いかな〜?と思って、色々調べていたら、 CDK ってライブラリが結構有名だっつぅ事に気付いた。


NCURSES - Frequently Asked Questions (FAQ)
http://dickey.his.com/ncurses/ncurses.faq.html

CDK - Curses Development Kit (a library of widgets)
http://dickey.his.com/cdk/cdk.html



で、CDKを扱うPythonライブラリがあって、pyCDK というらしい。


pyCDK
http://pycdk.sourceforge.net/



早速入れてみる事にする。…まずは、Cygwinで(笑)
ちなみに、Python 2.3 for Windows では、cursesライブラリが使えない。(2.4では使えるのかな?無理のような気もするが)
ActivePythonは使えるのかな?という気もするが、まだ入れてない。


という訳で、まずは、CDKからインストールする。
CDKのサイトから、 "cdk.tar.gz" を取ってくる。アーカイブファイルをチェックすると、 "cdk-5.0-20050424" 版らしい。

$ tar xzvf cdk.tar.gz
cdk-5.0-20050424/
cdk-5.0-20050424/CHANGES
cdk-5.0-20050424/COPYING
cdk-5.0-20050424/EXPANDING
cdk-5.0-20050424/INSTALL
cdk-5.0-20050424/Makefile.in
...
cdk-5.0-20050424/view_info.c
cdk-5.0-20050424/viewer.c

$ cd cdk-5.0-20050424/

$ ls
CHANGES      c++/            debug.c        histogram.c     popup_label.c
COPYING      calendar.c      demos/         include/        position.c
EXPANDING    cdk-4.9.11.lsm  dialog.c       install-sh*     radio.c
INSTALL      cdk.c           draw.c         itemlist.c      scroll.c
Makefile.in  cdk.spec        entry.c        label.c         select_file.c
NOTES        cdk_compat.c    examples/      man/            selection.c
README       cdk_objs.c      fselect.c      manlinks.sed    swindow.c
TODO         cdk_params.c    gen-scale.c    manlinks.sh*    template.c
VERSION      cdkscreen.c     gen-scale.sh*  marquee.c       traverse.c
aclocal.m4   cli/            gen-slider.c   matrix.c        version.c
alphalist.c  config.guess*   get_index.c    mentry.c        view_file.c
binding.c    config.sub*     get_string.c   menu.c          view_info.c
button.c     configure*      graph.c        mkdirs.sh*      viewer.c
buttonbox.c  configure.in    headers.sh*    popup_dialog.c

$ ./configure
creating cache ./config.cache
checking for X... no
checking host system type... i686-pc-cygwin
Configuring for cygwin
checking for package version... 5.0
checking for package patch date... 20050424
...
creating examples/Makefile
creating demos/Makefile
creating include/cdk_config.h

$ make
/bin/sh ./gen-scale.sh DSCALE DScale Double double ./include/gen-scale.h >includ
e/dscale.h
...
/bin/sh ./headers.sh -p CDK -c include/cdk_config.h -d /usr/local/include/cdk -s
 ./include -i -s include -i

$ make install
...
        ... mentry.h
        ... menu.h
        ... radio.h
...
        ... traverse.h
        ... viewer.h
Installing CDK library

どうでもいいが、コンパイルは数分なんだけど、インストール長いね。特にman関連のが。


次、pyCDKを入れる。
pyCDKのサイトから、 "pycdk.tar.gz" を取ってくる。SourceForge.netの表記を見ると、Version 0.1だった。

$ tar xzvf pycdk.tar.gz
pycdk/
pycdk/cdk.pyx
pycdk/LICENSE
pycdk/setup.py
pycdk/README
pycdk/examples/
pycdk/examples/label.py
pycdk/examples/alphalist.py
...
pycdk/examples/slider.py
pycdk/examples/template.py
pycdk/examples/swindow.py
pycdk/examples/viewer.py

$ cd pycdk
$ ls
LICENSE  README  cdk.pyx  examples/  setup.py
$ python setup.py install
Traceback (most recent call last):
  File "setup.py", line 3, in ?
    from Pyrex.Distutils import build_ext
ImportError: No module named Pyrex.Distutils

Ouch!コンパイルしようとすると、エラーが発生した。
どうやら、Pyrexってライブラリが必要らしい。
Pyrexってのは、以下のサイトに行けばある。


Pyrex
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/



ここから、"Pyrex-0.9.3.tar.gz"ってのを取ってきて、コンパイル

$ tar xzvf Pyrex-0.9.3.tar.gz
Pyrex-0.9.3/bin/pyrexc
Pyrex-0.9.3/bin/update_references
Pyrex-0.9.3/CHANGES.txt
Pyrex-0.9.3/Demos/callback/cheese.pyx
Pyrex-0.9.3/Demos/callback/cheesefinder.c
Pyrex-0.9.3/Demos/callback/cheesefinder.h
Pyrex-0.9.3/Demos/callback/Makefile
Pyrex-0.9.3/Demos/callback/Makefile.nodistutils
Pyrex-0.9.3/Demos/callback/README.txt
...
Pyrex-0.9.3/Tools/pyrex.st
Pyrex-0.9.3/USAGE.txt

$ cd Pyrex-0.9.3
$ ls
CHANGES.txt  INSTALL.txt  Pyrex/      Tools/     pyrexc.py*
Demos/       MANIFEST.in  README.txt  USAGE.txt  setup.py
Doc/         Makefile     ToDo.txt    bin/
$ python setup.py install
running install
running build
running build_py
...
byte-compiling /usr/lib/python2.4/site-packages/Pyrex/Utils.py to Utils.pyc
byte-compiling /usr/lib/python2.4/site-packages/Pyrex/__init__.py to __init__.py
c
running install_scripts
copying build/scripts-2.4/pyrexc -> /usr/bin
changing mode of /usr/bin/pyrexc to 755
running install_data
copying Pyrex/Compiler/Lexicon.pickle -> /usr/lib/python2.4/site-packages/Pyrex/
Compiler

うっしゃ、問題なし。
ってんで、元のpyCDKのコンパイルを再開。

$ python setup.py install
running install
running build
running build_ext
building 'cdk' extension
Traceback (most recent call last):
  File "setup.py", line 30, in ?
    cmdclass = {'build_ext': build_ext}
  File "/tmp/python.572/usr/lib/python2.4/distutils/core.py", line 149, in setup

  File "/tmp/python.572/usr/lib/python2.4/distutils/dist.py", line 946, in run_c
ommands
  File "/tmp/python.572/usr/lib/python2.4/distutils/dist.py", line 966, in run_c
ommand
  File "/tmp/python.572/usr/lib/python2.4/distutils/command/install.py", line 50
6, in run
  File "/usr/lib/python2.4/cmd.py", line 333, in run_command
    del help[cmd]
  File "/tmp/python.572/usr/lib/python2.4/distutils/dist.py", line 966, in run_c
ommand
  File "/tmp/python.572/usr/lib/python2.4/distutils/command/build.py", line 112,
 in run
  File "/usr/lib/python2.4/cmd.py", line 333, in run_command
    del help[cmd]
  File "/tmp/python.572/usr/lib/python2.4/distutils/dist.py", line 966, in run_c
ommand
  File "/tmp/python.572/usr/lib/python2.4/distutils/command/build_ext.py", line
279, in run
  File "/tmp/python.572/usr/lib/python2.4/distutils/command/build_ext.py", line
405, in build_extensions
  File "/tmp/python.572/usr/lib/python2.4/distutils/command/build_ext.py", line
442, in build_extension
TypeError: swig_sources() takes exactly 2 arguments (3 given)

Ouch!!だめじゃん!


で、ぐぐるったら、回答がわかった。バグのようだ。

[Pyrex] Newbie question
http://lists.copyleft.no/pipermail/pyrex/2005-July/001256.html

Here is a patch that should fix the problem. It is even backwards compatible with
Python 2.3. Apply it to lib/python2.4/site-packages/Pyrex/Distutils/build_ext.py .
On Windows it is Python2.4\Lib\site-packages\Pyrex\Distutils\build_ext.py .

35c35
< def swig_sources (self, sources):
---
> def swig_sources (self, sources, extension=None):


既にインストールしちゃっているので、直接修正しちゃう。

C:\cygwin\lib\python2.4\site-packages\Pyrex\Distutils\build_ext.py

      if self.compiler == 'mingw32':
        self.swig_cpp = 1

#  def swig_sources (self, sources):
def swig_sources (self, sources, extension=None):
    if not self.extensions:
      return

再度、pyCDKのコンパイルに挑戦。

$ python setup.py install
...
cdk.c:25: 警告: '__Pyx_TypeTest' declared `static' but never defined
cdk.c:26: 警告: '__Pyx_GetStarArgs' declared `static' but never defined
cdk.c:29: 警告: '__Pyx_ImportType' declared `static' but never defined
cdk.c:30: 警告: '__Pyx_SetVtable' declared `static' but never defined
cdk.c:31: 警告: '__Pyx_GetVtable' declared `static' but never defined
cdk.c:32: 警告: '__Pyx_CreateClass' declared `static' but never defined
creating build/lib.cygwin-1.5.18-i686-2.4
gcc -shared -Wl,--enable-auto-image-base build/temp.cygwin-1.5.18-i686-2.4/cdk.o
 -L/usr/local/lib -L/usr/lib -L/usr/lib/python2.4/config -lcdk -lncurses -lpytho
n2.4 -o build/lib.cygwin-1.5.18-i686-2.4/cdk.dll
Info: resolving _COLS by linking to __imp__COLS (auto-import)
Info: resolving _COLORS by linking to __imp__COLORS (auto-import)
running install_lib
copying build/lib.cygwin-1.5.18-i686-2.4/cdk.dll -> /usr/lib/python2.4/site-pack
ages

色々ワーニングが出るけど、ちゃんとコンパイルできた。
exampleフォルダの各サンプルも動く。

$ us.bat
$ python viewer.py

ほらね。

Red Hat Linux 9に、pyCDKを入れる

お次は、RH9にpyCDKを入れてみよう〜。


環境は、こんな感じね。

[root@wacky2 wacky]# uname -a
Linux wacky2 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
[root@wacky2 wacky]# gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@wacky2 wacky]# python -V
Python 2.2.2

早速、CDKを入れる。

[root@wacky2 wacky]# tar xzvf cdk.tar.gz 
cdk-5.0-20050424/
cdk-5.0-20050424/CHANGES
cdk-5.0-20050424/COPYING
cdk-5.0-20050424/EXPANDING
cdk-5.0-20050424/INSTALL
cdk-5.0-20050424/Makefile.in
cdk-5.0-20050424/NOTES
cdk-5.0-20050424/README
...
[root@wacky2 wacky]# cd cdk-5.0-20050424/
[root@wacky2 cdk-5.0-20050424]# ls
CHANGES      aclocal.m4      cdk.spec      configure.in  gen-scale.sh  itemlist.c    mkdirs.sh       template.c
COPYING      alphalist.c     cdk_compat.c  debug.c       gen-slider.c  label.c       popup_dialog.c  traverse.c
EXPANDING    binding.c       cdk_objs.c    demos         get_index.c   man           popup_label.c   version.c
INSTALL      button.c        cdk_params.c  dialog.c      get_string.c  manlinks.sed  position.c      view_file.c
Makefile.in  buttonbox.c     cdkscreen.c   draw.c        graph.c       manlinks.sh   radio.c         view_info.c
NOTES        c++             cli           entry.c       headers.sh    marquee.c     scroll.c        viewer.c
README       calendar.c      config.guess  examples      histogram.c   matrix.c      select_file.c
TODO         cdk-4.9.11.lsm  config.sub    fselect.c     include       mentry.c      selection.c
VERSION      cdk.c           configure     gen-scale.c   install-sh    menu.c        swindow.c
[root@wacky2 cdk-5.0-20050424]# ./configure
creating cache ./config.cache
checking for X... no
checking host system type... i686-pc-linux-gnu
Configuring for linux-gnu
checking for package version... 5.0
checking for package patch date... 20050424
checking for gcc... gcc
checking whether the C compiler (gcc  ) works... yes
checking whether the C compiler (gcc  ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
...
[root@wacky2 cdk-5.0-20050424]# make
/bin/sh ./gen-scale.sh DSCALE DScale Double double ./include/gen-scale.h >include/dscale.h
/bin/sh ./gen-scale.sh FSCALE FScale Float float ./include/gen-scale.h >include/fscale.h
/bin/sh ./gen-scale.sh FSLIDER FSlider Float float ./include/gen-slider.h >include/fslider.h
/bin/sh ./gen-scale.sh SCALE Scale Int int ./include/gen-scale.h >include/scale.h
/bin/sh ./gen-scale.sh SLIDER Slider Int int ./include/gen-slider.h >include/slider.h
/bin/sh ./gen-scale.sh USCALE UScale Unsigned unsigned ./include/gen-scale.h >include/uscale.h
/bin/sh ./gen-scale.sh USLIDER USlider Unsigned unsigned ./include/gen-slider.h >include/uslider.h
/bin/sh ./gen-scale.sh DSCALE DScale Double double ./gen-scale.c >dscale.c
...
gcc -g -O2  -DHAVE_CONFIG_H -I./include -I./include  -D_GNU_SOURCE -c viewer.c
creating libcdk.a
/bin/sh ./headers.sh -p CDK -c include/cdk_config.h -d /usr/local/include/cdk -s ./include -i -s include -i

[root@wacky2 cdk-5.0-20050424]# make install
...
        ... template.h
        ... traverse.h
        ... viewer.h
Installing CDK library

次、Pyrexな。

[root@wacky2 wacky]# tar xzvf Pyrex-0.9.3.tar.gz 
Pyrex-0.9.3/bin/pyrexc
Pyrex-0.9.3/bin/update_references
Pyrex-0.9.3/CHANGES.txt
Pyrex-0.9.3/Demos/callback/cheese.pyx
...
[root@wacky2 wacky]# cd Pyrex-0.9.3
[root@wacky2 Pyrex-0.9.3]# ls
CHANGES.txt  Doc          MANIFEST.in  Pyrex       ToDo.txt  USAGE.txt  pyrexc.py
Demos        INSTALL.txt  Makefile     README.txt  Tools     bin        setup.py
[root@wacky2 Pyrex-0.9.3]# python setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/Pyrex
copying Pyrex/__init__.py -> build/lib/Pyrex
copying Pyrex/Debugging.py -> build/lib/Pyrex
...
byte-compiling /usr/lib/python2.2/site-packages/Pyrex/Plex/Transitions.py to Transitions.pyc
running install_scripts
copying build/scripts-2.2/pyrexc -> /usr/bin
changing mode of /usr/bin/pyrexc to 755
running install_data
copying Pyrex/Compiler/Lexicon.pickle -> /usr/lib/python2.2/site-packages/Pyrex/Compiler

次、pyCDKな。

[root@wacky2 wacky]# tar xzvf pycdk.tar.gz 
pycdk/
pycdk/cdk.pyx
pycdk/LICENSE
pycdk/setup.py
pycdk/README
...
[root@wacky2 wacky]# cd pycdk
[root@wacky2 pycdk]# ls
LICENSE  README  cdk.pyx  examples  setup.py
[root@wacky2 pycdk]# python setup.py install
running install
running build
running build_ext
building 'cdk' extension
creating build
creating build/temp.linux-i686-2.2
gcc -DNDEBUG -O2 -g -pipe -march=i386 -mcpu=i686 -D_GNU_SOURCE -fPIC -fPIC -I/usr/local/include/cdk -I/usr/include -I/usr/include/python2.2 -c cdk.c -o build/temp.linux-i686-2.2/cdk.o
cdk.c: 関数 `__pyx_f_3cdk_6Matrix_getCurrentCell' 内:
cdk.c:4665: 警告: 引数 1 個の `PyString_FromString' を渡しますにより、キャストなしで整数からポインタを作りました
creating build/lib.linux-i686-2.2
gcc -shared build/temp.linux-i686-2.2/cdk.o -L/usr/local/lib -L/usr/lib -lcdk -lncurses -o build/lib.linux-i686-2.2/cdk.so
running install_lib
copying build/lib.linux-i686-2.2/cdk.so -> /usr/lib/python2.2/site-packages

何の問題もなしにインストールできた。
Cygwinの時に必要だった、 build_ext.py への修正は、このRH9のPythonが Ver.2.2 だから。って事でしょうな。


やれやれ、と思い、早速サンプルを実行しようとすると、こんな結果。うひゃ。

[root@wacky2 examples]# python viewer.py
Traceback (most recent call last):
  File "viewer.py", line 18, in ?
    viewer = cdk.Viewer(screen, cdk.CENTER, cdk.CENTER, 20, -1, buttons)
  File "cdk.pyx", line 1252, in cdk.Viewer.__new__
SystemError: Objects/longobject.c:231: bad argument to internal function

Python 2.4にした方がええんですかいのぅ?


ちなみに、試しに build_ext.py への修正を施し、pyCDKを再度インストールし直してみたが、案の定、エラーになった。がくし。