<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.teemu.im &#187; twisted</title>
	<atom:link href="http://blog.teemu.im/tags/twisted/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.teemu.im</link>
	<description>i'm teemu and this is my weblog</description>
	<lastBuildDate>Mon, 31 Oct 2011 08:44:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Using zc.buildout in a Twisted project</title>
		<link>http://blog.teemu.im/2009/02/08/using-zcbuildout-in-a-twisted-project/</link>
		<comments>http://blog.teemu.im/2009/02/08/using-zcbuildout-in-a-twisted-project/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 07:48:25 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[twisted]]></category>
		<category><![CDATA[zc.buildout]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=248</guid>
		<description><![CDATA[I&#8217;ve been learning how to use zc.buildout in my Python projects. It seems to be used mostly by the Zope and the Plone communities, but I feel that it might be a useful tool to be used in any Python project. I do lots of work with Twisted and I wanted to try zc.buildout with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been learning how to use <a href="http://pypi.python.org/pypi/zc.buildout">zc.buildout</a> in my Python projects. It seems to be used mostly by the <a href="http://www.zope.org/">Zope</a> and the <a href="http://plone.org/">Plone</a> communities, but I feel that it might be a useful tool to be used in any Python project. I do lots of work with <a href="http://twistedmatrix.com/trac/">Twisted</a> and I wanted to try zc.buildout with a Twisted project. It wasn&#8217;t trivial and I wasn&#8217;t able to find any good examples, so I thought I might as well document my experiences here.</p>
<p>Let&#8217;s start this exercise by setting a new virtual Python environment using <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a>. It gives you a nice constrained Python environment that does not mess up your system. Easiest way to get virtualenv on your system is by using <a href="http://pypi.python.org/pypi/setuptools">setuptools</a>. When you have setuptools installed you can get virtualenv by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ easy_install virtualenv</pre></div></div>

<p>Now that you have virtualenv installed it&#8217;s time to create the virtual environment. In this example I will create the environment in a directory called <code>twistedenv</code>. You can place it anywhere you want in your system.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> twistedenv
$ virtualenv <span style="color: #660033;">--no-site-packages</span> twistedenv<span style="color: #000000; font-weight: bold;">/</span>
New python executable <span style="color: #000000; font-weight: bold;">in</span> twistedenv<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python
Please <span style="color: #c20cb9; font-weight: bold;">make</span> sure you remove any previous custom paths from your <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>teemu<span style="color: #000000; font-weight: bold;">/</span>.pydistutils.cfg file.
Installing setuptools............done.</pre></div></div>

<p>Now that the virtual environment is setup you can activate it by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> twistedenv
$ <span style="color: #7a0874; font-weight: bold;">source</span> bin<span style="color: #000000; font-weight: bold;">/</span>activate</pre></div></div>

<p>This will activate the new Python interpreter that was installed in <code>twistedenv/bin/python</code>.</p>
<p>Ok, now we can get to the actual topic, that is how to use zc.buildout with a Twisted project. What you will need is a bootstrap script that will install zc.buildout inside your environment and a buildout configuration file. Let&#8217;s start with the configuration file, since we need that before running the bootstrap script. In zc.buildout the configuration file is called <code>buildout.cfg</code>. It will tell the buildout system, what packages are needed and setup your development system in a blink of an eye. I&#8217;ll start with an example that will install Twisted and some dependencies and a sample Twisted application that is under development. Here&#8217;s the <code>buildout.cfg</code> that will do this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>buildout<span style="">&#93;</span></span>
<span style="color: #000099;">parts</span> <span style="color: #000066; font-weight:bold;">=</span>
    depends
    twisted
    twisteds
<span style="color: #000099;">develop</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> ./src</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>versions<span style="">&#93;</span></span>
<span style="color: #000099;">Twisted</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 8.2.0</span>
<span style="color: #000099;">pyOpenSSL</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 0.8</span>
<span style="color: #000099;">pyserial</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 2.4</span>
<span style="color: #000099;">pycrypto</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 2.0.1</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>depends<span style="">&#93;</span></span>
<span style="color: #000099;">recipe</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> minitage.recipe:egg</span>
<span style="color: #000099;">eggs</span> <span style="color: #000066; font-weight:bold;">=</span>
    pyOpenSSL
    pyserial
    pycrypto
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>twisted<span style="">&#93;</span></span>
<span style="color: #000099;">recipe</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> minitage.recipe:egg</span>
<span style="color: #000099;">eggs</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Twisted</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>twisteds<span style="">&#93;</span></span>
<span style="color: #000099;">recipe</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> minitage.recipe:scripts</span>
<span style="color: #000099;">interpreter</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> twistedpy</span>
<span style="color: #000099;">extra-paths</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> $<span style="">&#123;</span>buildout:directory<span style="">&#125;</span>/src</span>
<span style="color: #000099;">eggs</span> <span style="color: #000066; font-weight:bold;">=</span>
  $<span style="">&#123;</span>twisted:eggs<span style="">&#125;</span>
  $<span style="">&#123;</span>depends:eggs<span style="">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;ll briefly explain what it does. Section <code>buildout</code> is the first thing to put in the config file. It lists what parts of the configuration file are used and the location of the code that is being developed. The section called <code>depends</code> installs the dependencies for Twisted using recipe <code>minitage.recipe</code>. Section <code>twisted</code> naturally installs Twisted, but it does not install the scripts that are installed to the <code>bin</code> directory. That is what the section <code>twisteds</code> is for. By the way, you are free to choose the names of the sections or parts except for the <code>buildout</code> section. In the <code>twisteds</code> section we also define the variable <code>interpreter</code> to create us a new Python interpreter that will be used when running these scripts. For this interpreter we define which eggs are included in the interpreter path and using <code>extra-paths</code> variable we can include our development code there also. It is useful that we can keep our own code separate from 3rd party code while developing it. Section <code>versions</code> can be used to specify which versions of 3rd party libraries we want to use. If you don&#8217;t specify a version, a newest version available is used.</p>
<p>Now that we have the <code>buildout.cfg</code> file ready, we can bootsrap the buildout. Download the bootstrap script from <a href="http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py">this URL</a> and put it in the <code>twistedenv</code> directory. Before running the bootstrap script you can make sure that you are using the Python interpreter from the virtual environment to run it instead of the syste Python. Do that by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">which</span> python
<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>teemu<span style="color: #000000; font-weight: bold;">/</span>Programming<span style="color: #000000; font-weight: bold;">/</span>twistedenv<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python</pre></div></div>

<p>As you can see, for me it shows that I&#8217;m using the Python interpreter from inside the <code>twistedenv</code> where we created the virtual environment. That is good. Naturally, for you the complete path is something else. Then we can run the bootstrap script by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python bootstrap.py
Creating directory <span style="color: #ff0000;">'/Users/teemu/Programming/twistedenv/parts'</span>.
Creating directory <span style="color: #ff0000;">'/Users/teemu/Programming/twistedenv/eggs'</span>.
Creating directory <span style="color: #ff0000;">'/Users/teemu/Programming/twistedenv/develop-eggs'</span>.
Generated script <span style="color: #ff0000;">'/Users/teemu/Programming/twistedenv/bin/buildout'</span>.</pre></div></div>

<p>As you can see from the output the script created couple of directories and generated a script called <code>buildout</code> to the <code>bin</code> directory. Now our buildout environment is almost ready. The only thing is missing is that our <code>./src</code> directory does not contain any code. You will see an error if you now try to run <code>bin/buildout</code>.</p>
<p>Let&#8217;s create a quick sample Twisted application to demonstrate a more complete buildout environment. This is what we need. Create the required directories for our code:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> src<span style="color: #000000; font-weight: bold;">/</span>sample
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> src<span style="color: #000000; font-weight: bold;">/</span>twisted<span style="color: #000000; font-weight: bold;">/</span>plugins
<span style="color: #c20cb9; font-weight: bold;">touch</span> src<span style="color: #000000; font-weight: bold;">/</span>sample<span style="color: #000000; font-weight: bold;">/</span>__init__.py</pre></div></div>

<p>Then we need the following files:</p>
<p><code>src/sample/tap.py</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">application</span> <span style="color: #ff7700;font-weight:bold;">import</span> internet
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> server
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span>.<span style="color: #dc143c;">resource</span> <span style="color: #ff7700;font-weight:bold;">import</span> Resource
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">python</span> <span style="color: #ff7700;font-weight:bold;">import</span> usage
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SimpleResource<span style="color: black;">&#40;</span>Resource<span style="color: black;">&#41;</span>:
    isLeaf = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> render_GET<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&lt;h1&gt;It Works!&lt;/h1&gt;&quot;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Options<span style="color: black;">&#40;</span>usage.<span style="color: black;">Options</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> makeService<span style="color: black;">&#40;</span>config<span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">site</span> = server.<span style="color: black;">Site</span><span style="color: black;">&#40;</span>SimpleResource<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> internet.<span style="color: black;">TCPServer</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8080</span>, <span style="color: #dc143c;">site</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><code>src/twisted/plugins/sample.py</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">application</span>.<span style="color: black;">service</span> <span style="color: #ff7700;font-weight:bold;">import</span> ServiceMaker
&nbsp;
Sample = ServiceMaker<span style="color: black;">&#40;</span>
    <span style="color: #483d8b;">&quot;Sample&quot;</span>,
    <span style="color: #483d8b;">&quot;sample.tap&quot;</span>,
    <span style="color: #483d8b;">&quot;Twisted sample application&quot;</span>,
    <span style="color: #483d8b;">&quot;sample&quot;</span>
<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>And last, but not least, <code>src/setup.py</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> setuptools <span style="color: #ff7700;font-weight:bold;">import</span> setup
&nbsp;
setup<span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">'sample'</span>,
      version=<span style="color: #483d8b;">'0.0.1'</span>,
      description=<span style="color: #483d8b;">'Sample Twisted application'</span>,
      author=<span style="color: #483d8b;">'Teemu Harju'</span>,
      author_email=<span style="color: #483d8b;">'teemu.harju@gmail.com'</span>,
      url=<span style="color: #483d8b;">'http://blog.teemu.im'</span>,
      packages=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'sample'</span><span style="color: black;">&#93;</span>,
      package_data=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'twisted.plugins'</span>: <span style="color: black;">&#91;</span><span style="color: #483d8b;">'twisted/plugins/sample.py'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span>,
      zip_safe=<span style="color: #008000;">False</span>
<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>These files will create a Twisted application called <code>sample</code> that runs a webserver on port <code>8080</code> that responds &#8220;It works!&#8221; when the root URL is accessed.</p>
<p>Now, run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ bin<span style="color: #000000; font-weight: bold;">/</span>buildout
...</pre></div></div>

<p>I&#8217;ve not put the output to the example since there is so much of it. But after the buildout is ready and if everything worked fine you can test that your environment is ready by running the <code>bin/twistd</code> script like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ bin<span style="color: #000000; font-weight: bold;">/</span>twistd <span style="color: #660033;">--help</span></pre></div></div>

<p>The output should contain our <code>sample</code> application and you can run it by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ bin<span style="color: #000000; font-weight: bold;">/</span>twistd <span style="color: #660033;">-n</span> sample</pre></div></div>

<p>Now you can start developing your Twisted application and test it in your safe and restricted environment using virtualenv and buildout. By the way, if you want to get out of your virtual Python environment you can do that simply by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ deactivate</pre></div></div>

<p>It would be probably good to include some kind of a test runner in the buildout script to make sure that your buildout is not broken. How that can be done is probably a blog post topic of it&#8217;s own. That&#8217;s all for now. Have fun with buildout and Twisted. Feel free to ask in the comments if this didn&#8217;t work for you for some reason.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2009/02/08/using-zcbuildout-in-a-twisted-project/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

