Best way to check if a directory is an brz branch

John Arbash Meinel john at arbash-meinel.com
Tue Feb 17 18:40:38 GMT 2009


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

Nicholas Allen wrote:
> 
>> def is_branch(p):
>>     from  bzrlib import (branch, errors)
>>     try:
>>         br, relpath = branch.Branch.open_containing(p)
>>     except errors.NotBranchError:
>>         return False
>>     if relpath != '':
>>         return False
>>     else:
>>         return True
> 
>> should work.
> 
> Would I not need to catch other exceptions too? What if it is a branch
> but there is a problem opening it? How do I know what open_containing(p)
> will throw?
> 
> With Python being so lenient and unverbose about everything it makes
> obtaining this information much harder. I really wish Bazaar had been
> written in Java ;-)
> 
> Nick
> 

I would certainly use "branch.Branch.open()" to force it to only open
the exact path that you are checking. Then you don't need the "relpath
!= ''" check.

As for other errors, there are certainly more that *could* be thrown as
part of Branch.open() but I don't think you'd want to catch them. (Stuff
like ConnectionError, etc.)

If you want, you could suppress all bzrlib errors with:

except errors.NotBranchError:
  return False
except errors.BzrError, e:
  trace.warning('suppressing unknown bzr error: %s', e)
  return False
except Exception, e:
  trace.warning('suppressing unknown error: %s', e)
  return False

However, if it isn't an error you explicitly know about, I would think
you want to propagate it.

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

iEYEARECAAYFAkmbBKYACgkQJdeBCYSNAAONFwCffBukGaQoEG3d7cMDysuKSumx
xNgAnjbUE9ESkRuVsLONRUTsAAUeh3i+
=qQMF
-----END PGP SIGNATURE-----



More information about the bazaar mailing list