Wednesday, 16 December 2009

Django

(Installing on Ubuntu Karmic.)

First of all we need Apache, mod_wsgi and Django itself:

sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
tar -xvf Django-1.1.1.tar.gz
cd Django-1.1.1
sudo python setup.py install


I found out later that Django is packaged for Ubuntu, it's called "python-django", so I needn't have installed from source. Never mind. I'm just setting up a local server on my laptop for development, so I'd also like it to server the media files (the CSS and images) for the admin interface. So I created a symlink:

sudo ln -sT /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/ /var/www/admin_media

We also need a bunch of additions to httpd.conf to set up Django using mod_wsgi:

sudoedit /etc/apache2/httpd.conf

Here I'm mapping "/" to serve my application, but overriding "/admin_media" to point to my media files symlink. Why did I bother with the symlink? Hmm, good question. httpd.conf:

WSGIScriptAlias / /home/weeble/hg/webtest/django.wsgi
Alias /admin_media/ /var/www/admin_media/
<Directory /var/www/admin_media>
Order deny,allow
Allow from all
</Directory>


We need to set up django.wsgi:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'webtest.settings'

sys.path.append('/home/weeble/hg/webtest')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


I had to configure a few settings for my Django application in settings.py. I'm using an sqlite database because it's easy. I have ended up with a silly directory structure here, with two nested "webtest" directories. You probably don't want to copy that. Here are the salient bits from settings.py:

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/home/weeble/hg/webtest/webtest/webtest.db'
ADMIN_MEDIA_PREFIX = '/admin_media/'


Apache runs as a different user, so we need to make sure it has permission to read and write the database file, as well as to write to the directory containing the database file. I did this the bad way (I think I should probably have used chgrp or chown):

chmod o+w webtest.db
chmod o+w .


I forgot to make the directory writeable at first, and this resulted in the admin interface getting a server error whenever I visited it, as did any of my application's pages if they tried to write to the database.

Of course, you need to restart the server to pick up all the configuration changes:

sudo apache2ctl restart

Friday, 11 December 2009

Gargoyle

Gargoyle is a user interface to play various different formats of interactive fiction (text adventure) files.

Get source from here: http://code.google.com/p/garglk/downloads/list

mkdir gargoyle-2009-08-25-sources
cd gargoyle-2009-08-25-sources
unzip ../gargoyle-2009-08-25-sources.zip
sudo apt-get install jam
sudo apt-get install libsdl-mixer1.2
sudo apt-get install g++
sudo apt-get install libgtk2.0-dev
jam
jam install


Binaries are now in build/dist

Should probably put them somewhere sensible and add a symbolic link from ~/bin/gargoyle

Monday, 15 June 2009

Bitmap fonts

Much as I love sub-pixel font rendering, I found that I was not impressed with the blurry mess that comes out when using small font sizes for Terminal windows. I had a look for good terminal fonts that clearly distinguish 0 (zero), O (capital O), I (capital I), l (lower case l) and 1 (one), and settled on Monte Carlo. However, Ubuntu disables bitmap fonts by default.

Enabling bitmap fonts
/etc/fonts/conf.d/ contains a bunch of symlinks to files in /etc/fonts/conf.avail/. One of these is 70-no-bitmaps.conf. I removed it and added a symlink to 70-yes-bitmaps.conf:

sudo rm /etc/fonts/conf.d/70-no-bitmaps.conf
sudo ln -s /etc/fonts/conf.avail/70-yes-bitmaps.conf /etc/fonts/conf.d/
sudo dpkg-reconfigure fontconfig


Note that I think I could have instead followed the instructions here to achieve the same thing in a slightly more pleasant way.

Installing the fonts
I put the font files, MonteCarloBold.pcf and MonteCarloMedium.pcf into the ~/.fonts/ directory. Then I rebuilt the font cache:

sudo fc-cache -v -f

Somewhere in the output there should be a line something like this:

/home/weeble/.fonts: caching, new cache contents: 4 fonts, 0 dirs

(I have some other fonts in there too.)

I needed to log out and in again to see the results.

My eyes, they burn!
Once you've done all that, you will sooner or later discover why bitmap fonts are disabled by default. There are a bunch of nasty bitmap fonts installed with names like "Helvetica" and "Times". These appear when applications or web pages request those font names expecting to get a lovely outline font. To fix this, you can either remove those fonts entirely or add rules to stop them from being used. I went with the latter option, creating a ~/.fonts.conf file:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- ~/.fonts.conf for per-user font configuration -->
<fontconfig>

<!--
    Private font directory
-->
<dir>~/.fonts</dir>

<rejectfont>
    <glob>/usr/share/fonts/X11/*</glob>
</rejectfont>

</fontconfig>


NOTE:I previously had a much more complicated .fonts.conf file which I didn't fully understand but had constructed by trial and error. Every so often I'd find a web-page that used some new obscure font that happened to be included in with the bitmap fonts, and have to add something for it. This new one just blacklists all the built-in bitmap fonts, so only the news ones I've installed to ~/.fonts will be available.

You do not need to rerun fc-cache or reboot after updating .fonts.conf, but you may need to restart an application before it will pick up the correct fonts.

When I was messing around with .fonts.conf, I found the fc-match command invaluable. If you give it a font-name it will tell you what font would actually be rendered. If you also specify --sort, it will list all the fonts that could match in order of preference. Be careful not to mistype font names in the .fonts.conf file, it took me ages to realise my rules weren't firing because I'd mistyped "Bitstream" as "Bitsteam".

Monte Carlo in a terminal window:

Python 2.6.2 and Pygame 1.8.1

After an unfortunate incident with Ubuntu Jaunty, I am back to a fresh install of Ubuntu Intrepid. I still have most of my user folder, but I have to reinstall everything. And I'll probably have to do it again. This blog is to keep a record of what I had to install.

First up, Python 2.6 and Pygame, because I have a little project that is dependent on both. There's no package for Python 2.6 in Intrepid, so I'm installing from source. As far as I can tell there's no risk of upsetting anything else on the system so long as you don't replace the /usr/bin/python symlink.

Getting the source
Python 2.6.2: http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tgz
Pygame 1.8.1: http://www.pygame.org/ftp/pygame-1.8.1release.tar.gz

Getting dependencies for Python
sudo apt-get build-dep python2.5
sudo apt-get install tk8.5-dev


Dependencies for Pygame
sudo apt-get install libsdl-ttf2.0-dev
sudo apt-get install libsdl-image1.2-dev


Build and install Python
tar -xvf Python-2.6.2.tgz
cd Python-2.6.2/
./configure
make
sudo make altinstall
cd ..


Build and install Pygame
tar -xvf pygame-1.8.1release.tar.gz
cd pygame-1.8.1release/
sudo python2.6 setup.py install
cd ..