From 867567e0ee4d40a870fffa03aaed4458a08db453 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 15 Apr 2024 12:55:09 -0500 Subject: build: T3664: initialize git vyos-1x submodule before imports Signed-off-by: Daniil Baturin --- scripts/image-build/build-vyos-image | 59 ++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'scripts') diff --git a/scripts/image-build/build-vyos-image b/scripts/image-build/build-vyos-image index 68b1f743..61431c16 100755 --- a/scripts/image-build/build-vyos-image +++ b/scripts/image-build/build-vyos-image @@ -31,14 +31,6 @@ import argparse import datetime import functools -# Add the vyos-1x submodule directory to the Python path -# so that we can import modules from it. -VYOS1X_DIR = os.path.join(os.getcwd(), 'vyos-1x/python') -if not os.path.exists(VYOS1X_DIR): - print("E: vyos-1x subdirectory does not exist, did you initialize submodules?") -else: - sys.path.append(VYOS1X_DIR) - # Import third-party modules try: import tomli @@ -49,6 +41,36 @@ except ModuleNotFoundError as e: print(f"E: Cannot load required library {e}") print("E: Please make sure the following Python3 modules are installed: tomli jinja2 git psutil") +# Initialize Git object from our repository +try: + repo = git.Repo('.', search_parent_directories=True) + repo.git.submodule('update', '--init') + + # Retrieve the Git commit ID of the repository, 14 charaters will be sufficient + build_git = repo.head.object.hexsha[:14] + # If somone played around with the source tree and the build is "dirty", mark it + if repo.is_dirty(): + build_git += "-dirty" + + # Retrieve git branch name or current tag + # Building a tagged release might leave us checking out a git tag that is not the tip of a named branch (detached HEAD) + # Check if the current HEAD is associated with a tag and use its name instead of an unavailable branch name. + git_branch = next((tag.name for tag in repo.tags if tag.commit == repo.head.commit), None) + if git_branch is None: + git_branch = repo.active_branch.name +except Exception as e: + print(f'W: Could not retrieve information from git: {repr(e)}') + build_git = "" + git_branch = "" + +# Add the vyos-1x submodule directory to the Python path +# so that we can import modules from it. +VYOS1X_DIR = os.path.join(os.getcwd(), 'vyos-1x/python') +if not os.path.exists(VYOS1X_DIR): + print("E: vyos-1x subdirectory does not exist, did you initialize submodules?") +else: + sys.path.append(VYOS1X_DIR) + # Import local modules from scripts/image-build # They rely on modules from vyos-1x import utils @@ -340,27 +362,6 @@ if __name__ == "__main__": # Assign a (hopefully) unique identifier to the build (UUID) build_uuid = str(uuid.uuid4()) - # Initialize Git object from our repository - try: - repo = git.Repo('.', search_parent_directories=True) - - # Retrieve the Git commit ID of the repository, 14 charaters will be sufficient - build_git = repo.head.object.hexsha[:14] - # If somone played around with the source tree and the build is "dirty", mark it - if repo.is_dirty(): - build_git += "-dirty" - - # Retrieve git branch name or current tag - # Building a tagged release might leave us checking out a git tag that is not the tip of a named branch (detached HEAD) - # Check if the current HEAD is associated with a tag and use its name instead of an unavailable branch name. - git_branch = next((tag.name for tag in repo.tags if tag.commit == repo.head.commit), None) - if git_branch is None: - git_branch = repo.active_branch.name - except Exception as e: - print(f'W: Could not retrieve information from git: {repr(e)}') - build_git = "" - git_branch = "" - # Create the build version string if build_config['build_type'] == 'development': try: -- cgit v1.2.3