Skip to content

Commit

Permalink
A few small fixes, mainly regarding environment variables. Still need…
Browse files Browse the repository at this point in the history
… to have a look at the call to Scala-Isabelle
  • Loading branch information
mi-ki committed Sep 2, 2023
1 parent 56494fa commit 0d60d9c
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 92 deletions.
57 changes: 36 additions & 21 deletions virage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.version>3.8.1</maven.version>
<maven.clean.version>3.3.1</maven.clean.version>
<maven.install.version>3.1.1</maven.install.version>
<maven.jar.version>3.3.0</maven.jar.version>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<jdk.version>17</jdk.version>
<exec.mainClass>edu.kit.kastel.formal.virage.core.VirageMain</exec.mainClass>
<log4j.version>2.20.0</log4j.version>
<spotbugs.version>4.7.3</spotbugs.version>
<swi.home.dir>/usr/lib/swi-prolog</swi.home.dir>
<main.class>edu.kit.kastel.formal.virage.core.VirageMain</main.class>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -83,12 +89,17 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- JPL bidirectional Prolog-Java: https://github.com/SWI-Prolog/packages-jpl -->
<dependency>
Expand Down Expand Up @@ -205,7 +216,7 @@
<version>3.2.4</version> <executions> <execution> <phase>package</phase>
<goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass> <manifestEntries>
<mainClass>${main.class}</mainClass> <manifestEntries>
<Multi-Release>true</Multi-Release> </manifestEntries> </transformer> <transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"
/> </transformers> </configuration> </execution> </executions> </plugin> -->
Expand All @@ -220,6 +231,7 @@
</configuration>
<executions>
<execution>
<id>versions-display</id>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
Expand All @@ -235,7 +247,7 @@
<executions>
<execution>
<id>read-settings</id>
<phase>process-resources</phase>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
Expand Down Expand Up @@ -267,21 +279,24 @@
<version>3.1.0</version>
<executions>
<execution>
<phase>test</phase>
<id>execute</id>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${exec.mainClass}</mainClass>
<environmentVariables>
<LD_PRELOAD>${SWI_PROLOG_LIBSWIPL_PATH}</LD_PRELOAD>
<LD_LIBRARY_PATH>${SWI_PROLOG_LIBRARIES_PATH}</LD_LIBRARY_PATH>
</environmentVariables>
<executable>java</executable>
<mainClass>${main.class}</mainClass>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>edu.kit.kastel.formal.virage.core.VirageMain</argument>
<argument>${main.class}</argument>
</arguments>
<environmentVariables>
<SWI_HOME_DIR>${swi.home.dir}</SWI_HOME_DIR>
<LD_PRELOAD>${swi.home.dir}/lib/x86_64-linux/libswipl.so</LD_PRELOAD>
</environmentVariables>
</configuration>
</plugin>
</plugins>
Expand All @@ -290,12 +305,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.3.1</version>
<version>${maven.clean.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<version>${maven.clean.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -305,24 +320,24 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<version>${maven.jar.version}</version>
<configuration>
<archive>
<manifest>
<mainClass>${exec.mainClass}</mainClass>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
<version>${maven.install.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
<version>${maven.install.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -335,7 +350,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<version>${maven.jar.version}</version>
<configuration>
<failsOnError>false</failsOnError>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
Expand All @@ -357,7 +372,7 @@
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.7.3.5</version>
<version>${spotbugs.version}.5</version>
<configuration>
<xmlOutput>true</xmlOutput>
<failOnError>false</failOnError>
Expand All @@ -373,7 +388,7 @@
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.7.3</version>
<version>${spotbugs.version}</version>
</dependency>
</dependencies>
</plugin>
Expand Down
97 changes: 58 additions & 39 deletions virage/src/main/java/edu/kit/kastel/formal/util/ProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class ProcessUtils {
private static final Logger LOGGER = LogManager.getLogger(ProcessUtils.class);

/**
* Executes a terminating command and logs it outputs, stderr foing to logger.warn(), stdout to
* logger.info(). <b>Does not return if the command is non-terminating!</b>
* Executes a terminating command and prints its output to System.out/System.err, respectively.
* <b>Does not return if the command is non-terminating!</b>
*
* @param command the command to be executed (as is, i.e. the String has to contain all
* parameters etc.)
* @return the exit code (usually 0 on success, but depending on the command)
* @return a Pair of strings representing stdout and stderr of the process
* @throws IOException if reading the outputs fails
* @throws InterruptedException if command execution is interrupted
*/
public static int runTerminatingProcessAndLogOutput(final String command)
public static Output runTerminatingProcess(final String command)
throws IOException, InterruptedException {
final Runtime rt = Runtime.getRuntime();

Expand All @@ -37,44 +37,29 @@ public static int runTerminatingProcessAndLogOutput(final String command)
final String stdErr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
final String stdOut = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);

if (!stdErr.isEmpty()) {
LOGGER.warn(stdErr);
}
if (!stdOut.isEmpty()) {
LOGGER.info(stdOut);
}

return status;
return new Output(stdOut, stdErr, status);
}

/**
* Executes a terminating command and prints its output to System.out/System.err, respectively.
* <b>Does not return if the command is non-terminating!</b>
* Executes a terminating command and logs it outputs, stderr going to logger.warn(), stdout to
* logger.info(). <b>Does not return if the command is non-terminating!</b>
*
* @param command the command to be executed (as is, i.e. the String has to contain all
* parameters etc.)
* @return the exit code (usually 0 on success, but depending on the command)
* @throws IOException if reading the outputs fails
* @throws InterruptedException if command execution is interrupted
*/
public static int runTerminatingProcessAndPrintOutput(final String command)
public static int runTerminatingProcessAndLogOutput(final String command)
throws IOException, InterruptedException {
final Runtime rt = Runtime.getRuntime();

final Process p = rt.exec(StringUtils.stripAndEscape(command));
final int status = p.waitFor();

final String stdErr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
final String stdOut = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);

if (!stdErr.isEmpty()) {
System.err.print(stdErr);
final Output output = runTerminatingProcess(command);
if (!output.stdErr.isEmpty()) {
LOGGER.warn(output.stdErr);
}
if (!stdOut.isEmpty()) {
System.out.print(stdOut);
if (!output.stdOut.isEmpty()) {
LOGGER.info(output.stdOut);
}

return status;
return output.status;
}

/**
Expand All @@ -83,20 +68,54 @@ public static int runTerminatingProcessAndPrintOutput(final String command)
*
* @param command the command to be executed (as is, i.e. the String has to contain all
* parameters etc.)
* @return a Pair of strings representing stdout and stderr of the process
* @return the exit code (usually 0 on success, but depending on the command)
* @throws IOException if reading the outputs fails
* @throws InterruptedException if command execution is interrupted
*/
public static Pair<String, String> runTerminatingProcess(final String command)
public static int runTerminatingProcessAndPrintOutput(final String command)
throws IOException, InterruptedException {
final Runtime rt = Runtime.getRuntime();

final Process p = rt.exec(StringUtils.stripAndEscape(command));
p.waitFor();

final String stdErr = new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
final String stdOut = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
final Output output = runTerminatingProcess(command);
if (!output.stdErr.isEmpty()) {
System.err.print(output.stdErr);
}
if (!output.stdOut.isEmpty()) {
System.out.print(output.stdOut);
}
return output.status;
}

return new Pair<String, String>(stdOut, stdErr);
/**
* The static data structure to store the text from the standard output and the error output
* stream as well as the integer status value.
*
* @author VeriVote
*/
public static class Output {
/**
* The standard output stream as a string.
*/
public final String stdOut;
/**
* The error output stream as a string.
*/
public final String stdErr;
/**
* The status output as an integer value.
*/
public final int status;

/**
* Constructor method for the static data structure to store the text from the standard
* output and the error output stream as well as the integer status value.
*
* @param outStream The standard output stream as a string.
* @param errStream The error output stream as a string.
* @param statusValue The status output as an integer value.
*/
public Output(final String outStream, final String errStream, final int statusValue) {
stdOut = outStream;
stdErr = errStream;
status = statusValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

Expand Down Expand Up @@ -67,13 +67,12 @@ public static void addDirToLibraryPath(final String s) throws IOException {
}

/**
* Returns current time (yyyy-MM-dd HH:mm:ss).
* Returns current time (yyyy-MM-dd HH:mm:ss OOOO).
* @return the time
*/
public static String getTime() {
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final LocalDateTime now = LocalDateTime.now();

final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss OOOO");
final ZonedDateTime now = ZonedDateTime.now();
return dtf.format(now);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ private static String getCommandOutput(final String command, final String option
throws IOException, InterruptedException {
final String none = "<NONE>" + '\n';
final String proc = command + " " + option;
final String output = ProcessUtils.runTerminatingProcess(proc).getFirstValue();
return output.isEmpty() ? none : output;
final ProcessUtils.Output output = ProcessUtils.runTerminatingProcess(proc);
final String result = output.stdOut.isEmpty() ? output.stdErr : output.stdOut;
return result.isEmpty() ? none : result;
}

/**
Expand Down Expand Up @@ -758,7 +759,9 @@ public void readConfigFile(final boolean overwriteIfNecessary) throws IOExceptio
final File virageDir = new File(virageFolderPath);
this.configFile = new File(configPath);
if (!this.configFile.exists()) {
Files.createDirectory(virageDir.toPath());
if (!virageDir.exists()) {
Files.createDirectory(virageDir.toPath());
}
Files.createFile(this.configFile.toPath());
this.copyConfigToExpectedPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -699,10 +697,9 @@ private void printSettings() {
}

private void printTimestamp() {
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final LocalDateTime now = LocalDateTime.now();
final String now = SystemUtils.getTime();
this.displayMessage(HEADER_LINE_START + "Version " + VirageCore.getVersion()
+ ", Timestamp: " + dtf.format(now));
+ ", Timestamp: " + now);
this.displayMessage(HEADER_LINE_START + "If you need help, type \"help\", \"h\" or \"?\".");
this.displayMessage(HEADER_LINE_START + "To exit ViRAGe, type \"exit\".");
}
Expand Down
Loading

0 comments on commit 0d60d9c

Please sign in to comment.