diff --git a/doc/overview.rst b/doc/overview.rst index c7e7891593935d6c27732b939c8a1a62da1ea9a4..87bc43e879d7e28f96c1611a3689401d8c408fec 100644 --- a/doc/overview.rst +++ b/doc/overview.rst @@ -24,6 +24,15 @@ In case the venv becomes unusable for some reason, the ``lisa-install`` shell command available after sourcing ``init_env`` will allow to create a new clean venv from scratch. +Git hooks +--------- + +A git `post-checkout` hook is provided in tools/post-checkout. It will check +that no `setup.py` file have been updated since last time ``lisa-install`` was +executed. If a modification is detected, it will ask the user to run +``lisa-install`` again, since a dependency might have been added, or a version +requirement might have been updated. + Without automatic ``venv`` -------------------------- diff --git a/shell/lisa_shell b/shell/lisa_shell index 44a51984e1776a4c87eec6e80556c4426fdf34c8..99bd89acfd3585341b2fce3ed68446955a3612f0 100755 --- a/shell/lisa_shell +++ b/shell/lisa_shell @@ -32,7 +32,7 @@ source "$LISA_HOME/shell/lisa_colors" # Add some shell utilities to the PATH, with lower priority than system's one # in case the user needs a different version of them export LISA_HOST_ABI=${LISA_HOST_ABI:-x86_64} -export PATH=$PATH:$LISA_HOME/shell/:$LISA_HOME/tools/$LISA_HOST_ABI +export PATH=$PATH:$LISA_HOME/shell/:$LISA_HOME/tools/$LISA_HOST_ABI:$LISA_HOME/tools ################################################################################ # Helpers @@ -103,6 +103,10 @@ function lisa-venv-activate { echo "Activating LISA Python venv ($LISA_VENV_PATH) ..." VIRTUAL_ENV_DISABLE_PROMPT=1 source "$LISA_VENV_PATH/bin/activate" fi + + # Check if lisa-install needs to be run again, but ignore the return value + # so it won't make activation fail just because of that + (check-setuppy; exit 0) } function lisa-venv-deactivate { @@ -127,6 +131,9 @@ function lisa-install { # cannot be installed using pip in a venv python3 "$LISA_HOME/setup.py" systemcheck + # Record the point in time when we ran that install command + check-setuppy --update-recorded-commit HEAD + _lisa-venv-create && _lisa-upgrade-pip || return 1 diff --git a/tools/check-setuppy b/tools/check-setuppy new file mode 100755 index 0000000000000000000000000000000000000000..503676ada8ae7632ab28f07138e84a1ff5fb9ffd --- /dev/null +++ b/tools/check-setuppy @@ -0,0 +1,89 @@ +#! /usr/bin/env python3 +# +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2018, Arm Limited and contributors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import argparse +import os +import subprocess +import sys + +LISA_HOME = os.getenv('LISA_HOME', '.') + +def call_git(*args): + return subprocess.check_output(('git', *args)).decode('utf-8') + +def get_git_sha1(ref): + return call_git('rev-parse', ref).strip() + +def main(): + parser = argparse.ArgumentParser(description=''' + Check if filenames matching a given pattern have been modified since + the last time the commit sha1 file was updated. + ''' + ) + + parser.add_argument('--commit-file', + default=os.path.join(LISA_HOME, '.lisa-install-commit'), + help='File containing a git revision. .. will be scanned, looking for the filename pattern' + ) + + parser.add_argument('--current-rev', + default='HEAD', + help='Git revision that is assumed to be the current one' + ) + + parser.add_argument('--update-recorded-commit', + help='Update the commit file using sha1 of given git ref' + ) + + parser.add_argument('--filename-pattern', + default='*setup.py', + help='git rev-list filename pattern to check' + ) + + args = parser.parse_args() + + # Update mode + if args.update_recorded_commit: + new_sha1 = get_git_sha1(args.update_recorded_commit) + with open(args.commit_file, 'w') as f: + f.write(new_sha1 + '\n') + # check mode + else: + try: + with open(args.commit_file, 'r', encoding='utf-8') as f: + recorded_sha1 = f.read().strip() + # If there is no commit file, just exit silently since there has been + # no setup already + except FileNotFoundError: + return 0 + + rev_range = '{}..{}'.format(recorded_sha1, args.current_rev) + relevant_commits = call_git('rev-list', rev_range, '--', args.filename_pattern) + + if relevant_commits: + print('{} files have been modified, re-run lisa-install to update dependencies.'.format( + args.filename_pattern + )) + print('THIS WILL RESET YOUR VENV !') + return 1 + +if __name__ == '__main__': + sys.exit(main()) + +# vim :set tabstop=4 shiftwidth=4 textwidth=80 expandtab diff --git a/tools/post-checkout b/tools/post-checkout new file mode 100755 index 0000000000000000000000000000000000000000..747643bffc2d55372d46d0e1cba87de8edf875bb --- /dev/null +++ b/tools/post-checkout @@ -0,0 +1,30 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2018, Arm Limited and contributors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +# Git post-checkout hook that checks if lisa-install needs to be run again due +# to updated setup.py files + +PREVIOUS_HEAD=$1 +NEW_HEAD=$2 +IS_BRANCH_CHECKOUT=$3 + +if which check-setuppy >/dev/null 2>&1; then + check-setuppy --current-rev "$NEW_HEAD" +fi +