跳转到主要内容

一个与zc.buildout.egg向后兼容的实验性Buildout配方,但为Pylons用户提供额外功能。

项目描述

这是一个用于Buildout的配方。它与Buildout的zc.recipe.egg完全相同,但具有以下额外功能

  • 可以从任何依赖包中安装脚本

  • 如果您为您的平台提供了合适的应用程序,则可以设置可执行的解释器

要开始,首先下载Buildout引导程序

wget "http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py"

然后创建一个buildout.cfg文件,将/path/to/pylons/app/src替换为您Pylons应用程序的路径,将PylonsApp替换为您应用程序的名称

[buildout]
develop = /path/to/pylons/app/src
parts = python

[python]
recipe = pylons_sandbox
interpreter = python
eggs = PylonsApp

现在您可以为您的应用程序构建Buildout

$ python bootstrap.py
$ bin/buildout

到目前为止,pylons_sandbox配方与默认的zc.buildout.egg配方表现完全相同,将所有必需的依赖项安装到本地的Buildout沙箱中。它还为您设置了一个bin/python脚本和一个bin/buildout脚本,您可以使用它们来构建任何未来的更改。

对于Pylons的使用,您应该设置选项dependent_scripts=True,以便在bin目录中创建来自例如NosePasteScript等包的脚本

[buildout]
develop = /path/to/pylons/app/src
parts = python

[python]
recipe = pylons_sandbox
interpreter = python
eggs = PylonsApp
dependent_scripts = True

现在运行以下命令重新构建目录

$ bin/buildout -N

-N 选项表示,如果 buildout 可以从已安装的文件中满足依赖项,它将不会寻找新的依赖项。这意味着重新构建目录会更快一些。

现在你应该有一个可以用来提供 Pylons 应用的 bin/paster 命令。

对于大多数用户来说,这个设置应该足够好了,但 pylons_sandbox 食谱还有一个额外的功能,即 launcher 选项。

如果你想将 buildout 设置作为一个真正的沙箱来处理,你需要一个实际的可执行 Python 解释器,这样其他脚本就可以在 Apache 等脚本中使用你的沙箱 Python 解释器。Buildout 生成的 python 文件实际上只是一个 Python 脚本,所以不能以这种方式使用。

如果你设置了 launcher 选项,pylons_sandbox 食谱将创建一个新的解释器,通过在 interpreter 选项中指定的名称后附加 .buildout 来完成。它还会添加一个功能,使得调用脚本的目录在 sys.path 上。然后,它会将 launcher 选项指定的应用程序复制到 interpreter 选项指定的名称中。在我们的例子中,这意味着 buildout python 脚本将位于 bin/python.buildout 中,而启动应用程序将位于 bin/python 中,现在可以在 #! 行中使用。

这些都很好,但你还需要应用程序本身。以下是一段 C++ 代码,编译后会创建一个合适的应用程序。它被描述为“令人毛骨悚然”,所以我很高兴接受一些更整洁的 C++ 代码补丁。创建一个 launcher.cc 文件,内容如下

/*
 * Buildout Launcher
 * +++++++++++++++++
 *
 * This application excutes a python script in the same directory as the
 * application. This is useful because it effectively turns a Python script
 * into a real executable which you can use on the #! line of other scripts.
 *
 * The script to be executed should have the same name as the the filename of
 * this compiled program but with a .py extension added to the end. The real
 * Python interpreter used to execute the script is dermined from the script's
 * #! line or /usr/bin/python is used as a fallback if no Python interpreter
 * can be found.
 *
 * The Python interpreters generated by Buildout are actually just Python
 * scripts so this application allows them to be run from a real executable.
 *
 * Compile this file with the following command:
 *
 *     g++ launcher.cc -o launcher
 *
 * Copyright James Gardner. MIT license. No warranty to the maximum extent
 * possible under the law.
 *
 */

#include <vector>
#include <string>
#include <unistd.h>
#include <fstream>

using namespace std;
int main(int argc,char *argv[])
{
    vector<string> args;
    int i;
    args.push_back("python");
    for (i=0;i<argc;i++)
        args.push_back(argv[i]);
    args[1] = strcat(argv[0], ".buildout");
    char *new_argv[argc+1];
    for (int i=0 ; i<argc+1 ; i++)  {
        new_argv[i] = (char *)args[i].c_str();
    }
    new_argv[argc+1] = NULL;
    vector<string> text_file;
    ifstream ifs(new_argv[1]);
    string temp;
    string temp_short;
    getline(ifs, temp);
    if (strncmp((char *)temp.c_str(), "#!", 2)) {
        /* default to /usr/bin/python if no #! header */
        temp_short = "/usr/bin/python";
    } else {
        temp_short = temp.substr(2,(temp.length()-2));
    }
    char python[temp_short.length()];
    strcpy(python, (char *)temp_short.c_str());
    return execv(python, new_argv);
}

用以下命令编译

$ g++ launcher.cc -o launcher

并将 launcher 应用程序放在与你的 buildout.cfg 相同的目录中。然后你可以更新你的 buildout.cfg,如下所示

[buildout]
develop = /path/to/pylons/app/src
parts = python

[python]
recipe = pylons_sandbox
interpreter = python
eggs = PylonsApp
dependent_scripts = True
launcher = launcher

现在再次重新构建

$ bin/buildout -N

你应该有一个运行良好的沙箱,其中包含真正的 Python 可执行文件以及使用 Buildout 系统部署的所有其他好处。

测试一下

$ bin/python
>>> import pylons
>>>

如你所见,所有模块依赖项都存在。

变更

0.1.0

  • 首次发布

下载

项目详情


下载文件

为您的平台下载文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分布

pylons_sandbox-0.1.0.tar.gz (6.3 kB 查看散列)

上传时间 源代码

构建分布

pylons_sandbox-0.1.0-py2.4.egg (9.7 kB 查看散列)

上传时间 源代码

由以下支持