By marr, Last updated Nov 10, 2008
zc.buildout 簡稱為 buildout,它是讓程式員建置 Python 應用軟體開發環境的工具,包括 Zope 或 Plone 系統也都適用。本文件介紹 buildout 的使用技巧,協助讀者建立 Plone3 的開發環境。
只要管理一個稱為 buildout.cfg 的設定檔,就可以自動下載 egg 檔案,安裝並設定想要的 Plone 系統,不必再擔心軟體相依或版本混亂的問題,也可以做到同時管理多份測試環境的需求。
想要使用 buildout 來管理 Plone 軟體,需要確認下列的系統工具已準備好,如果手邊有 Linux distro 環境,很可能已安裝完畢:
shell + text editor
Linux 最常見的是 bash 和 vim,Windows 最常見的是 cmd.exe 和 notepad。
C compiler
Linux 最常見的是 gcc,Windows 可使用 mingw32,下載網址在 http://www.mingw.org/
subversion client
接下來,要確認幾項 Python 相關的工具軟體,或是,直接跳到 利用 Unified Installer 來起步 段落,這是完成事前準備的簡化方式之一:
Python 2.4
Plone 3.x 搭配的 Zope 2.10.x 版本還未支援 Python 2.5,但許多新的 Linux distro 預設已使用 Python 2.5 以上的版本,因此需要再額外安裝 Python 2.4.x 才行。
PIL (Python Imaging Library)
要配合上述 Python 2.4 的 library 位置來安裝。 下載網址在 http://www.pythonware.com/products/pil/
如果是 利用 Unified Installer 來起步 的話,那麼 PIL 已搞定。
setuptools
elementtree
ZopeSkel
在安裝 setuptools 後,可用 easy_install ZopeSkel 方式完成安裝。
執行 Plone 3.1.x Unified Installer 後,會自動提供 Python 2.4 和配套的軟體,這是完成 buildout 事前準備的簡化方法。拿 Plone 3.1.5.1 的 Unified Installer 為例,可執行 sudo ./install standalone 指令,會在 /opt/Plone-3.1 目錄裡安裝完整的 Plone 與 Python 軟體,可以在 /opt/Plone-3.1/Python-2.4 目錄找到 Python 環境,包括 setuptools (easy_install)、paster、PIL 等,而 elementtree 則出現在 /opt/Plone-3.1/buildout-cache/eggs 目錄裡。簡言之,上述的事前準備工作都完成了。
不過,最好再執行 easy_install -U ZopeSkel 確保 ZopeSkel 版本已更新,下列是操作範例:
$ sudo /opt/Plone-3.1/Python-2.4/bin/easy_install -U ZopeSkel Searching for ZopeSkel Reading http://pypi.python.org/simple/ZopeSkel/ Best match: ZopeSkel 2.10 Downloading http://pypi.python.org/packages/2.4/Z/ZopeSkel/ZopeSkel-2.10-py2.4.egg#md5=eb21233f11c8f20b1c8537423c3a216f Processing ZopeSkel-2.10-py2.4.egg creating /opt/Plone-3.1/Python-2.4/lib/python2.4/site-packages/ZopeSkel-2.10-py2.4.egg Extracting ZopeSkel-2.10-py2.4.egg to /opt/Plone-3.1/Python-2.4/lib/python2.4/site-packages Removing ZopeSkel 1.8 from easy-install.pth file Adding ZopeSkel 2.10 to easy-install.pth file Installed /opt/Plone-3.1/Python-2.4/lib/python2.4/site-packages/ZopeSkel-2.10-py2.4.egg Processing dependencies for ZopeSkel Finished processing dependencies for ZopeSkel
最後,執行 paster 程式來測試一切是否就緒,例如使用 create --list-templates 參數:
$ /opt/Plone-3.1/Python-2.4/bin/paster create --list-templates Available templates: archetype: A Plone project that uses Archetypes basic_namespace: A project with a namespace package basic_package: A basic setuptools-enabled package basic_zope: A Zope project kss_plugin: A KSS plugin template nested_namespace: A project with two nested namespaces. paste_deploy: A web application deployed through paste.deploy plone: A Plone project plone2.5_buildout: A buildout for Plone 2.5 projects plone2.5_theme: A Theme for Plone 2.5 plone2_theme: A Theme Product for Plone 2.1 & Plone 2.5 plone3_buildout: A buildout for Plone 3 projects plone3_portlet: A Plone 3 portlet plone3_theme: A Theme for Plone 3.0 plone_app: A Plone App project plone_hosting: Plone hosting: buildout with ZEO and any Plone version plone_pas: A Plone PAS project recipe: A recipe project for zc.buildout silva_buildout: A buildout for Silva projects
螢幕回應 paster 能建立的 template 清單,表示一切已經就緒。如果覺得上述輸入 /opt/Plone-3.1/Python-2.4/bin 太麻煩,可考慮設定 PATH 環境變數,讓它成為預設執行目錄:
$ export PATH="/opt/Plone-3.1/Python-2.4/bin:$PATH" $ echo $PATH /opt/Plone-3.1/Python-2.4/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin $ which python /opt/Plone-3.1/Python-2.4/bin/python $ python -V Python 2.4.5
或是將 PATH 環境變數設定在 .bash_profile 之類的檔案裡。
以 paster 程式來啟動一個新的 Plone3 buildout 專案,程式要使用 create -t plone3_buildout 參數,還有專案的目錄名稱,例如 MyBuildout:
$ cd $ paster create -t plone3_buildout MyBuildout
接著,回答幾個問題,其中一定要輸入設定值的是 zope_password [1] 這一項,另外,如果處於測試或開發階段,可考慮指定 debug_mode 為 on,其餘項目可直接按 Enter 鍵,表示採用預設值,下列是訊息範例:
Selected and implied templates:
ZopeSkel#plone3_buildout A buildout for Plone 3 projects
Variables:
egg: MyBuildout
package: mybuildout
project: MyBuildout
Enter zope2_install (Path to Zope 2 installation; leave blank to fetch one) ['']:
Enter plone_products_install (Path to directory containing Plone products; leave blank to fetch one) ['']:
Enter zope_user (Zope root admin user) ['admin']:
Enter zope_password (Zope root admin password) ['']: mypass
Enter http_port (HTTP port) [8080]:
Enter debug_mode (Should debug mode be "on" or "off"?) ['off']: on
Enter verbose_security (Should verbose security be "on" or "off"?) ['off']:
Creating template plone3_buildout
Creating directory ./MyBuildout
Copying README.txt to ./MyBuildout/README.txt
Copying bootstrap.py to ./MyBuildout/bootstrap.py
Copying buildout.cfg_tmpl to ./MyBuildout/buildout.cfg
Recursing into products
Creating ./MyBuildout/products/
Copying README.txt to ./MyBuildout/products/README.txt
Recursing into src
Creating ./MyBuildout/src/
Copying README.txt to ./MyBuildout/src/README.txt
Recursing into var
Creating ./MyBuildout/var/
Copying README.txt to ./MyBuildout/var/README.txt
-----------------------------------------------------------
Generation finished
You probably want to run python bootstrap.py and then edit
buildout.cfg before running bin/buildout -v
See README.txt for details
-----------------------------------------------------------
| [1] | 如果 admin 帳號的密碼,直接按 Enter 鍵跳過,會造成 Zope 啟動時找不到管理者帳號,而無法進入 ZMI。 |
在上述建立的 MyBuildout 目錄裡,執行一個 bootstrap.py 檔案,這個步驟是要建立 buildout 的目錄架構,並下載必要的 egg 檔案,可能需要數十秒的時間:
$ cd MyBuildout $ python bootstrap.py Creating directory '/home/marr/MyBuildout/bin'. Creating directory '/home/marr/MyBuildout/parts'. Creating directory '/home/marr/MyBuildout/eggs'. Creating directory '/home/marr/MyBuildout/develop-eggs'. Generated script '/home/marr/MyBuildout/bin/buildout'.
最簡化的指令是直接執行 buildout 程式,這步驟會下載、安裝、設定 Zope 和 Plone 等軟體,如果網路頻寬不理想的話,耗用時間會更久:
$ bin/buildout Getting distribution for 'plone.recipe.plone>=3.1.1,<3.2dev'. Got plone.recipe.plone 3.1.5.1. Getting distribution for 'plone.recipe.distros'. zip_safe flag not set; analyzing archive contents... plone.__init__: module references __path__ plone.recipe.__init__: module references __path__ Got plone.recipe.distros 1.3. Getting distribution for 'zc.recipe.egg'. Got zc.recipe.egg 1.1.0. Getting distribution for 'plone.recipe.zope2install'. Got plone.recipe.zope2install 2.3. Getting distribution for 'plone.recipe.zope2instance'. Got plone.recipe.zope2instance 2.4. Installing plone. /usr/bin/gcc -pthread -shared build/temp.linux-i686-2.4/zope/app/container/_zope_app_container_contained.o -o zope/app/container/_zope_app_container_contained.so running build_ext Installing productdistros. Installing instance. Generated script '/home/marr/MyBuildout/bin/instance'. Generated script '/home/marr/MyBuildout/bin/repozo'. Installing zopepy. Generated interpreter '/home/marr/MyBuildout/bin/zopepy'.
此時,已完成 Plone 的安裝,整個 Plone3 buildout 環境的容量是 163MB 左右。
Note
若發現「Download error: (104, 'Connection reset by peer') -- Some packages may not be found!」訊息,通常是提供檔案下載的網站暫時連不上,可稍後再試,或參閱 Setting PyPI Mirrors 說明。
啟動程式是 instance 位於 bin 目錄裡,使用 start 參數,指令範例如下:
$ cd ~/MyBuildout $ bin/instance start
接著,就可以輸入 http://localhost:8080/ 之類的網址,看到 Zope Quick Start 頁面。
如果 buildout.cfg 檔案被修改,或是想將使用中的 egg 提昇至最新版本,都應該重新執行 ./bin/buildout 程式。如果不想花時間進行線上更新,可使用 offline 或 non-updating 模式,指令及參數如下:
$ bin/buildout -No
執行 buildout 生效後,可能會刪除 parts 目錄裡的部份檔案,但應該不會影響 Data.fs 或自己撰寫的原始碼檔案。
buildout.cfg 檔案是用來管理 Zope 與 Plone 的設定值,例如 HTTP 伺服器執行的埠號、管理員帳號密碼、新增模組等,它的內容包含數個設定區段,範例如下:
[buildout]
parts =
plone
zope2
productdistros
instance
zopepy
find-links =
http://dist.plone.org
http://download.zope.org/ppix/
http://download.zope.org/distribution/
http://effbot.org/downloads
eggs =
elementtree
develop =
[plone]
recipe = plone.recipe.plone>=3.1.1,<3.2dev
[zope2]
recipe = plone.recipe.zope2install
url = ${plone:zope2-url}
[productdistros]
recipe = plone.recipe.distros
urls =
nested-packages =
version-suffix-packages =
[instance]
recipe = plone.recipe.zope2instance
zope2-location = ${zope2:location}
user = admin:mypass
http-address = 8080
debug-mode = on
verbose-security = on
eggs =
${buildout:eggs}
${plone:eggs}
zcml =
products =
${buildout:directory}/products
${productdistros:location}
${plone:products}
[zopepy]
recipe = zc.recipe.egg
eggs = ${instance:eggs}
interpreter = zopepy
extra-paths = ${zope2:location}/lib/python
scripts = zopepy
其中 [buildout] 、 [plone] 、 [zope2] 等,都是設定區段的例子,分別說明如下:
這是 buildout.cfg 檔案的起始點,在 parts 它會指定其他設定區段的名稱,例如 plone、zope2、productdistros、instance、zopepy 等,它們會在後面依序出現。每個 part 都會指定一個 recipe,它包含了執行特定工作的指令,像是如何設定 Zope instance 之類等。
在 find-links 則指定了幾個網址,是用來尋找要下載的 egg 檔案,不過 Cheese Shop 是一定會尋找的網址。
在 eggs 可以指定想要的擴充模組,像 plone.reload 是一個線上重啟系統的工具,而且設定時,還可以指定想要的版本,例如:
eggs =
elementtree
plone.reload>=0.8,<0.9dev
上例表示要使用 plone.reload 的 0.8 版,而不是 0.9 版。
[plone] 是呼應 [buildout] parts 的設定區段,最簡化的設定值只包含 recipe = plone.recipe.plone 一行資料,這樣會下載最新版的 recipe,如果想要指定版本,同樣可用 plone.recipe.plone>=3.1,<3.2dev 之類的設定值。
當 recipe 被下載執行後,Plone 的模組程式會被安裝在 parts/plone 目錄裡。
最簡化的設定值是包含 recipe 和 url 兩項。如果在 paster 執行階段,指定既有的 Zope 安裝環境,那麼這個設定區段就會被省略。
當預設的 recipe 被下載執行後,Zope2 的模組程式會被安裝在 parts/zope2 目錄裡,而且 Zope Software Home 是指定在 parts/zope3/lib/python。