xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/hook.py (revision 2d57dc692b4dd10e696ca6922510f6cecded1bfd)
1*2d57dc69SVladimir Kotal#
2*2d57dc69SVladimir Kotal# CDDL HEADER START
3*2d57dc69SVladimir Kotal#
4*2d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the
5*2d57dc69SVladimir Kotal# Common Development and Distribution License (the "License").
6*2d57dc69SVladimir Kotal# You may not use this file except in compliance with the License.
7*2d57dc69SVladimir Kotal#
8*2d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific
9*2d57dc69SVladimir Kotal# language governing permissions and limitations under the License.
10*2d57dc69SVladimir Kotal#
11*2d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each
12*2d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt.
13*2d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the
14*2d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying
15*2d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner]
16*2d57dc69SVladimir Kotal#
17*2d57dc69SVladimir Kotal# CDDL HEADER END
18*2d57dc69SVladimir Kotal#
19*2d57dc69SVladimir Kotal
20*2d57dc69SVladimir Kotal#
21*2d57dc69SVladimir Kotal# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22*2d57dc69SVladimir Kotal#
23*2d57dc69SVladimir Kotal
24*2d57dc69SVladimir Kotalfrom .command import Command
25*2d57dc69SVladimir Kotalfrom .exitvals import (
26*2d57dc69SVladimir Kotal    FAILURE_EXITVAL,
27*2d57dc69SVladimir Kotal    SUCCESS_EXITVAL
28*2d57dc69SVladimir Kotal)
29*2d57dc69SVladimir Kotal
30*2d57dc69SVladimir Kotal
31*2d57dc69SVladimir Kotaldef run_hook(logger, script, path, env, timeout):
32*2d57dc69SVladimir Kotal    """
33*2d57dc69SVladimir Kotal    Change a working directory to specified path, run a command
34*2d57dc69SVladimir Kotal    and change the working directory back to its original value.
35*2d57dc69SVladimir Kotal
36*2d57dc69SVladimir Kotal    Return 0 on success, 1 on failure.
37*2d57dc69SVladimir Kotal    """
38*2d57dc69SVladimir Kotal
39*2d57dc69SVladimir Kotal    ret = SUCCESS_EXITVAL
40*2d57dc69SVladimir Kotal    logger.debug("Running hook '{}' in directory {}".
41*2d57dc69SVladimir Kotal                 format(script, path))
42*2d57dc69SVladimir Kotal    cmd = Command([script], logger=logger, work_dir=path, env_vars=env,
43*2d57dc69SVladimir Kotal                  timeout=timeout, doprint=True)
44*2d57dc69SVladimir Kotal    cmd.execute()
45*2d57dc69SVladimir Kotal    if cmd.state != "finished" or cmd.getretcode() != SUCCESS_EXITVAL:
46*2d57dc69SVladimir Kotal        logger.error("command failed: {} -> {}".format(cmd, cmd.getretcode()))
47*2d57dc69SVladimir Kotal        ret = FAILURE_EXITVAL
48*2d57dc69SVladimir Kotal
49*2d57dc69SVladimir Kotal    return ret
50