package org.openslx.taskmanager.tasks; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.SystemCommandTask; public class AptGetUpgradable extends SystemCommandTask { // Inst vivaldi-snapshot [6.4.3160.29-1] (6.5.3222.3-1 Official Vivaldi package repository:stable [amd64]) private static final Pattern REGEX = Pattern.compile( "^Inst (\\S+)(?: \\[(\\S+)\\])? \\((\\S+) ([^\\[\\)]+)" ); private final Output status = new Output(); private StringBuilder otherOutput = new StringBuilder(); @Override protected boolean initTask() { this.setStatusObject( status ); this.timeoutSeconds = 25; return true; } @Override protected String[] initCommandLine() { return new String[] { "apt-get", "-s", "full-upgrade" }; } @Override protected void initEnvironment( Map environment ) { environment.put( "LANG", "C.UTF-8" ); environment.put( "LC_ALL", "C.UTF-8" ); } @Override protected boolean processEnded( int exitCode ) { if ( this.status.packages.isEmpty() && Util.isEmpty( this.status.error ) ) { this.status.error = this.otherOutput.toString(); } return exitCode == 0; } @Override protected void processStdOut( String line ) { Matcher m = REGEX.matcher( line ); if ( m.find() ) { String source = null; String[] list = m.group( 4 ).split( ", " ); for ( String src : list ) { String tmp = src.trim().replaceFirst( "^.*[:/]", "" ); if ( source == null || tmp.endsWith( "-security" ) ) { source = tmp; } } if ( source == null ) { source = m.group( 4 ); } status.packages.add( new Package( m.group( 1 ), source, m.group( 2 ), m.group( 3 ) ) ); } else { otherOutput.append( line ); otherOutput.append( '\n' ); } } @Override protected void processStdErr( String line ) { if ( line.isEmpty() || line.contains( "stable CLI interface" ) ) return; if ( status.error == null ) { status.error = line; } else { status.error += "\n" + line; } } private static class Output { public final List packages = new ArrayList<>(); public String error; } @SuppressWarnings( "unused" ) private static class Package { public final String name; public final String source; public final String newVersion; public final String oldVersion; public Package( String name, String source, String oldVersion, String newVersion ) { this.name = name; this.source = source; this.newVersion = newVersion; this.oldVersion = oldVersion; } } }