summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/AptGetUpgradable.java
blob: 8c341432fbe080d216f36b0b59f1777c2ce62f22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
{

	private static final Pattern REGEX = Pattern.compile( "^(\\S+)/(\\S+)\\s+(\\S+)\\s.*\\[upgrad.*from:\\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", "list", "--upgradable" };
	}

	@Override
	protected void initEnvironment( Map<String, String> 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.matches() ) {
			status.packages.add( new Package( m.group( 1 ), m.group( 2 ), m.group( 4 ), 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<Package> 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;
		}
	}

}