<commit_before>#
# <NAME> (<EMAIL>)
# February 2015, Stony Brook University
#
# tracerout.py: interface to system traceroute
# for this to work, traceroute has to be installed
# and accessible.

import threading
import time
import trparse
from sys import platform

from centinel import command


def traceroute(domain, method="udp", cmd_arguments=None, external=None):
    """This function uses centinel.command to issue
    a traceroute command, wait for it to finish execution and
    parse the results out to a dictionary.

    Params:
    domain-        the domain to be queried
    method-        the packet type used for traceroute, ICMP by default
    cmd_arguments- the list of arguments that need to be passed
                   to traceroute.

    """

    # the method specified by the function parameter here will
    # over-ride the ones given in cmd_arguments because
    # traceroute will use the last one in the argument list.

    if cmd_arguments is None:
        cmd_arguments = []

    if method == "tcp":
        if platform in [ 'linux', 'linux2' ]:
            cmd_arguments.append('-T')
        elif platform == "darwin":
            cmd_arguments.append('-P')
            cmd_arguments.append('tcp')

    elif method == "udp":
        if platform in [ 'linux', 'linux2' ]:
            cmd_arguments.append('-U')
        elif platform == "darwin":
            cmd_arguments.append('-P')
            cmd_arguments.append('udp')

    elif method == "icmp":
        if platform in [ 'linux', 'linux2' ]:
            cmd_arguments.append('-I')
        elif platform == "darwin":
            cmd_arguments.append('-P')
            cmd_arguments.append('icmp')

    cmd = ['traceroute'] + cmd_arguments + [domain]
    caller = command.Command(cmd, _traceroute_callback)
    caller.start()
    if not caller.started:
        message = ""
        if caller.exception is not None:
            if "No such file or directory" in caller.exception:
                message = ": traceroute not found or not installed"
            else:
                message = (", traceroute thread threw an "
                           "exception: %s" (caller.exception))
        elif "enough privileges" in caller.notifications:
            message = ": not enough privileges"
        elif "not known" in caller.notifications:
            message = ": name or service not known"
        else:
            message = caller.notifications

        results = {}
        results["domain"] = domain
        results["method"] = method<commit_msg>fixed a traceroute bug where the arguments dictionary kept growing<commit_after>#
# <NAME> (<EMAIL>)
# February 2015, Stony Brook University
#
# tracerout.py: interface to system traceroute
# for this to work, traceroute has to be installed
# and accessible.

import copy
import threading
import time
import trparse
from sys import platform

from centinel import command


def traceroute(domain, method="udp", cmd_arguments=None, external=None):
    """This function uses centinel.command to issue
    a traceroute command, wait for it to finish execution and
    parse the results out to a dictionary.

    Params:
    domain-        the domain to be queried
    method-        the packet type used for traceroute, ICMP by default
    cmd_arguments- the list of arguments that need to be passed
                   to traceroute.

    """

    # the method specified by the function parameter here will
    # over-ride the ones given in cmd_arguments because
    # traceroute will use the last one in the argument list.
    _cmd_arguments = []


    if cmd_arguments is not None:
        _cmd_arguments = copy.deepcopy(cmd_arguments)

    if method == "tcp":
        if platform in [ 'linux', 'linux2' ]:
            _cmd_arguments.append('-T')
        elif platform == "darwin":
            _cmd_arguments.append('-P')
            _cmd_arguments.append('tcp')

    elif method == "udp":
        if platform in [ 'linux', 'linux2' ]:
            _cmd_arguments.append('-U')
        elif platform == "darwin":
            _cmd_arguments.append('-P')
            _cmd_arguments.append('udp')

    elif method == "icmp":
        if platform in [ 'linux', 'linux2' ]:
            _cmd_arguments.append('-I')
        elif platform == "darwin":
            _cmd_arguments.append('-P')
            _cmd_arguments.append('icmp')

    cmd = ['traceroute'] + _cmd_arguments + [domain]

    caller = command.Command(cmd, _traceroute_callback)
    caller.start()
    if not caller.started:
        message = ""
        if caller.exception is not None:
            if "No such file or directory" in caller.exception:
                message = "traceroute not found or not installed"
            else:
                message = ("traceroute thread threw an "
                           "exception: %s" (caller.exception))
        elif "enough privileges" in caller.notifications:
            message = "not enough privileges"
        elif "not known" in caller.notifications:
            message = "name or service not known"
        else:
            message = caller.notifications

        results = {}
        results["domain"] = domain
        results["method"] = method
{'fraction_non_alphanumeric': 0.09199384141647421, 'fraction_numerical': 0.0026943802925327174, 'mean_word_length': 2.278864353312303, 'pattern_counts': {'":': 15, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '951535', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1350, 'n_words': 487}