Skip to content

Commit

Permalink
Added new process using python script
Browse files Browse the repository at this point in the history
  • Loading branch information
Matus Kasak committed Nov 22, 2024
1 parent 2c8c732 commit 6c2fb01
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 239 deletions.
140 changes: 0 additions & 140 deletions dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

70 changes: 70 additions & 0 deletions dspace-api/src/main/java/org/dspace/testing/Testing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.testing;

import org.apache.commons.cli.ParseException;
import org.dspace.scripts.DSpaceRunnable;
import org.dspace.utils.DSpace;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Testing extends DSpaceRunnable<TestingScriptConfiguration> {

@Override
public TestingScriptConfiguration getScriptConfiguration() {
return new DSpace().getServiceManager()
.getServiceByName("testing-script", TestingScriptConfiguration.class);
}

@Override
public void setup() throws ParseException {

}

@Override
public void internalRun() throws Exception {
System.out.println("Hello world from java");
try {
// loading python scripts stored in resources
InputStream scriptInputStream = getClass().getClassLoader().getResourceAsStream("python-script.py");
if (scriptInputStream == null) {
throw new FileNotFoundException("Python script not found in resources");
}

File tempFile = File.createTempFile("python-script", ".py");
tempFile.deleteOnExit();
// transfer the content to the temporary file
try (FileOutputStream out = new FileOutputStream(tempFile)) {
scriptInputStream.transferTo(out);
}

// configure ProcessBuilder to run script using python commands
ProcessBuilder processBuilder = new ProcessBuilder("python", tempFile.getAbsolutePath());
processBuilder.directory(tempFile.getParentFile());
Process process = processBuilder.start();

InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader);

// reading the scripts output and then outputs it to the console
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}

inputStreamReader.close();
reader.close();
int exitCode = process.waitFor();
System.out.println("Exited with code: " + exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.testing;

import org.apache.commons.cli.Options;
import org.dspace.scripts.configuration.ScriptConfiguration;

public class TestingScriptConfiguration<T extends Testing> extends ScriptConfiguration<T> {

private Class<T> dspaceRunnableclass;

@Override
public Class<T> getDspaceRunnableClass() {
return dspaceRunnableclass;
}

@Override
public void setDspaceRunnableClass(Class<T> dspaceRunnableClass) {
this.dspaceRunnableclass = dspaceRunnableClass;
}

@Override
public Options getOptions() {
if (options == null) {
Options options = new Options();
options.addOption("h", "help", false, "Display help message");
super.options = options;
}
return options;
}
}
1 change: 1 addition & 0 deletions dspace-api/src/main/resources/python-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello world from python")
10 changes: 5 additions & 5 deletions dspace/config/spring/api/scripts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli"/>
</bean>

<bean id="process-cleaner-2" class="org.dspace.administer.ProcessCleanerCliConfiguration2">
<property name="description" value="Cleanup all the old processes in the specified state"/>
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli2"/>
</bean>

<bean id="filter-media" class="org.dspace.app.mediafilter.MediaFilterScriptConfiguration">
<property name="description" value="Perform the media filtering to extract full text from documents and to create thumbnails"/>
<property name="dspaceRunnableClass" value="org.dspace.app.mediafilter.MediaFilterScript"/>
Expand Down Expand Up @@ -96,4 +91,9 @@
<property name="dspaceRunnableClass" value="org.dspace.app.bulkaccesscontrol.BulkAccessControlCli"/>
</bean>

<bean id="testing-script" class="org.dspace.testing.TestingScriptConfiguration">
<property name="description" value="Description for testing script"/>
<property name="dspaceRunnableClass" value="org.dspace.testing.Testing"/>
</bean>

</beans>
10 changes: 5 additions & 5 deletions dspace/config/spring/rest/scripts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleaner"/>
</bean>

<bean id="process-cleaner-2" class="org.dspace.administer.ProcessCleanerCliConfiguration2">
<property name="description" value="Cleanup all the old processes in the specified state"/>
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli2"/>
</bean>

<bean id="orcid-bulk-push" class="org.dspace.orcid.script.OrcidBulkPushScriptConfiguration" primary="true">
<property name="description" value="Perform the bulk synchronization of all the BATCH configured ORCID entities placed in the ORCID queue"/>
<property name="dspaceRunnableClass" value="org.dspace.orcid.script.OrcidBulkPush"/>
Expand All @@ -74,4 +69,9 @@
<property name="dspaceRunnableClass" value="org.dspace.app.bulkaccesscontrol.BulkAccessControl"/>
</bean>

<bean id="testing-script" class="org.dspace.testing.TestingScriptConfiguration">
<property name="description" value="Description for testing script"/>
<property name="dspaceRunnableClass" value="org.dspace.testing.Testing"/>
</bean>

</beans>

0 comments on commit 6c2fb01

Please sign in to comment.