4
4
import subprocess
5
5
from pathlib import Path
6
6
7
+ def get_sigma_versions ():
8
+ """Fetch the 10 latest versions of sigma-cli from PyPI"""
9
+ import requests
10
+ response = requests .get ("https://pypi.org/pypi/sigma-cli/json" )
11
+ versions = sorted (response .json ()["releases" ].keys ())
12
+ return versions [- 10 :] # Return 10 latest versions
13
+
14
+ def get_sigma_backends ():
15
+ """Fetch all available Sigma backends from the plugin directory"""
16
+ import requests
17
+ try :
18
+ response = requests .get ("https://raw.githubusercontent.com/SigmaHQ/pySigma-plugin-directory/main/pySigma-plugins-v1.json" )
19
+ data = response .json ()
20
+
21
+ # Skip problematic backends
22
+ excluded_backends = {
23
+ "pySigma-backend-hawk" , # https://github.com/redsand/pySigma-backend-hawk/issues/1
24
+ "pySigma-backend-kusto" # Known issues with kusto backend
25
+ }
26
+
27
+ backends = []
28
+ for plugin_id , plugin_info in data .get ("plugins" , {}).items ():
29
+ if "package" in plugin_info :
30
+ package = plugin_info ["package" ]
31
+ # Skip if package or its git URL is in excluded list
32
+ if package not in excluded_backends and not any (excluded in package for excluded in excluded_backends ):
33
+ backends .append (package )
34
+
35
+ print (f"Found { len (backends )} backends to attempt installation" )
36
+ return backends
37
+ except Exception as e :
38
+ print (f"Error fetching backends: { e } " )
39
+ return ["pysigma-backend-splunk" , "pysigma-backend-elasticsearch" ]
40
+
7
41
def install_core_backends (python_path , sigma_version ):
8
42
"""Install core backends that are known to work"""
9
43
# First install required dependencies
10
44
base_packages = [
11
45
"pyyaml" ,
12
- f"sigma-cli=={ sigma_version } " ,
13
- "pysigma>=0.9.0,<0.12.0" , # Compatible with 1.0.x
14
46
"setuptools" ,
15
- "wheel"
47
+ "wheel" ,
48
+ "requests" ,
49
+ f"sigma-cli=={ sigma_version } " # Install sigma-cli first
16
50
]
17
51
18
- # Install using pip instead of uv for better dependency resolution
52
+ # Install base packages
19
53
for package in base_packages :
20
54
try :
21
55
print (f"Installing { package } ..." )
22
56
subprocess .run ([
23
- python_path , "-m" , "pip" , "install" , package
57
+ str ( python_path ) , "-m" , "pip" , "install" , "--no-cache-dir " , package
24
58
], check = True )
25
59
except subprocess .CalledProcessError as e :
26
- print (f"Warning: Failed to install { package } : { e } " )
60
+ print (f"Warning: Failed to install { package } " )
27
61
return False
28
62
29
- # Install minimal set of backends
30
- backends = [
31
- "pysigma-backend-splunk>=0.9.0" ,
32
- "pysigma-backend-elasticsearch>=0.9.0"
33
- ]
63
+ # Install all available backends
64
+ backends = get_sigma_backends ()
65
+ successful_installs = 0
34
66
35
67
for backend in backends :
36
68
try :
37
- print (f"Installing { backend } ... " )
69
+ print (f"Attempting to install backend: { backend } " )
38
70
subprocess .run ([
39
- python_path , "-m" , "pip" , "install" , backend
71
+ str ( python_path ) , "-m" , "pip" , "install" , "--no-cache-dir " , backend
40
72
], check = True )
73
+ successful_installs += 1
41
74
except subprocess .CalledProcessError as e :
42
- print (f"Warning: Failed to install { backend } : { e } " )
75
+ print (f"Note: Backend { backend } failed to install - might be incompatible with sigma-cli { sigma_version } " )
76
+ continue
43
77
44
- # Verify installation
45
- try :
46
- verify_cmd = [python_path , "-c" , "import yaml; import sigma.backends" ]
47
- subprocess .run (verify_cmd , check = True )
48
- return True
49
- except subprocess .CalledProcessError :
50
- print ("Failed to verify package installation" )
51
- return False
78
+ print (f"Successfully installed { successful_installs } out of { len (backends )} backends" )
79
+ return successful_installs > 0 # Return True if at least one backend was installed
52
80
53
81
def setup_sigma_versions ():
54
82
"""Setup Sigma versions with their virtual environments"""
55
- versions = [ "1.0.0" , "1.0.1" , "1.0.2" , "1.0.3" , "1.0.4" ] # Hardcode versions for now
83
+ versions = get_sigma_versions ()
56
84
installed_count = 0
57
85
base_path = Path (__file__ ).parent / "sigma"
58
86
base_path .mkdir (parents = True , exist_ok = True )
59
87
88
+ # Get the current Python executable
89
+ import sys
90
+ python_executable = sys .executable
91
+
60
92
for version in versions :
61
93
print (f"\n Setting up Sigma version { version } " )
62
94
try :
63
95
version_path = base_path / version
64
96
version_path .mkdir (parents = True , exist_ok = True )
65
97
66
- # Setup virtual environment using venv instead of uv
98
+ # Setup virtual environment
67
99
venv_path = version_path / "venv"
68
- subprocess .run ([
69
- "python3" , "-m" , "venv" , str (venv_path )
70
- ], check = True )
100
+ print (f"Creating virtual environment at: { venv_path } " )
101
+
102
+ # Ensure the venv directory doesn't exist
103
+ if venv_path .exists ():
104
+ import shutil
105
+ shutil .rmtree (venv_path )
106
+
107
+ # Create venv using current Python executable
108
+ try :
109
+ subprocess .run ([
110
+ python_executable , "-m" , "venv" ,
111
+ "--clear" , "--system-site-packages" ,
112
+ str (venv_path )
113
+ ], check = True , capture_output = True , text = True )
114
+ except subprocess .CalledProcessError as e :
115
+ print (f"venv creation output: { e .stdout } " )
116
+ print (f"venv creation error: { e .stderr } " )
117
+ raise
118
+
119
+ # Verify venv creation
120
+ python_path = venv_path / "bin" / "python"
121
+ if not python_path .exists ():
122
+ raise Exception (f"Python executable not found at { python_path } " )
71
123
72
- python_path = str (venv_path / "bin" / "python" )
124
+ print (f"Virtual environment created at: { venv_path } " )
125
+ print (f"Python executable path: { python_path } " )
73
126
74
127
# Install core backends and verify
75
- if install_core_backends (python_path , version ):
128
+ if install_core_backends (str ( python_path ) , version ):
76
129
# Copy worker script to version directory
77
130
worker_path = Path (__file__ ).parent / "worker.py"
78
131
if worker_path .exists ():
@@ -86,6 +139,8 @@ def setup_sigma_versions():
86
139
87
140
except Exception as e :
88
141
print (f"Error setting up version { version } : { e } " )
142
+ import traceback
143
+ traceback .print_exc ()
89
144
continue
90
145
91
146
return installed_count
@@ -95,4 +150,5 @@ def setup_sigma_versions():
95
150
if count == 0 :
96
151
print ("Error: No Sigma versions were installed successfully" )
97
152
exit (1 )
98
- print (f"Successfully installed { count } Sigma versions" )
153
+
154
+ print (f"Successfully installed { count } Sigma versions" )
0 commit comments