<?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>Thoughts from Mads Sülau Jørgensen &#187; Uncategorized</title> <atom:link href="http://madssj.com/blog/category/uncategorized/feed/" rel="self" type="application/rss+xml" /><link>http://madssj.com/blog</link> <description>Various articles about programming and systems administration.</description> <lastBuildDate>Thu, 09 Feb 2012 13:24:09 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>iptables throughput on Soekris net4801</title><link>http://madssj.com/blog/2010/07/21/iptables-thruput-on-soekris-net4801/</link> <comments>http://madssj.com/blog/2010/07/21/iptables-thruput-on-soekris-net4801/#comments</comments> <pubDate>Wed, 21 Jul 2010 12:33:17 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <guid
isPermaLink="false">http://swag.dk/blog/?p=219</guid> <description><![CDATA[Earlier today I had to find out what the capacity of a soekris net4801 running openwrt. Using a fairly normal rule set, it can forward 51.8 Mbits/sec which is alright for a 266 MHz processor. Sadly for me, we&#8217;ll be &#8230; <a
href="http://madssj.com/blog/2010/07/21/iptables-thruput-on-soekris-net4801/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Earlier today I had to find out what the capacity of a <a
href="http://soekris.com/net4801.htm">soekris net4801</a> running <a
href="http://www.openwrt.org/">openwrt</a>.</p><p>Using a fairly normal rule set, it can forward <strong>51.8 Mbits/sec</strong> which is alright for a 266 MHz processor.</p><p>Sadly for me, we&#8217;ll be getting a 100 Mbit fiber optic connection at work in a few months, so I guess I&#8217;ll have to move to a <a
href="http://soekris.com/net5501.htm">net5501</a> unit in order to max out the connection.</p><p><span
id="more-219"></span> Output from <code>iperf -s</code> (second run is over the lan):</p><div
class="wp_syntax"><div
class="code"><pre class="shell" style="font-family:monospace;">------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
[  4] local x port 5001 connected with x port 64183
[ ID] Interval       Transfer     Bandwidth
[  4]  0.0-10.0 sec  61.7 MBytes  51.7 Mbits/sec
[  5] local x port 5001 connected with x port 64184
[  5]  0.0-10.0 sec  1.07 GBytes    921 Mbits/sec</pre></div></div><p>Output from <code>iperf -c</code>:</p><div
class="wp_syntax"><div
class="code"><pre class="shell" style="font-family:monospace;">------------------------------------------------------------
Client connecting to x, TCP port 5001
TCP window size:   129 KByte (default)
------------------------------------------------------------
[  3] local x port 64183 connected with x port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec  61.7 MBytes  51.8 Mbits/sec</pre></div></div> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2010/07/21/iptables-thruput-on-soekris-net4801/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Extracting a single database from a pg_dumpall PostgreSQL dump</title><link>http://madssj.com/blog/2010/04/09/extracting-a-single-database-from-a-pg_dumpall-postgresql-dump/</link> <comments>http://madssj.com/blog/2010/04/09/extracting-a-single-database-from-a-pg_dumpall-postgresql-dump/#comments</comments> <pubDate>Fri, 09 Apr 2010 15:00:26 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <guid
isPermaLink="false">http://swag.dk/blog/?p=191</guid> <description><![CDATA[Some data got deleted from one of our databases, and we had to restore it from our backups. To make sure we get everything from our PostgreSQL cluster&#8217;s, we use pg_dumpall to make a SQL dump. It&#8217;s in no way &#8230; <a
href="http://madssj.com/blog/2010/04/09/extracting-a-single-database-from-a-pg_dumpall-postgresql-dump/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Some data got deleted from one of our databases, and we had to restore it from our backups.</p><p>To make sure we get <em>everything</em> from our PostgreSQL cluster&#8217;s, we use <code>pg_dumpall</code> to make a SQL dump. It&#8217;s in no way the most efficient way to do a backup of a PostgreSQL database cluster, but it is by far the most flexible.</p><p><span
id="more-191"></span> Numerous times I&#8217;ve had to fish our just one database from the dump created by <code>pg_dumpall</code> and finally got around to sticking it in a script.</p><p>Usage: <code>postgresql_dump_extract.sh &lt;dump_file&gt; &lt;db_name&gt; &gt; database.sql</code></p><p>The script does one pass over the file using <code>grep</code> and then uses byte offsets to cut the dump apart using <code>tail</code> and <code>head</code>. It&#8217;s fairly fast.</p><p>Enjoy!</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: $0 &lt;postgresql sql dump&gt; &lt;db_name&gt;&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;">fi</span>
&nbsp;
<span style="color: #007800;">db_file</span>=<span style="color: #007800;">$1</span>
<span style="color: #007800;">db_name</span>=<span style="color: #007800;">$2</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$db_file</span> <span style="color: #660033;">-o</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-r</span> <span style="color: #007800;">$db_file</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;error: <span style="color: #007800;">$db_file</span> not found or not readable&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;">2</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-b</span> <span style="color: #ff0000;">&quot;^\connect&quot;</span> <span style="color: #007800;">$db_file</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-m</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-A</span> <span style="color: #000000;">1</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$db_name</span>$&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line
<span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #007800;">bytes</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$line</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d: -f1<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$start_point</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;">start_point</span>=<span style="color: #007800;">$bytes</span>
	<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #007800;">end_point</span>=<span style="color: #007800;">$bytes</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$start_point</span>&quot;</span> <span style="color: #660033;">-o</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$end_point</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;error: start or end not found&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;">3</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">db_length</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$end_point</span> - <span style="color: #007800;">$start_point</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-c</span> +<span style="color: #007800;">$start_point</span> <span style="color: #007800;">$db_file</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-c</span> <span style="color: #007800;">$db_length</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> +<span style="color: #000000;">3</span></pre></div></div> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2010/04/09/extracting-a-single-database-from-a-pg_dumpall-postgresql-dump/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Figuring out largest/smallest/median filesizes</title><link>http://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/</link> <comments>http://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/#comments</comments> <pubDate>Thu, 04 Feb 2010 14:00:52 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Work]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/</guid> <description><![CDATA[I had to get some statistics about file sizes today, but couldn&#8217;t really find a tool for the job, so naturally, I wrote one. import os, sys, re from os.path import join, getsize, exists &#160; def median&#40;numbers&#41;: s = sorted&#40;numbers&#41; &#8230; <a
href="http://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I had to get some statistics about file sizes today, but couldn&#8217;t really find a tool for the job, so naturally, I wrote one.</p><p><span
id="more-189"></span></p><div
class="wp_syntax"><div
class="code"><pre class="python" style="font-family:monospace;"><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;">re</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span> <span style="color: #ff7700;font-weight:bold;">import</span> join, getsize, exists
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> median<span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>:
    s = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    l = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> l <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span>:
        a, b = s<span style="color: black;">&#91;</span>l / <span style="color: #ff4500;">2</span> - <span style="color: #ff4500;">1</span> : l / <span style="color: #ff4500;">2</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">!</span>= b:
            <span style="color: #ff7700;font-weight:bold;">return</span> a + b / <span style="color: #ff4500;">2.0</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> a
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> s<span style="color: black;">&#91;</span>l / <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
&nbsp;
sizes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
req_re = <span style="color: #008000;">None</span>
target = <span style="color: #483d8b;">'.'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:
    target = <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>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">3</span>:
    req_re = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</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;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> root, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> files:
        absp = join<span style="color: black;">&#40;</span>root, name<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> exists<span style="color: black;">&#40;</span>absp<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> req_re <span style="color: #ff7700;font-weight:bold;">or</span> req_re.<span style="color: black;">search</span><span style="color: black;">&#40;</span>absp<span style="color: black;">&#41;</span>:
                sizes.<span style="color: black;">append</span><span style="color: black;">&#40;</span>getsize<span style="color: black;">&#40;</span>absp<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
num = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span>
total = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Num files: %d&quot;</span> <span style="color: #66cc66;">%</span> num
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Average  : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>total / num<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Median   : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>median<span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Min      : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">min</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Max      : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">max</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span></pre></div></div><p>Usage should be self-explanatory.</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>AppleScript application to slow network</title><link>http://madssj.com/blog/2009/09/15/applescript-application-to-slow-network/</link> <comments>http://madssj.com/blog/2009/09/15/applescript-application-to-slow-network/#comments</comments> <pubDate>Tue, 15 Sep 2009 10:40:06 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <guid
isPermaLink="false">http://swag.dk/blog/?p=186</guid> <description><![CDATA[I keep doing the same ipfw commands over and over. Enough of that, here is my first applescript application every. Probably filled with bugs and other scary things, and I&#8217;m probably not the first one to do this, but I &#8230; <a
href="http://madssj.com/blog/2009/09/15/applescript-application-to-slow-network/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I keep doing the same <code>ipfw</code> commands over and over. Enough of that, here is my first applescript application every. Probably filled with bugs and other scary things, and I&#8217;m probably not the first one to do this, but I think I&#8217;m the first to stick the source out there.</p><p><span
id="more-186"></span></p><div
class="wp_syntax"><div
class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> FLUSH_TEXT : <span style="color: #009900;">&quot;Quit and flush&quot;</span>
<span style="color: #ff0033; font-weight: bold;">property</span> SET_TEXT : <span style="color: #009900;">&quot;Set speed&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- be damn carefull what you input here, it will run as root</span>
<span style="color: #ff0033; font-weight: bold;">on</span> ipfwLimit<span style="color: #000000;">&#40;</span>bandwidth<span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">my</span> ipfwFlush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw pipe 1 config bw &quot;</span> <span style="color: #000000;">&amp;</span> bandwidth <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;KB&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw add 10 pipe 1 tcp from any 80 to me&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw add 11 pipe 1 tcp from me to any 80&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
<span style="color: #ff0033; font-weight: bold;">end</span> ipfwLimit
&nbsp;
<span style="color: #808080; font-style: italic;">-- flush any ipfw rules</span>
<span style="color: #ff0033; font-weight: bold;">on</span> ipfwFlush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw -f flush&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
<span style="color: #ff0033; font-weight: bold;">end</span> ipfwFlush
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> question <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Control your http traffic speed&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span>FLUSH_TEXT, SET_TEXT<span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">2</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> answer <span style="color: #ff0033; font-weight: bold;">to</span> button returned <span style="color: #ff0033; font-weight: bold;">of</span> question
&nbsp;
	<span style="color: #ff0033; font-weight: bold;">if</span> answer <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> FLUSH_TEXT <span style="color: #ff0033; font-weight: bold;">then</span>
		<span style="color: #ff0033; font-weight: bold;">my</span> ipfwFlush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
	<span style="color: #ff0033; font-weight: bold;">if</span> answer <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> SET_TEXT <span style="color: #ff0033; font-weight: bold;">then</span>
		<span style="color: #ff0033; font-weight: bold;">set</span> bandwidth_question <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Enter bandwidth in KB/s (don't do something stupid like entering &quot;</span>; rm <span style="color: #000000;">-</span>rf <span style="color: #000000;">/</span><span style="color: #000000;">&#41;</span><span style="color: #009900;">&quot; default answer &quot;</span><span style="color: #000000;">56</span><span style="color: #009900;">&quot;
		set bandwidth to text returned of bandwidth_question
&nbsp;
		my ipfwLimit(bandwidth)
&nbsp;
		my main()
	end if
end main
&nbsp;
my main()</span></pre></div></div> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/09/15/applescript-application-to-slow-network/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Building postgresql8x and psycopg2 for x86_64 and i386 on Snow Leopard (OS X 10.6)</title><link>http://madssj.com/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/</link> <comments>http://madssj.com/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/#comments</comments> <pubDate>Thu, 03 Sep 2009 09:42:30 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <guid
isPermaLink="false">http://swag.dk/blog/?p=171</guid> <description><![CDATA[I&#8217;ve recently installed Apple&#8217;s new 64 bit OS Snow Leopard, on my work computer. I use postgresql extensivly together with python, and usually use apple&#8217;s bundled python2.5 for working with django. As the daredevil I am, I wanted to recompile &#8230; <a
href="http://madssj.com/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I&#8217;ve recently installed Apple&#8217;s new 64 bit OS Snow Leopard, on my work computer. I use postgresql extensivly together with python, and usually use apple&#8217;s bundled python2.5 for working with django.</p><p>As the daredevil I am, I wanted to recompile all my macports to use the new 64 bit system, and therefore deleted them all, and made a fresh install of macports. After building the postgresql81 port, I was about to build the psycopg2 python postgresql driver for python 2.5, when it gave me a warning about not being able to find some symbols in the postgresql library it had linked to. I quickly realized that this might be an architecture problem, and sure enough, it turns out that python 2.5 is a i386/ppc and python 2.6 is x86_64/i386/ppc binary, as can be seen here:</p><p><span
id="more-171"></span></p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> python<span style="color: #000000; font-weight: bold;">`</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>python: Mach-O universal binary with <span style="color: #000000;">3</span> architectures
<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>python <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture x86_64<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O <span style="color: #000000;">64</span>-bit executable x86_64
<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>python <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture i386<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable i386
<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>python <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture ppc7400<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable ppc
$ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> python2.5<span style="color: #000000; font-weight: bold;">`</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>python2.5: Mach-O universal binary with <span style="color: #000000;">2</span> architectures
<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>python2.5 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture i386<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable i386
<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>python2.5 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture ppc7400<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable ppc</pre></div></div><p>The solution seemed so simple. Recompile postgresql81 for both architectures, and let the linker figure out the rest.</p><p>Building the postgresql81 port as the +universal variant, does not work. It has something to do with the fact, that the linker (<code>ld</code>) does not know how to produce a binary for multiple architectures. After a good nights sleep, the solution was only a <a
href="http://trac.macports.org/ticket/14619">trac ticket</a> away.</p><p>So, to build a i386 and x86_64 version of postgresql8x via macports, you have to patch the <code>Portfile</code>, which is located in <code>/opt/local/var/macports/sources/rsync.macports.org/release/ports/databases/postgresql81</code>.</p><p>That can be done like this &#8211; notice that the patch seem to place the files wrong, so we&#8217;re moving them as well:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>macports<span style="color: #000000; font-weight: bold;">/</span>sources<span style="color: #000000; font-weight: bold;">/</span>rsync.macports.org<span style="color: #000000; font-weight: bold;">/</span>release<span style="color: #000000; font-weight: bold;">/</span>ports<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>postgresql81
$ curl <span style="color: #660033;">-s</span> http:<span style="color: #000000; font-weight: bold;">//</span>trac.macports.org<span style="color: #000000; font-weight: bold;">/</span>raw-attachment<span style="color: #000000; font-weight: bold;">/</span>ticket<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">14619</span><span style="color: #000000; font-weight: bold;">/</span>combined_updated_universal.patch <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">patch</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> files<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> ld.sh files<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> patch_pg_config_h files<span style="color: #000000; font-weight: bold;">/</span></pre></div></div><p>Now you can go ahead and build the postgresql81 port with both architectures, like so:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> postgresql81 +universal</pre></div></div><p>And then, finally, we can build the psycopg2 extension for python:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>initd.org<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>software<span style="color: #000000; font-weight: bold;">/</span>psycopg<span style="color: #000000; font-weight: bold;">/</span>psycopg2-2.0.12.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> zxf psycopg2-2.0.12.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> psycopg2-2.0.12
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.5 setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.5 setup.py clean
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.6 setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div><p>And you&#8217;re off.</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Keeping ssh connections alive</title><link>http://madssj.com/blog/2009/06/12/keeping-ssh-connections-alive/</link> <comments>http://madssj.com/blog/2009/06/12/keeping-ssh-connections-alive/#comments</comments> <pubDate>Fri, 12 Jun 2009 17:24:16 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[EC2]]></category> <category><![CDATA[SSH]]></category> <category><![CDATA[Work]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/06/12/keeping-ssh-connections-alive/</guid> <description><![CDATA[I&#8217;ve got nothing more to say than: mads@workmads ~ % cat .ssh/config ServerAliveInterval 60 Happy ssh&#8217;ing.]]></description> <content:encoded><![CDATA[<p>I&#8217;ve got nothing more to say than:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> .ssh<span style="color: #000000; font-weight: bold;">/</span>config
ServerAliveInterval <span style="color: #000000;">60</span></pre></div></div><p>Happy ssh&#8217;ing.</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/06/12/keeping-ssh-connections-alive/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Python http_head method</title><link>http://madssj.com/blog/2009/05/15/python-http_head-method/</link> <comments>http://madssj.com/blog/2009/05/15/python-http_head-method/#comments</comments> <pubDate>Fri, 15 May 2009 10:30:28 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[HEAD]]></category> <category><![CDATA[HTTP]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Work]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/05/15/python-http_head-method/</guid> <description><![CDATA[Seeing as there is no really easy way to do a HTTP HEAD request from python, I wrote up the following small method: In advance I&#8217;d like to apologize for the method that assemblies the request path. Update: Added handling &#8230; <a
href="http://madssj.com/blog/2009/05/15/python-http_head-method/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Seeing as there is no really easy way to do a HTTP <code>HEAD</code> request from python, I wrote up the following small method:</p><p>In advance I&#8217;d like to apologize for the method that assemblies the request path.</p><p><strong>Update:</strong> <em>Added handling of redirects.</em></p><div
class="wp_syntax"><div
class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> http_head<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">httplib</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urlparse</span>
&nbsp;
    redirects = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> redirects <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">10</span>:
        scheme, netloc, path, query, fragment = <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">urlsplit</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> scheme == <span style="color: #483d8b;">'https'</span>:
            conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPSConnection</span><span style="color: black;">&#40;</span>netloc<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPConnection</span><span style="color: black;">&#40;</span>netloc<span style="color: black;">&#41;</span>
&nbsp;
        conn.<span style="color: black;">request</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;HEAD&quot;</span>, <span style="color: #483d8b;">&quot;%s%s%s%s%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>path, query <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">&quot;?&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>, query,
                                             fragment <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">&quot;#&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>, fragment<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        res = conn.<span style="color: black;">getresponse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> res.<span style="color: black;">status</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">301</span>, <span style="color: #ff4500;">302</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> res.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'location'</span><span style="color: black;">&#41;</span>:
            url = res.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'location'</span><span style="color: black;">&#41;</span>
            redirects += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> res.<span style="color: black;">status</span>, res.<span style="color: black;">reason</span></pre></div></div> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/05/15/python-http_head-method/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Formatting xml with xmllint</title><link>http://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/</link> <comments>http://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/#comments</comments> <pubDate>Wed, 13 May 2009 13:11:48 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[Work]]></category> <category><![CDATA[XML]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/05/13/formatting-xml-with-xmllint/</guid> <description><![CDATA[I keep forgetting how to format and indent xml from the command line. The tool xmllint does a fine job of doing just that, which has saved me numerous times whilst working with sports results. So. Much. Data. Running xmllint &#8230; <a
href="http://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I keep forgetting how to format and indent xml from the command line. The tool <code>xmllint</code> does a fine job of doing just that, which has saved me numerous times whilst working with sports results. So. Much. Data.</p><p>Running</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">xmllint <span style="color: #660033;">--format</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div><p>will re-format and re-indent the xml in the input file, and check it for various errors while doing it.</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Very simple email sending method in python</title><link>http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/</link> <comments>http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/#comments</comments> <pubDate>Mon, 11 May 2009 17:30:24 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[Email]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[SMTP]]></category> <category><![CDATA[Work]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/05/11/very-simple-email-sending-method-in-python/</guid> <description><![CDATA[I needed to send an email, so I came up with this: &#160; def send_plain_mail&#40;subject, body, from_mail, to&#41;: import smtplib from email.MIMEText import MIMEText from email.Encoders import encode_quopri &#160; msg = MIMEText&#40;body, 'plain', 'iso-8859-1'&#41; &#160; msg&#91;'Subject'&#93; = subject msg&#91;'From'&#93; = &#8230; <a
href="http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I needed to send an email, so I came up with this:</p><div
class="wp_syntax"><div
class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> send_plain_mail<span style="color: black;">&#40;</span>subject, body, from_mail, to<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">smtplib</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEText</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEText
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">Encoders</span> <span style="color: #ff7700;font-weight:bold;">import</span> encode_quopri
&nbsp;
    msg = MIMEText<span style="color: black;">&#40;</span>body, <span style="color: #483d8b;">'plain'</span>, <span style="color: #483d8b;">'iso-8859-1'</span><span style="color: black;">&#41;</span>
&nbsp;
    msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Subject'</span><span style="color: black;">&#93;</span> = subject
    msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'From'</span><span style="color: black;">&#93;</span> = from_mail
    msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'To'</span><span style="color: black;">&#93;</span> = to
&nbsp;
    s = <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTP</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">sendmail</span><span style="color: black;">&#40;</span>from_mail, <span style="color: black;">&#91;</span>to<span style="color: black;">&#93;</span>, msg.<span style="color: black;">as_string</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div><p>Not rocket science, but it gets the job done.</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Concurrent aacgain on iTunes library</title><link>http://madssj.com/blog/2009/05/06/120/</link> <comments>http://madssj.com/blog/2009/05/06/120/#comments</comments> <pubDate>Wed, 06 May 2009 18:34:33 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <guid
isPermaLink="false">http://swag.dk/blog/?p=120</guid> <description><![CDATA[Lets face it, I&#8217;m way to lazy to write blog posts at the moment, so I&#8217;m just going to publish some python code that i wrote today. It&#8217;s a program that runs aacgain on all the albums in your iTunes &#8230; <a
href="http://madssj.com/blog/2009/05/06/120/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Lets face it, I&#8217;m way to lazy to write blog posts at the moment, so I&#8217;m just going to publish some python code that i wrote today.</p><p>It&#8217;s a program that runs <a
href="http://mp3gain.sourceforge.net/">aacgain</a> on all the albums in your iTunes library.</p><p><span
id="more-120"></span><blockquote>MP3Gain analyzes and adjusts mp3 files so that they have the same volume.</p><p>MP3Gain does not just do peak normalization, as many normalizers do. Instead, it does some statistical analysis to determine how loud the file actually sounds to the human ear.</blockquote></p><p>Yes, I am tired of all my tracks not having the same output level. I&#8217;m looking at you AC/DC!</p><p>For one, I wanted to utilize both my cores, which means running <code>aacgain</code> in parallel. I also didn&#8217;t want to resort to shell scripting some recursing nastyness, so this is what I came up with.</p><p>Save it, change <code>AACGAIN_PARAMS</code> and go make coffee. It takes a while to process.</p><p>Oh yeah, it even detects the number of cpus you have, and makes sure to start the appropriate number of worker threads. Neet.</p><div
class="wp_syntax"><div
class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">Queue</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">Queue</span>
&nbsp;
ITUNES_DIR = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;~/Music/iTunes/iTunes Music/&quot;</span><span style="color: black;">&#41;</span>
KNOWN_EXTENSIONS = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'.mp3'</span>, <span style="color: #483d8b;">'.aac'</span><span style="color: black;">&#41;</span>
AACGAIN_PARAMS = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'-a'</span>, <span style="color: #483d8b;">'-k'</span>, <span style="color: #483d8b;">'-d'</span>, <span style="color: #483d8b;">'3'</span><span style="color: black;">&#93;</span>
AACGAIN_LOCATION = <span style="color: #483d8b;">'aacgain'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_files<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    A generator that returns tuples with album name and a list of mp3 files.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> root, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>ITUNES_DIR<span style="color: black;">&#41;</span>:
        files = <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">splitext</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">in</span> KNOWN_EXTENSIONS, files<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> files:
            <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: black;">&#91;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root, f<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> files<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_num_cpus<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Returns the number of cpus in the machine, and 1 on failure.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'sysctl'</span>, <span style="color: #483d8b;">'-n'</span>, <span style="color: #483d8b;">'hw.ncpu'</span><span style="color: black;">&#93;</span>, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>.<span style="color: black;">communicate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</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;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> run_aacgain<span style="color: black;">&#40;</span>queue<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Returns a callable that will be called inside a thread.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
                files = queue.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> files <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
                p = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>AACGAIN_LOCATION<span style="color: black;">&#93;</span> + AACGAIN_PARAMS + files<span style="color: black;">&#41;</span>
                p.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">callable</span>
&nbsp;
queue = <span style="color: #dc143c;">Queue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
workers = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</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;">range</span><span style="color: black;">&#40;</span>get_num_cpus<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    worker = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=run_aacgain<span style="color: black;">&#40;</span>queue<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    worker.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    workers.<span style="color: black;">append</span><span style="color: black;">&#40;</span>worker<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> album_files <span style="color: #ff7700;font-weight:bold;">in</span> get_files<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    queue.<span style="color: black;">put</span><span style="color: black;">&#40;</span>album_files<span style="color: black;">&#41;</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;">range</span><span style="color: black;">&#40;</span>get_num_cpus<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    queue.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: black;">&#41;</span></pre></div></div> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/05/06/120/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
