Moving away from Mail.app

Posted by on November 12, 2008

As most people know, I have moved away from using Apple’s Mail.app as my email client. I now use mutt to perform all of my mail duties… or, so I thought.

The other day I decided to use iCal to send an event invitation to a friend. When I clicked on the send button, I was greeted with a copy of Mail.app, not exactly I wanted. At this point I changed the default Mail application to mutt (via iTerm).

Now you would expect iCal to listen to this setting, but alas, the mail was still (automatically) sent with Mail.app.

After a lot of investigating, I discovered there is a apple script within the iCal app bundle. The script of interest is /Applications/iCal.app/Contents/Resources/Mail.applescript. After spending a few hours learning applescript, I came up with a solution. You will need to replace the file /Applications/iCal.app/Contents/Resources/Mail.applescript with the one available from my repositories.

Once you have replaced this file, you must then recompile the script: osacompile -o /Applications/iCal.app/Contents/Resources/Scripts/Mail.scpt /Applications/iCal.app/Contents/Resources/Mail.applescript

Note: You will need to have at the mail script and modify it so that it points to the correct version of mutt on your system.


Not so smooth transition to xubuntu-intrepid

Posted by on November 4, 2008

So James and I decided to take the plunge and install the intrepid update on a newly installed machine we had here (it had had xubuntu-hardy installed only a week earlier).

This machine was using the nvidia binary package, but other than that did not have anything that was from a restricted repo.

Upon the upgrade we found that the nvidia package no longer works. In fact, it no longer compiles the kernel module (even though it claims that the package is installed correctly). This left us without an X11 server. Even when trying to use the nv driver we could not get it to work.

On a side note: We found that the nv driver is now no-longer installed by default.

After about an hour of trying to get things to work, we decided that we will just have to live with the vesa driver. Although this makes the system slow, at least we can use X11.

Another thing I noticed, is that it now spams me with an annoying motd, I mean sure system stats on login is may be nice for some people, but I already have most of that coded in my scripts. The easiest way to get rid of this is to remove the package landscape-client with the following command:
sudo aptitude purge landscape-client


Simple cgi script to grab images from your Mac’s built in iSight camera

Posted by on October 7, 2008

While I was meant to be working on my thesis, I decided to update my little script to grab images from the built in iSight camera. The older version depended upon a instance of procfs to be started from within a login shell (which is not always possible).

To get around this, I stared looking for a way to inject a process into a particular mach bootstrap session (After reading the OSX internals book, I knew this was where I should be looking). Now, I will not claim that this script is the best thing I have written… It requires you to set up a rule in the sudoers file to allow the _www user to execute the specified sudo command (as root). I will leave writing this line as an exercise to the user (I have written one version, it is just not completely secure). You will also have to grab the isightcapture binary from off the net, and update the script with the correct location.

#!/bin/bash

function pgrep {
    ps -A -o pid=,command= | grep "$1" | awk '{ print $1; }' | grep -v $$
}

function cleanup {
    if [ ! -z "$MYTEMP" ]; then
        rm -rf "$MYTEMP"
    fi
}

MYTEMP="$(mktemp -d)"
trap "cleanup" 15 0

LOGIN_WINDOW_PID="$(pgrep loginwindow.app)"
OUTPUT_FILENAME="${MYTEMP}/isightcapture.jpg"
sudo launchctl bsexec "${LOGIN_WINDOW_PID}" /Users/gregdarke/bin/isightcapture -t jpg "${OUTPUT_FILENAME}"

echo -en 'Content-type: image/jpeg\r\n\r\n'
cat "${OUTPUT_FILENAME}"

Of databases and repositories

Posted by on October 6, 2008

One thing that most programmers will cringe at, is the thought of placing a database into a version control repository such as subversion or mercurial. Now I know that many of have done this for various reasons (I know I am guilty of it myself).

The point of this post is to show how this can be made a little nicer under mercurial using encode/decode filters. With a carefully constructed set of filters, you are able to actually perform text diffs and make sane merges between repositories. All you have to do is drop the following into your hgrc file (either the one in your project or ~/.hgrc):

[encode]
data.db = tempfile: sqlite3 INFILE .dump > OUTFILE

[decode]
data.db = tempfile: sqlite3 OUTFILE '.read INFILE'

This requires that you have the sqlite3 binary installed, otherwise you will end up with a data.db file containing the raw sql used to generate the database.


Recursive tuples

Posted by on September 26, 2008

Now,
I have been thinking about this for a few days now… Is it possible to create a tuple in python that refers to itself. I don’t mean via some other object, so the following does not count:

def recursive():
    l = []
    t = (l,)
    l.append(t)
    return t

I believe it is possible to do directly from C, but I can not think of a way to do it from within python.

The idea of creating a recursive tuple was spawned from this little comment I found in the pickle source code: “… recursive tuples are a rare thing”. At first I thought it was talking about a tuple that directly refers to itself, but then figured that it must be talking about tuple’s that indirectly refer to themselves.


Python DNS server

Posted by on September 21, 2008

Can anybody figure out why these links are related?

  • http://pydns.sourceforge.net/index.html
  • http://www.doxpara.com

Significant whitespace

Posted by on September 18, 2008

One of the most common complains I hear from people about python is that whitespace is significant. I would have to disagree with them, I think that significant whitespace is an excellent feature in a programming language. Though only if it is implemented correctly, which I believe it is not in python.

My major complaint about python is that it allows users to mix both tabs and spaces, and also different amounts of spaces to mark a code block. I believe that python should not allow this. I think a there should be one consistent method of indenting used within a file. I would love if this method was tabs, but I don’t really mind that much if it spaces.

This problem usually occurs when you have multiple people modifying code, over an extended period of time. Each person has their own preferred style of indenting code, and will use it (generally without thinking about it).

A friend of mine recently came across this exact problem, and in an attempt to make the file “sane” used a regular expression to fix the whitespace in the file. This ended up being a major problem, as their had inadvertently changed the meaning of the code, by subtle changing the indentation levels of the code. I came up with the idea of parsing the python program, then outputting the program back from the parse tree (thus ensuring the meaning of the code is unchanged).

After being shown the compiler module, I started playing around with some code to reproduce code from the abstract parse tree that was provided to me. When I went looking for documentation, I found that there is a piece of sample code provided with the python source that does exactly what I was after (After a few bug fixes). This example is called unparse.py (located in the Demo/parser/ directory of the python 2.5.x source code).

I suggest anybody who is trying to fix inconsistent indentation in a python file to look at this program. There are a few things to note though:

  • The code must already work as wanted
  • This program will strip all comments from the program
  • The code will loose all of it’s layout - that is, code that may have been split over multiple lines, will now be over one line

iPod/iPhone Application update breakage

Posted by on September 5, 2008

I think I should prefix this post with one quick proviso… I have pwned my iPod, this may have caused the things I am about to talk about.

I noticed that after I updated my device to 2.0.1 (and restored the backup) a few things stopped working… These were, that I could no longer view my mail (it kept beeping every 15 minutes to tell me there was something new), and I could no longer update (AppStore) applications (either via iTunes, or directly on the device).

Since I had ssh access to the device, I was able to dive into the file-system structure to see if there was anything that seemed a little weird… After fiddling around for a little while, I discovered that some of the mail sub directories (~/Library/Mail/) were owned by the user root. This seemed odd since Apple had made all user applications run as the mobile user (as of about 1.0.2). I decided to chown these directories back to the mobile user. This Seemed to fix the mail bugs.

After a few more weeks, I decided to try to fix the bugs with application updates. After some more exploring, I discovered that the problem had the same root cause as the mail bug (directories were owned by the wrong user). This time the directory in question was ~/Media/ApplicationArchives/. It seems that this location is used by Apple in the process of updating apps… In particular, it contains a zip file of the application to be installed.


I can write normal code too

Posted by on August 23, 2008

Now,
I am sure that you are all thinking that I must write horrible code normally in python… But that is incorrect, I can write quite nice code. Below I have placed a “normal” version of a prime sieve. Please note, I usually have a bit of whitespace between functions, but I am unable to at the moment due to some wordpress bugs.

I quite like this code, even though I am using a little trick in that the list the generator is iterating over, is being changed while the generator is still using it.

import math
def primes(maxPrime):
    maxSearch = int(math.sqrt(maxPrime))
    nums = range(maxPrime+1)
    nums[1] = 0 # 1 is not a prime
    primes = (n for n in nums if n) # This line is nasty... Changing the thing
                                    # I am iterating over may cause problems
    for n in primes:
        if n <= maxSearch:
            for i in xrange(n**2, maxPrime+1, n):
                nums[i] = 0 # Mark as non prime
        yield n

def main():
    print ', '.join(str(i) for i in primes(10**6))

if __name__ == "__main__":
    main()

Round two of golf

Posted by on August 22, 2008

Well, here we go again. After my last posting about python golf, one of my friends pointed me to a little gem by the guys who are writing PyPy. This link talks about how list comprehensions are implemented in python, and this sparked another piece of code.

What you see below is actually a prime sieve that is written in a single statement. I have used as many tricks as I could think of to lower the number of characters. Note, this code only works on python 2.5 (as I am using the new ternary operator). I also know that this code give a deprecation warning, that is because I am using 1e3 instead of the longer 1000.

Please feel free to leave a comment if you either have a shorter version, or an idea on how to make it shorter.

print[n for(n,l)in[((i,locals()['_[2]']) if i>1 else(0,[]))for i in range(1e3)]if n and(not l.__setitem__(slice(n*2,None,n),[(0,None)]*len(l[n*2::n])))]

Edit: After a little trip home on the bus, I have it down to 132 characters ^^.

print[n for(n,l)in[(i,vars()['_[2]'])for i in range(2,1e3)]if n and(l.__setitem__(slice(n*2-2,None,n),[(0,0)]*len(l[n*2-2::n]))!=0)]

Edit: Make that 128 characters

print[n for(n,l)in[(i,vars()['_[2]'])for i in range(1e3)]if n>1 and(l.__setitem__(slice(n*n,None,n),[(0,0)]*len(l[n*n::n]))!=0)]

Edit: 84 characters, though it is no longer a one line/one statement thing

z=range(1e3);p=(n for n in z if n>1)for n in p:z[n*n::n]=[0]*len(z[n*n::n]);print n