Use newer API in bzr plugin.
[jelmer/etckeeper.git] / etckeeper-bzr / __init__.py
1 #!/usr/bin/python
2 # Bazaar plugin that runs etckeeper pre-commit when necessary
3
4 """Runs etckeeper pre-commit when necessary."""
5
6 import bzrlib
7 from bzrlib.mutabletree import MutableTree
8 from bzrlib.errors import BzrError, NotLocalUrl
9 import os
10 import subprocess
11
12 if not (hasattr(MutableTree, "hooks") and "start_commit" in MutableTree.hooks):
13     raise "Version of Bazaar installed does not support required hooks."
14
15 def etckeeper_startcommit_hook(tree):
16     if not os.path.exists(tree.abspath(".etckeeper")):
17         # Only run the commit hook when this is an etckeeper branch
18         return
19     ret = subprocess.call(["etckeeper", "pre-commit", tree.abspath(".")])
20     if ret != 0:
21         raise BzrError("etckeeper pre-commit failed")
22
23 install_named_hook = getattr(MutableTree.hooks, 'install_named_hook', None)
24 if install_named_hook is not None:
25     install_named_hook('start_commit', etckeeper_startcommit_hook, 'etckeeper')
26 else:
27     MutableTree.hooks.install_hook('start_commit', etckeeper_startcommit_hook)
28     MutableTree.hooks.name_hook(etckeeper_startcommit_hook, "etckeeper")
29
30 if __name__ == "__main__":
31     from distutils.core import setup
32     setup(name="bzr-etckeeper", 
33           packages=["bzrlib.plugins.etckeeper"],
34           package_dir={"bzrlib.plugins.etckeeper":"etckeeper-bzr"})