<commit_before># vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

__author__    = "<NAME>"
__copyright__ = "Copyright 2011, <NAME>"
__license__   = "MIT"

import traceback
import StringIO

from openssh_wrapper import SSHConnection

def get_traceback(prefix="\n*** "):
    '''returns the last traceback as a string with a given prefix'''
    fp = StringIO.StringIO()
    traceback.print_exc(file=fp)
    if fp.getvalue() == "None\n":
        return "(No Traceback)"
    else:
        return prefix+fp.getvalue() 

class CommandWrapper(object):
    '''This class represent a wrapper for arbitrary commands.
       It can either execute them local or via ssh.
    '''

    ######################################################################
    ##
    def __init__(self, via_ssh=None, ssh_username=None, 
                 ssh_hostname=None, ssh_key=None):
        '''Constructor'''
        self._via_ssh = via_ssh
        self._ssh_username = ssh_username
        self._ssh_hostname = ssh_hostname
        self._ssh_key = ssh_key

        if self._via_ssh is True:
            if self._ssh_hostname is None:
                raise Exception("No hostname defined")  
          
            self._ssh_connection = SSHConnection(server=self._ssh_hostname, 
                                                 identity_file=self._ssh_key,
                                                 login=self._ssh_username)

    def run(self, executable, arguments=[]):
        '''Runs a command (blocking)'''
        cmd = executable
        for arg in arguments:
            cmd += " %s " % (arg)

        if self._via_ssh is True:
            result = self._ssh_connection.run(cmd)

            cwr = CommandWrapperResult(command=cmd,
                                       stdout=result.stdout,
                                       stderr=result.stderr,
                                       returncode=result.returncode)
            return cwr                
        else:
            #pass #popen.communicate()
            cwr = CommandWrapperResult(command=cmd)
            return cwr

class CommandWrapperResult(object):
    '''Represents a result.
    '''
    def __init__(self, command, stdout=None, stderr=None, returncode=None):
        self.command = command
        self.stdout = stdout
        self.stderr = stderr
        self.returncode = returncode

    def __str__(self):
        return unicode(self).decode('utf-8', 'ignore')

    def __unicode__(self):
        ret = []
        ret.append(u'command: %s' % unicode(self.command))
        ret.append(u'stdout: %s' % unicode(self.stdout))
        ret.append(u'stderr: %s' % unicode(self.stderr))
        ret.append(u'returncode: %s' % unicode(self.returncode))
        return u'\n'.join(ret)
<commit_msg>attempt to fix cmdline wrapper<commit_after># vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

__author__    = "<NAME>"
__copyright__ = "Copyright 2011-2012, <NAME>"
__license__   = "MIT"

import traceback
import StringIO
import subprocess

from openssh_wrapper import SSHConnection

def get_traceback(prefix="\n*** "):
    '''returns the last traceback as a string with a given prefix'''
    fp = StringIO.StringIO()
    traceback.print_exc(file=fp)
    if fp.getvalue() == "None\n":
        return "(No Traceback)"
    else:
        return prefix+fp.getvalue() 

class CommandWrapper(object):
    '''This class represent a wrapper for arbitrary commands.
       It can either execute them locally or via ssh.
    '''

    ######################################################################
    ##
    def __init__(self, via_ssh=None, ssh_username=None, 
                 ssh_hostname=None, ssh_key=None):
        '''Constructor'''
        self._via_ssh = via_ssh
        self._ssh_username = ssh_username
        self._ssh_hostname = ssh_hostname
        self._ssh_key = ssh_key

        if self._via_ssh is True:
            if self._ssh_hostname is None:
                raise Exception("No hostname defined")  
          
            self._ssh_connection = SSHConnection(server=self._ssh_hostname, 
                                                 identity_file=self._ssh_key,
                                                 login=self._ssh_username)

    def run(self, executable, arguments=[]):
        '''Runs a command (blocking)'''
        cmd = executable
        for arg in arguments:
            cmd += " %s " % (arg)

        if self._via_ssh is True:
            result = self._ssh_connection.run(cmd)

            cwr = CommandWrapperResult(command=cmd,
                                       stdout=result.stdout,
                                       stderr=result.stderr,
                                       returncode=result.returncode)
            return cwr                
        else:
            job_error = None
            job_output = None
            returncode = None

            pid = subprocess.Popen(cmd, shell=True, 
                                      #executable=self.executable,
                                      stderr=job_error, 
                                      stdout=job_output)
            returncode = pid.wait()

            cwr = CommandWrapperResult(command=cmd,
                                       stdout=job_output,
                                       stderr=job_error,
                                       returncode=returncode)
            return cwr                


class CommandWrapperResult(object):
    '''Represents a result.
    '''
    def __init__(self, command, stdout=None, stderr=None, returncode=None):
        self.command = command
        self.stdout = stdout
        self.stderr = stderr
        self.returncode = returncode

    def __str__(self):
        return unicode(self).decode('utf-8', 'ignore')

    def __unicode__(self):
        ret = []
        ret.append(u'command: %s' % unicode(self.command))
        ret.append(u'stdout: %s' % unicode(self.stdout))
        ret.append(u'stderr: %s' % unicode(self.stderr))
        ret.append(u'returncode: %s' % unicode(self.returncode))
        return u'\n'.join(ret)

{'fraction_non_alphanumeric': 0.1175600131535679, 'fraction_numerical': 0.003288391976323578, 'mean_word_length': 1.8058118081180812, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1642303', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1573, 'n_words': 371}