wirespeed

the hypothetical maximum data transmission rate of a telecommunications medium

Archive for the ‘perl’ Category

Subversion – how many times has a file been modified?

Posted by dlandgren on 2011-06-08

Someone asked me the other day at work, which file in a project has undergone the most changes. The idea being to look at which files are “hot”, as in frequently touched, and which files are “cold”, rarely edited.

This information is not available directly, but can be assembled from the commit history.

The first step is to produce the list of files changed in each commit since the beginning of time:

svn log -qvr 1:HEAD

which will produce a rather verbose description of what files were changed in each revision:

r382 | david | 2011-04-07 15:32:57 +0200 (Thu, 07 Apr 2011)
Changed paths:
   M /trunk/Assemble.pm
   M /trunk/Changes
   M /trunk/MANIFEST
   M /trunk/README
   M /trunk/t/03_str.t
   M /trunk/t/09_debug.t
   A /trunk/t/10_perl514.t
------------------------------------------------------------------------
r387 | david | 2011-04-17 16:29:59 +0200 (Sun, 17 Apr 2011)
Changed paths:
   M /trunk
   M /trunk/MANIFEST
   M /trunk/t/03_str.t
   M /trunk/t/09_debug.t
   D /trunk/t/10_perl514.t
------------------------------------------------------------------------

What we want to do is throw away all the fluff in each revision stanza, and retain the file paths. A quick Perl one-liner will do that for us, by printing out only the lines between “Changed paths” and a line of dashes. Since this will also include the delimiting lines, the line is also tested to ensure it starts with a space. This is probably overkill, but offers a slightly improved guarantee against surprises.

perl -nle 'print if /^Changed paths:/ ... /^-+$/ and /^\s/'

If the svn log output it piped through this, we obtain

   M /trunk/Assemble.pm
   M /trunk/Changes
   M /trunk/MANIFEST
   M /trunk/README
   M /trunk/t/03_str.t
   M /trunk/t/09_debug.t
   A /trunk/t/10_perl514.t
   M /trunk
   M /trunk/MANIFEST
   M /trunk/t/03_str.t
   M /trunk/t/09_debug.t
   D /trunk/t/10_perl514.t

The next step is to throw away the Subversion action code, and discard any paths not under /trunk (such as /branches or /tags). To do this, we’ll attempt a substitution that eliminates the leading space, some non-space characters and a space, and then capture a path that begins with /trunk. If this succeeds, then print the line:

perl -nle '/^Changed paths:/ ... /^-+$/ and s/^\s+\S+\s+(\/trunk)/$1/ and print'

Now we’re down to:

/trunk/Assemble.pm
/trunk/Changes
/trunk/MANIFEST
/trunk/README
/trunk/t/03_str.t
/trunk/t/09_debug.t
/trunk/t/10_perl514.t
/trunk
/trunk/MANIFEST
/trunk/t/03_str.t
/trunk/t/09_debug.t
/trunk/t/10_perl514.t

Now it’s a simple matter to either grep for the file we want to look for, or count how many times each file occurs, and sort the files by the number of times they appear. The latter is done trivially with the Unix toolkit: sort, count unique occurrences, and sort by count:

sort | uniq -c | sort -n

Which results in

...
  40    M /trunk/t/00_basic.t
  58    M /trunk/Changes
  59    M /trunk/t/03_str.t
 109    M /trunk/Assemble.pm

So putting it altogether, the magic command is

svn log -qvr 1:HEAD|perl -nle 'print if /^Changed paths:/ ... /^-+$/ and /^\s/' \
    | sort | uniq -c | sort -n

And the deed is done.

Posted in perl, programming | Tagged: , | 1 Comment »

Reflections on the Perl6 logo

Posted by dlandgren on 2009-09-26

She: Oh… uh, isn’t that new?
He: Yes it is. Do you like it?
She: Well… what is it?
He: Uh… “New Young American Primitive”.
She: I have a new young American sister. She’s only three and her stuff is really primitive.

—Joan Chandler and John Dall, Rope (Alfred Hitchcock)

It would appear that Larry Wall has (his heart) settled on a logo for Perl6, and he’s been thinking about it for quite some time.

I would also like to make it clear that I’m a just a little tired of these “rounds”; more importantly, that I’ve been mulling over this particular issue for many years.

Which brings us to:

Does the flap of a butterfly's wings set off a tornado?

Does the flap of a butterfly's wings set off a tornado?

Woah, my eyes, talk about Angry Fruit Salad. Of course, this comes from the man who considers chartreuse to be a suitable background colour for a web page…

There is a textual representation of the logo as well, and in fact, it’s only after seeing the text version that the above makes any sense at all:

»ö«

Or if you have Unicode and decent typesetting software:

typographical perl6 logo

Now the meaning becomes clear, it’s meant to be a riff on Perl6’s hyper-operators. Well, fair enough, but really, those bug eyes, that crooked smile, the P and 6 in the wings, that’s supposed to be what? And what will that look like to a colour-blind person? Is this a peculiarly American need, to have a mascot? I’m baffled.

But seriously. In the 10th State of the Onion, Larry suggested that Perl 4 was a pre-teen, and Perl 5 was an adolescent. Perl 6 is when Perl grows up and becomes an adult. And the language is coming along nicely. There are lots of lovely features going into that is going to make it a fascinating language to work in over the next couple of decades. And now this curiously infantile regression.

The biggest problem is that I would have a hard time advocating the language without people snickering at me. I am reminded of Jamie Zawinskie, when he announced his resignation from Netscape, some months after the company was bought out by AOL.

…someone from the New York Times asked me what it would be like working for AOL, given that they represent (in her words) “all that is cheesy and mainstream about the net.” She asked if AOL had lost that stigma. I disengaged my brain and answered,

I think AOL still has all the stigma that it always has, as far as image goes. My friends keep saying jwz@aol.com and then laughing uncontrollably…

I envisage a conversation at work when I would suggest that Perl6 would be a secret weapon that would boost programmer productivity and give us the edge that allows us to roll out new applications faster and better, and the other people in my team rolling their eyes and laughing. “Hey guys, let’s break out the acrylic paints and do some fingerpainting!”

The language scene is a lot more crowded these days. Perl is going to find the going a lot harder this time around. Not to mention the rise of worse is better and the likes of PHP.

I can’t see how this is going to help matters.

I just can’t take any of you seriously
Fire up the batmobile, ’cause I gotta get outta here …

—Liz Phair, (Fire Up The) Batmobile

So please, anyone, anywhere, come up with a better one, before it’s too late, ok?

Posted in perl | Tagged: | 9 Comments »

Using Perl to scan a Lotus Notes database quickly

Posted by dlandgren on 2009-04-22

I had a couple of hundred messages lying around in the depths of my work e-mail account. They were old Majordomo subscribe/unsubscribe alerts for a mailing list I managed (until we switched over to Mailman). I had kept them around because one of these days I figured I’d load the information into a database to track the evolution of the subscriber base.

I haven’t managed to get around to doing that yet, but I did want to get rid of the messages. All I needed was the subject line of the mail (which contained all the necessary info) and the date the message was received. The idea of doing it manually would have been a nightmare. I searched around a bit for a way of automating the task, and discovered that it could be done through OLE.

So I wrote a quick Perl program to do it. It went like this:

use strict;
use warnings;

use Win32::OLE;

my $Notes = Win32::OLE->new('Notes.NotesSession')
    or die "Cannot start Lotus Notes Session object.\n";
my $db = $Notes->GetDatabase("MyServer/MyDomain", "mail/mymail.nsf")
    or die "Could not open database.\n";
my $all = $db->AllDocuments;

foreach my $n (1 .. $all->Count) {
    my $doc  = $all->GetNthDocument($n);
    my $item = $doc->GetFirstItem('Subject');
    if (!$item) {
        warn "doc $n has no subject\n";
        next;
    }

    my $subject = $item->{Text};
    next unless $subject =~ /^(?:UN)?SUBSCRIBE my-mailing-list/;
    print $doc->GetFirstItem('DeliveredDate')->{Text}, " $subject\n";
}

and presto, the deed was done. This saved me I don’t know how many hours of mind-crushingly boring and RSI-inducing cutting and pasting. It’s so trivial it’s not even worth bundling up, so this is probably the best place for other people to stumble across it in a search (hi!).

I’m not sure if Strawberry Perl is bundled with Win32::OLE, but this works straight out of the box with ActivePerl.

Posted in perl, programming | Tagged: , | 8 Comments »