I had my CouchDB development environment up and running along just fine until I tried to start adding external httpd handlers written Python. I was starting my CouchDB instance with

sudo launchctl load -w /Library/LaunchDaemons/org.apache.couchdb.plist

and stopping it with

sudo launchctl unload -w /Library/LaunchDaemons/org.apache.couchdb.plist

I would get a cryptic Erlang message with ‘OS Process timeout’. So I figured it was a permissions problem and I was right. I tried chmod on the handler scripts, the directory the handler scripts were in and all kinds of other shots in the dark, until it dawned on me that it was only a problem when the server was started with

sudo launchctl load -w /Library/LaunchDaemons/org.apache.couchdb.plist

so after way to much time was spent trying useless things I dug into the .plist file and found

<key>UserName</key>
<string>couchdb</string>

the problem is that user couchdb doesn’t have permission to do anything and the scripts were owned by root:wheel. I figured I was starting the server through launchctl as sudo it should have root permissions. That isn’t the case. I changed the entry in the .plist to

<key>UserName</key>
<string>root</string>

and all my external handlers started working when couchdb was started using launchctl. I am sure that is a “bad” thing to do, but for my development environment it is ok. What I will do in the actual server environment is make those handlers executable by user couchdb and that should work.

 

Most people don’t know you can do Spotlight searches from the command line. Why remember all the arcane find and grep options and what not when you can let Spotlight do the work for you. The command line interface to Spotlight is called mdfind. It has all the same power as the GUI Spotlight search and more because it is scriptable at the command line!

If you want to find all the files on your computer that have something to do with Erlang for example you can use the following command line:

mdfind erlang

If you wanted to find only the files with Erlang in the name you would use the following:

mdfind -name erlang

If you wanted to find only the Erlang source files you would use the following:

mdfind -name .erl

If you wanted to find all the header files in your Erlang projects src dir, assuming you are in the src dir you would use the following:

mdfind -onlyin . -name .hrl

One caveat the -name option only appears to work in Snow Leopard ( 10.6.x ).
I usually create some aliases to mdfind with common options like the following:

alias spot=‘mdfind -onlyin . $1
alias spotf=‘mdfind -name $1

So the next time you go to do a find and a grep for some contents of a file or a files name try mdfind

 

I am using Paramiko to do some remote ssh work and could not figure out how to change directories and execute a script with the SSHClient.execute_command() function. I finally figured out that .execute_command() is basically a single session, so doing a .execute_command('cd scripts') and then executing the script with another .execute_command() reverts back to your default directory. The alternatives are to send all the commands at once separated by a ; .execute_command('cd scripts; ./myscript.sh'), or to use the .interactive() shell support. Since I only needed to fire off this script I used the first solution.

 

Erlang has long had a love hate relationship with external code. There is now “experimental” code in the latest R13B03 release that has support for what they are calling Native Implemented Functions. It is a very basic interface with only 4 functions required to be implemented and a struct and macros for defining your public interface from your native C code. I am got the example code from Paul Joseph Davis’s blog to work. It took some delving into GCC compiler options on OSX and getting Erlang compiled with ./configure --enable-darwin-64bit flag. Now that I have an example working I am re-thinking my inet_mdns implementation. The fact that I can’t get gen_udp:send/4 to work on port 5353 cripples my implementation. I have about 80% of the mdns spec implemented, but without being able to send on source port 5353 it is just a partial solution. I am thinking about just using the Apple mDNSResponder code that is cross platform and I know it works in Java, Python and Objective-C. It shouldn’t be too hard to get a NIF based glue layer working. Since my target platforms are really OSX and Linux and maybe Solaris, I might as well spend some time seeing what I can get working.

 

I am using XCode 3.2.1 for some Cocoa development and I really miss the OPTION-CMD-L short cut in Intellij IDEA that re-formats your current source code file as you have it configured. I found you can get something similar but not as powerful by doing a Edit -> Select All and then CTRL-I in XCode 3.2.1. Not as configurable but it fixes up misaligned braces and other minor annoyances.

 

I have tried all the available plugins for WordPress to do inline code formatting and syntax highlighting. None of them do exactly what I want, which is no scroll bars horizontally or vertically and none of them support Erlang. So I found the best way to get exactly what I want is to use SubEthaEdit and do a Select All and use the Edit -> Copy as XHTML. Yeah it is a mess of ugly XHTML. But I just paste it into the HTML view when creating a post and I am done with it.

 

This is Objective-C code for listing all the files and directories below a given directory. I wrote and tested this on OSX 10.6.2 and XCode 3.2.1.

/*
 This is example code of how to walk a directory recurisively
 and create a flat list of fully qualified names for all the files
 and directories under the supplied virtual root directory.
 */

#import <CoreServices/CoreServices.h>
#import <AppKit/AppKit.h>
#import <stdarg.h>

int main (int argc, const char * argv[])
{
    int result = EXIT_SUCCESS;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
    NSString *dir = [args stringForKey:@"file"];
    NSMutableSet *contents = [[[NSMutableSet alloc] init] autorelease];
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL isDir;
    if (dir && ([fm fileExistsAtPath:dir isDirectory:&isDir] && isDir))
    {
        if (![dir hasSuffix:@"/"])
        {
            dir = [dir stringByAppendingString:@"/"];
        }

        // this walks the |dir| recurisively and adds the paths to the |contents| set
        NSDirectoryEnumerator *de = [fm enumeratorAtPath:dir];
        NSString *f;
        NSString *fqn;
        while ((f = [de nextObject]))
        {
            // make the filename |f| a fully qualifed filename
            fqn = [dir stringByAppendingString:f];
            if ([fm fileExistsAtPath:fqn isDirectory:&isDir] && isDir)
            {
                // append a / to the end of all directory entries
                fqn = [fqn stringByAppendingString:@"/"];
            }
            [contents addObject:fqn];
        }
        
        NSString *fn;
        // here we sort the |contents| before we display them
        for ( fn in [[contents allObjects] sortedArrayUsingSelector:@selector(compare:)] )
        {
            printf("%s\n",[fn UTF8String]);
        }
    }
    else
    {
        printf("%s must be directory and must exist\n", [dir UTF8String]);
        result = EXIT_FAILURE;
    }
    
    [pool drain];
    return result;
}

 

Here are a couple of aliases I create on a fresh OSX machine.

alias dir=‘ls -AlGh’
alias spot=‘mdfind -onlyin . $1

The dir alias is all the most useful ls options all at once.
mdfind is the command line interface to the Spotlight service on OSX. You can use the option -name to restrict the results to only search file names.
The spot alias is the most useful options that I use all the time. A faster replacement for find.

PS1="\[\e[0;37m\][\[\e[m\]\[\e[0;32m\]\u\[\e[m\]\[\e[0;31m@\[\e[m\]\[\e[0;32m\]\h\[\e[m\]\[\e[0;37m\]]\[\e[m\] \[\e[0;37m\][\[\e[m\]\[\e[0;34m\]\w\[\e[m\]\[\e[0;37m\]]\[\e[m\]\n"

Here is what it looks like:

shell preferences

shell preferences

© 2012 Jarrod Roberson: Programming Missives Suffusion theme by Sayontan Sinha