<commit_before># Copyright (c) 2010 by Cisco Systems, Inc.
"""
CLI Diff
"""

import sys
import os
import getopt
from instmakelib import instmake_cli
from instmakelib import imlib
from instmakelib import instmake_log as LOG
from instmakelib import climanager
from instmakelib import pysets
from instmakelib import jsonconfig

LOG_A = 0
LOG_B = 1
NAME_A = "build 1"
NAME_B = "build 2"

path_normalizer = None

description = "Command-Line Diff"

def usage():
    print "clidiff:", description
    print "\t[-p PID] if 1 log, process CLI for PID from log, and show results"
    print "\t         if 2 logs and 1 -p, find PID in first log and compare"
    print "\t         if 2 logs and 2 -p's, find PIDs in both logs and compare"
    print "\t[-n name] Give a better name to an instmake log (can be given twice)"
    print "\t[--cliopt=plugin,option[,...]] Pass an option to a CLI plugin"
    print "\t[--map1 plugin] use filename mapping for log #1"
    print "\t[--map2 plugin] use filename mapping for log #2"
    print "\t[--ok] Show the files that matched their CLI's correctly"

    plugins = LOG.GetPlugins()

    print
    print "CLI Plugins:"
    print "============"
    plugin_manager = climanager.CLIManager()
    plugin_manager.PrintHelp()

    print
    print "Filemap Plugins:"
    print "================"
    instmake_cli.print_plugin_descriptions(plugins,
            instmake_cli.FILEMAP_PLUGIN_PREFIX)

def report(log_file_names, args):
    global NAME_A
    global NAME_B

    pids = None

    # Command-line options
    optstring = "n:p:u"
    longopts = ["cliopt=", "map1=", "map2=", "ok"]

    try:
        opts, args = getopt.getopt(args, optstring, longopts)
    except getopt.GetoptError:
        usage()
        sys.exit(1)

    plugin_manager = climanager.CLIManager()

    plugins = LOG.GetPlugins()
    n_count = 0
    map1 = None
    map2 = None
    show_ok_files = 0
    pids = []

    print "Command-line used:"
    print ' '.join(sys.argv)
    print


    for opt, arg in opts:
        if opt == "-n":
            if n_count == 0:
                NAME_A = arg
                n_count += 1
            elif n_count == 1:
                NAME_B = arg
                n_count += 1
            elif n_count == 2:
                sys.exit("-n option can be given only twice")
        elif opt == "-p":
            pids.append(arg)
        elif opt == "--cliopt":
            plugin_manager.UserOption(arg)
        elif opt == "--ok":
            show_ok_files = 1
        elif opt == "--map1":
            try:
                map1 = plugins.LoadPlugin( instmake_cli.FILEMAP_PLUGIN_PREFIX,
                        arg)
            except ImportError, err:
                sys.exit("unable to import %s-plugin '%s':\n\t%s" % \
                        (instmake_cli.FILEMAP_PLUGIN_PREFIX, arg, err))
        elif opt == "--map2":
            try:
                map2 = plugins.LoadPlugin( instmake_cli.FILEMAP_PLUGIN_PREFIX,
                        arg)
            except ImportError, err:
                sys.exit("unable to import %s-plugin '%s':\n\t%s" % \
                        (instmake_cli.FILEMAP_PLUGIN_PREFIX, arg, err))
        else:
            sys.exit("Unhandled option %s" % (opt,))

    if args:
        usage()
        sys.exit(1)

    # Path normalizer being used?
    if imlib.config.has_key(jsonconfig.CLIDIFF_NORMPATH):
        plugin_name = imlib.config[jsonconfig.CLIDIFF_NORMPATH]
        try:
            module = jsonconfig.load_site_plugin(plugin_name)
            global path_normalizer
            path_normalizer = module.normalize_path
        except Exception, e:
            sys.exit("Unable to import %s: %s" % (plugin_name, e))

    # We need exactly 1 or 2 log files, depending on '-p'
    if len(log_file_names) == 1:
        if map2:
            sys.exit("Can't use --map2 with only one log file.")
        if len(pids) == 0:
            sys.exit("If giving only one log file, at least one -p is needed.")

        do_pids(plugin_manager, log_file_names[0], pids, map1)

    else:
        if len(log_file_names) != 2:
            sys.exit("'clidiff' report uses two log files.")

        do_regular_compare(plugin_manager, log_file_names, map1, map2,
                show_ok_files, pids)

def do_pids(plugin_manager, log_file_name, pid, map1):
    build = Build(log_file_name, plugin_manager, map1)
    build.ReadLogForPIDs(pid)

def do_regular_compare(plugin_manager, log_file_names, map1, map2,<commit_msg>Fix typo of global variable name.<commit_after># Copyright (c) 2010 by Cisco Systems, Inc.
"""
CLI Diff
"""

import sys
import os
import getopt
from instmakelib import instmake_cli
from instmakelib import imlib
from instmakelib import instmake_log as LOG
from instmakelib import climanager
from instmakelib import pysets
from instmakelib import jsonconfig

LOG_A = 0
LOG_B = 1
NAME_A = "build 1"
NAME_B = "build 2"

path_normalizer = None

description = "Command-Line Diff"

def usage():
    print "clidiff:", description
    print "\t[-p PID] if 1 log, process CLI for PID from log, and show results"
    print "\t         if 2 logs and 1 -p, find PID in first log and compare"
    print "\t         if 2 logs and 2 -p's, find PIDs in both logs and compare"
    print "\t[-n name] Give a better name to an instmake log (can be given twice)"
    print "\t[--cliopt=plugin,option[,...]] Pass an option to a CLI plugin"
    print "\t[--map1 plugin] use filename mapping for log #1"
    print "\t[--map2 plugin] use filename mapping for log #2"
    print "\t[--ok] Show the files that matched their CLI's correctly"

    plugins = LOG.GetPlugins()

    print
    print "CLI Plugins:"
    print "============"
    plugin_manager = climanager.CLIManager()
    plugin_manager.PrintHelp()

    print
    print "Filemap Plugins:"
    print "================"
    instmake_cli.print_plugin_descriptions(plugins,
            instmake_cli.FILEMAP_PLUGIN_PREFIX)

def report(log_file_names, args):
    global NAME_A
    global NAME_B

    pids = None

    # Command-line options
    optstring = "n:p:u"
    longopts = ["cliopt=", "map1=", "map2=", "ok"]

    try:
        opts, args = getopt.getopt(args, optstring, longopts)
    except getopt.GetoptError:
        usage()
        sys.exit(1)

    plugin_manager = climanager.CLIManager()

    plugins = LOG.GetPlugins()
    n_count = 0
    map1 = None
    map2 = None
    show_ok_files = 0
    pids = []

    print "Command-line used:"
    print ' '.join(sys.argv)
    print


    for opt, arg in opts:
        if opt == "-n":
            if n_count == 0:
                NAME_A = arg
                n_count += 1
            elif n_count == 1:
                NAME_B = arg
                n_count += 1
            elif n_count == 2:
                sys.exit("-n option can be given only twice")
        elif opt == "-p":
            pids.append(arg)
        elif opt == "--cliopt":
            plugin_manager.UserOption(arg)
        elif opt == "--ok":
            show_ok_files = 1
        elif opt == "--map1":
            try:
                map1 = plugins.LoadPlugin( instmake_cli.FILEMAP_PLUGIN_PREFIX,
                        arg)
            except ImportError, err:
                sys.exit("unable to import %s-plugin '%s':\n\t%s" % \
                        (instmake_cli.FILEMAP_PLUGIN_PREFIX, arg, err))
        elif opt == "--map2":
            try:
                map2 = plugins.LoadPlugin( instmake_cli.FILEMAP_PLUGIN_PREFIX,
                        arg)
            except ImportError, err:
                sys.exit("unable to import %s-plugin '%s':\n\t%s" % \
                        (instmake_cli.FILEMAP_PLUGIN_PREFIX, arg, err))
        else:
            sys.exit("Unhandled option %s" % (opt,))

    if args:
        usage()
        sys.exit(1)

    # Path normalizer being used?
    if imlib.config.has_key(jsonconfig.CONFIG_CLIDIFF_NORMPATH):
        plugin_name = imlib.config[jsonconfig.CONFIG_CLIDIFF_NORMPATH]
        try:
            module = jsonconfig.load_site_plugin(plugin_name)
            global path_normalizer
            path_normalizer = module.normalize_path
        except Exception, e:
            sys.exit("Unable to import %s: %s" % (plugin_name, e))

    # We need exactly 1 or 2 log files, depending on '-p'
    if len(log_file_names) == 1:
        if map2:
            sys.exit("Can't use --map2 with only one log file.")
        if len(pids) == 0:
            sys.exit("If giving only one log file, at least one -p is needed.")

        do_pids(plugin_manager, log_file_names[0], pids, map1)

    else:
        if len(log_file_names) != 2:
            sys.exit("'clidiff' report uses two log files.")

        do_regular_compare(plugin_manager, log_file_names, map1, map2,
                show_ok_files, pids)

def do_pids(plugin_manager, log_file_name, pid, map1):
    build = Build(log_file_name, plugin_manager, map1)
    build.ReadLogForPIDs(pid)

def do_regular_compare(plugin_manager, log_file_names, map1, map2,
{'fraction_non_alphanumeric': 0.10788243152972612, 'fraction_numerical': 0.0111333778668448, 'mean_word_length': 2.13542757417103, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, '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': '3725345', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2816, 'n_words': 832}