<?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>Mithro rants about stuff</title>
	<atom:link href="http://blog.mithis.net/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.mithis.net</link>
	<description></description>
	<lastBuildDate>Thu, 16 May 2013 00:53:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>FastComplete, making bash completion fast on remote file systems</title>
		<link>http://blog.mithis.net/archives/useful-bits/407-fastcomplete</link>
		<comments>http://blog.mithis.net/archives/useful-bits/407-fastcomplete#comments</comments>
		<pubDate>Fri, 16 Dec 2011 04:05:46 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Useful Bits]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=407</guid>
		<description><![CDATA[Here at Google we have quite a few remote file systems which contain various tools we use in our day-to-day work. As typing sucks we generally want the tools in our $PATH. When you try to tab complete Bash needs to stat a couple of thousand files and even on fast remote file systems this [...]]]></description>
				<content:encoded><![CDATA[<p>Here at Google we have quite a few remote file systems which contain various tools we use in our day-to-day work. As typing sucks we generally want the tools in our $PATH. When you try to tab complete Bash needs to <a href="http://linux.die.net/man/2/stat">stat</a> a couple of thousand files and even on fast remote file systems this takes a drastically long time.</p>
<p>I wrote FastComplete as a solution to this problem. The tool creates a local cache of links on your hard drive to everything in your $PATH. It uses a couple of tricks to make sure all the stats remain locally, while still allowing the remote file to change without needing to update the cache. Linux should also keep this information in memory disk cache making tab completion almost instant again. Yay!</p>
<p>You can find FastComplete at <a href="https://github.com/mithro/rcfiles/blob/master/bin/fastcomplete">https://github.com/mithro/rcfiles/blob/master/bin/fastcomplete</a> It is a stand alone python program which shouldn&#8217;t have any non-core dependencies. The usage documentation is as follows;</p>
<blockquote><p>
Fast complete creates a local disk cache of your path.<br />
It&#8217;s specifically designed to make bash tab complete run much faster. The correct fix would be to add caching to bash, but it was to hard to do so.</p>
<p>To find out what path fastcomplete is currently using:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span> ~tansell<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>fastcomplete
<span style="color: #666666; font-style: italic;"># Found 3977 commands</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>tansell<span style="color: #000000; font-weight: bold;">/</span>bin: ... :<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>build<span style="color: #000000; font-weight: bold;">/</span>google3<span style="color: #000000; font-weight: bold;">/</span>googledata<span style="color: #000000; font-weight: bold;">/</span>validators:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>build<span style="color: #000000; font-weight: bold;">/</span>google3<span style="color: #000000; font-weight: bold;">/</span>ads<span style="color: #000000; font-weight: bold;">/</span>db</pre></td></tr></table></div>

<p>To get fastcomplete to rebuild it&#8217;s cache:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span> ~tansell<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>fastcomplete <span style="color: #660033;">--rebuild</span>
<span style="color: #666666; font-style: italic;"># Using path of '/home/tansell/bin: ... :/home/build/google3/googledata/validators:/home/build/google3/ads/db'</span>
<span style="color: #666666; font-style: italic;"># Cache /usr/local/google/users//tansell/tabcache/d7e5fb63454ae33b4a171b6437be904a did not exist! Rebuilding....</span>
<span style="color: #666666; font-style: italic;"># Looking in: /home/tansell/bin (execv)</span>
...
<span style="color: #666666; font-style: italic;"># Looking in: /usr/bin (symlink)</span>
...
<span style="color: #666666; font-style: italic;"># Looking in: /home/build/google3/ads/db (execv)</span>
<span style="color: #666666; font-style: italic;"># Found 3977 commands</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>google<span style="color: #000000; font-weight: bold;">/</span>users<span style="color: #000000; font-weight: bold;">//</span>tansell<span style="color: #000000; font-weight: bold;">/</span>tabcache<span style="color: #000000; font-weight: bold;">/</span>d7e5fb63454ae33b4a171b6437be904a</pre></td></tr></table></div>

<p>To use fastcomplete all the time add the following as the *LAST* line in your ~/.bashrc file. Fastcomplete will echo some output to stderr so you can see what is happening.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Create a cache of the command</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #000000; font-weight: bold;">`</span>~tansell<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>fastcomplete <span style="color: #007800;">$PATH</span><span style="color: #000000; font-weight: bold;">`</span></pre></td></tr></table></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/useful-bits/407-fastcomplete/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Can haz kernel patches?</title>
		<link>http://blog.mithis.net/archives/lca/96-lguest-and-ksplice</link>
		<comments>http://blog.mithis.net/archives/lca/96-lguest-and-ksplice#comments</comments>
		<pubDate>Sun, 03 Jan 2010 03:05:37 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[linux.conf.au]]></category>
		<category><![CDATA[hot plug]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[ksplice]]></category>
		<category><![CDATA[lca09]]></category>
		<category><![CDATA[lca10]]></category>
		<category><![CDATA[lca2009]]></category>
		<category><![CDATA[lca2010]]></category>
		<category><![CDATA[lguest]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=96</guid>
		<description><![CDATA[As it&#8217;s only a couple of weeks till another Linux.conf.au is underway, I thought I better post this draft from the last one! This year LCA is in Wellington, New Zealand, sadly I didn&#8217;t get my act together enough to make a proper holiday of it. I&#8217;m definitely looking forward to catching up with all the cool [...]]]></description>
				<content:encoded><![CDATA[<p>As it&#8217;s only a couple of weeks till another<a href="http://www.lca2010.org.nz/"> Linux.conf.au</a> is underway, I thought I better post this draft from the last one! This year LCA is in <a href="http://maps.google.com/maps?q=Wellington,+New+Zealand&amp;btnG=Search+Maps">Wellington, New Zealand</a>, sadly I didn&#8217;t get my act together enough to make a proper holiday of it. I&#8217;m definitely looking forward to catching up with all the cool hackers at the conference.</p>
<p>One of the best talks I went to <a href="http://linux.conf.au">Linux.conf.au</a> last year was the <a href="http://www.ksplice.com/">Ksplice</a> talk. This is a wonderful tool which allows people to develop &#8220;hot patches&#8221; so that you never need to reboot again. The developers have done some very cool work which means for 88% of patches a hot fix can be generated automatically. The presenter was a really great speaker too, during the talk he explains some advanced concepts (like hot to fix-up memory structures) I was able to easily understand it all. I can&#8217;t watch until I <strong>never</strong> have to reboot my Linux machines again!</p>
<p>I also attended <a href="http://ozlabs.org/~rusty/">Rusty</a>&#8216;s tutorial on hacking <a href="http://http://lguest.ozlabs.org/">lguest</a>. Two years ago I submitted a <a href="http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.28.y.git;a=commitdiff;h=b488f22d70f1c2451618b9991b9665d2c6b31e2b">very important patch</a> to lguest which is now included in the kernel. This year I didn&#8217;t get as far as I had wanted mostly because I had not gotten enough sleep the night before. I did however submit one patch which was accepted. I guess that makes me a kernel hacker <img src='https://blog.mithis.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Sadly, Rusty won&#8217;t be running another lguest tutorial this year, so it&#8217;s unlikely I&#8217;ll submit a third patch.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/lca/96-lguest-and-ksplice/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starhunter, Firefly&#8217;s little known older cousin.</title>
		<link>http://blog.mithis.net/archives/sci-fi/102-starhunter-fireflys-little-known-older-cousin</link>
		<comments>http://blog.mithis.net/archives/sci-fi/102-starhunter-fireflys-little-known-older-cousin#comments</comments>
		<pubDate>Mon, 25 May 2009 14:35:06 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Sci Fi]]></category>
		<category><![CDATA[firefly]]></category>
		<category><![CDATA[scifi]]></category>
		<category><![CDATA[starhunter]]></category>
		<category><![CDATA[youtube]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/archives/sci-fi/102-starhunter-fireflys-little-known-older-cousin</guid>
		<description><![CDATA[I have recently been watching and really enjoying a little known series called Starhunter. At first I thought it was an enjoyable rip off of Firefly, so i was surprised to find out that it was made first. It has so many of the same elements, the quirky ship mechanic, the captain who doesn&#8217;t play [...]]]></description>
				<content:encoded><![CDATA[<div style="float: right; padding: 10px;"><img class="aligncenter size-full wp-image-103" title="Tulip - The ship from Starhunter" src="https://blog.mithis.net/wp-content/uploads/2009/05/screenshot.png" alt="Tulip - The ship from Starhunter" width="300" height="219" /></div>
<p>I have recently been watching and really enjoying a little known series called <a href="http://en.wikipedia.org/wiki/Starhunter">Starhunter</a>. At first I thought it was an enjoyable rip off of <a href="http://en.wikipedia.org/wiki/Firefly_(TV_series)">Firefly</a>, so i was surprised to find out that it was made first.</p>
<p>It has so many of the same elements, the quirky ship mechanic, the captain who doesn&#8217;t play by the rules with a darker background, raiders, etc. I have no doubt that <a href="http://www.imdb.com/name/nm0923736/">Joss Whedon</a> stole many the ideas for Firefly from this series and in the process improving on them in many ways. Sadly, like Firefly this series was canceled way before it reached a wide audience.</p>
<p>You can watch the &#8220;second season&#8221; (Starhunter 2300), which is closer to a spin off  rather then a second season (most of the characters have changed), on <a href="http://www.youtube.com/show?p=I9idlNtiYTs">Youtube</a>. I would highly recommend people who enjoyed Firefly check it out. Since its free, what have you got to loose?</p>
<p>While the first season is available on Joost, it is apparently horrible mangled. Sadly, the DVD&#8217;s are not available here in Australia so I&#8217;m going I&#8217;m going to have to buy them from <a href="http://www.amazon.com/Starhunter/e/B001CG5BH8/ref=ntt_tv_dp_pel">Amazon</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/sci-fi/102-starhunter-fireflys-little-known-older-cousin/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programmer art, it&#8217;s deadly!</title>
		<link>http://blog.mithis.net/archives/gaming-miniconf/97-programmer-art-its-deadly</link>
		<comments>http://blog.mithis.net/archives/gaming-miniconf/97-programmer-art-its-deadly#comments</comments>
		<pubDate>Wed, 28 Jan 2009 09:03:02 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Gaming Miniconf]]></category>
		<category><![CDATA[lca2009]]></category>
		<category><![CDATA[olpc]]></category>
		<category><![CDATA[ovpc]]></category>
		<category><![CDATA[raptors]]></category>
		<category><![CDATA[velociraptor]]></category>
		<category><![CDATA[xkcd]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=97</guid>
		<description><![CDATA[So one of the other things I worked on at Linux.conf.au was a project Joel Stanley started, &#8220;Open Velociraptor Per Child, the Game!&#8221;. The game comes out of the Open Velociraptor Per Child project&#8217;s ideas. The OVPC project aims to give every child in the world their very own Velociraptor. The game simulates what would [...]]]></description>
				<content:encoded><![CDATA[<p>So one of the other things I worked on at <a href="http://linux.conf.au/">Linux.conf.au</a> was a project <a href="http://shenki.net">Joel Stanley</a> started, &#8220;Open Velociraptor Per Child, the Game!&#8221;.</p>
<p>The game comes out of the <a href="http://velociraptorz.org/">Open Velociraptor Per Child</a> project&#8217;s ideas. The OVPC project aims to give every child in the world their very own Velociraptor. The game simulates what would happen if this project was ever to achieve it&#8217;s wonderful but lofty goal.</p>
<p>My main contribution was adding support for (<i>very bad</i>) collision detection, scoring and difficult levels. However, what I spent most of my time was spent making bad programmer art, that of the particularly gruesome child death animation. I like to think it was this artwork that meant we won top spot at the <a href="http://games.sericyb.com.au/">Gaming Miniconf</a> competition, beating the &#8220;Shave Bdale&#8221; game.</p>
<p>You can download the game from the <a href="http://git.sugarlabs.org/projects/one-velociraptor-per-child">git repository</a>. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/gaming-miniconf/97-programmer-art-its-deadly/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compiling for Windows using Cygwin on Linux&#8230;.</title>
		<link>http://blog.mithis.net/archives/tp/95-xcompiling-cygwin-on-linux-for-windows</link>
		<comments>http://blog.mithis.net/archives/tp/95-xcompiling-cygwin-on-linux-for-windows#comments</comments>
		<pubDate>Tue, 27 Jan 2009 06:52:22 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[linux.conf.au]]></category>
		<category><![CDATA[Thousand Parsec]]></category>
		<category><![CDATA[cross compile]]></category>
		<category><![CDATA[cross compiling]]></category>
		<category><![CDATA[cygwin]]></category>
		<category><![CDATA[mingw32]]></category>
		<category><![CDATA[thousand parsec]]></category>
		<category><![CDATA[tpserver-cpp]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=95</guid>
		<description><![CDATA[So for the past week while I have been at the best conference in the world I have been trying to compile tpserver-cpp for Windows. I had done the hard work and gotten it to compile (as documented here, here and here) on Windows previously. However, as I was in Hobart at a Linux conference [...]]]></description>
				<content:encoded><![CDATA[<p>So for the past week while I have been at the <a href="http://linux.conf.au">best conference</a> in the world I have been trying to compile <a href="http://git.thousandparsec.net/gitweb/gitweb.cgi?p=tpserver-cpp.git;a=summary">tpserver-cpp</a> for Windows. I had done the hard work and gotten it to compile (as documented <a href="http://blog.mithis.net/archives/tp/10-compiling-tpserver-cpp-under-windows">here</a>, <a href="http://blog.mithis.net/archives/tp/12-compiling-tpserver-cpp-under-windows-part-2">here</a> and <a href="http://blog.mithis.net/archives/tp/13-compiling-tpserver-cpp-under-windows-part-3">here</a>) on Windows previously. However, as I was in Hobart at a Linux conference I didn&#8217;t really have access to Windows computer. That was not going to stop me, so I attempted to <a href="http://en.wikipedia.org/wiki/Cross-compiling">cross compile</a> the binaries under Linux. This has a number of advantages as it would mean when <a href="http://jms.id.au/wiki">someone</a> finally gets around to creating a autobuilder, we can produce Windows binaries too.</p>
<p>Ubuntu provides the <a href="http://www.mingw.org/">mingw32 compilers</a> in the <a href="http://packages.ubuntu.com/search?keywords=mingw32&amp;searchon=names&amp;section=all">repository</a> so I didn&#8217;t think it would be all that hard to get working. The problem is that tpserver-cpp does not have a &#8220;native&#8221; Windows support but <a href="http://www.cygwin.com/">cygwin</a> comes to the rescue and provides a compatibly layer. Using cygwin turned out to not be as simple as using mingw32 compiler with the cygwin headers.</p>
<p>I ended up using <a href="http://www.kegel.com/crosstool/">crosstool</a> to build my own cygwin compiler. I battled for a long while with the fact that Ubuntu now enables &#8220;fortify source&#8221; by default. This breaks many versions of things like binutils and gcc (which often do <i>naughty</i> things which fortify source does not like). After I figured out how to disable it, I was still was only able to get an ancient version of gcc to compile (3.3.6) which meant I had to fix a lot of problems in the tpserver-cpp code. I guess someone had to do it eventually, but it was annoying that I was forced too.</p>
<p>I then manually downloaded a bunch of <a href="http://mirror.aarnet.edu.au/pub/cygwin/release/">cygwin packages</a> to build a tree for the dependencies (such as boost and guile). This was much faster then trying to compile them on my own.  Finally, I was able to build tperver-cpp and create a Windows binary! I can confirm it runs fine under Wine and am now getting friends who are still shacked to Windows to test it there. </p>
<p>It sounds much simpler now, but it took me over a week of work to boil it down to these steps. It was like a constant game of wack-a-mole, once I had solved one problem another popped up.</p>
<p>So what now in this area? I want to get a recent version of the compiler working and preferably build all the dependencies ourselves (rather then rely on the cygwin compiled versions). I would ultimately like to see the cygwin compilers being packaged with Ubuntu/Debian in the same way that the mingw32 compilers are. I don&#8217;t know if any of that is likely to happen however as I never seem to have enought time. For now I have uploaded a copy of <a href="http://blog.mithis.net/~tim/crosstool-cygwin-gcc336.tar.bz2">my cross compiler</a> (It needs to be extracted so it is found in /opt/crosstool).</p>
<p>I hope this helps someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/tp/95-xcompiling-cygwin-on-linux-for-windows/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSDC &amp; orbital death, better late then never&#8230;</title>
		<link>http://blog.mithis.net/archives/games/99-osdc-orbital-death-better-late-then-never</link>
		<comments>http://blog.mithis.net/archives/games/99-osdc-orbital-death-better-late-then-never#comments</comments>
		<pubDate>Mon, 26 Jan 2009 05:13:32 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[freedom]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[late]]></category>
		<category><![CDATA[osdc]]></category>
		<category><![CDATA[paper]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=99</guid>
		<description><![CDATA[So last year at the end of November I spoke at OSDC. I had been meaning to blog about the conference but never gotten around to doing so. The conference was great, but like previous OSDC&#8217;s I came down with something. Sadly, this meant that missed Andrew Tridgell&#8217;s talk about the EU-Microsoft agreement. Joel, who [...]]]></description>
				<content:encoded><![CDATA[<p>So last year at the end of November I spoke at OSDC. I had been meaning to blog about the conference but never gotten around to doing so. The conference was great, but like previous OSDC&#8217;s I came down with something. Sadly, this meant that missed <a href="http://en.wikipedia.org/wiki/Andrew_Tridgell">Andrew Tridgell&#8217;s</a> talk about the <a href="http://people.samba.org/people/2008/10/22#a-year-since-microsofts-appeal-failed">EU-Microsoft agreement</a>. Joel, who had been camped out in my lounge room for the week, said it was really worth listening too. I really wish OSDC recorded all the talks like Linux.conf.au does.</p>
<p>As part of speaking at the conference I had to produce a paper. This paper puts into writing a lot of what I have <a href="http://blog.mithis.net/archives/games/82-techtalk-gamingforfreedom">been talking about</a>. I wasn&#8217;t going to post it, but after getting a email out of the blue about the topic, I&#8217;ve decided to <a href="http://blog.mithis.net/wp-content/uploads/2009/01/osdc-paper.pdf">put a copy here on my blog</a>. I might as well also upload the <a href="http://blog.mithis.net/wp-content/uploads/2009/01/osdc-presentation.pdf">presentation I gave</a>, but it won&#8217;t be very interesting by itself. Both are released under a CC-BY-SA.</p>
<p>If there is one thing that any budding game developer (open source or otherwise) should take away from this talk, it is the following:</p>
<blockquote><p>When a person is looking for normal software, <span style="color: #ff0000;">they have an issue to solve.</span><br />
When looking for computer games, <span style="color: #ff0000;">they are looking to be entertained.</span></p></blockquote>
<p>This fundamental difference in mindset should drive every aspect of your game, website and release process.</p>
<p><i>Edit: How could I forget? A big thanks goes out to <a href="http://www.hawthornlandings.org/">Leslie Hawthorn</a> who helped proof read the paper and making it much easier to read!</i></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/games/99-osdc-orbital-death-better-late-then-never/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading cookies from most Firefox versions&#8230;</title>
		<link>http://blog.mithis.net/archives/python/94-reading-cookies-firefox</link>
		<comments>http://blog.mithis.net/archives/python/94-reading-cookies-firefox#comments</comments>
		<pubDate>Mon, 19 Jan 2009 23:59:49 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=94</guid>
		<description><![CDATA[Yesterday, I wrote about how to reading the cookies from Firefox 3.0 from Python. This code snippet extends the previous example by adding code which finds the cookie file on various different operating systems (Windows, Linux and Mac OS X). Hope this helps people who need to do this. #! /usr/bin/env python # Reading the [...]]]></description>
				<content:encoded><![CDATA[<p>Yesterday, I wrote about how to reading the <a href="http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python">cookies from Firefox 3.0</a> from Python. This code snippet extends the previous example by adding code which finds the cookie file on various different operating systems (Windows, Linux and Mac OS X). Hope this helps people who need to do this.</p>
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Reading the cookie's from Firefox/Mozilla. Supports Firefox 3.0 and Firefox 2.x</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Author: Noah Fontes &lt;nfontes AT cynigram DOT com&gt;, </span>
<span style="color: #808080; font-style: italic;">#         Tim Ansell &lt;mithro AT mithis DOT com&gt;</span>
<span style="color: #808080; font-style: italic;"># License: MIT</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sqlite2cookie<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">cStringIO</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> pysqlite2 <span style="color: #ff7700;font-weight:bold;">import</span> dbapi2 <span style="color: #ff7700;font-weight:bold;">as</span> sqlite
&nbsp;
    con <span style="color: #66cc66;">=</span> sqlite.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
&nbsp;
    cur <span style="color: #66cc66;">=</span> con.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;select host, path, isSecure, expiry, name, value from moz_cookies&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    ftstr <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FALSE&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;TRUE&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
    s <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
&quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> cur.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> % <span style="color: black;">&#40;</span>
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span>
            ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    s.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    cookie_jar <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">MozillaCookieJar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cookie_jar._really_load<span style="color: black;">&#40;</span>s<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">True</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> cookie_jar
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">cookielib</span>
<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;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">ConfigParser</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Set up cookie jar paths</span>
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_cookie_jar <span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    profiles_ini <span style="color: #66cc66;">=</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>path<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'profiles.ini'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>profiles_ini<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Open profiles.ini and read the path for the first profile</span>
    profiles_ini_reader <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">ConfigParser</span>.<span style="color: #dc143c;">ConfigParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    profiles_ini_reader.<span style="color: black;">readfp</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span>profiles_ini<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    profile_name <span style="color: #66cc66;">=</span> profiles_ini_reader.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Profile0'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'Path'</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
    profile_path <span style="color: #66cc66;">=</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>path<span style="color: #66cc66;">,</span> profile_name<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>profile_path<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">if</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>profile_path<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'cookies.sqlite'</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</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>profile_path<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'cookies.sqlite'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</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>profile_path<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'cookies.txt'</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</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>profile_path<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'cookies.txt'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_nt_cookie_jar <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">_winreg</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> win32api
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Cannot load winreg -- running windows and win32api loaded?'</span><span style="color: black;">&#41;</span>
    key <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">_winreg</span>.<span style="color: black;">OpenKey</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">_winreg</span>.<span style="color: black;">HKEY_CURRENT_USER</span><span style="color: #66cc66;">,</span> r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\W</span>indows<span style="color: #000099; font-weight: bold;">\C</span>urrentVersion<span style="color: #000099; font-weight: bold;">\E</span>xplorer<span style="color: #000099; font-weight: bold;">\S</span>hell Folders'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        result <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">_winreg</span>.<span style="color: black;">QueryValueEx</span><span style="color: black;">&#40;</span>key<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'AppData'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        key.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> ret<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #dc143c;">_winreg</span>.<span style="color: black;">REG_EXPAND_SZ</span>:
            result <span style="color: #66cc66;">=</span> win32api.<span style="color: black;">ExpandEnvironmentStrings</span><span style="color: black;">&#40;</span>ret<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            result <span style="color: #66cc66;">=</span> ret<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> _get_firefox_cookie_jar<span style="color: black;">&#40;</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>result<span style="color: #66cc66;">,</span> r<span style="color: #483d8b;">'Mozilla<span style="color: #000099; font-weight: bold;">\F</span>irefox<span style="color: #000099; font-weight: bold;">\P</span>rofiles'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_posix_cookie_jar <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'~/.mozilla/firefox'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _get_firefox_mac_cookie_jar <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># First of all...</span>
    result <span style="color: #66cc66;">=</span> _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'~/Library/Mozilla/Firefox/Profiles'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> result <span style="color: #66cc66;">==</span> <span style="color: #008000;">None</span>:
        result <span style="color: #66cc66;">=</span> _get_firefox_cookie_jar<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'~/Library/Application Support/Firefox/Profiles'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result
&nbsp;
FIREFOX_COOKIE_JARS <span style="color: #66cc66;">=</span> <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'nt'</span>: _get_firefox_nt_cookie_jar<span style="color: #66cc66;">,</span>
    <span style="color: #483d8b;">'posix'</span>: _get_firefox_posix_cookie_jar<span style="color: #66cc66;">,</span>
    <span style="color: #483d8b;">'mac'</span>: _get_firefox_mac_cookie_jar
<span style="color: black;">&#125;</span>
&nbsp;
cookie_jar <span style="color: #66cc66;">=</span> <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    cookie_jar <span style="color: #66cc66;">=</span> FIREFOX_COOKIE_JARS<span style="color: black;">&#91;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">name</span><span style="color: black;">&#93;</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;">KeyError</span>:
    cookie_jar <span style="color: #66cc66;">=</span> <span style="color: #008000;">None</span>
&nbsp;
path <span style="color: #66cc66;">=</span> <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Path to cookie jar file [%s]: '</span> % cookie_jar<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Some input specified, set it</span>
    cookie_jar <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">realpath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span>path.<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: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> cookie_jar.<span style="color: black;">endswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.sqlite'</span><span style="color: black;">&#41;</span>:
    cookie_jar <span style="color: #66cc66;">=</span> sqlite2cookie<span style="color: black;">&#40;</span>cookie_jar<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    cookie_jar <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">MozillaCookieJar</span><span style="color: black;">&#40;</span>cookie_jar<span style="color: black;">&#41;</span></pre></td></tr></table></div>

</blockquote>
<p><i>Edit: The latest version of this code can be found at <a href="http://blog.mithis.com/cgi-bin/gitweb.cgi">http://blog.mithis.com/cgi-bin/gitweb.cgi</a> and includes numerous fixes and updates.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/python/94-reading-cookies-firefox/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>WTF power scripts went in Intrepid&#8230;.</title>
		<link>http://blog.mithis.net/archives/uncategorized/92-power-scripts-in-intrepid</link>
		<comments>http://blog.mithis.net/archives/uncategorized/92-power-scripts-in-intrepid#comments</comments>
		<pubDate>Mon, 19 Jan 2009 05:42:19 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[acpi]]></category>
		<category><![CDATA[hal]]></category>
		<category><![CDATA[intrepid]]></category>
		<category><![CDATA[power]]></category>
		<category><![CDATA[resume]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[suspend]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=92</guid>
		<description><![CDATA[On previous versions of Ubuntu, the scripts which are called after a resume from suspend have been found in /etc/acpi/resume.d directory. I used this functionality to turn off some of the hardware in my Vaio which I don&#8217;t use (such as the bluetooth and the cdrom drive). This stopped working when I upgraded to Ubuntu [...]]]></description>
				<content:encoded><![CDATA[<p>On previous versions of <a href="http://www.ubuntu.com/">Ubuntu</a>, the scripts which are called after a resume from suspend have been found in <i>/etc/acpi/resume.d</i> directory. I used this functionality to turn off some of the hardware in my Vaio which I don&#8217;t use (such as the bluetooth and the cdrom drive). </p>
<p>This stopped working when I upgraded to Ubuntu Intrepid. Even more strangely while the scripts are still installed, even they are never called.</p>
<p>It appears that thanks to moving towards <a href="http://www.freedesktop.org/wiki/Software/hal">HAL</a> (which is probably a &#8220;Good Thing&#8221;) these scripts are no longer used. The scripts which are used can be found in <i>/etc/pm/</i>. Not only has the location changed, but the script format has too.</p>
<p>Previously, my script was found in <i>/etc/acpi/resume.d/99-custom.sh</i> looked like the following,</p>
<blockquote>

<div class="wp_syntax"><table><tr><td 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;"># Turn off the CD drive and the bluetooth device</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>cdpower
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>cdpower
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>bluetoothpower
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>bluetoothpower</pre></td></tr></table></div>

</blockquote>
<p>Now my script script must be found in <i>/etc/pm/sleep.d/10-custom</i> and looks like the following,</p>
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh -e</span>
<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>
	resume<span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #666666; font-style: italic;"># Turn off the CD drive and the bluetooth device</span>
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>cdpower
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>cdpower
&nbsp;
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>bluetoothpower
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>devices<span style="color: #000000; font-weight: bold;">/</span>platform<span style="color: #000000; font-weight: bold;">/</span>sony-laptop<span style="color: #000000; font-weight: bold;">/</span>bluetoothpower
	<span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

</blockquote>
<p>The main reason I&#8217;m posting this on my blog is that this change does <b>not seem to be documented anywhere</b>. Searching on Google for things like &#8220;resume script intrepid&#8221; or &#8220;/etc/acpi/resume.d intrepid&#8221; does not come up with anything useful. Hopefully some people will find this helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/uncategorized/92-power-scripts-in-intrepid/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>$#%#! UTF-8 in Python</title>
		<link>http://blog.mithis.net/archives/python/91-utf-8-in-python</link>
		<comments>http://blog.mithis.net/archives/python/91-utf-8-in-python#comments</comments>
		<pubDate>Mon, 19 Jan 2009 03:54:08 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[$LANG]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[sys.stdout]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=91</guid>
		<description><![CDATA[This is not a post about using UTF-8 properly in Python, but doing evil, evil things. Python dutifully respects the $LANG environment variable on the terminal. It turns out that a lot of the time this variable is totally wrong, it&#8217;s set to something like C even though the terminal is UTF-8 encoding. The problem [...]]]></description>
				<content:encoded><![CDATA[<p>This is <b>not</b> a post about using UTF-8 properly in Python, but doing <i>evil, evil</i> things.</p>
<p>Python dutifully respects the $LANG environment variable on the terminal. It turns out that a lot of the time this variable is totally wrong, it&#8217;s set to something like C even though the terminal is UTF-8 encoding. </p>
<p>The problem is that there is no easy way to change a file&#8217;s encoding after it&#8217;s open, well until this horrible hack! The following code will force the output encoding of stdout to UTF-8 even if started with LANG=C.</p>
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># License: MIT</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\u</span>263A&quot;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span><span style="color: #66cc66;">,</span> e:
    <span style="color: #ff7700;font-weight:bold;">print</span> e
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">encoding</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> pythonapi<span style="color: #66cc66;">,</span> py_object<span style="color: #66cc66;">,</span> c_char_p
PyFile_SetEncoding <span style="color: #66cc66;">=</span> pythonapi.<span style="color: black;">PyFile_SetEncoding</span>
PyFile_SetEncoding.<span style="color: black;">argtypes</span> <span style="color: #66cc66;">=</span> <span style="color: black;">&#40;</span>py_object<span style="color: #66cc66;">,</span> c_char_p<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> PyFile_SetEncoding<span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;UTF-8&quot;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\u</span>263A&quot;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span><span style="color: #66cc66;">,</span> e:
    <span style="color: #ff7700;font-weight:bold;">print</span> e</pre></td></tr></table></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/python/91-utf-8-in-python/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading Firefox 3.x cookies in Python</title>
		<link>http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python</link>
		<comments>http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python#comments</comments>
		<pubDate>Mon, 19 Jan 2009 03:28:28 +0000</pubDate>
		<dc:creator>mithro</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[cookie jar]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://blog.mithis.net/?p=90</guid>
		<description><![CDATA[I found the following code snippet on my hard drive today. It allows you to access Firefox 3.x cookies in Python. Firefox 3.x moved away from the older text file format to a sqlite database. This code is useful if you want to access something behind an authentication gateway and you also access the page [...]]]></description>
				<content:encoded><![CDATA[<p>I found the following code snippet on my hard drive today. It allows you to access <a href="http://www.getfirefox.com" target="_self">Firefox 3.x</a> cookies in Python. Firefox 3.x moved away from the older text file format to a <a href="http://www.sqlite.org/">sqlite</a> database.</p>
<p>This code is useful if you want to access something behind an authentication gateway and you also access the page through your web browser. You can also use this code to convert a sqlite database into a cookie file <a href="http://curl.haxx.se/">CURL</a> can read.</p>
<p>I didn&#8217;t write this code, it was written by Noah Fontes when we where doing some scraping of the <a href="http://code.google.com/soc">Google Summer of Code</a> website (before I joined Google).</p>
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Protocol implementation for handling gsocmentors.com transactions</span>
<span style="color: #808080; font-style: italic;"># Author: Noah Fontes nfontes AT cynigram DOT com</span>
<span style="color: #808080; font-style: italic;"># License: MIT</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sqlite2cookie<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">cStringIO</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> pysqlite2 <span style="color: #ff7700;font-weight:bold;">import</span> dbapi2 <span style="color: #ff7700;font-weight:bold;">as</span> sqlite
&nbsp;
    con <span style="color: #66cc66;">=</span> sqlite.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
&nbsp;
    cur <span style="color: #66cc66;">=</span> con.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;select host, path, isSecure, expiry, name, value from moz_cookies&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    ftstr <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;FALSE&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;TRUE&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
    s <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
&quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> cur.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        s.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\t</span>%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> % <span style="color: black;">&#40;</span>
            item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span>
            ftstr<span style="color: black;">&#91;</span>item<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> item<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    s.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    cookie_jar <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">cookielib</span>.<span style="color: black;">MozillaCookieJar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cookie_jar._really_load<span style="color: black;">&#40;</span>s<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">True</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> cookie_jar</pre></td></tr></table></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.mithis.net/archives/python/90-firefox3-cookies-in-python/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
