# -*- coding: utf-8 -*- """ Author: Hmily GitHub:https://github.com/ihmily Copyright (c) 2024 by Hmily, All Rights Reserved. """ from __future__ import annotations import os import subprocess import sys import platform import distro from .logger import logger current_platform = platform.system() def install_nodejs_centos(): try: logger.warning("Node.js is not installed.") logger.debug("Installing the latest version of Node.js for CentOS...") result = subprocess.run('curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm/setup_lts.x | ' 'bash -', shell=True, capture_output=True) if result.returncode != 0: logger.error("Failed to run NodeSource installation script") return result = subprocess.run(['yum', 'install', '-y', 'epel-release'], capture_output=True) if result.returncode != 0: logger.error("Failed to install EPEL repository") return result = subprocess.run(['yum', 'install', '-y', 'nodejs'], capture_output=True) if result.returncode == 0: logger.debug('Node.js installation was successful. Restart for changes to take effect.') return True else: logger.error("Node.js installation failed") except Exception as e: logger.error(f"type: {type(e).__name__}, Node.js installation failed {e}") def install_nodejs_ubuntu(): try: logger.warning("Node.js is not installed.") logger.debug("Installing the latest version of Node.js for Ubuntu...") install_script = 'curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -' result = subprocess.run(install_script, shell=True, capture_output=True) if result.returncode != 0: logger.error("Failed to run NodeSource installation script") return install_command = ['apt', 'install', '-y', 'nodejs'] result = subprocess.run(install_command, capture_output=True) if result.returncode == 0: logger.debug('Node.js installation was successful. Restart for changes to take effect.') return True else: logger.error("Node.js installation failed") except Exception as e: logger.error(f"type: {type(e).__name__}, Node.js installation failed, {e}") def get_package_manager(): dist_id = distro.id() if dist_id in ["centos", "fedora", "rhel", "amzn", "oracle", "scientific", "opencloudos", "alinux"]: return "RHS" else: return "DBS" def install_nodejs() -> bool: if current_platform != "Linux": logger.debug("Node.js 自动安装仅支持 Linux,请手动安装 Node.js。") return False os_type = get_package_manager() if os_type == "RHS": return install_nodejs_centos() return install_nodejs_ubuntu() def ensure_nodejs_installed(func): def wrapper(*args, **kwargs): try: result = subprocess.run(['node', '-v'], capture_output=True) version = result.stdout.strip() if result.returncode == 0 and version: return func(*args, **kwargs) except FileNotFoundError: pass return False def wrapped_func(*args, **kwargs): if sys.version_info >= (3, 7): res = wrapper(*args, **kwargs) else: res = wrapper(*args, **kwargs) if not res: install_nodejs() res = wrapper(*args, **kwargs) if not res: raise RuntimeError("Node.js is not installed.") return func(*args, **kwargs) return wrapped_func def check_nodejs_installed() -> bool: try: result = subprocess.run(['node', '-v'], capture_output=True) version = result.stdout.strip() if result.returncode == 0 and version: return True except FileNotFoundError: pass return False def check_node() -> bool: if not check_nodejs_installed(): return install_nodejs()