Zope3开发服务器设置菜谱
项目描述
z3c.recipe.dev:app生成了启动脚本和配置文件,用于启动基于egg的Zope 3设置。
z3c.recipe.dev:script生成了一个钩子到现有的Python模块,并允许执行一个Python方法,包括sys路径中的egg。
详细文档
Z3开发菜谱
z3c.recipe.dev app
这个Zope 3菜谱允许你定义Zope应用程序。
“app”菜谱可以用来定义一个Zope应用程序。它旨在仅使用egg与Zope一起工作。app菜谱将创建一个部分。该部分将包含应用程序的zope.conf、site.zcml、principals.zcml和securitypolicy.zcml。这些配置文件将在每次更新时重新创建。还将创建一个名为logs的文件夹,其中包含access.log和z3c.log文件。这些日志文件不会重新创建。启动脚本本身位于bin文件夹中,并使用相关部分的文件夹中的配置文件。
选项
“app”菜谱接受以下选项
- eggs
一个或多个egg的名称,包括应包含在生成脚本Python路径中的依赖项。
- 服务器
zserver 或 twisted 服务器选项。
- zope.conf
zope.conf 的内容。
- site.zcml
site.zcml 的内容。
- principals.zcml
securitypolicy.zcml 的内容。
- securitypolicy.zcml
securitypolicy.zcml 的内容。
- site.zcml
site.zcml 的内容。
测试
让我们定义一些(虚构的)egg,我们可以在我们的应用程序中使用
>>> mkdir('demo1') >>> write('demo1', 'setup.py', ... ''' ... from setuptools import setup ... setup(name = 'demo1') ... ''')>>> mkdir('demo2') >>> write('demo2', 'setup.py', ... ''' ... from setuptools import setup ... setup(name = 'demo2', install_requires='demo1') ... ''')
我们将创建一个 buildout.cfg 文件,用于定义我们的应用程序
>>> write('buildout.cfg', ... ''' ... [buildout] ... develop = demo1 demo2 ... parts = myapp var ... newest = false ... ... [myapp] ... recipe = z3c.recipe.dev:app ... eggs = demo2 ... z3c.recipe.dev [test] ... server = zserver ... zope.conf = ${var:zconfig} ... <eventlog> ... #level DEBUG ... <logfile> ... path STDOUT ... formatter zope.exceptions.log.Formatter ... </logfile> ... </eventlog> ... ... devmode on ... ... site.zcml = ... <include package="demo1" /> ... <include package="demo2" /> ... ... principals.zcml = ... <unauthenticatedPrincipal ... id="zope.anybody" ... title="Unauthenticated User" ... /> ... ... <unauthenticatedGroup ... id="zope.Anybody" ... title="Unauthenticated Users" ... /> ... ... <authenticatedGroup ... id="zope.Authenticated" ... title="Authenticated Users" ... /> ... ... <everybodyGroup ... id="zope.Everybody" ... title="All Users" ... /> ... ... <principal ... id="zope.manager" ... title="Manager" ... login="Manager" ... password="password" ... /> ... ... <grant ... role="zope.Manager" ... principal="zope.manager" ... /> ... ... securitypolicy.zcml = ... <include package="zope.app.securitypolicy" /> ... ... <securityPolicy ... component="zope.app.securitypolicy.zopepolicy.ZopeSecurityPolicy" ... /> ... ... <role id="zope.Anonymous" title="Everybody" ... description="All users have this role implicitly" /> ... <role id="zope.Manager" title="Site Manager" /> ... <role id="zope.Member" title="Site Member" /> ... ... <!-- Replace the following directive if you don't want public access --> ... <grant permission="zope.View" ... role="zope.Anonymous" ... /> ... <grant permission="zope.app.dublincore.view" ... role="zope.Anonymous" ... /> ... ... <grantAll role="zope.Manager" /> ... ... [var] ... recipe = zc.recipe.filestorage ... ... ''' % globals())
现在,让我们运行 buildout 并看看我们得到什么
>>> print system(join('bin', 'buildout')), Develop: '/sample-buildout/demo1' Develop: '/sample-buildout/demo2' Installing var. Installing myapp. Generated script '/sample-buildout/bin/myapp'.
bin 文件夹包含启动脚本
>>> ls('bin') - buildout-script.py - buildout.exe - myapp-script.py - myapp.exe
myapp-scrip.py 包含我们 zope 设置的启动代码
>>> cat('bin', 'myapp') #!"C:\Python24\python.exe" <BLANKLINE> import sys sys.path[0:0] = [ '/sample-buildout/demo2', '/z3c.recipe.dev/trunk/src', '/sample-buildout/eggs/zc.recipe.filestorage-1.0.1-py2.4.egg', '/sample-buildout/eggs/zope.testing-3.7.1-py2.4.egg', '/sample-buildout/eggs/zc.recipe.egg-1.1.0-py2.4.egg', '/sample-buildout/eggs/zc.buildout-1.1.1-py2.4.egg', '/sample-pyN.N.egg', '/sample-buildout/eggs/zconfig-2.6.1-py2.4.egg', '/sample-buildout/demo1', '/sample-buildout/eggs/zope.interface-3.5.0-py2.4-win32.egg', '/sample-pyN.N.egg', ] <BLANKLINE> import os sys.argv[0] = os.path.abspath(sys.argv[0]) <BLANKLINE> <BLANKLINE> import zope.app.server.main <BLANKLINE> if __name__ == '__main__': zope.app.server.main.main([ '-C', '/sample-buildout/parts/myapp/zope.conf', ]+sys.argv[1:])
myapp 文件夹包含配置文件
>>> ls('parts', 'myapp') - principals.zcml - securitypolicy.zcml - site.zcml - zope.conf
z3c.recipe.dev 脚本
脚本配方允许我们指向脚本,配方将为我们安装执行脚本钩子。如果您需要运行了解某些 egg 包的 Python 脚本,则可以使用此功能。
选项
‘script’ 脚本接受以下选项
- eggs
一个或多个egg的名称,包括应包含在生成脚本Python路径中的依赖项。
- 模块
包含要执行的方法的 module。
- 方法
从 module 调用的 method。
- 参数
使用 arguments 选项将参数传递给脚本。所有字符串将按 1:1 复制到脚本中。因此,您在此处输入的内容就是您得到的内容。
- 环境
您的脚本需要的环境
测试
让我们定义一个我们可以在应用程序中使用的 egg
>>> mkdir('hello') >>> write('hello', 'setup.py', ... ''' ... from setuptools import setup ... setup(name='hello') ... ''')
并定义一个我们用于测试的 Python 模块
>>> write('hello', 'helloworld.py', ... """ ... def helloWorld(*args): ... print 'Hello World' ... for a in args: ... print a ... """)
同时添加 __init__ 到 hello 包中
>>> write('hello', '__init__.py', '#make package')
我们将创建一个 buildout.cfg 文件,用于定义我们的脚本
>>> write('buildout.cfg', ... ''' ... [buildout] ... develop = hello ... parts = helloworld ... newest = false ... ... [helloworld] ... recipe = z3c.recipe.dev:script ... eggs = hello ... module = helloworld ... method = helloWorld ... ... ''' % globals())
让我们再次运行 buildout
>>> print system(join('bin', 'buildout')), Develop: '/sample-buildout/hello' Uninstalling myapp. Uninstalling var. Installing helloworld. Generated script '/sample-buildout/bin/helloworld'.
再次检查脚本。现在我们看到使用了 helloWorld() 方法
>>> cat('bin', 'helloworld') #!C:\Python24\python.exe <BLANKLINE> import sys sys.path[0:0] = [ '/sample-buildout/hello', ] <BLANKLINE> import os sys.argv[0] = os.path.abspath(sys.argv[0]) <BLANKLINE> <BLANKLINE> import helloworld <BLANKLINE> if __name__ == '__main__': helloworld.helloWorld()
现在我们可以调用脚本
>>> print system(join('bin', 'helloworld')), Hello World
使用参数进行测试
与上面定义的相同脚本。
使用 arguments = `` 选项将参数传递给脚本。所有字符串将按 1:1 复制到脚本中。因此,您在此处输入的内容就是您得到的内容。
我们将创建一个 buildout.cfg 文件,用于定义我们的脚本
>>> write('buildout.cfg', ... ''' ... [buildout] ... develop = hello ... parts = helloworld ... newest = false ... ... [helloworld] ... recipe = z3c.recipe.dev:script ... eggs = hello ... module = helloworld ... method = helloWorld ... arguments = 'foo', 'bar' ... ... ''' % globals())
让我们再次运行 buildout
>>> print system(join('bin', 'buildout')), Develop: '/sample-buildout/hello' Uninstalling helloworld. Installing helloworld. Generated script '/sample-buildout/bin/helloworld'.
再次检查脚本。现在我们看到使用了 helloWorld() 方法
>>> cat('bin', 'helloworld') #!C:\Python24\python.exe <BLANKLINE> import sys sys.path[0:0] = [ '/sample-buildout/hello', ] <BLANKLINE> import os sys.argv[0] = os.path.abspath(sys.argv[0]) <BLANKLINE> <BLANKLINE> import helloworld <BLANKLINE> if __name__ == '__main__': helloworld.helloWorld('foo', 'bar')
现在我们可以调用脚本
>>> print system(join('bin', 'helloworld')), Hello World foo bar
创建目录
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = data-dir ... find-links = http://download.zope.org/distribution ... newest = false ... ... [data-dir] ... recipe = z3c.recipe.dev:mkdir ... path = mystuff ... """) >>> print system(buildout), Uninstalling helloworld. Installing data-dir. data-dir: Creating directory mystuff>>> ls(sample_buildout) - .installed.cfg d bin - buildout.cfg d demo1 d demo2 d develop-eggs d eggs d hello d mystuff d parts
如果我们更改目录名,则旧目录('mystuff')不会被删除。
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = data-dir ... find-links = http://download.zope.org/distribution ... newest = false ... ... [data-dir] ... recipe = z3c.recipe.dev:mkdir ... path = otherdir ... """) >>> print system(buildout), Uninstalling data-dir. Installing data-dir. data-dir: Creating directory otherdir>>> ls(sample_buildout) - .installed.cfg d bin - buildout.cfg d demo1 d demo2 d develop-eggs d eggs d hello d mystuff d otherdir d parts
我们还可以创建一个完整路径。
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = data-dir ... find-links = http://download.zope.org/distribution ... newest = false ... ... [data-dir] ... recipe = z3c.recipe.dev:mkdir ... path = with/subdir ... """) >>> print system(buildout), data-dir: Cannot create /sample-buildout/with/subdir. /sample-buildout/with is not a directory. While: Installing. Getting section data-dir. Initializing part data-dir. Error: Invalid Path
但我们需要显式激活此功能。
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = data-dir ... find-links = http://download.zope.org/distribution ... newest = false ... ... [data-dir] ... recipe = z3c.recipe.dev:mkdir ... createpath = True ... path = with/subdir ... """) >>> print system(buildout), Uninstalling data-dir. Installing data-dir. data-dir: Creating directory with/subdir>>> ls(sample_buildout) - .installed.cfg d bin - buildout.cfg d demo1 d demo2 d develop-eggs d eggs d hello d mystuff d otherdir d parts d with
创建文件
mkfile 脚本创建具有给定路径、内容和权限的文件。
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = script ... newest = false ... ... [script] ... recipe = z3c.recipe.dev:mkfile ... path = file.sh ... content = hoschi ... mode = 0755 ... """) >>> print system(buildout) Uninstalling data-dir. Installing script. script: Writing file /sample-buildout/file.sh <BLANKLINE>>>> ls(sample_buildout) - .installed.cfg d bin - buildout.cfg d demo1 d demo2 d develop-eggs d eggs - file.sh d hello d mystuff d otherdir d parts d with
内容写入到文件中。
>>> cat(sample_buildout, 'file.sh') hoschi
并设置模式。注意在 Windows 上不支持设置模式。
>>> import os, stat, sys >>> path = os.path.join(sample_buildout, 'file.sh') >>> if sys.platform[:3].lower() != "win": ... oct(stat.S_IMODE(os.stat(path)[stat.ST_MODE])) ... else: ... '0755' '0755'
如果我们更改文件名,则旧文件将被删除。
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = script ... newest = false ... ... [script] ... recipe = z3c.recipe.dev:mkfile ... path = newfile.sh ... content = hoschi ... mode = 0755 ... """) >>> print system(buildout) Uninstalling script. Installing script. script: Writing file /sample-buildout/newfile.sh <BLANKLINE>>>> ls(sample_buildout) - .installed.cfg d bin - buildout.cfg d demo1 d demo2 d develop-eggs d eggs d hello d mystuff - newfile.sh d otherdir d parts d with
我们还可以指定为文件创建路径。
>>> write(sample_buildout, 'buildout.cfg', ... """ ... [buildout] ... parts = script ... newest = false ... ... [script] ... recipe = z3c.recipe.dev:mkfile ... createpath = On ... path = subdir/for/file/file.sh ... content = hoschi ... mode = 0755 ... """) >>> print system(buildout) Uninstalling script. Installing script. script: Creating directory /sample-buildout/subdir/for/file script: Writing file /sample-buildout/subdir/for/file/file.sh <BLANKLINE>>>> ls(sample_buildout + '/subdir/for/file') - file.sh
变更记录
0.6.0 (2010-08-19)
添加了对环境的支持。
修复了测试,以便与当前包版本一起运行。
修复了测试,以便在 *nix 系统上运行(不仅限于 win)。
0.5.4 (2009-02-22)
功能:实现了 mkdir 脚本
功能:实现了 mkfile 脚本
修复测试
0.5.3 (2008-04-07)
错误:脚本 defaults 存在一个错误,阻止了其使用,已将其重命名为 arguments,现在它正在工作
0.5.2(未发布)
清理代码,删除注释掉的代码部分
0.5.1 (2008-01-24)
错误:正确并更新元数据。
0.5.0 (2008-01-21)
初始发布