<?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>Guy Rutenberg &#187; Python</title>
	<atom:link href="http://www.guyrutenberg.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:53:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Audio Based True Random Number Generator POC</title>
		<link>http://www.guyrutenberg.com/2010/05/14/audio-based-true-random-number-generator-poc/</link>
		<comments>http://www.guyrutenberg.com/2010/05/14/audio-based-true-random-number-generator-poc/#comments</comments>
		<pubDate>Fri, 14 May 2010 12:18:14 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=678</guid>
		<description><![CDATA[Few days ago I came up with an idea to create a true random number generator based on noise gathered from a cheap microphone attached to my computer. Tests showed that when sampling the microphone, the least significant bit behaves pretty randomly. This lead me to think it might be good source for gathering entropy [...]]]></description>
			<content:encoded><![CDATA[<p>Few days ago I came up with an idea to create a true random number generator based on noise gathered from a cheap microphone attached to my computer. Tests showed that when sampling the microphone, the least significant bit behaves pretty randomly. This lead me to think it might be good source for gathering entropy for a true random number generator.<br />
<span id="more-678"></span><br />
The base design was to gather the noise from the microphone than apply a process that will make in more uniform and refine its randomness. After some design iterations I came up with a process based on applying a hash function to the noise. Each iteration involves filling block of the hash function from the least significant bits of the microphone output and applying the hash. Each iteration outputs the current hash digest. Assuming the hash function is uniform, this will output a uniformly distributed blocks of bits. Furthermore, because there the previous state of the hash function influences the next digest computation, the process accumulates entropy that can smooth out potentially less random blocks. Because for all commonly used hash function the block size is much larger than the digest size the output can tell much about the current state or any future or past state. This also holds true even if someone can find all pre-images of the hash function as the amount of possible states will be too big.</p>
<p>I&#8217;ve built a Python proof of concept (using md5 as a hash function) suitable for Linux.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> hashlib
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">struct</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> GRandom:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">audio</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/dev/dsp&quot;</span>,<span style="color: #483d8b;">&quot;rb&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span> = hashlib.<span style="color: #dc143c;">md5</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_raw_block<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        buffer = <span style="color: #008000;">self</span>.<span style="color: black;">audio</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">block_size</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">bytes</span> = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%iB&quot;</span><span style="color: #66cc66;">%</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>buffer<span style="color: black;">&#41;</span>, buffer<span style="color: black;">&#41;</span>
&nbsp;
        longs = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">block_size</span>/<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
            temp = <span style="color: #ff4500;">0</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">bytes</span><span style="color: black;">&#91;</span>i<span style="color: #66cc66;">*</span><span style="color: #ff4500;">32</span>:<span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">32</span><span style="color: black;">&#93;</span>:
                temp = <span style="color: black;">&#40;</span>temp <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> ^ <span style="color: black;">&#40;</span>b <span style="color: #66cc66;">&amp;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
            longs.<span style="color: black;">append</span><span style="color: black;">&#40;</span>temp<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">struct</span>.<span style="color: black;">pack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%iI&quot;</span><span style="color: #66cc66;">%</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>longs<span style="color: black;">&#41;</span>, <span style="color: #66cc66;">*</span>longs<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_block<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">get_raw_block</span><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> <span style="color: #008000;">self</span>.<span style="color: #008000;">hash</span>.<span style="color: black;">digest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The amount of generated bits per second is given by (sample rate)*(digest size)/(block size). So for 8KHz (default) sampling rate and md5 we&#8217;ll get a theoretical speed of ~250Kbs. SHA type hashes have higher digest to block size ration thus may result in higher speeds. Another source of speed up may be to change the sample rate of the microphone. But setting it to high may have negative effects on the entropy. The code may get a considerable performance gain by porting it to c/c++, as it uses both bit manipulations and calculates hashes. Anyways, even the Python implementation&#8217;s speed allows us it be used for many cases where true randomness is required, such as generating passwords.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/05/14/audio-based-true-random-number-generator-poc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An Early Release of the New cssrtl.py-2.0</title>
		<link>http://www.guyrutenberg.com/2009/09/20/an-early-release-of-the-new-cssrtl-py-2-0/</link>
		<comments>http://www.guyrutenberg.com/2009/09/20/an-early-release-of-the-new-cssrtl-py-2-0/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 10:00:50 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cssrtl.py]]></category>
		<category><![CDATA[RTL]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=578</guid>
		<description><![CDATA[It has been three years since I&#8217;ve released the ]]></description>
			<content:encoded><![CDATA[<p>It has been three years since I&#8217;ve released the <a href=/2007/12/28/convert-css-layout-to-rtl-cssrtlpy/">original version</a> of <code>cssrtl.py</code> (and two since it&#8217;s re-release). The old version did a nice job, but experience gained during that time led me to write from scratch a new version. I&#8217;ve detailed more than a month ago, the <a href="/2009/08/05/designing-a-better-a-css-rtl-convertor/">basic principles and ideas</a> that guided me to design a better tool to help adapting CSS files from left-to-right to right-to-left.</p>
<p>The guidelines weren&#8217;t just empty words, they were written while working on the <a href="/2009/08/15/rtl-and-hebrew-adaptation-of-the-fusion-wordpress-theme/">Hebrew adaptation to the Fusion theme</a> and in the same time writing a new proof-of-concept version of <code>cssrtl.py</code>. The original intent was to release a more mature version of that code when it will be completed. However, due to the apparent shortage of time in the present and foreseeable future, I can&#8217;t see myself complete the project any time soon. So following the &#8220;release early&#8221; mantra, I&#8217;ve decided to release the code as-is. As I said, the code is in working state, but not polished, so it may be of benefit but may contain bugs. If you find any bugs or have any suggestions, I would be glad to hear.<br />
<span id="more-578"></span></p>
<h3>Download</h3>
<p>You can download the new version from here: <a href="/wp-content/uploads/2009/09/cssrtl.py-2.0.tar.bz2">cssrtl.py-2.0.tar.bz2</a>. The code is available under the GPLv2 or any later version.</p>
<h3>Usage</h3>
<p>The new version works by creating a seperate css &#8220;fix&#8221; file that fixes the directionality of the css styles.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./cssrtl.py &lt; main.css &gt; main-rtl.css</pre></div></div>

<p>Afterward, just link the <code>main-rtl.css</code> after <code>main.css</code> in your HTML files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/09/20/an-early-release-of-the-new-cssrtl-py-2-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>tarsum-0.2 &#8211; A read only version of tarsum</title>
		<link>http://www.guyrutenberg.com/2009/04/29/tarsum-02-a-read-only-version-of-tarsum/</link>
		<comments>http://www.guyrutenberg.com/2009/04/29/tarsum-02-a-read-only-version-of-tarsum/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 06:58:31 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tarsum]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=315</guid>
		<description><![CDATA[When I first scratched the itch of calculating checksums for every file in a tar archive, this was my original intention. When I decided I want the script in bash for simplicity, I forfeited the idea and settled for extracting the files and then going over all the files to calculate their checksum value.
So when [...]]]></description>
			<content:encoded><![CDATA[<p>When I first scratched the itch of calculating checksums for every file in a tar archive, this was my original intention. When I decided I want the script in bash for simplicity, I forfeited the idea and settled for extracting the files and then going over all the files to calculate their checksum value.</p>
<p>So when <a href="/2008/10/24/tarsum-calculate-checksum-for-files-inside-tar-archive/#comment-19087">Jon Flowers asked</a> in the comments of the original <a href="/2008/10/24/tarsum-calculate-checksum-for-files-inside-tar-archive/"><code>tarsum</code> post</a> about the possibility of getting the checksums of files in the tar file without extracting all the archive, I&#8217;ve decided to re-tackle the problem.</p>
<p><span id="more-315"></span></p>
<p>This time I&#8217;ve chose python and by using the <code>tarfile</code> and <code>hashlib</code> modules I came up with a solution that allowed me to go over tar files to calculate the checksum values without extracting all of them to the disk. However some sacrifices where made in the form of back-compatibility of the output. I&#8217;ve tried to make the interface similar to the old one, and have kept all the command line options. Instead of specifying a program name to calculate the checksum values (such as <code>sha1sum</code>) as argument to <code>--checksum</code> you specify the name of the checksum algorithm such as md5, sha1, sha256, sha512 (or any other supported by <code>hashlib</code>).</p>
<p>Other changes where made so tar files can be piped directly into <code>tarsum</code> (which also works transparently with bzip2 and gzip compression).</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">tarsum &lt; sometarfile.tar.gz &gt; sometarfile.tar.gz.md5</pre></div></div>

<p>Performance-wise, according to some tests I&#8217;ve carried out, the new version is faster with big tar files than the old one, but it&#8217;s the other way around with small archives (which I find less important).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Copyright (C) 2008-2009 by Guy Rutenberg</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This program is free software; you can redistribute it and/or modify</span>
<span style="color: #808080; font-style: italic;"># it under the terms of the GNU General Public License as published by</span>
<span style="color: #808080; font-style: italic;"># the Free Software Foundation; either version 2 of the License, or</span>
<span style="color: #808080; font-style: italic;"># (at your option) any later version.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># This program is distributed in the hope that it will be useful,</span>
<span style="color: #808080; font-style: italic;"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span style="color: #808080; font-style: italic;"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span style="color: #808080; font-style: italic;"># GNU General Public License for more details.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># You should have received a copy of the GNU General Public License</span>
<span style="color: #808080; font-style: italic;"># along with this program; if not, write to the</span>
<span style="color: #808080; font-style: italic;"># Free Software Foundation, Inc.,</span>
<span style="color: #808080; font-style: italic;"># 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> hashlib
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">tarfile</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> tarsum<span style="color: black;">&#40;</span>input_file, <span style="color: #008000;">hash</span>, output_file<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        input_file  - A FILE object to read the tar file from.
        hash - The name of the hash to use. Must be supported by hashlib.
        output_file - A FILE to write the computed signatures to.
        &quot;&quot;&quot;</span>
        tar = <span style="color: #dc143c;">tarfile</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>mode=<span style="color: #483d8b;">&quot;r|*&quot;</span>, fileobj=input_file<span style="color: black;">&#41;</span>
&nbsp;
        chunk_size = <span style="color: #ff4500;">100</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">1024</span>
        store_digests = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> member <span style="color: #ff7700;font-weight:bold;">in</span> tar:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> member.<span style="color: black;">isfile</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">continue</span>
            f = tar.<span style="color: black;">extractfile</span><span style="color: black;">&#40;</span>member<span style="color: black;">&#41;</span>
            h = hashlib.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #008000;">hash</span><span style="color: black;">&#41;</span>
            data = f.<span style="color: black;">read</span><span style="color: black;">&#40;</span>chunk_size<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">while</span> data:
                h.<span style="color: black;">update</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
                data = f.<span style="color: black;">read</span><span style="color: black;">&#40;</span>chunk_size<span style="color: black;">&#41;</span>
            output_file.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s  %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>h.<span style="color: black;">hexdigest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, member.<span style="color: black;">name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    version=<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%prog 0.2.1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
             <span style="color: #483d8b;">&quot;Copyright (C) 2008-2009 Guy Rutenberg &lt;http://www.guyrutenberg.com/contact-me&gt;&quot;</span><span style="color: black;">&#41;</span>
    usage=<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%prog [options] TARFILE<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
           <span style="color: #483d8b;">&quot;Print a checksum signature for every file in TARFILE.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
           <span style="color: #483d8b;">&quot;With no FILE, or when FILE is -, read standard input.&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span>usage=usage, version=version<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-c&quot;</span>, <span style="color: #483d8b;">&quot;--checksum&quot;</span>, dest=<span style="color: #483d8b;">&quot;checksum&quot;</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;string&quot;</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;use HASH as for caclculating the checksums. [default: %default]&quot;</span>, metavar=<span style="color: #483d8b;">&quot;HASH&quot;</span>,
        default=<span style="color: #483d8b;">&quot;md5&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-o&quot;</span>, <span style="color: #483d8b;">&quot;--output&quot;</span>, dest=<span style="color: #483d8b;">&quot;output&quot;</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;string&quot;</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;save signatures to FILE.&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: black;">&#40;</span>option, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    output_file = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> option.<span style="color: black;">output</span>:
        output_file = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>option.<span style="color: black;">output</span>, <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    input_file = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>==<span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">!</span>=<span style="color: #483d8b;">&quot;-&quot;</span>:
        input_file = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    tarsum<span style="color: black;">&#40;</span>input_file, option.<span style="color: black;">checksum</span>, output_file<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># vim: ai ts=4 sts=4 et sw=4</span></pre></div></div>

<p>You can get a wget&#8217;able version here: <a href="/wp-content/uploads/2009/04/tarsum-0.2.bz2">tarsum-0.2.bz2</a>.</p>
<p><strong>Update 2009-08-12:</strong> Removed excess argument to <code>tarsum()</code> and switched the <code>filemode</code> to <code>r|*</code> (from <code>r:*</code>). Bumped version string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/04/29/tarsum-02-a-read-only-version-of-tarsum/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Damerau-Levenshtein Distance in Python</title>
		<link>http://www.guyrutenberg.com/2008/12/15/damerau-levenshtein-distance-in-python/</link>
		<comments>http://www.guyrutenberg.com/2008/12/15/damerau-levenshtein-distance-in-python/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 13:08:41 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=182</guid>
		<description><![CDATA[Damerau-Levenshtein Distance is a metric for measuring how far two given strings are, in terms of 4 basic operations:

deletion
insertion
substitution
transposition

The distance of two strings are the minimal number of such operations needed to transform the first string to the second. The algorithm can be used to create spelling correction suggestions, by finding the closest word from [...]]]></description>
			<content:encoded><![CDATA[<p>Damerau-Levenshtein Distance is a metric for measuring how far two given strings are, in terms of 4 basic operations:</p>
<ul>
<li>deletion</li>
<li>insertion</li>
<li>substitution</li>
<li>transposition</li>
</ul>
<p>The distance of two strings are the minimal number of such operations needed to transform the first string to the second. The algorithm can be used to create spelling correction suggestions, by finding the closest word from a given list to the users input. See <a href="http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance">Damerau–Levenshtein distance</a> (Wikipedia) for more info on the subject.</p>
<p>Here is an of the algorithm (restricted edit distance version) in Python. While this implementation isn&#8217;t perfect (performance wise) it is well suited for many applications.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;
Compute the Damerau-Levenshtein distance between two given
strings (s1 and s2)
&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> DamerauLevenshteinDistance<span style="color: black;">&#40;</span>s1, s2<span style="color: black;">&#41;</span>:
    d = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    lenstr1 = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s1<span style="color: black;">&#41;</span>
    lenstr2 = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s2<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">1</span>,lenstr1+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i,-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = i+<span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">1</span>,lenstr2+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">1</span>,j<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = j+<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,lenstr1<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,lenstr2<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> s1<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == s2<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>:
                cost = <span style="color: #ff4500;">0</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                cost = <span style="color: #ff4500;">1</span>
            d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">min</span><span style="color: black;">&#40;</span>
                           d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i-<span style="color: #ff4500;">1</span>,j<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>, <span style="color: #808080; font-style: italic;"># deletion</span>
                           d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i,j-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>, <span style="color: #808080; font-style: italic;"># insertion</span>
                           d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i-<span style="color: #ff4500;">1</span>,j-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> + cost, <span style="color: #808080; font-style: italic;"># substitution</span>
                          <span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> i<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> j<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> s1<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>==s2<span style="color: black;">&#91;</span>j-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">and</span> s1<span style="color: black;">&#91;</span>i-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> == s2<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>:
                d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">min</span> <span style="color: black;">&#40;</span>d<span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>i,j<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>, d<span style="color: black;">&#91;</span>i-<span style="color: #ff4500;">2</span>,j-<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> + cost<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># transposition</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> d<span style="color: black;">&#91;</span>lenstr1-<span style="color: #ff4500;">1</span>,lenstr2-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/12/15/damerau-levenshtein-distance-in-python/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Retrieving Google&#8217;s Cache for a Whole Website</title>
		<link>http://www.guyrutenberg.com/2008/10/02/retrieving-googles-cache-for-a-whole-website/</link>
		<comments>http://www.guyrutenberg.com/2008/10/02/retrieving-googles-cache-for-a-whole-website/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 20:07:30 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=92</guid>
		<description><![CDATA[Some time ago, as some of you noticed, the web server that hosts my blog went down. Unfortunately, some of the sites had no proper backup, so some thing had to be done in case the hard disk couldn&#8217;t be recovered. My efforts turned to Google&#8217;s cache. Google keeps a copy of the text of [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, as some of you noticed, the web server that hosts my blog went down. Unfortunately, some of the sites had no proper backup, so some thing had to be done in case the hard disk couldn&#8217;t be recovered. My efforts turned to Google&#8217;s cache. Google keeps a copy of the text of the web page in it&#8217;s cache, something that is usually useful when the website is temporarily unavailable. The basic idea is to retrieve a copy of all the pages of a certain site that Google has a cache of.<br />
<span id="more-92"></span><br />
While this is easily done manually when only few pages are cached, the task needs to be automated when a need for retrieving several hundreds of pages rises. This is exactly what the following Python script does.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #dc143c;">socket</span>.<span style="color: black;">setdefaulttimeout</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#adjust the site here</span>
search_term=<span style="color: #483d8b;">&quot;site:guyrutenberg.com&quot;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    headers = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'User-Agent'</span>: <span style="color: #483d8b;">'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4'</span><span style="color: black;">&#125;</span>
    url = <span style="color: #483d8b;">&quot;http://www.google.com/search?q=&quot;</span>+search_term
    regex_cache = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'&lt;a href=&quot;(http://<span style="color: #000099; font-weight: bold;">\d</span>*<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>*<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>*<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\d</span>*/search<span style="color: #000099; font-weight: bold;">\?</span>q<span style="color: #000099; font-weight: bold;">\=</span>cache.*?)&quot;.*?&gt;Cached&lt;/a&gt;'</span><span style="color: black;">&#41;</span>
    regex_next = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;a href=&quot;([^&quot;]*?)&quot;&gt;&lt;span id=nn&gt;&lt;/span&gt;Next&lt;/a&gt;'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#this is the directory we will save files to</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'files'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
    counter = <span style="color: #ff4500;">0</span>
    pagenum = <span style="color: #ff4500;">0</span>
    more = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>more<span style="color: black;">&#41;</span>:
        pagenum += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;PAGE &quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>pagenum<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;: &quot;</span>+url
        req = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>url, <span style="color: #008000;">None</span>, headers<span style="color: black;">&#41;</span>
        page = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>req<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        matches = regex_cache.<span style="color: black;">findall</span><span style="color: black;">&#40;</span>page<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> match <span style="color: #ff7700;font-weight:bold;">in</span> matches:
            counter+=<span style="color: #ff4500;">1</span>
            tmp_req = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span>match.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&amp;amp;'</span>,<span style="color: #483d8b;">'&amp;'</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">None</span>, headers<span style="color: black;">&#41;</span>
            tmp_page = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>tmp_req<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> counter,<span style="color: #483d8b;">&quot;: &quot;</span>+match
            f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'files/'</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>counter<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">'.html'</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
            f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>tmp_page<span style="color: black;">&#41;</span>
            f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#now check if there is more pages</span>
        match = regex_next.<span style="color: black;">search</span><span style="color: black;">&#40;</span>page<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> match == <span style="color: #008000;">None</span>:
            more = <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            url = <span style="color: #483d8b;">&quot;http://www.google.com&quot;</span>+match.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&amp;amp;'</span>,<span style="color: #483d8b;">'&amp;'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">&quot;__main__&quot;</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># vim: ai ts=4 sts=4 et sw=4</span></pre></div></div>

<p>Before using the script you need to adjust the <code>search_term</code> variable near the beginning of the script. In this variable goes the search term for which all the available cache would be downloaded. E.g. to retrieve the cache of all the pages of http://www.example.org you should set <code>search_term</code> to <code>site:www.example.org</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/10/02/retrieving-googles-cache-for-a-whole-website/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>Start Trac on Startup &#8211; Init.d Script for tracd</title>
		<link>http://www.guyrutenberg.com/2008/06/04/start-trac-on-startup-initd-script-for-tracd/</link>
		<comments>http://www.guyrutenberg.com/2008/06/04/start-trac-on-startup-initd-script-for-tracd/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 07:12:30 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Trac]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=59</guid>
		<description><![CDATA[As part of a server move, I went on to reinstall Trac. I&#8217;ve tried to install it as FastCGI but I failed to configure the clean URLs properly. I got the clean URLs to work if the user access them, but Trac insisted on addeing trac.fcgi to the beginning of every link it generated. So [...]]]></description>
			<content:encoded><![CDATA[<p>As part of a server move, I went on to reinstall Trac. I&#8217;ve tried to install it as FastCGI but I failed to configure the clean URLs properly. I got the clean URLs to work if the user access them, but Trac insisted on addeing <code>trac.fcgi</code> to the beginning of every link it generated. So I&#8217;ve decided to use the Trac standalone server, <code>tracd</code>.</p>
<p>The next problem I faced was how to start the Trac automatically upon startup. The solution was to use an <code>init.d</code> script for stating Trac. After some searching, I didn&#8217;t find an <code>init.d</code> script for <code>tracd</code> that were satisfactory (mostly poorly written). So I went on an wrote my own <code>init.d</code> script for <code>tracd</code>.<br />
<span id="more-59"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;">### BEGIN INIT INFO</span>
<span style="color: #666666; font-style: italic;"># Provides:          tracd</span>
<span style="color: #666666; font-style: italic;"># Required-Start:    networking</span>
<span style="color: #666666; font-style: italic;"># Required-Stop:     networking</span>
<span style="color: #666666; font-style: italic;"># Default-Start:     2 3 4 5</span>
<span style="color: #666666; font-style: italic;"># Default-Stop:      0 1 6</span>
<span style="color: #666666; font-style: italic;"># Short-Description: Start the tracd standalone Trac web server.</span>
<span style="color: #666666; font-style: italic;">### END INIT INFO</span>
<span style="color: #666666; font-style: italic;"># (C) 2008 Guy Rutenberg &lt;http://www.guyrutenberg.com&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">## Options you should probably change ##</span>
<span style="color: #007800;">HOSTNAME</span>=127.0.0.1 <span style="color: #666666; font-style: italic;"># to which hostname should we listen</span>
<span style="color: #666666; font-style: italic;"># If you only want to serve one project keep this variable</span>
<span style="color: #666666; font-style: italic;"># empty and set the PROJECT_ENV variable </span>
<span style="color: #007800;">ENV_PARENT_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>guyru<span style="color: #000000; font-weight: bold;">/</span>trac.guyrutenberg.com
<span style="color: #007800;">PROJECT_ENV</span>=
<span style="color: #007800;">PORT</span>=<span style="color: #000000;">9090</span>
<span style="color: #666666; font-style: italic;"># add any additional options (such as authentication) here. If you only have one</span>
<span style="color: #666666; font-style: italic;"># project you should probably add -s here</span>
<span style="color: #007800;">ADDITIONAL_OPTS</span>=
&nbsp;
<span style="color: #666666; font-style: italic;">## Options you should probably not change ##</span>
<span style="color: #007800;">DAEMON</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>tracd
<span style="color: #007800;">NAME</span>=tracd
<span style="color: #007800;">DESC</span>=<span style="color: #ff0000;">&quot;web server&quot;</span>
<span style="color: #007800;">PIDFILE</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>.pid
<span style="color: #007800;">SCRIPTNAME</span>=<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>
<span style="color: #007800;">SSD</span>=<span style="color: #ff0000;">&quot;/sbin/start-stop-daemon&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-e</span>
&nbsp;
. <span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>lsb<span style="color: #000000; font-weight: bold;">/</span>init-functions
&nbsp;
<span style="color: #007800;">DAEMON_OPTS</span>=<span style="color: #ff0000;">&quot;--daemonize --pidfile=<span style="color: #007800;">$PIDFILE</span> --port=<span style="color: #007800;">$PORT</span> --hostname=<span style="color: #007800;">$HOSTNAME</span> <span style="color: #007800;">$ADDITIONAL_OPTS</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ENV_PARENT_DIR</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">DAEMON_OPTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$DAEMON_OPTS</span> --env-parent-dir=<span style="color: #007800;">$ENV_PARENT_DIR</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #007800;">DAEMON_OPTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$DAEMON_OPTS</span> <span style="color: #007800;">$PROJECT_ENV</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
  start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
	log_daemon_msg <span style="color: #ff0000;">&quot;Starting <span style="color: #007800;">$DESC</span>&quot;</span> <span style="color: #007800;">$NAME</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #007800;">$SSD</span> <span style="color: #660033;">--start</span> --quiet\
	<span style="color: #660033;">--pidfile</span> <span style="color: #007800;">$PIDFILE</span> <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #660033;">--</span> <span style="color: #007800;">$DAEMON_OPTS</span> ; <span style="color: #000000; font-weight: bold;">then</span>
            log_end_msg <span style="color: #000000;">1</span>
	<span style="color: #000000; font-weight: bold;">else</span>
            log_end_msg <span style="color: #000000;">0</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">;;</span>
  stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
	log_daemon_msg <span style="color: #ff0000;">&quot;Stopping <span style="color: #007800;">$DESC</span>&quot;</span> <span style="color: #007800;">$NAME</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #007800;">$SSD</span> <span style="color: #660033;">--stop</span> <span style="color: #660033;">--retry</span> <span style="color: #000000;">30</span>\
	<span style="color: #660033;">--pidfile</span> <span style="color: #007800;">$PIDFILE</span> ; <span style="color: #000000; font-weight: bold;">then</span>
	    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$PIDFILE</span>
	    log_end_msg <span style="color: #000000;">0</span>
	<span style="color: #000000; font-weight: bold;">else</span>
	    log_end_msg <span style="color: #000000;">1</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #000000; font-weight: bold;">;;</span>
  restart<span style="color: #000000; font-weight: bold;">|</span>force-reload<span style="color: #7a0874; font-weight: bold;">&#41;</span>
	$<span style="color: #000000;">0</span> stop
	<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-r</span>  <span style="color: #007800;">$PIDFILE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">pidof</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$NAME</span> <span style="color: #000000; font-weight: bold;">|</span>\
		 <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-q</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$PIDFILE</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null ; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">done</span>
	$<span style="color: #000000;">0</span> start
	<span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #007800;">$SCRIPTNAME</span> {start|stop|restart|force-reload}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
	<span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></div></div>

<p>The script begins with a configuration section. You should go over each variable and make sure its value suits you need. The first part is configuration variables you are more likely to change. It includes:</p>
<ul>
<li><code>HOSTNAME</code> &#8211; This tells <code>tracd</code> on what hostname to listen. From my experience setting it to anything other than 127.0.0.1 or localhost will cause it to operate on all available hostnames. If you use <code>tracd</code> in conjunction with <ocde>mod_proxy</code> (this is usually the case if you have Lighttpd) you would want to leave the hostname on 127.0.0.1 so the <code>tracd</code> server won't be accessed directly.</li>
<li><code>ENV_PARENT_DIR</code> - If you have multiple projects (all under then same directory) set this to the parent directory. If you only want to serve one project you should leave this empty and set the next variable.</li>
<li><code>PROJECT_ENV</code> - Set this to the project environment directory of you project if you serve only one. This variable is ignored if <code>ENV_PARENT_DIR</code> is set to anything but empty.</li>
<li><code>PORT</code> - This simply tells <code>tracd</code> on what port to listen.</li>
<li><code>ADDITIONAL_OPTS</code> - This variable allows you to pass additional parameters to the <code>tracd</code>. It's the place to add you authentication flags. If you have only a single project and would like to omit the project name in the url (e.g. something like http://trac.example.com) add the <code>-s</code> flag to this variable.</li>
</ul>
<p>The other part of the configuration allows you to easily adjust the script to your machine. It has variables for specifying the path to the <code>tracd</code> executable and the paths of other things required of the correct operation of the script. In most cases you wouldn't have to change this part.</p>
<p>Now copy the script to <code>/etc/init.d/trac</code> and adjust the configuration to your needs. To enable the script on startup you should use your distro specific tools for managing the <code>rc</code> scripts. On fedora that means you will have to do <code>chkconfig trac on</code>. On Ubuntu you should do <code>update-rc.d trac defaults</code>.</p>
<p>I hope you will find the script useful. If you have any comments or suggestions regarding the script please comment.</p>
<p>UPDATE (13/10/2008):<br />
Here is an version of the script for <a href="/wp-content/uploads/2008/10/trac.gz">download</a>.<br />
UPDATE (17/2/2009):<br />
See <a href="/2009/02/17/starting-tracd-without-root-privileges-at-startup/">Starting tracd without Root Privileges at Startup</a> for a different way to start <code>tracd</code> that doesn't require root privileges.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/06/04/start-trac-on-startup-initd-script-for-tracd/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Convert CSS layout to RTL &#8211; cssrtl.py</title>
		<link>http://www.guyrutenberg.com/2007/12/28/convert-css-layout-to-rtl-cssrtlpy/</link>
		<comments>http://www.guyrutenberg.com/2007/12/28/convert-css-layout-to-rtl-cssrtlpy/#comments</comments>
		<pubDate>Fri, 28 Dec 2007 08:28:12 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cssrtl.py]]></category>
		<category><![CDATA[RTL]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/12/28/convert-css-layout-to-rtl-cssrtlpy/</guid>
		<description><![CDATA[This is a re-release of a script of mine that helps convert CSS layouts to RTL. I originally released it about a year ago but it was lost when I moved to the new blog. The script, cssrtl.py, utilizes a bunch of regular expressions to translate a given CSS layout to RTL. 

Download
You can download [...]]]></description>
			<content:encoded><![CDATA[<p>This is a re-release of a script of mine that helps convert CSS layouts to RTL. I originally released it about a year ago but it was lost when I moved to the new blog. The script, <code>cssrtl.py</code>, utilizes a bunch of regular expressions to translate a given CSS layout to RTL. </p>
<p><span id="more-33"></span></p>
<h4>Download</h4>
<p>You can download the script from here: <a href="/wp-content/uploads/2007/12/cssrtl.tar.gz"><code>cssrtl.py</code></a>.</p>
<h4>Usage</h4>
<p>Using the script is pretty simple. Just pass the name of the script to <code>cssrtl.py</code> and he will automatically translate it for you. </p>
<p>Example:<br />
<code>python cssrt.py main.css</code></p>
<p>You can use the <code>-o</code> flag to specify a different output file instead of overwriting the existing file.</p>
<p>If the CSS style for the body tag is missing a <code>direction</code> statement, you should add the <code>-d</code> flag to the list of command line arguments. This will cause the script to add one for you when translating the layout.</p>
<p>See <code>python cssrtl.py --help</code> for more information.</p>
<h4>Limitations</h4>
<p>While this script is pretty useful as it is, it still isn&#8217;t perfect. The major limitation, is that the script doesn&#8217;t automatically mirrors images used in the layout. So if your layout contains images you will need to flip them manually (however the script will position them in right place by automatically). Another limitation is that the script doesn&#8217;t alway handle correctly scripts with IE hacks, so it always good to keep a backup and manually check the result on IE.</p>
<p>Overall, despite some (minor) limitations, the script is very useful, and it helped me a lot when having to translate CSS layouts to RTL. I&#8217;ve successfully used it to translate Wordpress, Joomla, Druple and MediaWiki themes with minimal intervention on my side, thus saving a lot of my time. If you find the script useful, it will be nice of your to send a word. Also the script is released under the GPL so if you have any patches/improvements, please send them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/12/28/convert-css-layout-to-rtl-cssrtlpy/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Conditional Expressions in Python 2.4</title>
		<link>http://www.guyrutenberg.com/2007/10/12/conditional-expressions-in-python-24/</link>
		<comments>http://www.guyrutenberg.com/2007/10/12/conditional-expressions-in-python-24/#comments</comments>
		<pubDate>Fri, 12 Oct 2007 09:52:10 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/10/12/conditional-expressions-in-python-before-25/</guid>
		<description><![CDATA[Python 2.5 introduced new syntax structure: the conditional expressions. For programmers in languages such as C these structures seem very basic and fundamental but Python lacked them for many years. As I said Python 2.5 introduced such syntax structure, one may use it in the following form:

x =  a if condition else b

As you [...]]]></description>
			<content:encoded><![CDATA[<p>Python 2.5 introduced new syntax structure: the conditional expressions. For programmers in languages such as C these structures seem very basic and fundamental but Python lacked them for many years. As I said Python 2.5 introduced such syntax structure, one may use it in the following form:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x =  a <span style="color: #ff7700;font-weight:bold;">if</span> condition <span style="color: #ff7700;font-weight:bold;">else</span> b</pre></div></div>

<p>As you probably guessed <code>a</code> is assigned to <code>x</code> if <code>condition</code> evaluates to true and <code>b</code> is assigned otherwise. This is pretty much equivalent to the C conditional expressions. But as I said, this structure was only introduced in 2.5. Previous versions of Python are still widely deployed and in use, so how do you achieve the same thing in older version of Python?<br />
<span id="more-23"></span><br />
To create a conditional expression in earlier versions of Python, we can utilize the tuple for the values and the tuple&#8217;s index for the condition. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: black;">&#40;</span>b,a<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>condition<span style="color: black;">&#93;</span></pre></div></div>

<p>will yield the wanted result. <code>x</code> will be assigned <code>a</code> if <code>condition</code> is true and b otherwise. It works because Python treats the <code>condition</code> inside the index as 1 if <code>condition</code> is true, and as 0 if false.</p>
<p>This method provides very nice and clean alternative to the conditional expressions available in Python 2.5. However it does require some special attention in some cases.</p>
<p>Let&#8217;s say we want to assign <code>x</code> the value of <code>f()</code> if <code>condition</code> is true, and the value of <code>g()</code> otherwise. But we won&#8217;t to make sure that only the function that is assigned to <code>x</code> is ran, not both of them. In this case the intuitive way:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: black;">&#40;</span>g<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>condition<span style="color: black;">&#93;</span></pre></div></div>

<p>will not be any good, because Python will generate first the tuple, and to so evaluate both <code>f()</code> and <code>g()</code>. To prevent it we will need to change a bit the order of things. Consider the following snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>g,f<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>condition<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It will do what we wanted because the conditional expression gives back the function itself first, so neither <code>f()</code> nor <code>g()</code> are evaluated. After the conditional expression returned then the selected function is called. While this method works it has some drawbacks. For example if <code>f</code> and <code>g</code> would require arguments, and this arguments would be different for each function, the above method couldn&#8217;t be utilized. However this is only a small drawback, and under this circumstances one can just go for regular <code>if..else</code> structure.</p>
<p>Overall the method described above, offers clean and simple alternative to the built in conditional expressions in Python 2.5, which is also compatible with previous versions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/10/12/conditional-expressions-in-python-24/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>radio.py &#8211; a Wrapper Script for Listening to Radio in Linux</title>
		<link>http://www.guyrutenberg.com/2007/08/17/radiopy-a-wrapper-script-for-listening-to-radio-in-linux/</link>
		<comments>http://www.guyrutenberg.com/2007/08/17/radiopy-a-wrapper-script-for-listening-to-radio-in-linux/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 10:44:58 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[radio.py]]></category>
		<category><![CDATA[mplayer]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/17/radiopy-a-wrapper-script-for-listening-to-radio-in-linux/</guid>
		<description><![CDATA[Download radio-0.3.tar.gz.
Update: radio.py-0.4 is now available.
I like listening to music and radio while working, and fortunately there are numerous ways to do that. Unfortunately, most ways that allow you to listen to radio are very resource consuming/memory hogs (such as listening to streaming-media via web-browsers) or very unfriendly to users (listening via mplayer for example). [...]]]></description>
			<content:encoded><![CDATA[<p>Download <a href="/wp-content/uploads/2007/08/radio-0.3.tar.gz">radio-0.3.tar.gz</a>.</p>
<p>Update: radio.py-0.4 is now <a href="http://www.guyrutenberg.com/2007/10/02/radiopy-04-listening-to-radio-the-easy-way/">available</a>.</p>
<p>I like listening to music and radio while working, and fortunately there are numerous ways to do that. Unfortunately, most ways that allow you to listen to radio are very resource consuming/memory hogs (such as listening to streaming-media via web-browsers) or very unfriendly to users (listening via mplayer for example). So, I set out to find a way that will use as little system resources as possible while keeping it user-friendly. One other requirement that I had, that I will be able to do all that from the command-line, so it will work great with <a href="http://www.gnu.org/software/screen/">GNU Screen</a> and won&#8217;t require an X server (if I work without one).</p>
<p>I used for some time mplayer for listening to radio. I had a file with a list of web-radio streams URLs which I would copy and pass to <code>mplayer -playlist</code>. This method answered two of the requirements (minimal resources and command-line interface), but wasn&#8217;t really user friendly. So, I wrote a little wrapper script in python around mplayer &#8211; radio.py. After quick installation (download and extract the tar archive and copy radio.py to somewhere in you PATH), radio.py will allow you to listen to stations easily, and it will also do couple more things for you.</p>
<p>To listen to a station just call radio.py with the station&#8217;s name, e.g. in the command-line enter <code>radio.py BBC1</code> to listen for BBC radio channel 1. To view a list of know stations run <code>radio.py --list</code>. Currently there aren&#8217;t many stations (just stations I thought that are needed or I listen to). You can easily edit radio.py to add new stations (the script is documented and very clear). If you do so, please write a comment or email me so I will be able to add those stations to next release by default.</p>
<p>So, as you seen radio.py allows you to easily listen to radio, as easy as writing the station&#8217;s name. But, as I said, it can do more things that I thought should be in a radio script. It has both a sleep feature (that turns off the radio after specified amount of time) and a wake-up feature (that starts the radio after a specified amount of time). This two features can be used together, and practically allow you to use radio.py as an alarm clock. </p>
<p>You can find more information about radio.py options by calling <code>radio.py --help</code>. I hope you will find this script useful as I do. </p>
<p>Download:<br />
<a href="/wp-content/uploads/2007/08/radio-0.3.tar.gz">radio-0.3.tar.gz</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/08/17/radiopy-a-wrapper-script-for-listening-to-radio-in-linux/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Convert KDevelop&#8217;s Source Archive to Source Package</title>
		<link>http://www.guyrutenberg.com/2007/07/26/convert-kdevelops-source-archive-to-source-package/</link>
		<comments>http://www.guyrutenberg.com/2007/07/26/convert-kdevelops-source-archive-to-source-package/#comments</comments>
		<pubDate>Thu, 26 Jul 2007 17:43:29 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[KDevelop]]></category>

		<guid isPermaLink="false">http://guy.sikumuna.com/2007/07/26/convert-kdevelops-source-archive-to-source-package/</guid>
		<description><![CDATA[I use KDevelop as my main IDE and I&#8217;m pretty satisfied. KDevelop can create a source archive of the project&#8217;s source code automatically for you which simplifies the distribution of the project. Unfortunately  the archive created isn&#8217;t ready for distribution. The user can&#8217;t just run ./configure ; make as he needs to run all [...]]]></description>
			<content:encoded><![CDATA[<p>I use KDevelop as my main IDE and I&#8217;m pretty satisfied. KDevelop can create a source archive of the project&#8217;s source code automatically for you which simplifies the distribution of the project. Unfortunately  the archive created isn&#8217;t ready for distribution. The user can&#8217;t just run <code>./configure ; make</code> as he needs to run all the automake tools before. Not ideal for distributing. So you need to convert this source archive to a source package which is ready for the user to compile immediately</p>
<p><span id="more-8"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
packsource.py
&nbsp;
Version: 1.0
&nbsp;
This script takes a tar.gz source archive created by KDevelop and turns
it into a source package, which the will be able just to use
./configure ; make ; make install
to build (instead of using autotools).
&nbsp;
Synopsis:
        python packsource.py SomeProject-1.4.tar.gz
&nbsp;
The SomeProject-1.4.tar.gz will be overwritten with a new tar containg
the source package.
&nbsp;
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">###########################################################################</span>
 <span style="color: #808080; font-style: italic;">#   Copyright (C) 2007 by Guy Rutenberg                                   #</span>
 <span style="color: #808080; font-style: italic;">#   guyrutenberg@gmail.com                                                #</span>
 <span style="color: #808080; font-style: italic;">#                                                                         #</span>
 <span style="color: #808080; font-style: italic;">#   This program is free software; you can redistribute it and/or modify  #</span>
 <span style="color: #808080; font-style: italic;">#   it under the terms of the GNU General Public License as published by  #</span>
 <span style="color: #808080; font-style: italic;">#   the Free Software Foundation; either version 2 of the License, or     #</span>
 <span style="color: #808080; font-style: italic;">#   (at your option) any later version.                                   #</span>
 <span style="color: #808080; font-style: italic;">#                                                                         #</span>
 <span style="color: #808080; font-style: italic;">#   This program is distributed in the hope that it will be useful,       #</span>
 <span style="color: #808080; font-style: italic;">#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #</span>
 <span style="color: #808080; font-style: italic;">#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #</span>
 <span style="color: #808080; font-style: italic;">#   GNU General Public License for more details.                          #</span>
 <span style="color: #808080; font-style: italic;">#                                                                         #</span>
 <span style="color: #808080; font-style: italic;">#   You should have received a copy of the GNU General Public License     #</span>
 <span style="color: #808080; font-style: italic;">#   along with this program; if not, write to the                         #</span>
 <span style="color: #808080; font-style: italic;">#   Free Software Foundation, Inc.,                                       #</span>
 <span style="color: #808080; font-style: italic;">#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #</span>
<span style="color: #808080; font-style: italic;">############################################################################</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>,<span style="color: #dc143c;">sys</span>,<span style="color: #dc143c;">shutil</span>,<span style="color: #dc143c;">tempfile</span>
&nbsp;
tempdir = <span style="color: #dc143c;">tempfile</span>.<span style="color: black;">mkdtemp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Extracting source archive...&quot;</span>
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'tar'</span>, <span style="color: #483d8b;">'-zxf'</span>, <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'-C'</span>, tempdir<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Source archive extracted&quot;</span>
&nbsp;
origpwd = <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
dirlist = <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span>tempdir<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span>tempdir + <span style="color: #483d8b;">&quot;/&quot;</span> + dirlist<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'aclocal'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Running&quot;</span>, <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
&nbsp;
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'autoheader'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Running&quot;</span>, <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
&nbsp;
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'libtoolize'</span>, <span style="color: #483d8b;">'--copy'</span>, <span style="color: #483d8b;">'--force'</span>, <span style="color: #483d8b;">'--automake'</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Running&quot;</span>, <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
&nbsp;
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'automake'</span>, <span style="color: #483d8b;">'-a'</span>, <span style="color: #483d8b;">'-c'</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Running&quot;</span>, <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
&nbsp;
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'autoconf'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Running&quot;</span>, <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Removing automake cache files&quot;</span>
<span style="color: #dc143c;">shutil</span>.<span style="color: black;">rmtree</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'autom4te.cache/'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'..'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Building source package&quot;</span>
&nbsp;
args=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'tar'</span>, <span style="color: #483d8b;">'-zcf'</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, dirlist<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">spawnvp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">P_WAIT</span>, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">shutil</span>.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, origpwd<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span>origpwd<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Clearing up...&quot;</span>
<span style="color: #dc143c;">shutil</span>.<span style="color: black;">rmtree</span><span style="color: black;">&#40;</span>tempdir<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Done&quot;</span></pre></div></div>

<p>To use the script just call it and pass the source archive filename to the script as an argument. The script takes the source archive and overwrites it as a source package which will be easy for users to install.</p>
<h6>Update (16/9/2007)</h6>
<p> Removed an unneeded module dependency.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/07/26/convert-kdevelops-source-archive-to-source-package/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.418 seconds -->
