星期五, 十月 27, 2006

gettext 资料

基本使用:
首先,在代码中引入 gettext 模块,在 python 中一般可以这样作:
import gettext
_ = gettext
...
print _("string to be stranslated")

然后创建翻译文件(/usr/local/share/locale),MO 文件则应该位于:
/usr/local/share/locale/zh_CN/LC_MESSAGES/crablfs.mo
例:
# mkdir -p locale/zh_CN/LC_MESSAGES
# xgettext --default-domain="crablfs" --keyword=_ crablfs.py
注意:这是根据下面参考资料来的,xgettext 可能只能用于 C 语言,对 python 可能要使用 pygettext script 替代。

# mv crablfs.po crablfs.pot
// 拷贝成模板
# cp crablfs.pot zh_CN.po

然后编辑 zh_CN.po,对所有的 msgid "original string" 完成 msgstr "翻译后字符串"。

接着创建 MO 文件:
# msgfmt --output-file=zh_CN.gmo zh_CN.po
# mv zh_CN.gmo locale/zh_CN/LC_MESSAGES/crablfs.mo

调用程序:
# LANG=zh_CN crablfs.py

升级翻译文件:
如果更改了源文件中的文本,特别是比较大的改动,不进行合并则不是很麻烦就是要重来。GNU gettext 提供了 msgmerge 工具:
# xgettext --default-domain="crablfs" --keyword=_ crablfs.py
# mv crablfs.po crablfs.pot
# mv zh_CN.po zh_CN.old.po
# msgmerge zh_CN.old.po crablfs.pot --output-file zh_CN.po
..done
# diff -u zh_CN.old.po zh_CN.po >zh_CN.po.diff
# rm -f zh_CN.old.po
注意,msgmerge 用一个新的 msgid 字符串对旧的进行替换,把原来在 msgstr 中的翻译保留下来。检查新的 PO 文件并确定翻译正确即可。最后运行 msgfmt 创建升级的 MO 文件。

参考:《GNOME 应用程序开发指南》.(美) John R Sheets.机械工业出版社

growisofs trouble shooting

# growisofs -Z /dev/dvd=/mnt/file/Yellow/example.iso
Executing 'builtin_dd if=/mnt/file/Yellow/example.iso of=/dev/dvd obs=32k seek=0'
:-( /dev/dvd: 2295104 blocks are free, 2344000 to be written!
这是说 /dev/dvd 只有 2295104 个块(blocks),但是却要刻录 2344000 块,空间不够。虽然标明的容量是 4.7GB,而 iso 文件的大小只有 4.4G。

这可能和 iso 上具体的文件大小有关,参考 Linux 的文件系统的知识,如果文件小而多,则 inode 可能不够,如果文件大而少,则 block 可能不够。

星期三, 十月 25, 2006

DVD 刻录机刻录 CD-R

用 growisofs 提示不能识别为 DVD Removable disk,于是改用 cdrecord:
# cdrecord -v speed=48 dev=0,0,0 -data /mnt/file/iso/lfslivecd-x86-6.2-3.iso
cdrecord: Invalid or incomplete multibyte or wide character. Cannot open SCSI driver.
cdrecord: For possible targets try 'cdrecord -scanbus'.
cdrecord: For possible transport specifiers try 'cdrecord dev=help'.

# cdrecord -scanbus
Cdrecord-Clone 2.01.01a03-dvd (x86_64-unknown-linux-gnu) Copyright (C) 1995-2005 J枚rg Schilling
NOTE: This version contains the OSS DVD extensions for cdrtools and thus may
have bugs related to DVD issues that are not present in the original
cdrtools. Please send bug reports or support requests to
http://bugzilla.redhat.com/bugzilla The original cdrtools author should
not be bothered with problems in this version.
Linux sg driver version: 3.5.27
Using libscg version 'schily-0.8'.
cdrecord: Warning: using inofficial libscg transport code version (schily - Red Hat-scsi-linux-sg.c-1.85-RH '@(#)scsi-linux-sg.c 1.85 05/05/16 Copyright 1997 J. Schilling').
scsibus2:
2,0,0 200) 'DVDRW ' 'IDE H16X ' 'B02C' Removable CD-ROM
2,1,0 201) *
2,2,0 202) *
2,3,0 203) *
2,4,0 204) *
2,5,0 205) *
2,6,0 206) *
2,7,0 207) *

# cdrecord -v speed=48 dev=2,0,0 -data /mnt/file/iso/lfslivecd-x86-6.2-3.iso

DVD 复制

$ dd if=/dev/hda of=./example.iso
// IDE 接口的内置 DVD-ROM 默认是 /dev/hda(FC5)
$ growisofs -Z /dev/dvd=./example.iso
// 外置的 DVD 刻录机(IDE 转 USB 接口)默认为 /dev/scd0,/dev/dvd 为符号链接
// 参见:前述

gpg 简单文件加密

加密:
# gpg -cv file
Password: ********
解密:
# gpg -v file
Password: ********

星期二, 十月 24, 2006

crablfs: 基于用户的包管理系统(v0.1)

安装档:
http://cheeseshop.python.org/pypi?:a...fs&version=0.1

crablfs: 基于用户的包管理系统 -- 用户简明手册
crablfs: User Based Package Management System -- User's Concise Manual

Author: 周鹏(Chowroc)
dATE: 2006-10-20
Email: chowroc.z@gmail.com

(1) 概述
crablfs 是基于用户的包管理系统,其基本原理是:系统中的每一个软件包都对应一个单独的用户,当安装一个包时便以这个用户的身份进行操作,这样该包在系统中生成的所有文件和用户的组权限都是该用户;同时所有的包用户都在一个 install 组中,并且令系统中的一些关键目录如 /usr/local, /usr/local/bin 等拥有 g+s,o+t 权限,并属于 install 组,以利于不同的包用户在这些目录中创建属于它们自己的文件和目录。

这种方法有诸多的好处。关于其原理的具体描述,请参见 LFS hints:
More Control and Package Management using Package Users (v1.2)
http://www.linuxfromscratch.org/hint...nd_pkg_man.txt

基于这种原理,包管理器 userpack 是一个命令行接口,它拥有与 rpm, paco 这样的包管理器相似的命令行接口,以便完成包管理器所必须的若干操作。它只对一个包进行安装、卸载、查询等操作,或查询某个文件的包属主,或列出系统当前所有的包(rpm -qa)。安装的包名字和版本信息会加入指定的列表文件。

同时,每一个包属主都会有自己的 HOME 目录,默认放置在 /usr/src 下。在 HOME 目录中将存放这个包的源代码归档,和所有的补丁(包括一些其它的文件,不仅仅是 patch)以及相关的"安装配置信息文件",其中包括包的信息,如用户/组名、版本号、源代码归档名、所有补丁和安装时执行的命令以及包在系统中建立的时间。

crablfs 脚本是一个批量处理脚本,可以说是一种 ALFS 的实现。它可以使用上述的列表文件,然后指定所有源代码归档和"安装配置信息文件"所在的目录,以实现自动化的处理;也可以指定一个与原系统的 HOME 前缀目录(/usr/src)相同的目录树,从而可以很方便的实现老系统到新系统的迁移 -- 这意味这你可以在任何情况下很快捷的建立你自己熟的个性化系统。

Linux 一个发行版本与另一个发行版本的根本区别,就在于包管理方式的不同。LFS 作为一个发行版本,实际上却并没有很完善的包管理系统,当然这也是 LFS 的灵活性所决定的,但作为日常使用,一个完善而简单的包管理系统是必要的。BLFS 文档中建议使用的就是 User Based Management,但只在 LFS hints 中提出了基本原理,这个原理极有创意,但作者提供的脚本并不是很完善,并没有形成一个自洽的系统,所以在下不惜现丑,先作出一个,希望起到抛砖引玉的作用。

(2) 安装 crablfs
为了保证 crablfs 自身也置于包管理控制之下,可以使用如下方法进行安装:
# export PYTHONPATH=/opt/lib/python2.4/site-packages
// sys.path 不受 PYTHONPATH 的影响
# tar xfz pexpect-2.1.tar.gz
# cd pexpect-2.1
# python setup.py install --prefix=/opt
# cd ..

# tar xfz crablfs-0.1.tar.gz
# cd crablfs-0.1
# vi userpack.dirs
// 定义所有的关键目录
// 这些目录将属于 install 组,并拥有 g+s,o+t 权限
/usr/local/
/usr/local/bin/
/usr/local/doc/
/usr/local/etc/
/usr/local/include/
/usr/local/info/
/usr/local/lib/
/usr/local/libexec/
/usr/local/man/
/usr/local/man/man1/
/usr/local/man/man2/
/usr/local/man/man3/
/usr/local/man/man4/
/usr/local/man/man5/
/usr/local/man/man6/
/usr/local/man/man7/
/usr/local/man/man8/
/usr/local/sbin/
/usr/local/share/
/usr/local/share/doc/
/usr/local/share/info/
/usr/local/share/locale/
/usr/local/share/man/
/usr/local/share/man/man1/
/usr/local/share/man/man2/
/usr/local/share/man/man3/
/usr/local/share/man/man4/
/usr/local/share/man/man5/
/usr/local/share/man/man6/
/usr/local/share/man/man7/
/usr/local/share/man/man8/
/usr/local/share/misc/
/usr/local/share/terminfo/
/usr/local/share/zoneinfo/
/usr/lib/python2.4/site-packages/
/usr/lib64/python2.4/site-packages/
// 请根据自己的需要调整
# python setup.py install --prefix=/opt
# cd ..

# /opt/bin/userpack init
# /opt/bin/userpack install -f /tmp/crablfs-0.1.tar.gz crablfs-0.1
crablfs> cmd tar xfz crablfs-0.1.tar.gz
crablfs> cmd cd crablfs-0.1
crablfs> cmd python setup.py install --install-scripts=/usr/local/bin
// 上面没有给出对 /usr/bin 进行初始化,所以重新指定
crablfs> cmd cd ..
crablfs> cmd rm -rf crablfs-0.1
crablfs> commit

# userpack install -f /tmp/pexpect-2.1.tar.gz pexpect-2.1
crablfs> cmd tar xfz pexpect-2.1.tar.gz
crablfs> cmd cd pexpect-2.1
crablfs> cmd python setup.py install
crablfs> cmd cd ..
crablfs> cmd rm -rf pexpect-2.1
crablfs> commit

# unset PYTHONPATH

(3) 包管理
包管理器:userpack

a. 安装一个包:
# userpack install $pkgname-$version

# userpack install rxvt-2.7.10
你可以使用自己的分类:
# userpack install meida.mplayer-1.0pre8
因为系统用户支持 "."

******
注意:
* 在 LFS 系统中并非如此,LFS 系统使用的 shadow 包只支持[-a-z0-9_]这些字符,所以我使用 了自己编写的 shadow 模块直接写 /etc/passwd, /etc/group, /etc/shadow 和 /etc/gshadow 这些文件。

* 目前分类的功能还不强,没有经过严格的测试。
******

在上面的情况下,会执行默认的动作,即认为源代码的包和补丁已经在 HOME 目录中了,即 /usr/src/$pkgname,否则应该在参数中指定:
# userpack install -f /mnt/file/packages/rxvt-2.7.10.tar.gz rxvt-2.7.10
以将相应的包拷贝到 HOME 目录下。

这会启动一个交互式的命令行界面,这个命令行比较简单,只有为数不多的几个命令。实际上有点类似于一个 shell,在这里执行安装时所需要的命令,这可以非常灵活,根据不同的软件使用相应的命令即可。只是在命令之前要增加"cmd"标识,这样包管理器会记录相应的安装命令到其 HOME 目录下的 .config(安装配置信息文件)中。
crablfs> cmd tar xfz rxvt-2.7.10.tar.gz
crablfs> cmd cd rxvt-2.7.10
crablfs> cmd ./configure
crablfs> cmd make
crablfs> cmd make install
crablfs> cmd cd ..
crablfs> cmd rm -rf rxvt-2.7.10
crablfs> commit
需要说明的是,这时候你的当前用户是这个包用户,工作目录是这个包用户的主目录,所以会有权限限制(这也正是包用户系统的目的所在)。

在敲打命令时,你可以用 list 查看你键入的所有命令:
crablfs> list
0, tar xfz rxvt-2.7.10.tar.gz
1, cd rxvt-2.7.10
2, ./configure
3, make
4, make install

使用 rollback 可以清除所有的命令;如果只是想删除某个多余或错误的命令,使用 del N 即可 。

最后必须 commit,这样所有的命令会记录到 .config 中,这个包的 $pkgname-$version 也会记 录到列表文件中(默认 /usr/src/packages.list),以表明这个包被安装了。如果运行命令失败而导致无法安装,如果不能排除错误,则以 quit 退出,这样相应的内容不会被记录。但通常归档、补丁等会被复制到 HOME 中。

如果带有其它补丁文件等:
# userpack install -f /mnt/file/packages/MPlayer-1.0per8.tar.bz2 \
> -p /mnt/file/packages/all-20060611.tar.bz2 \
> -p /mnt/file/packages/Blue-1.6.tar.bz2 \
> mplayer-1.0pre8
// all 是 mplayer 的 codecs,Blue 是 GUI Skin。

如果已经拥有以前安装包时遗留的配置信息文件,则可以用 -a|--auto 执行非交互式的自动安装
# userpack install -a rxvt-2.7.10
这时意味着 $HOME/.config 已经存在,否则指定之(-c|--profile):
# userpack install -ac /mnt/packages/confiles/rxvt-2.7.10 rxvt-2.7.10

如果你已经将 /usr/src 刻录到了光盘上的 sources 目录,你也可以执行如下的自动安装:
# userpack install -as /mnt/dvdrom/sources rxvt-2.7.10

另外,安装源可以在网络上,如 ftp 或 ssh:
ssh://localhost/sources/mlterm-2.9.3.tar.gz
scp://localhost/sources/mlterm-2.9.3.tar.gz
上面两个是一个意思
ftp://localhost/pub/sources/mlterm-2.9.3.tar.gz
即:
# userpack install -f ftp://192.168.0.1/pub/sources/mlterm-2.9.3.tar.gz mlterm-2.9.3

******
如果没有指定 $pkgname-$version,则可以从包名(mlterm-2.9.3.tar.gz)解析出来。
******

如果一个包压缩档有问题,因为安装时会自动拷贝到 HOME,且下一次包管理器会先检查 HOME 中 是否有此压缩包,如果已存在则默认不会拷贝新的,所以需要参数指定强制拷贝(-C|--copy-force):
# userpack install -Caf libesmtp-1.0.3r1.tar.bz2

关于用户和组 ID,包管理器有自己的设定范围,默认值是从 1000 到 20000,可以在 /etc/login.defs 中定义:
PACK_UID_MIN 10000
PACK_UID_MAX 25000
PACK_GID_MIN 10000
PACK_GID_MAX 25000

b. 卸载:
# userpack remove rxvt-2.7.10

c. 列出所有受包报管理器控制的包:
# userpack packs
rxvt-2.7.10
mplayer-1.0pre8
crablfs-0.1
pexpect-2.1
mlterm-2.9.3

d. 查询一个文件属于那个包:
# userpack owner /usr/local/bin/mlterm
mlterm-2.9.3

e. 列出一个包拥有那些文件:
# userpack files pexpect-2.1
/usr/lib/python2.4/site-packages/fdpexpect.py
/usr/lib/python2.4/site-packages/pxssh.py
/usr/lib/python2.4/site-packages/pexpect.py
/usr/lib/python2.4/site-packages/pxssh.pyc
/usr/lib/python2.4/site-packages/pexpect.pyc
/usr/lib/python2.4/site-packages/fdpexpect.pyc

(4) 关于包及其版本的命名约定
即传递给包管理器的参数,形式为 $pkgname-$version,其 python re 的模式匹配为:
'^(?P(\w+-{0,1})+)(-(?P(\d[\w]*[-\.])*\d[\w]*))'

即 $pkgname 部分为 part1[-part2][-part3][...],每个 part 可以是[-a-zA-Z0-9_],对开头字母没有限制,但建议使用字母开头;当遇到 -[0-9][a-zA-Z0-9_]\. 这样的模式时(-后跟这一个数字开头的串,在串后跟着一个'.'号的情况),则将其作为 $version 部分,$version 部分也可以包含其他'-'号,但当有遇到非数字开头的部分时则结束 $version 部分,后面的内容忽略。

(5) ALFS
前面已经讲到,所有的命令都会记录到每个包 HOME 下的 .config 中,并且可以利用这些命令进行自动安装操作,所有可以使用一个批量处理脚本,根据在安装时生成的 packages.list 一次性安装所有的包。在这个设想上,我们可以建立一种 ALFS 的实现。

这个 ALFS 的实现与传统的 nALFS 和 jhalfs 的目标有所扩展,可以面向更个性化的系统,比如 :我首先在一台主机上使用 step by step 的方法建立了一个 LFS 系统,那么下一次我可以利用这次生成的所有安装配置信息文件自动生成我需要的软件环境,与原来的系统保持一致;这意味着我可以实现多次的迁移,同时别人也可以利用我的安装配置信息文件为基础建立他自己的个性化系统。最终的目标当然还要实现跨平台的操作。

这个批量处理的脚本就是 crablfs 了。

******
目前只在 LFS 已经建立的情况下,对使用 crablfs 建立 BLFS 的情况进行了测试,因此还不能将 LFS 基本系统的所有包置于包管理系统控制之下。
******

首先需要通过工具链技术将 python 和 crablfs 都置于报管理控制之下:
# cd /blfs-sources
# tar xfj python-2.4.1.tar.bz2
# cd python-2.4.1
# patch -Np1 -i ../python-2.4.1-gdbm-1.patch
# ./configure --prefix=/opt --enable-shared
# make && make install
# cd ..
# rm -rf python-2.4.1
// 最好删除,否则可能会有问题

# export PYTHONPATH=/opt/lib/python2.4/site-packages
# tar xfz pexpect-2.1.tar.gz
# cd pexpect-2.1
# /opt/bin/python setup.py install --prefix=/opt
# cd ..
# rm -rf pexpect-2.1

# tar xfz crablfs-0.1.tar.gz
# cd crablfs-0.1
# cp userpack.dirs.blfs userpack.dirs
// 请根据系统具体情况调整你的目录列表
// 如果以后遇到目录权限的问题,只需要调整 /etc/userpack.dirs
// 然后运行 userpack init 即可
# /opt/bin/python setup.py install --prefix=/opt

# /opt/bin/userpack init
# /opt/bin/userpack install -f python-2.4.1.tar.bz2 -p
# python-2.4.1-gdbm-1.patch python-2.4.1
crablfs> cmd tar xfj python-2.4.1.tar.bz2
crablfs> cmd cd python-2.4.1
crablfs> cmd ./configure --prefix=/usr --enable-shared
crablfs> cmd make
crablfs> cmd make install
crablfs> cmd cd ..
crablfs> cmd rm -rf python-2.4.1
crablfs> commit

# /opt/bin/userpack install -f crablfs-0.1.tar.gz crablfs-0.1
crablfs> cmd tar xfz crablfs-0.1.tar.gz
crablfs> cmd cd crablfs-0.1
crablfs> cmd python setup.py install --install-scripts=/usr/local/bin
crablfs> cmd cd ..
crablfs> cmd rm -rf crablfs-0.1
crablfs> commit

# userpack install -f pexpect-2.1.tar.gz pexpect-2.1
crablfs> cmd tar xfz pexpect-2.1.tar.gz
crablfs> cmd cd pexpect-2.1
crablfs> cmd python setup.py install
crablfs> cmd cd ..
crablfs> cmd rm -rf pexpect-2.1
crablfs> commit

# unset PYTHONPATH
# userpack packs
python-2.4.1
crablfs-0.1
pexpect-2.1

按照相同的方法,使用 userpack 又安装了若干包:
# userpack packs
# cat /usr/src/packages.list
python-2.4.1
crablfs-0.1
pexpect-2.1
openssl-0.9.7g
cracklib-2.8.3
Linux-PAM-0.80
iptables-1.3.3
gnupg-1.4.1
pcre-6.1
libxml-1.8.17
libxml2-2.6.20
libxslt-1.1.14
gdbm-1.8.3
pkg-config-0.19
glib2-2.6.4
expat-1.95.8
libesmtp-1.0.3r1
lzo-2.01
libusb-0.1.10a
libjpeg-6b
libpng-1.2.8
which-2.16

# mv /usr/src/packages.list /blfs-sources/
# config.copy /usr/src
// 在当前目录下生成一个 profiles 目录,里面会有所有 /usr/src
// 下安装配置信息文件的拷贝
// 这个 shell 脚本不会安装,可以在包的压缩档中找到
# mv profiles /blfs-sources
# crablfs -t alfs \
> -C /blfs-sources/profiles/ \
> -F /blfs-sources/ \
> /blfs-sources/packages.list

另外,如果你之前安装过一个系统,并且将 /usr/src 全部拷贝下来了 -- 比如刻录到了 DVD-R 上,你也可以这样做:
# crablfs -t crablfs[/default] \
> -s /mnt/dvdrom/sources/ \
> /mnt/dvdrom/sources/packages.list

crablfs 会记录当前成功安装的包的名字到 /var/log/crablfs/.mark 中。因为各个包之间常常有前后依赖关系,所以如果某一步出错,则 crablfs 进程会终止,后续包不会安装。待排除错误后,重新运行原来的命令,则可以从断点开始执行。

所以如果 .mark 指向 packages.list 中的最后一个包,则 crablfs 什么都不会做。请注意作出 调整。

另外,crablfs 会先校验 packages.list 文件,如果其中包含非法的包名等会使 crablfs 终止而不进行任何安装操作;如果 .mark 中的包名不在 packages.list 中,会得到 "x not in list" 报错,你可以作出相应的调整,通常删除 .mark 即可。

从目前的情况看,还没有遇到太多权限问题,且都可以通过调整安装时的参数来避免。可能需要对 chown, chmod, chgrp, install 这些程序编写包裹器,但目前还没有看到必须之处。这个再看以后测试的情况来定吧。

(6) 下一步的计划
* BLFS 中 crablfs 工具链建立脚本(shell script)
* 增加 upgrade 部分
* 增加 gettext 国际化支持
* 每个包完整的描述信息
* PyUnit 单元测试
* 增强 cmdline 的命令补全功能
* 增加 cmdline 读取 .config 历史命令和修改指定命令的功能
* 最好有一个命令编辑器
* 对 packages.list 增加文件锁
* 将 .config 改为 $pkgname-$version 形式
* 对 LFS 基本系统实现控制
* 增强跨平台支持:
增加环境变量和 cmdline 变量支持以适应不同的 $ARCH 和 $LOCALE
* 增加对多个安装源的支持
* 增强包分类机制
* 增加完整性校验
* 增强配置解析: Plain Tree & XML
* 考虑依赖性检查问题
* 更完善的文档:内部文档、设计文档和手册

**************************************************************************
这是我第一次发布自己的作品,由于水平有限,其中还有许多不尽人意之处,我会在以后努力的改进和完善,同时希望大家不吝赐教,尽可能给予我反馈意见。如愿意,您也可以在 GPL 2.0 协议及其后续版本下随意改进代码。

希望这个程序对大家有用,也希望由此可以做出一个真正通用型的、方便的 LFS 发行版。

谢谢!
**************************************************************************

crablfs Copyright (c) 2006, 周鹏(chowroc.z@gmail.com)

This file is part of crablfs.

crablfs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

crablfs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

星期二, 十月 17, 2006

crablfs: BLFS manully

# cd blfs-sources
# tar xfj python-2.4.1.tar.bz2
# cd python-2.4.1
# patch -Np1 -i ../python-2.4.1-gdbm-1.patch
# ./configure --prefix=/opt --enable-shared
# make && make install
# cd ..
# rm -rf python-2.4.1

# export PYTHONPATH=/opt/lib/python2.4/site-packages
# tar xfz pexpect-2.1.tar.gz
# cd pexpect-2.1
# /opt/bin/python setup.py install --prefix=/opt
# cd ..
# rm -rf pexpect-2.1

# tar xfz crablfs-0.1.tar.gz
# cd crablfs-0.1
# cp userpack.dirs.blfs userpack.dirs
/etc/
/lib/
/usr/
/usr/bin/
/usr/doc/
/usr/etc/
/usr/include/
/usr/info/
/usr/lib/
/usr/lib/pkgconfig/
/usr/libexec/
/usr/man/
/usr/man/man1/
/usr/man/man2/
/usr/man/man3/
/usr/man/man4/
/usr/man/man5/
/usr/man/man6/
/usr/man/man7/
/usr/man/man8/
/usr/sbin/
/usr/share/
/usr/share/doc/
/usr/share/info/
/usr/share/
/usr/share/man/
/usr/share/man/man1/
/usr/share/man/man2/
/usr/share/man/man3/
/usr/share/man/man4/
/usr/share/man/man5/
/usr/share/man/man6/
/usr/share/man/man7/
/usr/share/man/man8/
/usr/share/misc/
/usr/share/terminfo/
/usr/share/zoneinfo/
/usr/local/
/usr/local/bin/
/usr/local/doc/
/usr/local/etc/
/usr/local/include/
/usr/local/info/
/usr/local/lib/
/usr/local/libexec/
/usr/local/man/
/usr/local/man/man1/
/usr/local/man/man2/
/usr/local/man/man3/
/usr/local/man/man4/
/usr/local/man/man5/
/usr/local/man/man6/
/usr/local/man/man7/
/usr/local/man/man8/
/usr/local/sbin/
/usr/local/share/
/usr/local/share/doc/
/usr/local/share/info/
/usr/local/share/locale/
/usr/local/share/man/
/usr/local/share/man/man1/
/usr/local/share/man/man2/
/usr/local/share/man/man3/
/usr/local/share/man/man4/
/usr/local/share/man/man5/
/usr/local/share/man/man6/
/usr/local/share/man/man7/
/usr/local/share/man/man8/
/usr/local/share/misc/
/usr/local/share/terminfo/
/usr/local/share/zoneinfo/
/usr/lib/python2.4/
/usr/lib/python2.4/site-packages/

# /opt/bin/python setup.py install --prefix=/opt

# /opt/bin/userpack init
# /opt/bin/userpack install -f python-2.4.1.tar.bz2 -p python-2.4.1-gdbm-1.patch python-2.4.1
crablfs> cmd tar xfj python-2.4.1.tar.bz2
crablfs> cmd cd python-2.4.1
crablfs> cmd ./configure --prefix=/usr --enable-shared
crablfs> cmd make
crablfs> cmd make install
crablfs> cmd cd ..
crablfs> cmd rm -rf python-2.4.1
crablfs> commit

# /opt/bin/userpack install -f crablfs-0.1.tar.gz crablfs-0.1
crablfs> cmd tar xfz crablfs-0.1.tar.gz
crablfs> cmd cd crablfs-0.1
crablfs> cmd python setup.py install --install-scripts=/usr/local/bin
crablfs> cmd cd ..
crablfs> cmd rm -rf crablfs-0.1
crablfs> commit

# userpack install -f pexpect-2.1.tar.gz pexpect-2.1
crablfs> cmd tar xfz pexpect-2.1.tar.gz
crablfs> cmd cd pexpect-2.1
crablfs> cmd python setup.py install
crablfs> cmd cd ..
crablfs> cmd rm -rf pexpect-2.1
crablfs> commit

find 目录排除及 -o, -a 参数

find /usr -path /usr/src -prune -o -user python -a -print
列出 /usr 下除开 /usr/src 并且属于用户 python 的所有文件和目录

星期一, 十月 16, 2006

ALFS: jhalfs-1.0 CLFS

要在非 x86 平台上编译 LFS,需要使用 CLFS。

jhalfs 已经被用了替代 nALFS,它直接从 LFS BOOK 的 xml 文档中提取命令。目前 livecd 中使用的是 jhalfs-1.0,因为 jhalfs-2.0 似乎还只支持 x86 平台。

首先从 livecd 中拷贝 jhalfs-1.0 和源代码包:
# useradd lfs
# mount -t iso9660 /mnt/file/iso/lfslivecd-x86-6.2-3.iso -o loop /mnt/cdrom
# mount -t ext2 /mnt/cdrom/root.ext2 -o loop /mnt/lfslivecd
# su - lfs
$ cp /mnt/lfslivecd/jhalfs-1.0 .
$ cp /mnt/lfslivecd/lfs-sources .
$ exit

调整:
# cd jhalfs-1.0
# vi common/config
# declare -r SVN="svn://svn.linuxfromscratch.org"
declare -r SVN="http://svn.cross-lfs.org/svn/repos"

这是因为 CLFS 的站点已经变化,所以需要修改相应的 SVN 指向,否则无法得到需要的 CLFS BOOK。参见:
http://linux-phreak.biz/pipermail/clfs-dev/2006-September/000332.html

查看 Books Supported by jhalfs
可以看到当前 stable 版本是 1.0.0,于是可以运行如下命令开始安装:
# export SRC_ARCHIVE=~lfs/lfs-sources
# export CLFS=/mnt/lfs
// 一定要有这个环境变量
# vi CLFS/config
TIMEZONE=Asia/Shanghai
# ./clfs -B 1.0.0 -D /mnt/lfs -K ~lfs/skeleton/linux-kernel-config -F ~lfs/skeleton/etc/fstab -G -T 0 -M
-B: 指定 CLFS BOOK version
-D: 目标目录,mount /dev/sda7 /mnt/lfs
-K: 内核 .config 文件
-F: /etc/fstab
-G: 设定如果包不在 ~lfs/lfs-sources,则下载之
-T:
-M: Make,即前面所有步骤会生成 Makefiles,执行之。否则可以在 /mnt/lfs/jhalfs 下运行 make

最后,为方便和简单起见,将主系统中的 kernel, initrd, /etc/modprobe.conf, /lib/modules/`uname -r` 全部拷贝到 $CLFS,并使用主系统的 grub 启动。

troubleshooting:
使用 jhalfs-1.0 安装 CLFS 于 x86_64 pure64,从 $CLFS/jhalfs/logs 来看,没有报什么错误,于是我将原来系统的 kernel, initrd, /etc/modprobe.conf, /lib/modules/$version 拷贝到 $CLFS 相应目录(我以前也这么干过,是可以的),在 grub.conf 中增加相应的记录:
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda8 39G 20G 17G 54% /
tmpfs 217M 0 217M 0% /dev/shm
/dev/sda7 40G 2.3G 36G 7% /mnt/lfs
/dev 217M 112K 217M 1% /mnt/lfs/dev
tmpfs 217M 112K 217M 1% /mnt/lfs/dev/shm
devpts 217M 112K 217M 1% /mnt/lfs/dev/pts
/dev/sda9 40G 16G 22G 42% /mnt/file

# vi $CLFS/etc/fstab
# filesystem    mount-point     fs-type options         dump fsck-order
/dev/hda7 / ext3 defaults 1 1
# /dev/hda6 swap swap pri=1 0 0
/dev/hda6 swap swap defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
devpts /dev/pts devpts gid=4,mode=620 0 0
shm /dev/shm tmpfs defaults 0 0
# usbfs /proc/bus/usb usbfs devgid=14,devmode=0660 0 0


重启后启动 LFS,报错如下:
Checking file system ...
/dev/hda7
The superblock could not be read or does not describe a correct ext2 filesystem. If the device is valid and it really contains an ext2 filesystem(and not swap or ufs or something else), then the superblock is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193

FAILURE:
File system error were encountered that could not be fixed automatically...

Press Enter and the system wlll halt

重启到主系统,运行如下命令:
# vi CHROOT.sh
#!/bin/sh
# mount /dev/sda7 /mnt/lfs -o remount
export CLFS=/mnt/lfs
mount -vt proc proc ${CLFS}/proc
mount -vt sysfs sysfs ${CLFS}/sys
mount -v -o bind /dev ${CLFS}/dev
mount -f -vt tmpfs tmpfs ${CLFS}/dev/shm
mount -f -vt devpts -o gid=4,mode=620 devpts ${CLFS}/dev/pts
chroot "${CLFS}"

# vi LOGOUT.sh
#!/bin/sh
logout
export CLFS=/mnt/lfs
umount ${CLFS}/dev/pts
umount ${CLFS}/dev/shm
umount ${CLFS}/dev
umount ${CLFS}/proc
umount ${CLFS}/sys
umount ${CLFS}


# mount /dev/sda7 /mnt/lfs
# export CLFS=/mnt/lfs
# sh CHROOT.sh
// 上面三个命令每次从主系统进入 CLFS 都必须执行,在重启主系统时则需要执行
// sh LOGOUT.sh
# dumpe2fs /dev/sda7 | less
// 找到所有 superblocks 所在的块,选择一个,比如第二个
# fsck.ext3 -b 32768 /dev/sda7 -y
这样并无效果。

再次启动到 LFS,仔细观察输出,可以发现远在报错之前,就已经出现:
Loading jbd.ko module
Loading ext3.ko module
Trying to resume from /dev/sda6
No suspend signature on swap, not resuming.
Creating root device.
Mounting root filesystem.
kjournald starting. Commit internal 5 seconds
EXT3-fs: mounted filesystem with ordered data mode
setting up other filesystems
setting up new root fs
no fstab.sys, mounting internal defaults
Swithing to new root and running init

回到主系统,chroot 到 CLFS,将 fstab 拷贝为 fstab.sys,再次重启 LFS。

这次明确提示没有找到 /dev/hda7,无法 mount。再仔细看 fstab,原来是将 sda7 写成了 hda7!

DON'T PANIC!
DON'T PANIC!!
DON'T PANIC!!!

星期日, 十月 15, 2006

vim encoding

今天安装 ALFS 时不小心改动了主系统 glibc 的 locale(localedef),导致一些程序出问题,比如 xfce4 我开的 fcitx,不行了。刚才使用 vim 发现竟然出现了乱码,于是手工指定 vim 的 encoding:
$ vi ~/.vimrc
set encoding=cp936
用 utf-8 却不行。

另资料:
支持多种编码的 vimrc
set tabstop=4
set number
" set encoding=cp936

" multi-encoding setting
if has("multi_byte")
"set bomb
set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1
" CJK environment detection and corresponding setting
if v:lang =~ "^zh_CN"
" Use cp936 to support GBK, euc-cn == gb2312
set encoding=cp936
set termencoding=cp936
set fileencoding=cp936
elseif v:lang =~ "^zh_TW"
" cp950, big5 or euc-tw
" Are they equal to each other?
set encoding=big5
set termencoding=big5
set fileencoding=big5
elseif v:lang =~ "^ko"
" Copied from someone's dotfile, untested
set encoding=euc-kr
set termencoding=euc-kr
set fileencoding=euc-kr
elseif v:lang =~ "^ja_JP"
" Copied from someone's dotfile, untested
set encoding=euc-jp
set termencoding=euc-jp
set fileencoding=euc-jp
endif
" Detect UTF-8 locale, and replace CJK setting if needed
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
endif
else
echoerr "Sorry, this version of (g)vim was not compiled with multi_byte"
endif

注意 vimrc 的注释!

ALFS: nALFS

首先从 LFS 的官方网站下载 nALFS 以及相应的 profile:
nALFS-1.2.6.tar.bz2
profile-LFS-6.1.1-1.tar.bz2

安装 nALFS:
# userpack install -f ./nALFS-1.2.6.tar.bz2 nALFS-1.2.6
crablfs> cmd tar xfj nALFS-1.2.6.tar.bz2
crablfs> cmd cd nALFS-1.2.6
crablfs> cmd make
crablfs> cmd make install
crablfs> cmd cd ..
crablfs> cmd rm -rf nALFS-1.2.6
crablfs> commit

我的 Fedora Core 5 在安装 nALFS 时遇到 libxml2 的问题,提示找不到 libxml2.la,于是用 libxml2 的源代码进行了安装。

然后解压 profile-LFS-6.1.1-1.tar.bz2,进入 LFS-6.1.1-1/config,编辑 general.ent,对一些变量进行必要的调整,如构建目录(/mnt/lfs)、源代码归档目录、时区等。

然后:
# cd LFS-6.1.1-1/
# cp skeleton -R chowroc
# cd chowroc
# vi boot/grub/menu.lst
# vi etc/fstab
# vi etc/hosts
# cp /etc/modprobe.conf etc/
# vi etc/profile
# vi etc/sysconfig/network-devices/ifconfig.eth0/ipv4
# vi etc/vimrc
# cp /boot/config-2.6.17.8 linux-kernel-config
# cp /boot/grub/grub.conf chowroc/boot/grub/menu.lst
# vi chowroc/boot/grub/menu.lst
// 作出自己需要的调整

# vi chapter06/glibc.xml
"<"execute command="localedef -i en_US -f ISO-8859-1 en_US" /">"
"<"execute command="localedef -i zh_CN -f GB18030 zh_CN" /">"
"<"execute command="localedef -i zh_CN -f GBK zh_CN" /">"
"<"execute command="localedef -i zh_CN -f UTF-8 zh_CN" /">"
"<"execute command="localedef -i zh_TW -f EUC-TW zh_TW" /">"
"<"execute command="localedef -i zh_TW -f UTF-8 zh_TW" /">"
"<"execute command="localedef -i de_DE -f ISO-8859-1 de_DE" /">"
"<"execute command="localedef -i de_DE@euro -f ISO-8859-15 de_DE@euro" /">"
"<"execute command="localedef -i en_HK -f ISO-8859-1 en_HK" /">"
"<"execute command="localedef -i fr_FR -f ISO-8859-1 fr_FR" /">"
"<"execute command="localedef -i fr_FR@euro -f ISO-8859-15 fr_FR@euro" /">"
"<"execute command="localedef -i ja_JP -f EUC-JP ja_JP" /">"

调整 glibc 的 locale(由于 blog 上无法正确显示 "<"">" 部分的内容──用 "<"pre">"标签也不行,我只好加了 "" 号。)

然后运行:
# useradd lfs
# chown lfs.lfs /mnt/lfs -R
# ./runit.sh chowroc
// runit.sh 会调用 nALFS
进入交互式界面后,移动到 LFS.xml 这个顶级树,按's'键开始运行,接着按'c'键运行所有的子分支。

但是 nALFS 似乎不能自动下载缺少的包,比如现在的官方版本是 LFS 6.2,而 nALFS profile 是 6.1.1-1,结果有些包的版本更新了,就不行了,不知道怎样设置;另外不知道对于 x86_64 等平台,nALFS 是怎样解决的?

而且目前官方的 livecd 似乎已经用 jhalfs-1.0 代替了 nALFS。

星期五, 十月 13, 2006

Linux 外置 USB 光驱及刻录机的使用

USB 光驱与其它的 USB 存储设备使用类似,只不过 USB 光驱会被识别为 SCSI 光驱,如 /dev/sr0或 /dev/scd0 等设备,它需要 sr_mod.o(SCSI CD-ROM 设备)驱动模块。
# modprobe sr_mod
//如果sr_mod模块没有编译,请重新编译内核,在SCSI部分
# mount -t iso9660 /dev/scd0 /mnt/cdrom

# mkisofs -Jv -V examplecd -o example.iso /root/
OR
# mkisofs -r /root >/tmp/example.iso
# mount -t iso9660 -o loop example.iso /mnt/iso
# modprobe loop
// 然后可以在 /mnt/iso 中调整内容
# umount /mnt/iso

# cdrecord -scanbus
// 查看SCSI总线中刻录机的配置信息:
Cdrecord-Clone 2.01.01a03-dvd (x86_64-unknown-linux-gnu) Copyright (C) 1995-2005 J枚rg Schilling
NOTE: This version contains the OSS DVD extensions for cdrtools and thus may
have bugs related to DVD issues that are not present in the original
cdrtools. Please send bug reports or support requests to
http://bugzilla.redhat.com/bugzilla The original cdrtools author should
not be bothered with problems in this version.
Linux sg driver version: 3.5.27
Using libscg version 'schily-0.8'.
cdrecord: Warning: using inofficial libscg transport code version (schily - Red Hat-scsi-linux-sg.c-1.85-RH '@(#)scsi-linux-sg.c 1.85 05/05/16 Copyright 1997 J. Schilling').
scsibus2:
2,0,0 200) 'DVDRW ' 'IDE H16X ' 'B02C' Removable CD-ROM
2,1,0 201) *
2,2,0 202) *
2,3,0 203) *
2,4,0 204) *
2,5,0 205) *
2,6,0 206) *
2,7,0 207) *
从上面可以看到目前光驱在SCSI总线的参数,然后根据参数输入下面的命令刻录光盘:
# cdrecord -v speed=24 dev=0,0,0 -data example.iso
OR
# cdrecord -v -eject speed=24 dev=0,0,0 example.iso
"dev="后加上刚才显示的刻录机的SCSI参数。

外置DVD,既可以使用 cdrecord,也使用dvdrecord命令,其用法与cdrecord相同。dvdrecord 实际上是属于 cdrecord 包的,并且实际上会执行 cdrecord。

目前更好的方法是使用 dvd+rw-tools 包的 growisofs:
# dvd+rw-mediainfo /dev/dvd
# mkisofs -r -V chowroc-20061014.iso -o /opt/chowroc-20061014.iso /opt/loop
# growisofs -Z /dev/dvd=/opt/chowroc-20061014.iso

# ls /dev/dvd -l
lrwxrwxrwx 1 root root 4 10月 14 10:11 /dev/dvd -> scd0
# ls /dev/dvd-hda -l
lrwxrwxrwx 1 root root 3 10月 14 08:58 /dev/dvd-hda -> hda
// 系统中另外一个 ASUS DVD-ROM

参考:http://uplinux.com/www/sys/02/487_2.shtml
http://www.cyberciti.biz/tips/howto-linux-write-burn-data-to-dvd-or-dvdrw.html

问题:
# mkisofs -vJ -V chowroc-20061014 -o /opt/chowroc-20061014.iso /opt/loop/
...
Using BBS_P001.MYD;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_privmsgs.MYD (bbs_privmsgs_text.MYD)
Using BBS_F000.MYI;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_forum_prune.MYI (bbs_forums.MYI)
Using BBS_F000.FRM;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_forums.frm (bbs_forum_prune.frm)
Using BBS_T001.MYD;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_themes.MYD (bbs_themes_name.MYD)
Using BBS_C000.MYI;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_config.MYI (bbs_confirm.MYI)
Using BBS_S001.MYI;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_search_results.MYI (bbs_search_wordlist.MYI)
Using BBS_T001.FRM;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_themes_name.frm (bbs_themes.frm)
Using BBS_T001.MYI;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_themes_name.MYI (bbs_themes.MYI)
Using BBS_V001.MYD;1 for /opt/loop/FILE/__temp__/var/lib/mysql/BBS/bbs_vote_desc.MYD (bbs_vote_results.MYD)
mkisofs: Error: /opt/loop/FILE/work/sum/__collections/Re Linux真的比Windows更安全吗?-无知仅博一看而已。 - China Linux Forum.files and /opt/loop/FILE/work/sum/__collections/Re Linux真的比Windows更安全吗?-无知仅博一看而已。 - China Linux Forum.htm have the same Joliet name
mkisofs: Joliet tree sort failed.

第二次删除上面文件后:
Incorrectly encoded string ((72 X 88).bmp) encountered.
Possibly creating an invalid Joliet extension. Aborting.

且 Using 的名字也大有问题,在 Windows 下根本无法正确显示。

星期日, 十月 08, 2006

python: Exceptions try/except/finally syntax?

注意:没有 try/except/finally 语法
>>> try:
... raise "trying"
... except "trying":
... print "catch it"
... finally:
File "", line 5
finally:
^
SyntaxError: invalid syntax
只能如下:
>>> try:
... try:
... raise "trying"
... finally:
... print "finally"
... except "trying":
... print "catch it"
...
finally
catch it

星期四, 十月 05, 2006

crablfs 安装自洽

# export PYTHONPATH=/opt/lib/python2.4/site-packages
// sys.path 不受 PYTHONPATH 的影响

# tar xfz pexpect-2.1.tar.gz
# cd pexpect-2.1
# python setup.py install --prefix=/opt

# tar xfz crablfs-0.1.tar.gz
# cd crablfs-0.1
# vi userpack.dirs
// 定义所有的关键目录
// 这些目录将属于 install 组,并拥有 g+s,o+t 权限
/usr/local/
/usr/local/bin/
/usr/local/doc/
/usr/local/etc/
/usr/local/include/
/usr/local/info/
/usr/local/lib/
/usr/local/libexec/
/usr/local/man/
/usr/local/man/man1/
/usr/local/man/man2/
/usr/local/man/man3/
/usr/local/man/man4/
/usr/local/man/man5/
/usr/local/man/man6/
/usr/local/man/man7/
/usr/local/man/man8/
/usr/local/sbin/
/usr/local/share/
/usr/local/share/doc/
/usr/local/share/info/
/usr/local/share/locale/
/usr/local/share/man/
/usr/local/share/man/man1/
/usr/local/share/man/man2/
/usr/local/share/man/man3/
/usr/local/share/man/man4/
/usr/local/share/man/man5/
/usr/local/share/man/man6/
/usr/local/share/man/man7/
/usr/local/share/man/man8/
/usr/local/share/misc/
/usr/local/share/terminfo/
/usr/local/share/zoneinfo/
/usr/lib/python2.4/site-packages/
/usr/lib64/python2.4/site-packages/

// 请根据自己的需要调整
# python setup.py install --prefix=/opt

# /opt/bin/userpack init
# /opt/bin/userpack install -f /tmp/crablfs-0.1.tar.gz crablfs-0.1
crablfs> cmd tar xfz crablfs-0.1.tar.gz
crablfs> cmd cd crablfs-0.1
crablfs> cmd python setup.py install --install-scripts=/usr/local/bin
// 上面没有给出对 /usr/local/bin 进行初始化,所以重新指定
crablfs> cmd cd ..
crablfs> cmd rm -rf crablfs-0.1
crablfs> commit

# userpack install -f /tmp/pexpect-2.1.tar.gz pexpect-2.1
crablfs> cmd tar xfz pexpect-2.1.tar.gz
crablfs> cmd cd pexpect-2.1
crablfs> cmd python setup.py install
crablfs> cmd cd ..
crablfs> cmd rm -rf pexpect-2.1
crablfs> commit

crablfs 部分整体需求

粗体为未完成部分:
  1. 2006-10-02: python distutils
  2. 2006-10-01: auto commands execution
  3. 2006-10-08: crablfs -t ALFS/CRABLFS
  4. wrappers
  5. 2006-10-04: 各个 actions 的原子操作:pkglist -- find_list()
  6. 2006-10-05: 配置信息文件 confile 指定
  7. 改善 Exceptions Raiser
  8. 2006-10-05: __get_copy(fsrc, rename)
  9. 2006-10-04: package name regular expression parse
  10. 2006-10-20: pkgname-version 放到 __main__ 中解
  11. 2006-10-05: -d|--home-prefix 或 -o homepre='...'?
  12. 2006-10-05: homesrc -- archive 指定
  13. 2006-10-05: 实现对自己的安装(安装自洽)

subversion 修改 log

[roc@chowroc trunk]$ svnadmin setlog /mnt/file/data/ea/ -r 25 ~/tmp
svn: Repository has not been enabled to accept revision propchanges;
ask the administrator to create a pre-revprop-change hook
[roc@chowroc trunk]$ svnadmin setlog /mnt/file/data/ea/ -r 25 ~/tmp --bypass-hooks

星期三, 十月 04, 2006

在 blogger 中张贴代码并保持缩进(相当于 verbatim)

google blogger 的编辑器中没有提供诸如 [CODE] 这样的环境,张贴的 python 等代码都没有缩进效果了。

解决办法是使用 "Edit Html",在代码前后增加"<"pre">""<"/pre">"。

http://blog.oasisfeng.com/2006/09/20/avoid-char-conversion-by-wordpress/

python: raise a string for Exception?

>>> try:
... raise "a string"
... except str:
... print 'catch'
...
Traceback (most recent call last):
File "", line 2, in ?
a string

>>> try:
... raise "a string"
... except Exception, strerr:
... print "catch it"
...
Traceback (most recent call last):
File "", line 2, in ?
a string

>>> try:
... raise "a string"
... except "a string":
... print "catch it"
...
catch it

>>> ex = "a string"
>>> try:
... raise ex
... except ex, strerr:
... print "catch it: %s" % strerr
...
catch it: None

星期一, 十月 02, 2006

python distutils

首先将目录解构调整为如下的形式
$ find | grep -v '\.svn'
.
./userpack
./MANIFEST
./setup.py
./wrappers
./wrappers/chown
./wrappers/chgrp
./wrappers/install
./wrappers/chmod
./userpack.dirs
./MANIFEST.in
./upm
./lib
./lib/__init__.py
./lib/cmdline.py
./lib/filetools.py
./lib/filetrans.py
./lib/plainconf.py
./README.txt

然后编辑 setup.py
$ cat setup.py
#!/usr/bin/python
# -*- encoding: utf-8 -*-

# Author: 周鹏
# Date: 2006-10-02
# Email: chowroc.z@gmail.com

from distutils.core import setup

setup(
name = 'crablfs',
version = '0.1',
author = '周鹏(chowroc)',
author_email = 'chowroc.z@gmail.com',
description = 'User Based Package Management System',
# classifier = [
# 'Environment :: Console',
# 'Operating System :: POSIX',
# 'Programming Language :: Python',
# 'Topic :: Communications :: Email',
# ],
package_dir = {'crablfs' : 'lib'},
packages = ['crablfs'],
scripts = ['userpack', 'upm'],
data_files = [('/etc', ['userpack.dirs'])],
)

这 里 package_dir 将源代码目录下的 lib 目录在安装时映射为 crablfs,安装时会拷贝到 /usr/lib/python-2.4/site-packages/crablfs 下,而 scripts 会将相应的 userpack 和 upm 拷贝到 /usr/bin 下。

制作源代码形式的包压缩档
$ python setup.py sdist

注 意有些文件不会被自动打包到压缩档中,如 lib/__init__.py(很奇怪,为什么?)和 userpack.dirs,需要在 MANIFEST.in 中指定,在 distribute 时会先生成 MANIFEST(其中象 README.txt 和 setup.py中指定的 scripts 和 packages 等会自动加入):
$ cat MANIFEST.in
recursive-include lib *.py
userpack.dirs
$ cat MANIFEST
README.txt
setup.py
upm
userpack
lib/__init__.py
lib/cmdline.py
lib/filetools.py
lib/filetrans.py
lib/plainconf.py

还可以用如下形式指定不同的压缩格式:
$ python setup.py sdist --formats=bztar,zip

最终生成 dist/crablfs-0.1.tar.gz。

安装
$ cp dist/crablfs-0.1.tar.gz /tmp
$ cd /tmp
$ tar xfz crablfs-0.1.tar.gz
$ cd crablfs-0.1
$ su -
# python setup.py install
或使用 paco 这样的包管理器:
# paco -lD "python setup.py install"