|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2025 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# Auto-install dependency-check on first import |
| 7 | +import logging |
| 8 | +import os |
| 9 | +import stat |
| 10 | +import subprocess |
| 11 | +import tempfile |
| 12 | +import urllib.request |
| 13 | +import zipfile |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | +# OWASP dependency-check version to install |
| 18 | +DEPENDENCY_CHECK_VERSION = "12.1.3" |
| 19 | + |
| 20 | + |
| 21 | +def _install_dependency_check(): |
| 22 | + """Install OWASP dependency-check""" |
| 23 | + try: |
| 24 | + # Skip if explicitly disabled |
| 25 | + if os.environ.get('FOSSLIGHT_SKIP_AUTO_INSTALL', '').lower() in ('1', 'true', 'yes'): |
| 26 | + logger.info("Auto-install disabled by environment variable") |
| 27 | + return |
| 28 | + |
| 29 | + # Fixed install directory: always use ./fosslight_dc_bin in current working directory |
| 30 | + install_dir = os.path.abspath(os.path.join(os.getcwd(), 'fosslight_dc_bin')) |
| 31 | + bin_dir = os.path.join(install_dir, 'dependency-check', 'bin') |
| 32 | + dc_path = os.path.join(bin_dir, 'dependency-check.sh') |
| 33 | + |
| 34 | + # Check if dependency-check already exists |
| 35 | + if os.path.exists(dc_path): |
| 36 | + try: |
| 37 | + result = subprocess.run([dc_path, '--version'], capture_output=True, text=True, timeout=10) |
| 38 | + if result.returncode == 0: |
| 39 | + logger.debug("dependency-check already installed and working") |
| 40 | + os.environ['DEPENDENCY_CHECK_VERSION'] = DEPENDENCY_CHECK_VERSION |
| 41 | + os.environ['DEPENDENCY_CHECK_HOME'] = os.path.join(install_dir, 'dependency-check') |
| 42 | + return |
| 43 | + except (subprocess.TimeoutExpired, FileNotFoundError): |
| 44 | + pass |
| 45 | + |
| 46 | + # Download URL |
| 47 | + download_url = (f"https://github.com/dependency-check/DependencyCheck/releases/" |
| 48 | + f"download/v{DEPENDENCY_CHECK_VERSION}/" |
| 49 | + f"dependency-check-{DEPENDENCY_CHECK_VERSION}-release.zip") |
| 50 | + |
| 51 | + os.makedirs(install_dir, exist_ok=True) |
| 52 | + logger.info("Downloading OWASP dependency-check") |
| 53 | + logger.info(f"URL: {download_url}") |
| 54 | + |
| 55 | + # Download and extract |
| 56 | + with urllib.request.urlopen(download_url) as response: |
| 57 | + content = response.read() |
| 58 | + |
| 59 | + with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp_file: |
| 60 | + tmp_file.write(content) |
| 61 | + tmp_file.flush() |
| 62 | + with zipfile.ZipFile(tmp_file.name, 'r') as zip_ref: |
| 63 | + zip_ref.extractall(install_dir) |
| 64 | + os.unlink(tmp_file.name) |
| 65 | + |
| 66 | + # Make shell scripts executable |
| 67 | + if os.path.exists(bin_dir): |
| 68 | + for script in ["dependency-check.sh", "completion-for-dependency-check.sh"]: |
| 69 | + script_path = os.path.join(bin_dir, script) |
| 70 | + if os.path.exists(script_path): |
| 71 | + st = os.stat(script_path) |
| 72 | + os.chmod(script_path, st.st_mode | stat.S_IEXEC) |
| 73 | + |
| 74 | + logger.info("✅ OWASP dependency-check installed successfully!") |
| 75 | + logger.info(f"Installed to: {os.path.join(install_dir, 'dependency-check')}") |
| 76 | + |
| 77 | + # Set environment variables after successful installation |
| 78 | + os.environ['DEPENDENCY_CHECK_VERSION'] = DEPENDENCY_CHECK_VERSION |
| 79 | + os.environ['DEPENDENCY_CHECK_HOME'] = os.path.join(install_dir, 'dependency-check') |
| 80 | + |
| 81 | + return True |
| 82 | + |
| 83 | + except Exception as e: |
| 84 | + logger.error(f"Failed to install dependency-check: {e}") |
| 85 | + logger.info("dependency-check can be installed manually from: https://github.com/dependency-check/DependencyCheck/releases") |
| 86 | + return False |
| 87 | + |
| 88 | + |
| 89 | +def _auto_install_dependencies(): |
| 90 | + """Auto-install required dependencies if not present.""" |
| 91 | + # Only run this once per session |
| 92 | + if hasattr(_auto_install_dependencies, '_already_run'): |
| 93 | + return |
| 94 | + _auto_install_dependencies._already_run = True |
| 95 | + |
| 96 | + try: |
| 97 | + # Install binary version |
| 98 | + _install_dependency_check() |
| 99 | + |
| 100 | + logger.info(f"✅ dependency-check setup completed with version {DEPENDENCY_CHECK_VERSION}") |
| 101 | + except Exception as e: |
| 102 | + logger.warning(f"Auto-install failed: {e}") |
| 103 | + |
| 104 | + |
| 105 | +# Auto-install on import |
| 106 | +_auto_install_dependencies() |
0 commit comments