Skip to content

Commit 778b062

Browse files
committed
Started integrating possibility for Python version check
1 parent 742e96b commit 778b062

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins {
88
id "com.gorylenko.gradle-git-properties" version "2.5.0"
99
id 'io.freefair.lombok' version '8.13.1'
1010
id 'java'
11+
id 'application'
1112
id 'jacoco'
1213
}
1314

@@ -75,6 +76,7 @@ dependencies {
7576
implementation "org.javers:javers-spring-boot-starter-sql:${javersVersion}"
7677
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
7778
implementation 'org.apache.commons:commons-collections4:4.4'
79+
implementation 'org.apache.maven:maven-artifact:3.6.3'
7880
implementation 'org.json:json:20250107'
7981
implementation 'com.github.jknack:handlebars:4.4.0'
8082
implementation 'com.google.guava:guava:33.4.8-jre'

src/main/java/edu/kit/datamanager/mappingservice/configuration/ApplicationProperties.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package edu.kit.datamanager.mappingservice.configuration;
1717

18-
import edu.kit.datamanager.annotations.ExecutableFileURL;
1918
import edu.kit.datamanager.annotations.LocalFolderURL;
2019
import edu.kit.datamanager.validator.ExecutableFileValidator;
2120
import lombok.Data;

src/main/java/edu/kit/datamanager/mappingservice/util/PythonRunnerUtil.java

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
import edu.kit.datamanager.mappingservice.configuration.ApplicationProperties;
1818
import edu.kit.datamanager.mappingservice.plugins.MappingPluginException;
1919
import edu.kit.datamanager.mappingservice.plugins.MappingPluginState;
20+
import java.io.ByteArrayOutputStream;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223
import org.springframework.stereotype.Component;
2324

2425
import java.io.OutputStream;
2526
import java.util.ArrayList;
2627
import 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

Comments
 (0)