Category Archives: Misc - Page 8

Changing date format in Trac using mod_wsgi

Trac is a marvelous tool to use for us developers. Unfortunately there are some quirks to it. One is that you can not change the data format in a “normal” way eq. through the admin panel. To change it you have to add a bit of code to the trac.wsgi:

environ['trac.locale'] = 'sv_SE.UTF-8' # Any valid locale will do

Be sure to add it after the “def application” row like this:

...
import os

def application(environ, start_request):
    environ['trac.locale'] = 'sv_SE.UTF-8'
    if not 'trac.env_parent_dir' in environ:
        environ.setdefault('trac.env_path', '/var/trac/my_project')
...

After you have made the change be sure to restart the webserver!
For Apache on Debian:

/etc/init.d/apache2 restart

Tested on Debian Wheezy and Trac v0.12.3

Validate elements in any order and any number of times using XSD

Sometimes you just want to validate any number of elements any number of times. There is no intuitive way in XSD to accomplish this so we have to use a trick to get it to work. The solution looks a little like this:

<xs:element name="myElement">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:choice>
          <xs:element name="myId" type="xs:int" />
          <xs:element name="myName" type="xs:string" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

This lets us validate any of the elements in the <choice> (myId and MyName) list any number of times and in any order, so the following will validate:

<myElement>
  <myId>3</myId>
  <myName>Niklas</myName>
</myElement>

and:

<myElement>
  <myName>Niklas</myName>
  <myId>3</myId> 
</myElement>

and:

<myElement>
  <myId>3</myId>
  <myName>Niklas</myName>
  <myName>Anders</myName>
</myElement>

but not:

<myElement>
  <myId>hello</myId>
  <myName>Niklas</myName>
</myElement>

Last one does not validate since ‘hello’ is not a integer.

Tested with xmllint with libxml version 20708

Using rsync to download large files over ssh with resume option

In the last months I have been doing this a lot so it might be best to put it up here so I don’t have to google it every time. There is a lot of cool features to rsync. The one we will concentrate on here is the –partial option that lets us resume a broken download. Have to start over with the download of a large file can be a real pain

Basic syntax

rsync --partial --progress --rsh=ssh user@host:remote_file local_file

I also use the –progress option to output the progress of the download

Example:

rsync --partial --progress --rsh=ssh niklas@niklasottosson.com:.vimrc .

This will download the file ‘.vimrc’ from my home directory on niklasottosson.com and into the folder I’m currently in. If the download is interrupted rsync will keep the partial downloaded file and resume from it when the downloaded is started again

Tested on OSX 10.7.4 and rsync version 3.0.3