Archive for June, 2008

Linux tip of the day — Working with links

25.06.2008 19:44

posted by: Benjamin Smith

Posted Under: , ,

I recently decided that I needed to come up with a system for managing custom and commonly used python modules. In doing so I decided upon a directory structure similar to this:

volatile/
dev/
prod/

Where volatile would be an unsafe set of packages, not yet tested in a staging environment (constantly updated), dev would be the packages currently in the staging environment and prod would be the packages currently installed, or ready to deploy to the production environment. This has been done before, nothing new. Along the way I thought to myself, how can I conserve disk space, avoiding up to three copies of the same file? An easy and logical way to avoid this would be hard links. So, rather than having three copies of a file that gets approved through to production, I would hardlink back to the original. I will demand that new files into volatile be uniquely named, so avoid overwriting. As files become obsolete, they will be pruned down the line (a system to handle this would need to be developed).

SO! Now that we got passed the background, too the yummy tips.

I have this awesome directory structure of existing files, right? How do I duplicate it creating links instead of files, and creating the directories as needed(can’t safely hardlink directories). I knew I could use rsync with --link-dest. It would look something like this:

rsync -avz --link-dest /volatile /dev/ \
rsync -avz --link-dest /volatile /prod/

-a says to preserve things, -v is verbose, -z says compress and --link-dest says to make links of the destination files.

I also thought of some find/exec magic creating directories and linking files, it would look something like this:

cd /volatile; find . -type d -exec mkdir -p /dev/{} \; \
find . type f -exec ln {} /dev/{} \;

Repeating the same thing for the /prod/ directory.

Both the above solutions feel hackish, but would work. I thought, there has to be a simpler way, this is a fairly routine operation. After checking out google, I was pointed to the cp(1) man page, which uncovered this gem:

-l, --link
       link files instead of copying

Woah, I knew it. So much shorter and uses the right tool for the job!

cp -r -l volatile/* dev/; cp -r -l volatile/* prod/

It’s amazing what you’ll uncover with a tad bit of googling and man page searching!

0 comments | 0 pingbacks

All the cool kids are doing it, right?

16.06.2008 9:05

posted by: Benjamin Smith

Posted Under: , ,

Here’s my wordle, with my name removed. All my posts contain my name, so it was among the largest of words! I couldn’t look conceded, now could I?

4 comments | 0 pingbacks

Wow, out of date much?

09.06.2008 22:42

posted by: Benjamin Smith

Posted Under: ,

So, I was installing FormEncode on some servers today and ran across a fun ‘warngin’.

Check it out:

Running FormEncode-1.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-iKCYq0/FormEncode-1.0.1/egg-dist-tmp-3yiKdV
warngin: no files found matching '*.py' under directory '.'
warngin: no files found matching '*.html' under directory 'docs'
no previously-included directories found matching '**/.svn'
zip_safe flag not set; analyzing archive contents...
formencode.fields: module references __file__
formencode.api: module references __file__
formencode.util.doctest24: module references __file__
formencode.util.doctest24: module MAY be using inspect.getsourcefile
FormEncode 1.0.1 is already the active version in easy-install.pth

Installed /usr/lib/python2.3/site-packages/FormEncode-1.0.1-py2.3.egg
Processing dependencies for FormEncode==1.0.1
Finished processing dependencies for FormEncode==1.0.1

At first, I naively emails the modules author, Ian Bicking. Shortly thereafter I realized that the message most likely wasn’t from his module, but the distribution utility used to install it.

I got home and quickly rushed to my Inbox, which revealed that Ian had confirmed my thoughts. A quick google search (which I should have done in the first place) showed that this is an old “bug” from waaay back when with distutils. It kind of hurt to know that some of my servers have code that old on them…Good thing they’re not publicly accessible.

Man, it really helps to do some quick searching/checking before jumping the gun, and man do I need to get these systems up to date!

3 comments | 0 pingbacks

Using python with postfix pipe.

08.06.2008 13:56

posted by: Benjamin Smith

Posted Under: , ,

Dear Lazyweb,

I was wondering if anyone here had experience with using a postfix pipe to a python program for processing mails. Most of my background in mail processing is with qmail, Perl and procmail.

From what I can tell there appears to be a ‘pipe’ command that I could use in an alias to point it at my python program, then use the ‘email.FeedParser’ class to keep it from blocking. For now I’m parsing mails from the maildir via the ‘email.Parser’ class, then moving them around. It would be very nice to process them as they come in. This would eliminate overhead from firing up python whenever I want to parse messages and make my process somewhat real time.

Any ideas/snippets/caveats are greatly appreciated!

P.S. I’m doing this in python to hook into byteflow, the django app powering this site.

5 comments | 0 pingbacks

Amazon outage.

06.06.2008 14:59

posted by: Benjamin Smith

Posted Under: , ,

I’ve been following the amazon outage via google news and google blog search.. Anyone else cackling an evil laugh about this? If you work for a larg(ish) web site, you know how funny this is and how much it sucks for everyone (at amazon) involved. I am just relieved that it’s not us.

1 comment | 0 pingbacks
Posted Under: ,

The PyOhio organizers have extended the Call for Proposals deadline to June 15! Several potential speakers have requested extra time to prepare submissions, and now you can too.

http://wiki.python.org/moin/PyOhio/CallForProposals

Don’t be shy… speaking at an event like PyOhio helps you share your knowledge, deepen your expertise, broaden your horizons, meet people, practice for bigger events like PyCon, and have fun.

See you at PyOhio!

0 comments | 0 pingbacks

What happened to K.I.S.S?

05.06.2008 16:40

posted by: Benjamin Smith

Posted Under: ,

I was just sitting here, looking at some code that I had previously stared at for hours trying to dig out a solution from a nasty mess. It’s relatively old code (> 2 years) that I’ve not dove into for some time, but I felt it necessary to hunt this bug.

As I’m pulling my hair out and repeatedly yelling “Had I wrote this..” in my head, I drifted away for a moment and remembered an old saying I used to tell myself when going off on a wild idea. K.I.S.S.- Keep It Simple Stupid!.

This also applies to system architecture! Why complicate things! There is usually a linear, yet secure, scalable and robust way to do something, hunt it out! Never, never, never setting on your first idea, and never, never, never settle on anything without some sort of peer review. If you work in an environment with smart people, use them!

Ok, done ranting :D Thanks for listening!

0 comments | 0 pingbacks

Byteflow usability quick change.

05.06.2008 10:11

posted by: Benjamin Smith

Posted Under: , , ,

Submitted this to the byteflow hackers, figured some might be able to use it as well. Code blocks don’t wrap lines (<pre>), probably want to have control of how it overflows.

Here’s the patch:

# HG changeset patch
# User bsmith@linode.just-another.net
# Date 1212606740 14400
# Node ID 9337a362eeacbe88db2da11d38e238db2644e31b
# Parent  93c1106436dd09dde52ec1dd25cfbbeca7cec00b
Add scroll if lines don't wrap in code blocks.

diff -r 93c1106436dd -r 9337a362eeac static/css/highlight.css
--- a/static/css/highlight.css  Wed Jun 04 09:47:44 2008 +0300
+++ b/static/css/highlight.css  Wed Jun 04 15:12:20 2008 -0400
@@ -1,6 +1,7 @@ pre code {
 pre code {
    display: block;
    background: #F0F0F0;
+   overflow:auto;
 }

pre code,
2 comments | 0 pingbacks

Byteflow preview blog post

04.06.2008 10:55

posted by: Benjamin Smith

Posted Under: , ,

Byteflow has a feature when authoring blogs that allows you to save your post as a draft. I use it constantly, but someone requested via a ticket to have a “Preview” option, allowing you to see your blog post (logically the same as saving it as a draft) before you post it. If anyone is interested in utilizing a “Preview” option, see the patch below. Basically all it does is utilize the draft option, but takes you to the generated draft to “Preview” it before you make it public. I don’t think it will be committed into the code base, but if anyone is looking to have this functionality now, see below code.

# HG changeset patch
# User bsmith@linode.just-another.net
# Date 1212543690 14400
# Node ID 4282e3429846d5193a798bdd14807f6c3ef579ac
# Parent  2c4fb2b21cc5c7b9cfbf7a1692af6e34a9bb5391
Preview post (and draft it).

diff -r 2c4fb2b21cc5 -r 4282e3429846 apps/blog/models.py
--- a/apps/blog/models.py       Mon Jun 02 19:41:33 2008 -0400
+++ b/apps/blog/models.py       Tue Jun 03 21:41:30 2008 -0400
@@ -7,7 +7,7 @@ from django.utils.html import strip_tags
 from django.utils.html import strip_tags
 from django.contrib.contenttypes import generic
 from django.dispatch import dispatcher
—
+from lib.exceptions import RedirectException
 from lib.helpers import reverse
 from render import render
 from blog.managers import PostManager, PublicPostManager, FeaturedPostManager
 @@ -38,6 +38,7 @@ class Post(models.Model):
    upd_date = models.DateTimeField(_(u'Date'), auto_now=True, editable=False)
     is_draft = models.BooleanField(verbose_name=u'Post would act as draft', default=False)
     is_featured = models.BooleanField(verbose_name=u'Featured post', default=False)
+    is_preview = models.BooleanField(verbose_name=u'Preview Before Submission', default=True)
    enable_comments = models.BooleanField(default=True)
    tags = TagField()

@@ -53,7 +54,7 @@ class Post(models.Model):
        search_fields = ('name', 'text')
        list_filter = ('date', )
        fields = (
—            (None, {'fields': ('author', ('name', 'slug'), 'tags', 'text', 'render_method', 'date', ('is_draft', 'enable_comments'))}),
+            (None, {'fields': ('author', ('name', 'slug'), 'tags', 'text', 'render_method', 'date',('is_preview','is_draft', 'enable_comments'))}),
            ('Featured post', {'classes': 'collapse', 'fields': ('is_featured', 'teaser')}),
            )
        if settings.WYSIWYG_ENABLE:
@@ -80,7 +81,13 @@ class Post(models.Model):
            self.slug = slugify(self.name)
            self.text = self.text.strip()
            self.html = render(self.text, self.render_method, unsafe=True)
—        super(Post, self).save()
+        if self.is_preview:
+            message = 'This is a preview of your post'
+            self.is_draft = True
+            super(Post, self).save()
+            raise RedirectException(self.get_absolute_url(), notice_message=message)
+        else:
+            super(Post, self).save()

    def comments_open(self):
        if settings.COMMENTS_EXPIRE_DAYS:

And to implement it via a template, try something like this:

{% if object.is_preview %}
  <div id="post-draft">THIS IS A PREVIEW <a href="/admin/blog/post/{{ object.id }}/">Continue Editing</a></div>
{% endif %}
5 comments | 0 pingbacks

Yay for contribution!

02.06.2008 19:45

posted by: Benjamin Smith

Posted Under: , , ,

I was having troubles with the way my rss item categories (tags) were displaying on technorati and other feed readers, so I decided to fix it. This is why I love F/OSS! Since I’ve been using this software I’ve contributed a whopping 2 patches (heh, heh). It might not be a lot, but it matters! I didn’t post my last patch, but I will from now on. Here’s the most recent one:

# HG changeset patch
# User bsmith@linode.just-another.net
# Date 1212450093 14400
# Node ID 2c4fb2b21cc5c7b9cfbf7a1692af6e34a9bb5391
# Parent  fb8d51dd50e13eb7d2b63c646a9f2bb5cf21ec3c
Atom class wants a dict for item categories, django does not.

diff -r fb8d51dd50e1 -r 2c4fb2b21cc5 apps/feed/blog_feeds.py
--- a/apps/feed/blog_feeds.py   Fri May 30 11:13:58 2008 +0300
+++ b/apps/feed/blog_feeds.py   Mon Jun 02 19:41:33 2008 -0400
@@ -59,7 +59,10 @@ def _BlogEntries(Feed, type='atom'):
             return {'type': 'html'}, html

         def item_categories(self, item):
—            return ({'term': unicode(tag)} for tag in item.get_tags())
+            if (type == 'atom'):
+                return ({'term': unicode(tag)} for tag in item.get_tags())
+            else:
+                return (unicode(tag) for tag in item.get_tags())

         def item_links(self, item):
             return ({'rel': u'self', 'href': self.item_id(item)},

Hope this helps!

0 comments | 0 pingbacks

Yay! Just moved hosting providers

02.06.2008 14:31

posted by: Benjamin Smith

Posted Under: , ,

I just made the move to Linode for my hosting needs. After some initial ab tests, I’ve seen improved results out of my current setup. Previously I had hosted my site at home on an older Linux box I had laying around, and before that I had hosted on my friends colo via a business cable line. Now I’m on a true hosting environment that fits my needs nicely.

0 comments | 0 pingbacks
Previous month Next month