1717import edu .kit .datamanager .mappingservice .configuration .ApplicationProperties ;
1818import edu .kit .datamanager .mappingservice .plugins .MappingPluginException ;
1919import edu .kit .datamanager .mappingservice .plugins .MappingPluginState ;
20+ import java .io .ByteArrayOutputStream ;
2021import org .slf4j .Logger ;
2122import org .slf4j .LoggerFactory ;
2223import org .springframework .stereotype .Component ;
2324
2425import java .io .OutputStream ;
2526import java .util .ArrayList ;
2627import java .util .Collections ;
28+ import org .apache .maven .artifact .versioning .ComparableVersion ;
2729
2830/**
2931 * Utility class for running python scripts.
@@ -37,7 +39,6 @@ public class PythonRunnerUtil {
3739
3840 private static final Logger LOGGER = LoggerFactory .getLogger (PythonRunnerUtil .class );
3941
40-
4142 public static void init (ApplicationProperties configuration ) {
4243 PythonRunnerUtil .configuration = configuration ;
4344 }
@@ -53,6 +54,85 @@ public static void printPythonVersion() {
5354 }
5455 }
5556
57+
58+
59+
60+ /**
61+ * This method checks if the local Python installation version is larger or
62+ * equal the provided version number. The version should be provided as
63+ * semantic version number, i.e., 3.13.2
64+ *
65+ * The method will return TRUE if the minimal requirements are met and false
66+ * otherwise. False is also returned if obtaining/parsing the local python
67+ * version fails. for any reason.
68+ *
69+ * @param versionString The semantic version string to compare the local Python version against.
70+ *
71+ * @return True if versionString is smaller or equal the local Python version, false otherwise.
72+ */
73+ public static boolean hasMinimalPythonVersion (String versionString ) {
74+ boolean result = false ;
75+ try {
76+ LOGGER .trace ("Checking for minimal Python version {}." , versionString );
77+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
78+ PythonRunnerUtil .runPythonScript ("--version" , bout , System .err );
79+ LOGGER .trace ("Version command output: {}" , bout .toString ());
80+
81+ String [] split = bout .toString ().split (" " );
82+
83+ if (split .length == 2 ) {
84+ String localPythonVersion = bout .toString ().split (" " )[1 ].trim ();
85+ LOGGER .trace ("Obtained local Python version: {}" , localPythonVersion );
86+ ComparableVersion localVersion = new ComparableVersion (localPythonVersion );
87+ ComparableVersion minimalVersion = new ComparableVersion (versionString );
88+ result = minimalVersion .compareTo (localVersion ) <= 0 ;
89+ } else {
90+ LOGGER .info ("Unexpected Python version output. Unable to check for minimal version." );
91+ }
92+ } catch (MappingPluginException e ) {
93+ LOGGER .error ("Failed to obtain python version." , e );
94+ }
95+ return result ;
96+ }
97+
98+ /**
99+ * This method checks if the local Python installation version is larger or
100+ * equal the provided version number. The version should be provided as
101+ * semantic version number, i.e., 3.13.2
102+ *
103+ * The method will return TRUE if the minimal requirements are met and false
104+ * otherwise. False is also returned if obtaining/parsing the local python
105+ * version fails. for any reason.
106+ *
107+ * @param versionString The semantic version string to compare the local Python version against.
108+ *
109+ * @return True if versionString is smaller or equal the local Python version, false otherwise.
110+ */
111+ public static boolean hasMaximalPythonVersion (String versionString ) {
112+ boolean result = false ;
113+ try {
114+ LOGGER .trace ("Checking for minimal Python version {}." , versionString );
115+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
116+ PythonRunnerUtil .runPythonScript ("--version" , bout , System .err );
117+ LOGGER .trace ("Version command output: {}" , bout .toString ());
118+
119+ String [] split = bout .toString ().split (" " );
120+
121+ if (split .length == 2 ) {
122+ String localPythonVersion = bout .toString ().split (" " )[1 ].trim ();
123+ LOGGER .trace ("Obtained local Python version: {}" , localPythonVersion );
124+ ComparableVersion localVersion = new ComparableVersion (localPythonVersion );
125+ ComparableVersion minimalVersion = new ComparableVersion (versionString );
126+ result = minimalVersion .compareTo (localVersion ) > 0 ;
127+ } else {
128+ LOGGER .info ("Unexpected Python version output. Unable to check for minimal version." );
129+ }
130+ } catch (MappingPluginException e ) {
131+ LOGGER .error ("Failed to obtain python version." , e );
132+ }
133+ return result ;
134+ }
135+
56136 /**
57137 * This method executes an argument/option on the python interpreter.
58138 *
@@ -102,4 +182,25 @@ public static MappingPluginState runPythonScript(String script, OutputStream out
102182 }
103183 return ShellRunnerUtil .run (output , error , command .toArray (String []::new ));
104184 }
185+
186+ public static void main (String [] args ) throws Exception {
187+ ArrayList <String > command = new ArrayList <>();
188+ command .add ("/opt/homebrew/bin/python3" );
189+ command .add ("--version" );
190+ if (args != null ) {
191+ Collections .addAll (command , args );
192+ }
193+
194+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
195+
196+ ShellRunnerUtil .run (bout , System .err , command .toArray (String []::new ));
197+ System .out .println ("VERS " + bout .toString ());
198+ System .out .println (bout .toString ().split (" " )[1 ]);
199+ ComparableVersion verr = new ComparableVersion ("unknown" );
200+ System .out .println ("ER " + verr .getCanonical ());
201+ ComparableVersion v = new ComparableVersion (bout .toString ().split (" " )[1 ].trim ());
202+ ComparableVersion vMin = new ComparableVersion ("4" );
203+ System .out .println (verr .compareTo (v ));
204+
205+ }
105206}
0 commit comments