Coverage for kwasa\libs\helper.py: 0%

63 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-14 18:06 +0300

1import os 

2import subprocess 

3import platform 

4from contextlib import suppress 

5from typing import Any 

6 

7from kwasa.logger.log import get_logger 

8 

9logger = get_logger("utility") 

10 

11 

12class HelperUtility: 

13 def __init__(self, full_path: Any) -> None: 

14 self.full_path = full_path 

15 

16 def manage_local_repository(self, installation: bool = True) -> None: 

17 os.chdir(self.full_path) 

18 

19 subprocess.run(["git", "init"], check=True) 

20 subprocess.run(["git", "checkout", "-b", "main"], check=True) 

21 subprocess.run(["git", "add", "--all"], check=True) 

22 subprocess.run( 

23 ["git", "commit", "-m", "Reinitialized the git repository after update."], 

24 check=True, 

25 ) 

26 

27 if installation: 

28 self.manage_virtualenv_and_install() 

29 

30 def manage_virtualenv_and_install(self) -> None: 

31 """Set up virtual environment and install both Python and Node dependencies""" 

32 

33 if not os.path.exists(".venv"): 

34 logger.info("🛠️ .venv not found. Creating virtual environment...") 

35 subprocess.run(["python", "-m", "venv", ".venv"], check=True) 

36 logger.info("✔️ .venv created successfully.") 

37 

38 self.install_python_dependencies() 

39 self.install_node_dependencies() 

40 

41 def install_python_dependencies(self) -> None: 

42 """Install Python dependencies from requirements.txt if it exists""" 

43 if not os.path.exists("requirements.txt"): 

44 logger.info( 

45 "ℹ️ No requirements.txt found. Skipping Python package installation." 

46 ) 

47 return 

48 

49 platform_name = platform.system() 

50 pip_cmd = ( 

51 ".venv\\Scripts\\pip" if platform_name == "Windows" else ".venv/bin/pip" 

52 ) 

53 logger.info(f"📦 Installing Python packages using {pip_cmd}...") 

54 

55 with suppress(subprocess.CalledProcessError): 

56 subprocess.run([pip_cmd, "install", "-r", "requirements.txt"], check=True) 

57 logger.info("✔️ Python packages installed successfully.") 

58 return 

59 

60 logger.warning("⚠️ Python package installation failed.") 

61 

62 def install_node_dependencies(self) -> None: 

63 """Install Node.js dependencies using npm or pnpm""" 

64 if not os.path.exists("package.json"): 

65 logger.warning("📦 No package.json found. Skipping Node.js setup.") 

66 return 

67 

68 if self.check_package_manager("npm"): 

69 logger.info("📦 Installing Node.js packages using npm...") 

70 with suppress(subprocess.CalledProcessError): 

71 subprocess.run(["npm", "install"], check=True) 

72 logger.info("✔️ Node.js packages installed successfully via npm.") 

73 return 

74 logger.warning("⚠️ Node.js package installation failed using npm.") 

75 

76 elif self.check_package_manager("pnpm"): 

77 logger.info("📦 Installing Node.js packages using pnpm...") 

78 with suppress(subprocess.CalledProcessError): 

79 subprocess.run(["pnpm", "install"], check=True) 

80 logger.info("✔️ Node.js packages installed successfully via pnpm.") 

81 return 

82 logger.warning("⚠️ Node.js package installation failed using pnpm.") 

83 

84 else: 

85 logger.warning("⚠️ No supported package manager found (npm or pnpm).") 

86 

87 def check_package_manager(self, manager: Any) -> bool: 

88 """Check if a package manager like npm or pnpm is installed""" 

89 with suppress(subprocess.CalledProcessError): 

90 subprocess.run( 

91 [manager, "--version"], 

92 check=True, 

93 stdout=subprocess.PIPE, 

94 stderr=subprocess.PIPE, 

95 ) 

96 logger.info(f"✔️ Found package manager: {manager}") 

97 return True 

98 

99 logger.warning(f"⚠️ {manager} not found.") 

100 return False