Amass

Recursive Line Count

October 2nd, 2008 Luke

I often get curious about just how many lines of code I’ve written for a particular project. This short snippet will determine that for you, when run from within the project directory:

for file in `find . -type f`; do cat $file; done | wc -l

How to clear the cache in Django

September 23rd, 2008 Luke

Recently, I was tasked with building a ‘clear cache’ button for a Django project. While this is something that I’m sure a lot of people have approached, none of them seem to have shared their code. Here’s what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
from django.core.cache import cache
try:
	cache._cache.clear()	# in-memory caching
except AttributeError:
	# try filesystem caching next
	old = cache._cull_frequency
	old_max = cache._max_entries
	cache._max_entries = 0
	cache._cull_frequency = 1
	cache._cull()
	cache._cull_frequency = old
	cache._max_entries = old_max

Fixing _default_ VirtualHost overlaps

September 16th, 2008 Luke

I’m currently working on setting up a server at work, and one of the things that we have been working on adding is a placeholder page for any requests that go to the server but don’t have a website currently set up for them. I added this configuration file, and symlinked it as 000-default in /etc/apache2/sites-enabled/:

<VirtualHost *:8080>
	ServerName *
	LogLevel info
	DocumentRoot /www/placeholder/website/
	ErrorLog  /www/placeholder/logs/error.log
	CustomLog /www/placeholder/logs/access.log combined
 
</VirtualHost>

However, after adding that configuration file and restarting apache, these are the messages I got:

 * Stopping web server apache2
[Tue Sep 16 16:59:07 2008] [warn] _default_ VirtualHost overlap on port 8080, the first has precedence
   ...done.
 * Starting web server apache2
[Tue Sep 16 16:59:08 2008] [warn] _default_ VirtualHost overlap on port 8080, the first has precedence
   ...done.

…And after that happened, you could only access the placeholder site. The fix was simple, however - just add these two lines to your primary apache2.conf:

ServerName 127.0.0.1
NameVirtualHost *:8080

And that’s all there was to it! Once that was finished, the placeholder page (and all the other pages on that server) worked like a charm.

Using Capistrano to deploy Rails apps without Source Control

July 17th, 2008 Luke

Someone I take very seriously does a lot of Ruby on Rails work - and as a result of that, I figured that I should at least take a look at the framework.

Over the weekend, I whipped up a quick little app, without using source control - it’s a quick project, and I’m not really concerned if it suddenly vanishes into the ether.

However, when it finally came time to deploy it, there was a bit of a hiccup: Capistrano’s deployment process usually involves some sort of source control - whether that’s Subversion, CVS, Darcs, or something different entirely(like Git).

Luckily however, getting Capistrano to deploy your app when it’s not under source control is a matter of only 3 lines of code, inside deploy.rb:

set :repository, "."
set :scm, :none
set :deploy_via, :copy

Once you’ve made those changes, all you need to do is run your deployment commands from within your project’s directory - and Capistrano will copy all the files over for you, without requiring you to have them under source control.

DotNetNuke’s IE7 printer-friendly bug - fixed!

July 15th, 2008 Luke

If you’ve worked much with DotNetNuke, you may have noticed a bit of an odd bug: if you use IE7 and click on the printer icon next to a module for a printer-friendly page, the CSS on the page you clicked the icon on…breaks.

This is because the way that DotNetNuke’s module actions work is to inject Javascript into the page, using Response.Write() - having a <script> tag at the start of the HTML document makes IE7 throw up.

The fix is actually very simple(for a developer), however - you need to download the DotNetNuke source, and change line 159 of Library\Components\Skins\ActionBase.vb to this:

159
Page.ClientScript.RegisterStartupScript(Me.GetType(), "DotNetNuke.NewWindow", String.Format("<script>window.open('{0}','_blank')</script>", Command.Url))

Once that’s finished, build your DotNetNuke library - and copy the resulting DotNetNuke.dll into the bin directory of any website that you want to add the fix to. If you’re feeling really lazy, you can download my copy of the dll(built for version 4.8.4) below:

DotNetNuke IE7 print bugfix DLL