Leopard changed default partition table type breaking makediskimage.sh

Not sure where exacly I found makediskimage.sh, heck probably saw another project using it, but for sure it has saved a lot of hassle back when I was learning how to create disk image files.

Unfortunately it doesn’t have any author or history so I never knew who to thank :(
With the Leopard upgrade it suddenly stopped working. The HFS format step was failing and was just outputing it’s help text. Looking at the line just above in the script that generated that output, I noticed that the echo output was not correct:

...
created: /tmp/20096.dmg
Creating HFS partition Chandler_iosx_debug_0.7.3.dev-r15733 on /tmp/20096.dmg at

usage: newfs_hfs [-h | -w] [-N] [hfsplus-options] special-device
options:
...

The line that starts “Creating HFS partition…” should end with “at /dev/disk5s1″ - what drive the HFS formatting would have happened on.

The bash script segment responsible for that is:

DEVICES=`hdid -nomount $TMPFILE`
DEVMASTER=`echo $DEVICES| awk '{ print $1 }'`
DEVHFS=`echo $DEVICES| awk '{ print $5 }'`
echo Creating HFS partition $NAME on $TMPFILE at $DEVHFS
newfs_hfs -v "$NAME" $DEVHFS

The line of interest is the output of hdid - up until Leopard it would output something like:

oliver:~ bear$ hdid -nomount /tmp/bear.dmg
/dev/disk2          	Apple_partition_scheme
/dev/disk2s1        	Apple_partition_map
/dev/disk2s2        	Apple_HFS

Which shows the drive and two partitions, but under Leopard it now outputs:

imac3:~ bear$ hdid -nomount /tmp/bear.dmg
/dev/disk5          	GUID_partition_scheme
/dev/disk5s1        	Apple_HFS

When you pass that to an environment variable (which, remember, removes all line feeds and makes it into a long string) you now get:

DEVHFS=/dev/disk5  GUID_partition_scheme  /dev/disk5s1  Apple_HFS

Instead of

DEVHFS=/dev/disk2  Apple_partition_scheme  /dev/disk2s1  Apple_partition_map  /dev/disk2s2  Apple_HFS

Basically to try and get to an actual point :) - when

awk '{ print $5 }'

is run on the new output there isn’t a fifth item so you get nothing back.

The fix is to switch to a method that isn’t dependent on the layout of the columns *and* the lines:

DEVHFS=`hdid -nomount $TMPFILE | grep Apple_HFS | awk '{ print $1 }'`

Now we take the raw output, grep for the line we really want and grab the first column of that. With the extra bonus of it working now on all OS X’s I could get my hands on to test.

Anywho, just wanted to post this in case someone else runs into their makediskimage.sh script suddenly start to fail.

reply to The Tao of Mac post about Chandler

comments have not been enabled so I’m answering the question here and going to do a trackback…

Rui Carmo posted a small bit about Chandler:

I used to think this would be able to stand up to the Outlook hegemony, but I find the current GUI hopelessly confusing. And what’s with the need for a separate Intel package?

While I can’t answer about the GUI thought I can about why we publish a separate package for Intel-based OS X Macs.

Basically the decision was made back when our choices when Panther and Tiger were both PPC and Intel was *very* new, was to go with two package that were smaller in size. Another concern was that the techniques for creating a universal binary was still new and un-proven in the Python and wxPython realm.

Thankfully that is not an issue now and one of the things I’m working on is a change to do exactly that. I still feel that we will probably have a huge download size but I guess we will find out.

update: of course after hitting send I realized I can speak also about the GUI issue: The Chandler GUI reflects/exposes a very powerful set of tools to help manage and deal with incoming items (mail, tasks, etc) and also the daily routine of deciding just what needs to be done when and how. So it *is* very busy and that can, I think, come off as confusing. What’s missing in our Preview is a road-map for completely new users.

my take on where RCS is now

Back when OSAF was looking into replacing CVS about the only stable and wide-spread option I found was Subversion and while it had some flaws, compared to CVS it was much better. At the time I had heard of other projects using things like BitKeeper and Perforce and other commercial apps., but that’s the kicker - they were commercial and as an Open Source shop we really wanted to use Open Source tools.

So we went with Subversion and really haven’t had any regrets. Merges and branches were much saner and after the Subversion team released the FSFS (filesystem storage) the server side repository issues went away.

Recently tho people have been asking me about distributed version control systems like mercurial, bazaar and darcs.

The first two I’ve been following indirectly by keeping track of the work the Mozilla build/release team have done in trying to pick their next system. See J. Paul Reeds blog entry Version Control System Redux Redux and also their wiki page for details. The short-form result is they haven’t chosen one yet.

darcs I’ve been following more directly because I’ve started using it to try and help the Buildbot team with some patch and bug work. So far, while it’s been different, I haven’t had any issues but I also haven’t gotten much beyond the more basic operations. I plan on posting a more detailed look at it after I do some true distributed operations.

One of the newer programs (for me that is) that has hit the nets with some force is GIT but I think that is because recently Linus Torvalds recently gave a talk at Google about it and seems to be on a tear promoting it :)
But it really seems to be worth looking into. A recent article about it’s design, GIT for Computer Scientists, really showed some interesting internal concepts and I like what I see. So I need to figure out how to work it into my project space to give it a fair look.

There are a couple other distributed systems I haven’t talked about, Monotone and Arch, but that’s because I know next to nothing about them. If you want to find out details on them I would recommend reading the Version Control Blog for articles about them - it’s a great read.

getting the exit code from a batch file that is run from a python program

Normally I like to keep titles short and simple, but this time I wanted to get all of the keywords up front so anyone else who has this problem can find it :)
First an outline of the problem and how Chandler uses batch files.

Chandler uses two batch files (and two bash scripts for OS X and Linux) to start Chandler and our own build of Python from the command line. The scripts make sure the environment is somewhat sane and preset the PATH and PYTHONPATH variables to avoid any local environment issues from “leaking” into the Chandler runtime environment.

To make sure that our values don’t leak back out into the user’s environment we use setlocal/endlocal to “wall off” our changes, but this comes with a price. Once the endlocal boundary is crossed you lose the value of ERRORLEVEL and any variable you may have put ERRORLEVEL into for safe keeping, for example:

set RC=
setlocal
somecommand.exe
set RC=%ERRORLEVEL%
echo %RC% %ERRORLEVEL%
endlocal
echo %RC% %ERRORLEVEL%

Assuming somecommand.exe returns 1 you will get this for your output:

1 1
0 0

Not exactly helpful is it? While working on the problem Andi from work discovered a possible solution that is pretty slick - take advantage of the command chaining of & and the delayed expansion of environment variables:

set RC=
setlocal
somecommand.exe
echo %ERRORLEVEL%
endlocal & set RC=%ERRORLEVEL%
echo %RC%

This will generate the proper result so combine that with exit /B and it generates the proper result.

endlocal & exit /B %ERRORLEVEL%

Except for one small problem :)
exit /B doesn’t “transfer” properly when the batch file is called from a Python program using subprocess. I even worked on variations that involved shell=True and other attempts. It just would not take the return code and pass it on.

After spinning my wheels for a while on this, dropping it for a couple days and then coming back and spinning more wheels I finally decided to look at some of the bug reports for Ant and Maven to see how they solved it. It turns out that they really don’t solve the problem of getting the exit code value but rather just get *any* value to be set to flag that something had happened. The reason for this is because they have a wider range of OS’s to support: windows 95 on up to the current version.

But while reading the bug comments I hit upon an idea that ended up working! Yea for Open Source because otherwise the info would have been buried behind some corporate firewall.

The variation I ended up with was this:

set RC=
setlocal
somecommand.exe
endlocal & set RC=%ERRORLEVEL%
goto omega

:returncode
exit /B %RC%

:omega
call :returncode %RC%

This combines the exit /B trick with the & trick and adds a pretty slick wrinkle (if I do say so myself ;) — by calling the subroutine I can use exit /B to set the errorlevel and because the call is the last command of the batch file that errorlevel is passed out to the caller just like you expect it to be.

So hopefully Google will find this and enable someone to spin their wheels for far less time than I ended up doing :)