tags: #code | #python | #til

2012-10-10 12:00

Regular Expression Non-Capturing Groups (or, what’s "(?:" mean?)

(originally from https://web.archive.org/web/20150422001421/http://ajcsystems.com/blog/blog/2012/10/10/regular-expression-non-capturing-groups-or-whats-mean/)

When I was working recently on changing poodledo‘s parser from using a lexer to using regular expressions, I found myself implementing some crazy logic to try and ignore the results from matched groups whose matches I didn’t actually want (since I was using parentheses for their “pick one of these two alternatives” function instead of their “pull out this specific chunk as a match” function).

For example, take the string “task title @context name #due date”. Starting with the at-sign, I want to match all words until some other symbol is reached; or, to avoid confusing the parser, match everything following the at-sign and between two square brackets. Here’s the regex I came up with:

(^|\W)@([\b ]?(([\w\"\'.@]+ )*[\w\"\'.@]+)[\b]??|[\b(.+?)\b])

Using this regex, though, gave me a lot of extraneous matching groups:

>>> q = r'(^|\W)@([\b ]?(([\w\"\'.@]+ )*[\w\"\'.@]+)[\b]??|[\b(.+?)\b])'
>>> task = 'task title @context name #due date'
>>> re.findall(q, task)
[(' ', 'context name', 'context name', 'context ', '')]

How do I determine programmatically which of these groups is the one I want?

Digging around in the Python regex documentation, I found a section about non-capturing groups, which seemed like it would save me a significant amount of sanity. Apparently, Perl-5-compatible regular expression parsers often implement a regex extension syntax by following an open parenthesis with a question mark, and then some other symbols that indicate which extension is in use. In this specific case, “(?:” indicates a group whose matches shouldn’t be captured in the output list. Eureka!

>>> q = r'(?:^|\W)@(?:[\b ]?((?:[\w\"\'.@]+ )*[\w\"\'.@]+)[\b ]??|[\b(.+?)\b])'
>>> re.findall(q, task)
[('context name', '')]

Now the only logic I need is “Ignore matching groups which are empty”, my sanity is preserved, and all is well with the world.

tags: #code | #javascript

2012-10-08 12:00

Dr. jQuery-Cycle, or how I learned to stop using Keynote and love HTML

(originally from https://web.archive.org/web/20150422001009/http://ajcsystems.com/blog/blog/2012/10/08/dr-jquery-cycle-or-how-i-learned-to-stop-using-keynote-and-love-html/)

We have a number of fancy dashboards displaying on TVs here at $work, and some of them are implemented with Keynote to get the endless-cycling effect. This is lame because Keynote is very hard to use for this purpose (precise grid positioning, versioned changes, lots of other stuff).

Read more…

tags: #linux

2012-10-02 12:00

Magic SysRq to the Rescue!

(originally from https://web.archive.org/web/20150422203246/http://ajcsystems.com/blog/page/2/)

I recently had to fix a server which had been knocked off the network due to some bad configuration (very long story). When I logged into the remote graphical console, I was greeted with constant, unremitting spam from the kernel log! I wasn’t able to log in because the screen would clear instantly when I started typing.

This calls for the Magic SysRq key! A simple press of Alt+SysRq+0 to set the console log level to 0 silenced all of the log spam and let me log in and get the server back on its feet.

All hail Magic SysRq!

tags: #openstack | #til

2012-10-02 12:00

The Unbearable Lightness of User-Data

(originally from https://web.archive.org/web/20150423071705/http://ajcsystems.com/blog/blog/2012/10/02/the-unbearable-lightness-of-user-data/)

When you launch an instance in OpenStack, it has a big empty box called “User Data”:

image

I never knew what that box was for until today.

How it’s provided

If an image is configured to use cloud-init, when it starts up it will curl a special path:

$ curl http://169.254.169.254/2009-04-04/user-data/

Since this is a link-local “I don’t have a DHCP address yet” IP, it will work even before networking is configured. The output from this URL is whatever you put in “User Data”.

How it’s used

cloud-init has a bunch of different tweaks it can apply as an instance launches; it can run apt-get upgrade, import ssh keys, disable sshd PasswordAuthentication, run arbitrary commands, and much more. This sample config will give you a pretty good idea about the full capabilities of cloud-init.

Basic example

When you launch an instance, put the following in “User Data”:

#cloud-config
ssh_pwauth: True

This will make cloud-init enable PasswordAuthentication in sshd (which it otherwise disables by default), so that you don’t need to use a key to log in to the instance.

tags: #mythtv

2012-08-11 12:00

I love it when a plan comes together

(originally from https://web.archive.org/web/20150504012625/http://ajcsystems.com/blog/blog/2012/08/11/i-love-it-when-a-plan-comes-together/)

I recently purchased a HDHomeRun Prime to replace my first-gen HDHomeRun, with the intention of using a CableCard to replace my ugly kludge of grabbing video from a cable box via Firewire. TL;DR – it just worked.

I set up the new HDHR and pointed MythTV at it, and it was detected and set up easily. I chatted with Comcast’s online tech support to ask how to get a CableCard, and they said I could either have a tech deliver one or pick it up myself at a service center. I opted to drive to the service center, where I waited for just a couple of minutes in a line of three people. The desk representative knew what a CableCard was and dug one out for me in a flash; I don’t think I was in the building more than five minutes. When I got home, I connected it to the new HDHR and went through the online activation process. After about a half-hour (as they warned me it might take that long), I fired up mythtv-setup and tried to scan channels on the CableCard. It worked great the first time, all of the channels and schedules downloaded, and I was able to start watching every channel I cared about on my HTPC immediately.

It’s so very rare that everything just works the way it’s supposed to with all this high-tech computer stuff, I like to savor the moments when things do go well. I had read a lot of horror stories on the web before planning all of this, but I couldn’t be happier about how it’s worked out.

Now to resolve the issue with the OpenGL UI painter so the program guide can occupy the entire screen… it’s good to know that there’ll always be something else to fix, too.

tags: #code

2011-11-13 12:00

Neat new software I’ve written recently

(originally from https://web.archive.org/web/20150422203246/http://ajcsystems.com/blog/page/2/)

I’ve been on a bit of a coding tear lately. I’ve written a FUSE filesystem for Amazon Cloud Drive, and also made a lot of improvements to my Toodledo API library, including writing a command-line tool modeled on Tom Limoncelli’s Cycle System from Time Management for System Administrators.

FUSE Filesystem for Amazon Cloud Drive

Github; does what it says on the tin. I couldn’t find a supported way to upload music from Linux, so I made one. Currently I’m a bit hesitant to call it production-ready, as Amazon has an eight-device-per-account limit and this appears to consume multiple slots instead of counting as just one extra device. I’m hopeful I can get that sorted soon.

Poodledo (Toodledo API Library)

Github; I wrote (well, forked and extended) a Python library for interacting with the web-based task management system Toodledo. Toodledo has quite an extensive ecosystem, including a light version of the website suitable for embedding, SMS and Twitter integration, an official iOS client, and several Android clients (Due Today is what I use). However, there was no easy way to add tasks from a command line, and also it wasn’t possible to create tasks with metadata (due date, project name, priority, etc.).

With the Python library in hand, I wrote a couple of tools to add and manage tasks from a command line. tdcli implements a super-fancy lexer which can add metadata to a task while creating it, e.g. “Create a task that’s due today and part of the Poodledo project #today *Poodledo”.

“cycle” for Toodledo

The other tool I wrote for interacting with Toodledo is cycle, modeled after Tom Limoncelli’s Cycle System from Time Management for System Administrators. It would take a while to explain; you should read the book, as it’s excellent. Here are some examples of its use.

tags: #puppet

2011-10-13 12:00

How to make a custom Facter fact that’s also a script

(originally from https://web.archive.org/web/20150422203246/http://ajcsystems.com/blog/page/2/)

At $work, we use Puppet to manage some of our servers, and part of that involves writing custom facts for Facter, Puppet’s tool for gathering node information. (If you’re not familiar, here’s a quick description of Facter.)

These custom facts can use information present on a given host, such as hostname or IP address, to make their determination. However, we’ve also found it useful to be able to ask a custom fact, “what answer would you give if you were run on a given host?”

The answer:

This is a simple Facter fact that takes a hostname and determines which Ganglia grid to place it in. When run as a part of Facter, it will operate as a fact:

$ facter architecture => x86_64 [...] grid => build hardwareisa => x86_64 hardwaremodel => x86_64 [...]

But when called as a standalone script, it will operate as though it were running on that host:

$ ./grid.rb build01 build

Building facts like this will let you keep any custom logic you might have in one place, but reuse it in different contexts. Code reuse FTW.

P.S.- If you’re thinking about posting a comment to the effect that Facter can be run with one or more arguments of facts to output:

$ facter architecture x86_64

you’re missing the point. This technique allows you to run Facter facts on a different host than the target and still get the benefit of the logic embedded in the fact.

tags: #code

2011-10-06 12:00

Poodledo library updated

(originally from https://web.archive.org/web/20150421212536/http://ajcsystems.com/blog/blog/2012/10/06/poodledo-library-updated/)

I spent some time the past few days updating and improving the Python library I maintain for integrating with Toodledo. The big new features I’m excited about are automatically tab-completing your folders, goals, etc. when creating a new task (as I can never remember exactly what I called them without checking), and adding multi-line notes. Now I can add a new task, pick out the right project and such right from the command line, and ramble on to add whatever other notes I want to write down right there.

I’m hoping that this new set of features will help me get back in the habit of adding new tasks directly to Toodledo, instead of just writing them in a text file and going back to reprocess them later. :-P

Check it out at github.

tags: #linux | #mac os x | #til

2007-02-12 12:00

Caveats when migrating from Mac OS X to Linux for serving Mac OS X home folders

(originally from https://web.archive.org/web/20150422203246/http://ajcsystems.com/blog/page/2/)

Also known as, “Things I wish I’d known about on Friday, part 1″. :-P

  1. Mac OS X is, generally speaking, case-insensitive. It’s possible to format a disk with a case-sensitive version of HFS+, but that’s not usually done and (I imagine) not well supported or documented. The underlying BSD layer will accept any case for filenames, and preserves the case of files as they are created, but does not actually care about it when accessing them.

    Samba hosted on a Linux box, on the other hand, cares quite a bit about case. By default, the “case sensitive” parameter is set to “auto”, which means “no for Windows clients and yes for everybody else”. Mac OS X reports itself as a Samba client, of course, which means that the Samba server assumes it is asking for a case-sensitive file or folder and attempts to serve it. Unfortunately, it’s not.

    As a workaround, it’s possible to set “case sensitive = no” as a share-level option for shares accessed in Mac OS X, which works to a point. Finder will properly ignore the case of files when requesting them from the Samba server (or, more likely, will accept a filename of any case as a successful open), but other apps in OS X do not honor this setting. Notably among them in my testing is Microsoft Word.

    Dealbreaker. =( I had to go manually rename the home folders of each user that uses the Samba server to match the case of their usernames in the Active Directory (which is CamelCase).

  2. Network mounts over the AFP protocol will permit users to save files which contain various scary characters in their names (such as slashes, question marks, asterisks, etc.). Network mounts over Samba do not permit these characters, and mangle the filenames for display by the user agent. The mangled filenames look much like the Windows backward-compatible-long-file-names (i.e. 6CHARS~1), probably on purpose. Mac OS X Server does its magic by way of the UTF-8-MAC file encoding, which as far as I can tell is supported only on Mac OS X Server’s version of Samba. :'(

    I have 5,000 or so files to rename now.

There’s more, but I have to take a call, so that’ll all be in part 2.

tags: #humor

2006-12-21 12:00

Nerds have a sense of humor, too

(originally from https://web.archive.org/web/20150421220346/http://ajcsystems.com/blog/blog/2006/12/)

This will also be a place for me to foment discussions and post funny or interesting things that would not be of general interest to my LiveJournal folk.

Case in point: The ABCs of Linux, which is quite geeky and entertained me thoroughly. I’m pleased that I knew all of the commands except for the one for “O”.

Well, pleased and distraught in equal measure. ;-)