Source code for fvm.manage_allure

# Copyright 2024-2026 Universidad de Sevilla
# SPDX-License-Identifier: Apache-2.0

"""
Manage allure as external non-python dependency.

This module manages the installation of Allure Report, and only installs it if
it is not already in the target installation directory. Both allure version and
installation directory can be specified.
"""
import os
import sys
import zipfile
import urllib.request
import shutil
import argparse
import importlib.resources
from pathlib import Path

DEFAULT_ALLURE_VERSION = "2.32.0"

[docs] def get_allure_zip_path(allure_version, install_dir): """"Get the path to the downloaded zip file""" return os.path.join(install_dir, f"allure-{allure_version}.zip")
[docs] def get_allure_dir(allure_version, install_dir): """Get the path to the allure directory""" return os.path.join(install_dir, f'allure-{allure_version}')
[docs] def get_allure_bin_path(allure_version, install_dir): """Get the path to the allure executable""" return os.path.join(install_dir, f'allure-{allure_version}', "bin", "allure")
[docs] def download_allure(allure_version, install_dir): """Download a specific allure release""" allure_download_url = f"https://github.com/allure-framework/allure2/releases/download/{allure_version}/allure-{allure_version}.zip" allure_zip_path = get_allure_zip_path(allure_version, install_dir) allure_dir = get_allure_dir(allure_version, install_dir) print(f""" Downloading Allure {allure_version} from {allure_download_url}, writing it to {allure_zip_path}""") Path(install_dir).mkdir(parents=True, exist_ok=True) urllib.request.urlretrieve(allure_download_url, allure_zip_path)
[docs] def extract_allure(allure_version, install_dir): """Extract the allure zip file""" # If a previous installation exists, remove it allure_dir = get_allure_dir(allure_version, install_dir) if Path(allure_dir).exists(): shutil.rmtree(allure_dir) print(" Extracting Allure...") allure_zip_path = get_allure_zip_path(allure_version, install_dir) with zipfile.ZipFile(allure_zip_path, "r") as zip_ref: zip_ref.extractall(install_dir) os.remove(allure_zip_path)
[docs] def make_allure_executable(allure_version, install_dir): """Make the allure binary executable if necessary""" allure_bin = get_allure_bin_path(allure_version, install_dir) if sys.platform != "win32": os.chmod(allure_bin, 0o755)
[docs] def create_parser(): """Create argument parser""" parser = argparse.ArgumentParser(description="Install Allure CLI.") parser.add_argument( "-v", "--allure_version", default=DEFAULT_ALLURE_VERSION, help="Specific version to download (default: %(default)s)", ) parser.add_argument( "-d", "--install_dir", default=os.getcwd(), help="Directory to install Allure (default: .)", ) parser.add_argument('-f', '--force', default=False, action='store_true', help='Install Allure even if it exists in the target directory. (default: %(default)s)' ) return parser
[docs] def install_allure(allure_version, install_dir): """Install allure""" download_allure(allure_version, install_dir) extract_allure(allure_version, install_dir) make_allure_executable(allure_version, install_dir) change_logo(allure_version, install_dir)
[docs] def ensure_allure(allure_version, install_dir): """Install allure, but only if it is not already installed in the specified directory""" allure_bin = Path(os.path.join(install_dir, f'allure-{allure_version}', 'bin', 'allure')) if not allure_bin.exists(): install_allure(allure_version, install_dir)
[docs] def main(): """Main function; to use when the python script is directly executed""" parser = create_parser() args = parser.parse_args() print(f'manage_allure.py: {args=}') allure_version = args.allure_version install_dir = args.install_dir force = args.force if not os.path.exists(install_dir): os.makedirs(install_dir) if force: print(f" Installing Allure at {os.path.join(install_dir, f'allure-{allure_version}', 'bin', 'allure')}") install_allure(allure_version, install_dir) else: print(f" Ensuring Allure is at {os.path.join(install_dir, f'allure-{allure_version}', 'bin', 'allure')}") ensure_allure(allure_version, install_dir)
if __name__ == "__main__": main()