How to use bzr inside a python script.

John Arbash Meinel john at arbash-meinel.com
Thu Feb 1 22:54:40 GMT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mariano Mara wrote:
> Hi everyone,
> I wrote this Python script that basically connects to an Oracle schema,
> retrieves the whole model and then creates a DML script (let's call it
> output.sql) that can be use to install elsewhere the schema. I want
> output.sql to be versioned with bzr and for that users have an option in
> case they want to commit the new output.sql into a branch.
> My doubt is: how can I call bzr commands from within the python script?
> Can I use it as a imported module? If so, how? or do I need to use it 
> as command lines commands?
> 
> If you can point me to some documents addressing this kind  of
> information, I'll appreciate it.
> Regards

We don't have a lot of documentation (yet) for using bzrlib as a part of
a python script. However, we do have a lot of experience doing it :)

In general the 'bzrlib' module is a self-contained python module that
you can import into your script, and have direct access to a fairly full
featured api. We certainly recommend it over trying to script 'bzr' as a
command-line process.

To get an idea of how to use it, the best place to look is either in
bzrlib/builtins.py or bzrlib/tests/*

builtins.py is where all of the current commands are written. So they
have good examples of how to open a working tree, update it, commit it, etc.

Also, we have a very extensive test suite which makes good use of our
bzrlib apis. They may have things that fit closer to what you might want
to do, but it is a little harder to search for a test that does what you
want.

So my recommendation is to look at builtins.py for things like 'cmd_commit'.

For your basic needs, something like this might also give you a good start:


from bzrlib import (
  workingtree,
  )

# Open up whatever tree we are currently in. open_containing()
# is used because we may not be in the root directory.
tree, relpath = workingtree.WorkingTree.open_containing('.')
tree.commit('Commit message')

Or if you only want to commit specific files
tree.commit('Commit message", specific_files=['path/one', 'path/2'])

# Or if you want to create a new branch
tree.bzrdir.sprout(new_url)

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFwm+wJdeBCYSNAAMRAqAzAJ9+hddEUpof0Xsp5tEvT3iPXO4Z/QCgmI8z
P41vHYjhUzW1BOZzaGfCGtM=
=0sZh
-----END PGP SIGNATURE-----



More information about the bazaar mailing list