Recent Updates RSS Hide threads | Keyboard Shortcuts

  • Snakes on Demand: How to write a Python Launchd Agent

    mike 9:23 pm on March 14, 2010 | 0 Permalink | Reply
    Tags: launchd, , macdev, multiprocessing, , python

    Suppose you want to write a Launchd agent in Python that communicates using UNIX domain sockets. There’s no sample code for that, but the information is out there to figure out how (especially because the launchd source code is available). Most search results will tell you that you need to read the Daemons and Agents Tech Report TN2083, but it’s pretty long and not a great tutorial. A better intro reference is Chris Hanson’s blog post “Launchd: better than sliced bread!”, but that doesn’t tell you everything, and doesn’t mention Python.

    I decided I’d share what I’ve learned recently to make getting started a little easier. I’ll give a short description here and post the resulting code to bitbucket at py-launchd.

    Note: This was written and tested on OS X 10.5.8 with the default /usr/bin/python, version 2.5.1. Since I wanted to use multiprocessing, I used the backport to 2.5 available at google code: python-multiprocessing . For convenience, it’s included in the repository, and so is its license.

    Goal

    What we’re trying to do is have some python code that gets called when launchd notices someone connecting to a UNIX domain socket. UNIX domain sockets are local-only, so this is ideal for agents that only serve local apps. We’re also using launchd “agents” not “daemons”, so we’re assuming that it’s OK to have one agent process for each user. If you’re managing access to something that needs to be unique system-wide, then this won’t work (but you can still use Launchd).

    launchd overview

    The launchd process will read a launchd plist you give it (at login, via launchctl or the 10.6 framework), and listen on the socket you tell it to. Once it sees a connection, it’ll start the agent program you specified in the plist, and that program can make some calls using the launchd C API to get a file descriptor for the socket that was connected. This is important – you don’t need to call bind() or listen() on the socket, because launchd already did. It’s just handing the open socket’s file descriptor straight to you and your code can just call accept() on it.

    Using launchd with Python

    You need to create a C/ObjC tool that can do the launchd check-in to get the open file descriptor, then pass that off to your Python code. This is pretty straightforward using the Python framework included in OS X and the Python C APIs.

    What I’ve done is create an agent loader that I called PyLaunchd that loads and runs server code in Agent.py. It expects Agent.py to be in the same directory.

    PyLaunchd is built separately and copied into the test app’s Resources folder with an XCode script phase.

    I have the app delegate copy PyLaunchd and Agent.py to ~/Library/Application Support/PyLaunchd/ on loading. It also customizes the launchd plist to set the path correctly for the current user, then writes that to ~/Library/LaunchAgents/, and loads it. (Actually it first unloads it, then reloads it. I’m not convinced this is the right way to do it). It uses system() to call launchctl, but I believe in OS X 10.6 there’s an API you can call to do it directly.

    Finally, the sample app is really simple – it just opens the socket using the multiprocessing Client class, and sends whatever you type. The example Agent I’ve included will ROT13 it and send it back.

    Please let me know if you have any comments, questions, or improvements.

     
  • How BibDesk generates Apple Help and a web manual

    mike 11:51 pm on February 16, 2010 | 1 Permalink
    Tags: apple-help, bibdesk, help, texinfo

    This is something that had been on my old blog in 2006 and went away, so I’m reposting something I wrote to the macsb list a few years ago:

    For the BibDesk open source project, we wanted to have legit in-app apple help, as well as a web page (and PDF), without maintaining separate sources, since we were all working out of spare time.

    We tried a few free options, and eventually worked out something decent with Texinfo, a latex-like markup language that can be processed into latex (and then PDF) or HTML.

    I wrote a custom init file to have the (standard) texi2html script print out apple help compatible HTML. One post-processing script and a simple xcode script build phase later, and we get apple help.

    We also get a version a little more suitable for the web (more content per page) from the same source: http://bibdesk.sf.net/manual/

    It’s worked well for a couple of years, and it’s a good choice if you don’t mind learning texinfo (it’s pretty nice compared to writing in Docbook or HTML, IMO), and very good for version-controlled collaboration, since it’s just text.

    For an example, here’s what the help source text looks like:

    http://bibdesk.svn.sourceforge.net/viewvc/bibdesk/trunk/bibdesk/English.lproj/BibDesk%20Help/bibdesk.texi?view=markup

    The init file and other stuff is here: http://bibdesk.svn.sourceforge.net/viewvc/bibdesk/trunk/bibdesk/BibDesk%20Help/

    If anyone’s curious about it, feel free to ask me questions here. I’ll try to remember how it works.

     
    • Kevin Grant 8:21 pm on February 21, 2010 Permalink | Reply

      I had a similar problem in my project (also creating Apple Help pages), although I was mostly just looking for something that was more compact to maintain than HTML.

      I found Textile: http://en.wikipedia.org/wiki/Textile_(markup_language)

      Specifically, the pytextile tool: http://code.google.com/p/pytextile/

      Like in your case, I still felt that I needed a processing script and build step to make the most complete web pages.

      But in terms of input, the Textile format is pretty trivial; the most common page elements are reduced to one or two prefix characters most of the time, e.g. a list bullet is a “*”, a heading might be “h1.”. And raw HTML can be directly embedded for corner cases.

      The other neat thing about the language is that it interprets intent pretty well, e.g. if you were to type “2 x 3″, it would assume that the “x” should become the Unicode multiplication sign in the HTML output; similarly, it handles curly quotes and other conversions implicitly from a raw ASCII line.

  • Tinkering in the Sideshow

    mike 2:31 am on February 3, 2010 | 1 Permalink
    Tags: ipad, melancholy,

    Last week, Alex Payne and Mark Pilgrim both made some heartfelt arguments explaining why they think the fact that Apple’s vision of the computing future as a relatively closed appliance is depressing. I agree, and I want to explain exactly why, now that I think I’ve figured it out.

    Each post got plenty of responses. Dissenters have a wide variety of reasons for why it’s no big deal, ranging from “tomorrows tinkerers will just play with different technology, like bio-mecha” (fascinating!), to a lot of people saying, essentially, “Just don’t buy the appliance if you want a computer” (obvious!).

    Others have made a solid point that the iPad is the next big computing paradigm. Steven Frank calls it a “New World” device. I can’t help but agree, and of course powerful devices that empower instead of confuse the user are a good thing. Replacing most of the world’s overcomplicated, fussy laptops with focused and reliable devices is a good thing.

    Of course, general purpose, hackable computers are not really going away, not soon. Even if everyone uses an iPad for personal stuff and a Chrome OS netbook to access private cloud services for business work, there will still be a need for workstations, and I hope the people building complete personal systems out of Free Software don’t give up.

    However, in a response to comments on his post, Mark Pilgrim made a gloomy prediction: “People haven’t figured it out yet, but Mac OS X is on its last legs. By 2015, Apple will make appliances and developer add-ons. Not general purpose computing devices.”

    If you ask me, this is the real problem.

    The problem is that hackable computers that I want to use could very well be fading out. Maybe Mark’s overstating things, but I would not be surprised to see the Mac, and OS X, as a relatively low priority for Apple in the near future. Design and development effort and creativity will naturally go increasingly toward the more profitable platform – the one making computing finally pleasant for the normal person.

    I’m worried that this will happen for third-party developers too. I’m not the only one. Back in 2002, Brent Simmons described developing user code for the Mac as “the show”. Is there any doubt that Apple’s mobile OS is “the show” now?

    Where does that leave the rest of us who still have to or want to use a more powerful platform?

     
    • Sean 11:19 pm on February 13, 2010 Permalink | Reply

      Well said. I worry about the exact same thing. Maybe the linux world of the future will be getting a lot of Mac refugees?

  • App History

    mike 1:02 pm on November 25, 2009 | 0 Permalink
    Tags: ,

    I just saw Brent’s post from a couple weeks ago about an App History App – I think that’d be really cool. I especially like the screen shot idea, since I love seeing posts from a developer showing what an app looked like along the progression. It’s too easy to forget to document that kind of thing, and I’d love it if we saw more of these behind-the-scenes posts.

     
  • Measuring PDFs with Skim

    mike 5:00 pm on November 6, 2009 | 0 Permalink
    Tags: , , skim,

    Skim Measurement

    I love it when I can guess how to do something new in a program. Skim just did this for me – I needed to figure out how big the rectangle of text in a paper I’m working on is, in inches. I looked for rulers, and then decided I’d just see if I get any size feedback when I make a rectangular selection. Turns out there’s a nice feedback status line in the bottom right – clicking toggles between measurement in pts and in inches.

    I like how this solves my problem without adding a lot of new UI for measurement – no rulers, no extra tool to measure length.

     
  • mike 10:25 am on November 3, 2009 | 2 Permalink
    Tags: electropaint, nostalgia, screensaver, SGI

    If your childhood memories include an SGI Indy set up underneath the coat rack downstairs, maybe you’ll get the same time-traveling wonder out of the Electropaint Screensaver. I found a Universal Build of ElectropaintOSX at vonBelow.com, and it’s just how I remembered. There’s a picture there, but you have to see it in motion. It’s like a deck of cards circling a bug-zapper. It’s beautiful.

     
    • Buzz Andersen 11:05 pm on November 3, 2009 Permalink | Reply

      My dad worked for NCAR, a big SGI customer back in the day, so you’re not far off. My dad got them to donate an O2 as a web server for my college newspaper when I was the web editor.

    • Nicholas Riley 12:49 pm on November 4, 2009 Permalink | Reply

      Thanks! We had Indys at my undergrad school (screenshot of one of my sessions from 13 years ago, aiee: http://sabi.net/ex-sites/brandeis-staff/beryl-screenshot.jpg). I vaguely recall an issue where you couldn’t use OpenGL screensavers with the screen locker, so I never got to see as much of Electropaint as I wanted.

  • mike 12:08 pm on October 26, 2009 | 3 Permalink
    Tags: apps, awards, community,

    I’d like to see a community-run version of the Apple Design Awards.

    Anyone could nominate an app, and the developer community would vote on who wins, in just a few categories.

    I’m thinking the categories could be a little more user-centric than Apple’s. For instance, I don’t really care about best adoption of OS X technologies, but something like “best refinement of a common task” or “best default behavior” or “best scriptability” would be interesting, and something that’d be easier to use as a marketing badge…

    This is just off the top of my head, so there are probably others. “Least data lock-in”? “Easiest to learn?”, “Best plugin community”?

     
    • Josh 6:25 pm on October 26, 2009 Permalink | Reply

      Good idea! Especially for those apps that will never get an ADA (regardless how good they are -> conflict of interest at Apple).

      My nominations (not sure about the exact categories yet): Productivity: Mindnode, CSSEdit, Mellel Helper-Apps: Dropbox, fruux, Expandrive Dev Tools: HTTP Client, TextMate, iLocalize, Notification Watcher

    • Ann Omyous 6:29 am on October 30, 2009 Permalink | Reply

      Great idea. Right now, you can only get a semblance of this via the great developers who blog, or when someone runs a dev challenge.

  • Email Death Link Dump

    mike 1:01 pm on October 14, 2009 | 0 Permalink
    Tags: , facebook, twitter, wave

    A recent Wall Street Journal article noted (pay link) the rise of alternatives to email. The article itself is balanced and mentions that new ways of communicating are likely to just add to how much time we spend with this stuff, not replace email and cure its problems. Whether that’s good or bad depends on what you’re communicating, I guess.

    The article inspired a number of posts about the death of email, misunderstanding the article, but providing some interesting info. Here’s a few of them. Both The End of Email Predicted, Wrong as Usual, from emailserviceguide.com, and Why Twitter and Facebook Will Never Kill E-mail from the technomix blog at FastCompany.com make some good points about some uses for which emerging ‘cloud’ services are just not as good as email. They mostly focus on business, which makes sense. You probably don’t want another company between you and your customers or partners. Imagine if you could only hold meetings at Starbucks – hit or miss, right?

    Rackspace.com’s email hosting blog gives us some data in a post entitled “Is Email dead?” short answer – no. It’s getting bigger.

    And surprisingly, there were a good number of lucid and interesting comments in the slashdot thread about the article. Provided you filter out comments with a score of 3 and lower…

     
  • Etiquette for the resend ping.

    mike 10:33 am on October 1, 2009 | 0 Permalink
    Tags: , etiquette, inboxzero

    inspired - from merlinmanns photostream

    inspired - merlinmann

    From Merlin Mann’s flickr stream, I noticed the book “Time Management for System Administrators” hiding under there, and went over to the O’Reilly site to take a look – there’s a full google preview of the book, and it has a chapter with some good advice about handling email.

    I’ll highlight the one piece of advice that I see there and elsewhere – learn to let go of messages that you didn’t get to in time. You don’t need to waste time thinking about your email back-log.

    I’m not sure if everyone’s to that point yet, but I think a lot of busy people effectively are, and so I’m comfortable proposing a corollary: It’s not rude to re-send an important email request to a busy person.

    Peeking from that pile

    You can’t assume they’ll eventually get back to you even if they read it, and they might be assuming that you’ll re-send if it’s really important.

    We need a convention for a polite way to phrase the resent ping. I’ve been using “This might have fallen off the end of your inbox, so I’m resending it.” The problem is how to remind them without implying that they’re disorganized. Maybe a convention of putting something like [reping] in the subject line would make sense. It might give email clients (or filters) a chance to adjust the priority of a message in a way that’s more meaningful than the useless priority header that only seems to get set on marketing messages and free food notices.

     
  • Better SVN workflow in Emacs

    mike 12:36 pm on September 18, 2009 | 3 Permalink
    Tags: emacs, , vc, workflow

    I’ve found a way to get the 3-column window layout with vc-dir, vc-diff and vc-log I showed in my previous post about SVN workflow in Emacs by default, without the manual setup I was complaining about.

    (Update: It looks like this customization only exists in Emacs 23.)

    The key is that vc-mode uses a standard function, pop-to-buffer, to create the new window for vc-diff and vc-log. This is the function that keeps creating horizontal splits that make no sense on my widescreen display. However, it ends up calling a function called split-window-sensibly, which can be customized (of course!). So, if I set the “Split Height Threshold” to nil, split-window-sensibly will never split horizontally, and then the default behavior just magically does what I want. If I start with just a single vc-dir window and invoke vc-diff and vc-log, I get my three columns!

    To get to the right customization screen, just do M-x customize-apropos <ret> ^Split.*Threshold. That’ll get you the two relevant customization items. Enjoy!

     
    • Ivan Andrus 7:22 am on September 20, 2009 Permalink | Reply

      I’m a bit confused. How do you actually go about running vc-diff and vc-log? When I run the second command it takes over the window instead of creating a new one. Is that the bit that vc-checkin-mike does? If so is there a way that I could see it? That’s the part that I wold be interested in. I don’t that it’s not polished etc.

    • mike 7:55 am on September 20, 2009 Permalink | Reply

      Hi Ivan, here’s what I do:

      1. start with one window (no splits). Usually a file in the directory I’m checking in.

      2. run vc-dir (C-x v d) on the directory I need to check in from.

      3. using the vc-dir mode, type ‘=’ to see the diff for a file (or set of marked files). This splits the window and opens a new vc-diff buffer.

      4. go back to vc-dir mode, and type ‘v’ on that same file or set. Now I have three columns, vc-dir, vc-log, and vc-diff.

      5. I write the log and use M-C-v to scroll down the diff window with the cursor in the log window.

      This works best on really wide screens, and with the Split Width Threshold set to about 100 columns, so it’ll split vertically more than once before reusing windows.

    • Ivan Andrus 4:45 pm on September 20, 2009 Permalink | Reply

      Thanks, split-width-threshold was the piece I was missing.

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel