예제 #1
0
    def execute(self, *args):
        """Execute the hook with given args"""

        if len(args) != self.numparam:
            raise HookError(
                "Hook %s executed with wrong number of args. \
                            Expected %d. Saw %d. args: %s"
                % (self.name, self.numparam, len(args), args)
            )

        if self.pre_exec_callback is not None:
            args = self.pre_exec_callback(*args)

        try:
            ret = subprocess.call([self.filepath] + list(args), cwd=self.cwd)
            if ret != 0:
                if self.post_exec_callback is not None:
                    self.post_exec_callback(0, *args)
                raise HookError(
                    "Hook %s exited with non-zero status %d" % (self.name, ret)
                )
            if self.post_exec_callback is not None:
                return self.post_exec_callback(1, *args)
        except OSError:  # no file. silent failure.
            if self.post_exec_callback is not None:
                self.post_exec_callback(0, *args)
예제 #2
0
    def execute(self, client_refs):
        # do nothing if the script doesn't exist
        if not os.path.exists(self.filepath):
            return None

        try:
            env = os.environ.copy()
            env["GIT_DIR"] = self.controldir

            p = subprocess.Popen(
                self.filepath,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                env=env,
            )

            # client_refs is a list of (oldsha, newsha, ref)
            in_data = "\n".join([" ".join(ref) for ref in client_refs])

            out_data, err_data = p.communicate(in_data)

            if (p.returncode != 0) or err_data:
                err_fmt = "post-receive exit code: %d\n" + "stdout:\n%s\nstderr:\n%s"
                err_msg = err_fmt % (p.returncode, out_data, err_data)
                raise HookError(err_msg)
            return out_data
        except OSError as err:
            raise HookError(repr(err))