def setup(self, repo, hook): """Setup hook infrastructure. In addition to common things it also updates .hg/hgrc.""" HookAPI.setup(self, repo, hook) rc_path = os.path.join(repo, ".hg", "hgrc") parser = ConfigParser() if os.path.exists(rc_path): parser.read([rc_path]) section = "hooks" if not parser.has_section(section): parser.add_section(section) hook_path = self.hook_path(repo, hook) if parser.has_option(section, hook): if parser.get(section, hook) != hook_path: raise HookError("%s: %s hook is already set" % (rc_path, hook)) else: parser.set(section, hook, hook_path) with open(rc_path, "w") as fhandle: parser.write(fhandle)
def teardown(self, repo, hook): """Remove hooks infrastructure.""" rc_path = os.path.join(repo, ".hg", "hgrc") if not os.path.exists(rc_path): return if HookAPI.teardown(self, repo, hook): parser = ConfigParser() parser.read([rc_path]) section = "hooks" if parser.has_option(section, hook) and parser.get(section, hook) == self.hook_path(repo, hook): parser.remove_option(section, hook) return True