Wednesday, April 9, 2008

Gotcha with git-svn dcommit?

http://kerneltrap.org/mailarchive/git/2007/8/21/255431: this should be kept in mind/verified when using git-svn.


Tracking multiple branches in svn repo with git-svn

For some time now, have wanted to use git and git-svn to track the subversion repo at work. That repo has lots of branches, and individual changesets are merged very often between branches, making it a nightmare to find out what has already been merged, and what not.
Used to keep track of all this in a text file. Its becoming tedious. Have been trying to use git-svn to ease the pain.
On the first try, git-svn gave up halfway during the clone operation with an error message about protocol/network error. This git-svn was the one in ubuntu repositories.
Since then, installed git from source.
First I tried this way. It works, but then gitk will only show me one branch: the one I have currently checked out.
Now, I am trying this other way. Hopefully this will work better.

Saturday, April 5, 2008

Code merging

Wanting to merge two branches of code, both of which have had concurrent commits going on them for some time. Code is kept in svn, so the branches actually live in separate directories. Most changesets have been merged back and forth, yet residual changes remain.

I get diff to tell the files which are different between the branches.

diff -q -r -x '\.svn' -x '*.pyx' -x '*~' branch1 branch2


This produces lines like:



Files zeroc-ice-python_3.2.1/config/Make.rules and zeroc-ice-python-3.2.1-new/config/Make.rules differ
Only in zeroc-ice-python-3.2.1-new/config: Make.rules.GNU
Only in zeroc-ice-python-3.2.1-new/config: Make.rules.GNU_kFreeBSD

I need to munge the output of diff. I want to have 4 columns: directory (sans the branchname), filename, link to the file in branch1 if it exists there, link to file in branch2 if it exists there. I want this ordered/sorted by directory, and I want the output in emacs org mode format. I filter the output of the above diff command through this perl script:


while (<>) {
if (/^Only in/) {
m/Only in (.*): (.*)$/; $dirname=$1; $fname=$2; # sep dirpath and filename
($branch = $dirname) =~ s/([^\/]+).*/$1/; # the branch name is the first part of the dirpath (e.g. branch-xy/dir/path/file)
$dirname =~ s/$branch\/?(.*)/$1/; # remove the 'branch-xy' from dirpath to get the remaining dirpath
$_ = "| $dirname/$fname | [[file:~/workspace/couffable/branches/$branch/$dirname/$fname][Only in: $branch]] |";
}

if (/^Files /) {
m/Files (.*) and (.*) differ/; $left=$1;
$left =~ m/release-3-11\/(.*\/)?([^\/]+)/;
if (defined($1)) { $dirname=$1; } else { $dirname = ""; }
$left=$2; # remove branch name, sep dir and file
$_ = "| $dirname$left | [[file:~/workspace/couffable/branches/release-3-11/$dirname$left][r311]] | [[file:~/workspace/couffable/branches/release-3-11-airtel/$dirname$left][r311-airtel]] |";
}
print $_ . "\n";
}

Friday, April 4, 2008

Tesla = Pylons + SQLAlchemy + Elixir

Tesla is a Paster template creating Pylons applications using SQLAlchemy/Elixir ORM. Adds some simple database paster commands. Includes the following features:


  1. Create model classes

  2. Simple database commands (create/drop tables)

  3. Migrations (using SoC migrate library)

  4. Create and run batch scripts

  5. Handles SQLAlchemy setup and session refresh


Now, will Tesla/Elixir allow me to have composite primary keys of my choosing? Can I have, for most models, an Elixir definition, and for some, a more flexible SQLAlchemy mapper? How?


The reason I'd like to do that is: ActiveRecord does a poor job of structuring database tables for performance


What a bunch of libraries! Sometimes you wonder if plain PHP with embedded SQL isn't better after all. No, no, that was heresy...

Tuesday, April 1, 2008

Blogging from GNOME Blog

After several futile attempts at blogging from within Emacs to Blogger.com, I found the GNOME Blog applet and am using it now. It is not emacs, but at least it is less clunky than the blogspot web interface.

Monday, March 31, 2008

Nose: db setup and teardown

In the last post I noted some documentation related to nose and ORM. Well, they did not work for me because my setup was not exactly like others'. Here is what worked for me. In

tests/__init__.py
:

  • I imported
    from pylons import config
    to get the SQLAlchemy engine embedded in pylons's config variable

  • I imported
    import quickwiki.model as model
    so that I could get hold of my models and metadata

  • I created a class
    TestModel
    inheriting from
    TestCase
    to hold the setup and teardown code

  • In the
    tearDown
    method, I do
    model.metadata.drop_all(bind=engine)
    to destroy all tables

  • In the
    setUp
    method, I call
    tearDown
    to destroy tables if they have not already been cleaned up, and then call
    model.metadata.create_all(bind=engine)
    to create the tables.



Here is the final code:

"""Pylons application test package

When the test runner finds and executes tests within this directory,
this file will be loaded to setup the test environment.

It registers the root directory of the project in sys.path and
pkg_resources, in case the project hasn't been installed with
setuptools. It also initializes the application via websetup (paster
setup-app) with the project's test.ini configuration file.
"""

import os
import sys
from unittest import TestCase

import pkg_resources
import paste.fixture
import paste.script.appinstall
from paste.deploy import loadapp
from routes import url_for

__all__ = ['url_for', 'TestController']

here_dir = os.path.dirname(os.path.abspath(__file__))
conf_dir = os.path.dirname(os.path.dirname(here_dir))

sys.path.insert(0, conf_dir)
pkg_resources.working_set.add_entry(conf_dir)
pkg_resources.require('Paste')
pkg_resources.require('PasteScript')

test_file = os.path.join(conf_dir, 'test.ini')
cmd = paste.script.appinstall.SetupCommand('setup-app')
cmd.run([test_file])

from pylons import config
import quickwiki.model as model

class TestModel(TestCase):
"""
We want the database to be created from scratch before each test and dropped
after each test (thus making them unit tests).
"""

def setUp(self):
self.tearDown()
engine = config['pylons.g'].sa_engine
model.metadata.create_all(bind=engine)

page = model.Page()
page.title = 'FrontPage'
page.content = 'Welcome to the QuickWiki front page'
model.Session.save(page)
model.Session.commit()



def tearDown(self):

engine = config['pylons.g'].sa_engine
model.metadata.drop_all(bind=engine)


class TestController(TestModel):

def __init__(self, *args, **kwargs):
wsgiapp = loadapp('config:test.ini', relative_to=conf_dir)
self.app = paste.fixture.TestApp(wsgiapp)
TestCase.__init__(self, *args, **kwargs)




Well, that almost worked. I fell foul of the 'setup-app' command and 'setup-config' in websetup.py. As you can see, the 'tests/__init__.py' file loads and executes the paster 'setup-app' command. Stands to reason: the app should be 'set up' before I run tests.

My setup-app is responsible for creating the DB and populating it with some initial data. But now I can't repeat my tests, because the first time I run the tests, the db is created and initial data put in, and the next time I run the tests... poof:

IntegrityError: (IntegrityError) column title is not unique
u'INSERT INTO pages (title, content) VALUES (?, ?)' ['FrontPage',
'Welcome to the QuickWiki front page']


Well, I thought, I would just drop everything in the db in the tearDown and ensure that tearDown is called before setUp is run. No go. For some reason, it seems, the tearDown() is not working.

Well, it seems I must have at least one test, for the fixtures to be run. So I created a dummy test, and all was well.

Pylons, Paste, Nose and ORMs

Trying to do some unit tests in Pylons. Pylons uses nose. However, the Pylons Unit Testing guide is a little short on describing how to setup and teardown the database before each test. Here is all the docco to hand: