<commit_before>package org.safehaus.subutai.impl.packagemanager.handler;

import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import org.safehaus.subutai.api.commandrunner.*;
import org.safehaus.subutai.api.packagemanager.*;
import org.safehaus.subutai.impl.packagemanager.PackageManagerImpl;
import org.safehaus.subutai.shared.protocol.Agent;

public class ListHandler extends AbstractHandler<Collection<PackageInfo>> {

    private final String hostname;
    private Pattern lineStartPattern = Pattern.compile("[a-z]{2,3}\\s+");

    public ListHandler(PackageManagerImpl pm, String hostname) {
        super(pm);
        this.hostname = hostname;
    }

    @Override
    public Collection<PackageInfo> performAction() {
        Agent agent = pm.getAgentManager().getAgentByHostname(hostname);
        if(agent == null) return Collections.emptyList();

        AgentRequestBuilder rb = new AgentRequestBuilder(agent, "dpkg -l");
        Command cmd = pm.getCommandRunner().createCommand(new HashSet<>(Arrays.asList(rb)));
        pm.getCommandRunner().runCommand(cmd);
        if(cmd.hasSucceeded()) {
            Collection<PackageInfo> ls = new ArrayList<>();
            AgentResult res = cmd.getResults().get(agent.getUuid());
            String s = res.getStdOut();
            Pattern delim = Pattern.compile("\\s+");
            try(BufferedReader br = new BufferedReader(new StringReader(s))) {
                while((s = br.readLine()) != null) {
                    PackageInfo pi = parseLine(s, delim);
                    if(pi != null) ls.add(pi);
                }
            } catch(IOException ex) {
                // TODO:
            }
            return ls;
        }
        return Collections.emptyList();
    }

    PackageInfo parseLine(String s, Pattern delim) {
        if(!lineStartPattern.matcher(s).matches()) return null;
        String[] arr = delim.split(s);
        PackageInfo p = null;
        if(arr.length > 3) {
            char[] status = arr[0].toCharArray();
            p = new PackageInfo(arr[1], arr[2]);
            p.setDescription(arr[3]);
            p.setSelectionState(SelectionState.getByAbbrev(status[0]));
            p.setState(PackageState.getByAbbrev(status[1]));
            if(status.length > 2) {
                PackageFlag f = PackageFlag.getByAbbrev(status[2]);
                if(f != null) p.getFlags().add(f);
            }
        }
        return p;<commit_msg>handling of `dpkg -l' output regardless of its version<commit_after>package org.safehaus.subutai.impl.packagemanager.handler;

import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import org.safehaus.subutai.api.commandrunner.*;
import org.safehaus.subutai.api.packagemanager.*;
import org.safehaus.subutai.impl.packagemanager.PackageManagerImpl;
import org.safehaus.subutai.shared.protocol.Agent;

public class ListHandler extends AbstractHandler<Collection<PackageInfo>> {

    private final String hostname;
    private Pattern lineStartPattern = Pattern.compile("^[a-z]{2,3}\\s+");

    public ListHandler(PackageManagerImpl pm, String hostname) {
        super(pm);
        this.hostname = hostname;
    }

    @Override
    public Collection<PackageInfo> performAction() {
        Agent agent = pm.getAgentManager().getAgentByHostname(hostname);
        if(agent == null) return null;

        AgentRequestBuilder rb = new AgentRequestBuilder(agent, "dpkg -l");
        Command cmd = pm.getCommandRunner().createCommand(new HashSet<>(Arrays.asList(rb)));
        pm.getCommandRunner().runCommand(cmd);
        if(cmd.hasSucceeded()) {
            Collection<PackageInfo> ls = new ArrayList<>();
            AgentResult res = cmd.getResults().get(agent.getUuid());
            String s = res.getStdOut();
            Pattern delim = Pattern.compile("\\s+");
            try(BufferedReader br = new BufferedReader(new StringReader(s))) {
                int cols = 0;
                while((s = br.readLine()) != null) {
                    if(cols == 0 && s.startsWith("+++"))
                        // http://stackoverflow.com/questions/275944
                        // count occurences of a seperator minus sign
                        cols = 1 + s.length() - s.replace("-", "").length();
                    PackageInfo pi = parseLine(s, cols, delim);
                    if(pi != null) ls.add(pi);
                }
            } catch(IOException ex) {
                // TODO:
            }
            return ls;
        }
        return Collections.emptyList();
    }

    PackageInfo parseLine(String s, int columns, Pattern delim) {
        if(!lineStartPattern.matcher(s).find()) return null;
        String[] arr = delim.split(s, columns);
        PackageInfo p = null;
        if(arr.length > columns - 1) {
            char[] status = arr[0].toCharArray();
            p = new PackageInfo(arr[1], arr[2]);
            if(columns == 5) {
                p.setArch(arr[3]);
                p.setDescription(arr[4]);
            } else
                p.setDescription(arr[3]);
            p.setSelectionState(SelectionState.getByAbbrev(status[0]));
            p.setState(PackageState.getByAbbrev(status[1]));
            if(status.length > 2) {
                PackageFlag f = PackageFlag.getByAbbrev(status[2]);
                if(f != null) p.getFlags().add(f);
            }
        }
        return p;
{'fraction_non_alphanumeric': 0.1298219584569733, 'fraction_numerical': 0.006305637982195846, 'mean_word_length': 2.2487951807228916, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5534129', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1498, 'n_words': 298}