<commit_before>#!/usr/bin/python3

from sys import argv, exit
from getopt import getopt, GetoptError
from subprocess import Popen, PIPE
from storage import Environment, Storage, ProbeMode_STANDARD, ProbeMode_STANDARD_WRITE_MOCKUP, TargetMode_DIRECT
from storage import RemoteCallbacks, RemoteCommand, RemoteFile, set_remote_callbacks
from storage import set_logger, get_logfile_logger


host = "localhost"
save_mockup = False
save_devicegraph = False


def run_command(name):

    cmd = "ssh -l root %s %s" % (host, name)

    p = Popen(cmd, shell = True, stdout = PIPE, stderr = PIPE, close_fds = True)
    stdout, stderr = p.communicate()

    stdout = stdout.rstrip()
    stderr = stderr.rstrip()

    ret = RemoteCommand()

    # TODO rethink difference between no line and single empty line

    if stdout:
        for line in stdout.split('\n'):
            ret.stdout.push_back(line)

    if stderr:
        for line in stderr.split('\n'):
            ret.stderr.push_back(line)

    ret.exit_code = p.returncode

    return ret


class MyRemoteCallbacks(RemoteCallbacks):

    def __init__(self):
        super(MyRemoteCallbacks, self).__init__()

    def get_command(self, name):
        print("command '%s'" % name)
        command = run_command(name)
        return command

    def get_file(self, name):
        print("file '%s'" % name)
        command = run_command("cat '%s'" % name)
        file = RemoteFile(command.stdout)
        return file


def doit():

    set_logger(get_logfile_logger())

    my_remote_callbacks = MyRemoteCallbacks()

    set_remote_callbacks(my_remote_callbacks)

    environment = Environment(True, ProbeMode_STANDARD_WRITE_MOCKUP if save_mockup else ProbeMode_STANDARD, TargetMode_DIRECT)
    if save_mockup:
        environment.set_mockup_filename("mockup.xml");

    storage = Storage(environment)
    storage.probe()

    print()

    probed = storage.get_probed()

    print(probed)

    if save_devicegraph:
        probed.save("devicegraph.xml");


def usage():
    print("usage: remote-probe.py [--host=name] [--save-mockup] [--save-devicegraph]")
    exit(1)

try:
    opts, args = getopt(argv[1:], "", ["host=", "save-mockup", "save-devicegraph"])

except GetoptError:
    usage()

if len(args) > 0:
    usage()

for o, a in opts:
    if o == "--host":
        host = a
    if o == "--save-mockup":
        save_mockup = True
    if o == "--save-devicegraph":
        save_devicegraph = True

doit()
<commit_msg>- allow to set ssh port<commit_after>#!/usr/bin/python3

from sys import argv, exit
from getopt import getopt, GetoptError
from subprocess import Popen, PIPE
from storage import Environment, Storage, ProbeMode_STANDARD, ProbeMode_STANDARD_WRITE_MOCKUP, TargetMode_DIRECT
from storage import RemoteCallbacks, RemoteCommand, RemoteFile, set_remote_callbacks
from storage import set_logger, get_logfile_logger, get_stdout_logger


host = "localhost"
port = "22";
save_mockup = False
save_devicegraph = False


def run_command(name):

    cmd = "ssh -l root %s -p %s %s" % (host, port, name)

    p = Popen(cmd, shell = True, stdout = PIPE, stderr = PIPE, close_fds = True)
    stdout, stderr = p.communicate()

    stdout = stdout.rstrip()
    stderr = stderr.rstrip()

    ret = RemoteCommand()

    # TODO rethink difference between no line and single empty line

    if stdout:
        stdout = stdout.decode("utf-8")
        for line in stdout.split('\n'):
            ret.stdout.push_back(line)

    if stderr:
        stderr = stderr.decode("utf-8")
        for line in stderr.split('\n'):
            ret.stderr.push_back(line)

    ret.exit_code = p.returncode

    return ret


class MyRemoteCallbacks(RemoteCallbacks):

    def __init__(self):
        super(MyRemoteCallbacks, self).__init__()

    def get_command(self, name):
        print("command '%s'" % name)
        command = run_command(name)
        return command

    def get_file(self, name):
        print("file '%s'" % name)
        command = run_command("cat '%s'" % name)
        file = RemoteFile(command.stdout)
        return file


def doit():

    set_logger(get_logfile_logger())

    my_remote_callbacks = MyRemoteCallbacks()

    set_remote_callbacks(my_remote_callbacks)

    environment = Environment(True, ProbeMode_STANDARD_WRITE_MOCKUP if save_mockup else ProbeMode_STANDARD, TargetMode_DIRECT)
    if save_mockup:
        environment.set_mockup_filename("mockup.xml");

    storage = Storage(environment)
    storage.probe()

    print()

    probed = storage.get_probed()

    print(probed)

    if save_devicegraph:
        probed.save("devicegraph.xml");


def usage():
    print("usage: remote-probe.py [--host=name] [--save-mockup] [--save-devicegraph]")
    exit(1)

try:
    opts, args = getopt(argv[1:], "", ["host=", "port=", "save-mockup", "save-devicegraph"])

except GetoptError:
    usage()

if len(args) > 0:
    usage()

for o, a in opts:
    if o == "--host":
        host = a
    if o == "--port":
        port = a
    if o == "--save-mockup":
        save_mockup = True
    if o == "--save-devicegraph":
        save_devicegraph = True

doit()

{'fraction_non_alphanumeric': 0.11708365910867866, 'fraction_numerical': 0.0023455824863174357, 'mean_word_length': 3.103448275862069, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2352725', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1631, 'n_words': 419}