November 21, 2011
Installing PostgreSQL Server on OS X 10.6 using MacPorts

Installing PostgreSQL server using MacPorts used to be a simple matter. For some reason, using the latest MacPorts 2.0.3 on Snow Leopard seems to break the relatively simple install process.

The main problem is the install process doesn’t create the postgres user and group. This is done during the destroot phase of the install process, which doesn’t seem to be run automatically anymore.

Luckily, you can manually run the destroot phase. The following example shows you how to install PostgreSQL 8.4 server:

sudo port install postgresql84-server
sudo port destroot postgresql84-server
sudo port clean postgresql84-server

Now you can happily create the defaultdb and assign the proper user and group ownership.

Note that the problem only seems to happen on Snow Leopard, and not on Lion.

10:19pm
(View comments  
Filed under: MacPorts OS X 
October 29, 2011
Compiling MacPorts Python on Lion using GCC

Edit: As of November 21, 2011, this is fixed. See https://trac.macports.org/ticket/30031 for more information.

I use MacPorts for my Python installations. With the latest Xcode 4.2, MacPorts uses clang as the default compiler. This causes all sorts of fun with decimal arithmetics, such as:

$ python
Python 2.6.7 (r267:88850, Oct 28 2011, 17:33:57) 
[GCC 4.2.1 Compatible Apple Clang 3.0] on darwin
>>> from decimal import Decimal
>>> Decimal(10)/Decimal(5)
Decimal('8.955976040786690048E-10')

Note that Python is compiled with Clang 3.0.

The fix is to compile Python with GCC. The easiest way to accomplish this is to edit the Portfile and for MacPorts to use a specific compiler.

MacPorts keeps a local version of available Python ports at:

/opt/local/var/macports/sources/rsync.macports.org/release/tarballs/ports/lang/

For example, Python 2.6 is at:

/opt/local/var/macports/sources/rsync.macports.org/release/tarballs/ports/lang/python26

Edit Portfile and add the following line:

configure.compiler gcc-4.2

I add it in the # ensure that correct compiler is used section, before configure.cc is used, like so:

# ensure that correct compiler is used
configure.compiler gcc-4.2
build.args-append       MAKE="${build.cmd} CC=${configure.cc}"
destroot.args-append    MAKE="${destroot.cmd} CC=${configure.cc}"

Now recompile python:

sudo port -n upgrade --force python26

After waiting for a while everything will be OK again:

$ python
Python 2.6.7 (r267:88850, Oct 29 2011, 21:09:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> from decimal import Decimal
>>> Decimal(10)/Decimal(5)
Decimal('2')

Note that Python is now compiled with GCC 4.2.1.

The steps I described is a hack and the next time you perform port selfupdate your changes will be wiped out. But I imagine this bug will be fixed before you have to update Python again. If you want to do things properly by setting up your own local repository, see the MacPorts guide.

You should obviously recompile all versions of MacPorts Python you use, as this bug isn’t isolated to 2.6.

For more detailed discussions on the bug, see:

For information on selecting the right compiler for MacPorts, see the wiki page.

10:16pm
(View comments
Filed under: MacPorts OS X 
August 25, 2011
Automatically Unmount External Drives On Sleep

One of the features I want with my laptop is to be able to put it to sleep and be ready to go. Ideally I can accomplish this by simply closing the lid.

There are two issues that prevent me from doing this currently on OS X Lion:

  1. If I’m using an external monitor, instead of going to sleep, Lion moves all the windows to the external monitor1.

  2. When I’m at home, I always have external drives connected to my laptop. I have to consciously unmount the drives or else I get the dreaded “disk not ejected properly” message when I wake up the laptop.

Currently there doesn’t appear to be a way to affect the lid closing behavior so I just have to manually put the laptop to sleep. However, there is a way to automatically unmount external drives on sleep.2

The primary tool I’m using is SleepWatcher, a daemon which monitors sleep and wakeup events of a Mac. I’ll walk through the installation of SleepWatcher and the creation of associated scripts to unmount external drives on sleep and mount them on wakeup.

Installing SleepWatcher

If you use MacPorts, it’s as simple as:

  1. sudo port install sleepwatcher to install the binary, man page, launchd configuration file, and system-wide sleep and wake up scripts.
  2. sudo port load sleepwatcher to load the launchd configuration.

If you don’t use MacPorts, download the latest version of SleepWatcher from http://www.bernhard-baehr.de/ and follow the instructions in the ReadMe.rtf.

Create the .sleep script

The system-wide sleep script iterates through each user’s home directory looking for a .sleep script and executes the script as the user3. Follow these steps to create the script:

  1. cat > $HOME/.sleep
  2. Paste the following into your terminal:
#!/bin/sh
osascript -e 'tell application "Finder" to eject disks'
  1. ⌃D (Ctrl-D) to terminate the input stream.
  2. chmod +x $HOME/.sleep

At this point you can test to see if your script is working properly by running $HOME/.sleep. Your external hard drives should be unmounted.

Create the .wakeup script

This script is used to mount any HFS volumes that the laptop knows about upon waking up. Follow these steps to create the script:

  1. cat > $HOME/.wakeup
  2. Paste the following into your terminal:
#!/bin/sh
/usr/sbin/diskutil list | \
  awk '/Apple_HFS/ {print $NF}' | \
  xargs -I{} /usr/sbin/diskutil mount {}
  1. ⌃D (Ctrl-D) to terminate the input stream.
  2. chmod +x $HOME/.wakeup

Test the script by running $HOME/.wakeup. Your external drives should be mounted.

That’s it. Now when you sleep your computer, any external drives should automatically be unmounted4. Upon wakeup, any HFS volumes should automatically be mounted.


  1. Basically, with more than one monitor hooked up to the laptop, the lid acts as an on/off switch for the laptop monitor. 

  2. I’m only dealing with external USB/Firewire HFS drives. The one test I ran with a USB key fob and Samba network share worked fine. Test everything out in your own environment! 

  3. Since I’m always logged in as an admin user, I created my .sleep and .wakeup scripts in my home directory. If you log in as a user that can’t mount/unmount drives, then you should modify the system-wide rc.sleep and rc.wakeup scripts, or create your .sleep and .wakeup scripts in the admin user’s home directory. 

  4. There is still a chance that this may not work if a process has a file lock on a specific volume. In my testing this has worked fine with Time Machine, which will stop the backup properly. 

9:00am
(View comments  
Filed under: OS X tips 
May 13, 2011
MacPorts Maintenance

I use MacPorts to install add-ons for my Mac in an isolated way from the core OS. This is the routine I go through to keep my ports up to date and as lean as possible.

I typically run updates once a month, and this is the procedure I use.

The first thing is to update MacPorts itself:

sudo port selfupdate

I keep older versions of ports around for one update revision in case I need to roll back, so let’s remove those inactive ports.

sudo port uninstall inactive

Are any leaves that we can get rid of1:

port echo leaves

To uninstall the leaves:

sudo port uninstall leaves

Now we can upgrade our ports:

sudo port upgrade outdated

And you’re all set until the next update.


  1. http://guide.macports.org/#using.common-tasks. You can mark certain ports as requested, preventing them from being reported as leaves, i.e.: sudo port -d setrequested autoconf help2man p5-locale-gettext m4 automake libtool pkgconfig 

9:00am
(View comments  
Filed under: MacPorts tips OS X 
April 24, 2011
To a Mentor, Bart Jackson

There are two managers and one teacher whom I would consider my mentors. When I was 21, my manager, Bart Jackson, shaped so many aspects of my life with just couple sentences during a meeting.

“If you want something done, get anyone to do it. If you want something done right, get George to do it.”

With that, he set me on a never-ending pursuit of craftsmanship.

I worked for Bart in the IT department at The North Face1. With almost no experience whatsoever, I suddenly found myself in charge of the entire networking infrastructure of the headquarters, the warehouse, 20 or so stores around the country, and offices in Glasgow and Toronto.

Of course it wasn’t all at once. Bart very smartly had me introduce PCs here and there to replace dumb terminals, allowing me to build up my confidence and trust with our customers. But it was a fast ride from teaching accounting how to use Lotus-1232 to developing complex databases that ran the whole business. I studied far more for work in those three years than for the UC Berkeley classes I was attending at the same time.

That was the easy part. I learned that technical challenges can be tough but usually have tangible and tidy solutions. For most issues that we encounter, the willingness of people to change their process or frame of reference is a much tougher nut to crack.

Bart allowed me to reach far beyond my knowledge, and deep into my capabilities. He allowed me to learn professionalism and self-discipline with invisible guidance, rather than imposing his will on me. Most importantly, he created the space for me to nurture my critical thinking and creative skills by giving me more freedom, not more constraints.

I learned that Bart spent most of his time removing obstacles for his team. He was very good at keeping the bullshit from our group, but always keeping us informed. He also made sure the good work of the department was acknowledged throughout the company. We were able to interact with our customers in a very positive, cooperative, and productive way.

This pride and self-confidence came at the expense of others. The context of being better is almost always in comparison with another department or another person. While some people (like me) thrived on the competition, the effect was demoralizing for others. This too, is something that I carry with me and try very hard not to repeat.

I kept in touch with Bart off and on after we both left The North Face, with the communication trailing off the past 5 years. We never made the transition from a professional relationship to a friendship. Few weeks ago, I received a Facebook friend request from him and I was delighted.

There, on his wall, a single post announced his memorial, with the comments full of fond farewells and how he will be missed.

I won’t miss Bart, because it’s clear to me how much I’ve incorporated him into my work ethics, my management style, and indeed, my personality.

9:26am
(View comments  
Filed under: Musing