Best way to check if a directory is an brz branch

Eduardo O. Padoan eduardo.padoan at gmail.com
Tue Feb 17 12:01:50 GMT 2009


On Tue, Feb 17, 2009 at 8:51 AM, Nicholas Allen
<nick.allen at onlinehome.de> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> I'm wondering what's the best way to check if a directory on the file
> system is a Bazaar branch. At the moment I do something like:
>
> if os.path.isdir(os.path.join(directory, '.bzr/branch')):
>
> but this seems overly tied to the implementation details of Bazaar. I
> couldn't find a method to do this. One way would be to try to open a
> branch for a directory and catch exceptions but that seems inefficient
> and would also fail if there was a problem with the branch itself.
>
> I expected a static method in Branch or BzrDir to check this. eg
> is_branch(directory).

You canb try to open it, and catch the NotBranchError.

from bzrlib.errors import NotBranchError
from bzrlib.branch import Branch
try:
    b = Branch.open(dir)
    # do something if it is a branch...
except NotBranchError:
    # do something else or pass if not a branch

-- 
    Eduardo de Oliveira Padoan
http://importskynet.blogspot.com
http://djangopeople.net/edcrypt/

"Distrust those in whom the desire to punish is strong."
   -- Goethe, Nietzsche, Dostoevsky



More information about the bazaar mailing list