Tagged: python

Using python-dulwich to load any version of a file from a local git repo

On Monday we are kicking off an innovation week (more to come on that topic) and I’ve devised a little project that includes nearly every buzzword I’m interested in.

I’m spending some time doing some technical spikes to see what is possible and I found a need to load a particular file from a git repo given the path and tree hash.

I grabbed my trusty python, dulwich (native python-git library) and gave it a shot. After a few minutes writing complicated looking recursive code I jumped over to irc where the friendly author pointed me to a convenience function that does what I needed.

Here is the short answer:

from dulwich.repo import Repo
from dulwich.object_store import tree_lookup_path

r = Repo('/Documents/projects/gitdep/rails')
def get_file(tree, path):
    (mode,sha) = tree_lookup_path(r.get_object,tree,path)
    return r[sha].data

tree = '7e7331fce169bbe1d6be71a30c1e1f7ab2e6ceba'
path = 'activemodel/examples/validations.rb'

print get_file(tree,path)

This gives me a rails validation file from last year. Nothing special about this file, I just find the rails git repo an interesting playground for git experimentation.

OSX vs Ubuntu, Windows wins?

Ever since Barcamp I’ve been shopping for a personal laptop for general use as well as a development machine that I could use for work.  Our work issued machine is a loaded mac powerbook.  With a unix core osx has given me much of the power that I used to enjoy when I’d used linux as a primary os. However Apple is not doing wonders for free software and I felt the need to get back to my roots and move back to Linux.

I picked up a 4lb HP DM4 and dual booted to Ubuntu with not issues.  The trackpad didn’t work right, but a script off someones blog made it work reasonably ok.  A quick hop into debug mode showed that the drivers returned negative x-y coordinates when you use 2 fingers.  I downloaded the open source drivers to take a look at the code and saw that a patch was already in head.

Developing was a joy and eclipse opened nearly instantly.

Microsoft silverlight DRM is not (yet?) ported to linux so I dual boot to windows 7 to get the customer experience and use CIM products.  A funny thing happened to me while getting the ‘customer’ experience.   I liked it!   Windows 7 is much more usable then either osx or Linux and IE9 looks like it will be a really powerful platform for future development.

Around this time I picked up a python update on OSX and spent about 4 hours trying to get mysql and python to talk to each other.  I booted into windows and thought about how to use this platform as a development machine.

Enter BitNami. Rather then deal with version conflicts I grabbed a virtual machine that is close to my target server and installed VMware Player.  Mapping a windows directory to the virtual machine lets me edit files in native windows while running my build chain on Linux.  The browser I use for development is finally the same browser used by the majority of my customers and my dev environment is much closer to my server environment as well.

For not I’m working through the rough edges of this setup and trying to find a decent windows SSH client but this setup seems to have legs.