<commit_before>/*!
GPII Process Reporter processes bridge -- gpii.processes.

Copyright 2016 Inclusive Design Research Centre, OCAD University

Licensed under the New BSD license. You may not use this file except in
compliance with this License.

You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/

#r "System.Management.dll"

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Dynamic;
using System.Management;

public class ProcInfo
{
    public string command = "";
    public int pid = -1;
    public string fullPath = "";
    public string[] argv;
    public string state = "";
}

public class Startup
{
    public async Task<object> Invoke(dynamic input)
    {
        Process[] processes;
        Process aProcess;
        ProcessModule mainModule;
        ProcInfo aProcInfo;
        ArrayList result;

        // Either get a list of processes with the given name, or a list of
        // all processes.
        if (input != null) {
            processes = Process.GetProcessesByName(input);
        }
        else {
            processes = Process.GetProcesses();
        }

        result = new ArrayList(processes.Length);
        for (int i = 0; i < processes.Length; i++) {
            aProcess = processes[i];
            aProcInfo = new ProcInfo();
            aProcInfo.pid = aProcess.Id;
            aProcInfo.command = aProcess.ProcessName;

            // Get full path via the process's MainModule.
            try {
                mainModule = aProcess.MainModule;
                aProcInfo.fullPath = mainModule.FileName;
            }
            catch (Exception e) {
                mainModule = null;
            }
            // Get the command line and its arguments.  Based on:
            // https://stackoverflow.com/questions/2633628/can-i-get-command-line-arguments-of-other-processes-from-net-c
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " +
                aProcess.Id
            );
            StringBuilder commandLine = new StringBuilder();
            foreach (ManagementObject mo in searcher.Get()) {
                commandLine.Append(mo["CommandLine"]);
                commandLine.Append(" ");
            }
            aProcInfo.argv = makeArgv(commandLine.ToString());

            // If full path wasn't retrieved via MainModule, take it from
            // argv[0], and/or insure that argv[0] is the full path.
            if (aProcInfo.argv.Length > 0) {
                if (aProcInfo.fullPath == "") {
                    aProcInfo.fullPath = aProcInfo.argv[0];
                }
                else {
                    aProcInfo.argv[0] = aProcInfo.fullPath;
                }
            }
            // Use only the main thread's state (the first one).
            // Also, map Windows thread states to Linux-y process states
            // This may not be correct thing to do:  revisit.
            ProcessThreadCollection threads = aProcess.Threads;
            switch (aProcess.Threads[0].ThreadState) {
                case ThreadState.Running:
                case ThreadState.Wait:
                    aProcInfo.state = "Running";
                    break;

                case ThreadState.Initialized:
                case ThreadState.Ready:
                case ThreadState.Standby:
                case ThreadState.Transition:
                    aProcInfo.state = "Sleeping";
                    break;

                case ThreadState.Terminated:
                case ThreadState.Unknown:
                    aProcInfo.state = "Zombie";
                    break;
            }
            result.Add(aProcInfo);
        }
        return result.ToArray();<commit_msg>GPII-442:  C# processes access code.<commit_after>/*!
GPII Process Reporter processes bridge -- gpii.processes.

Copyright 2016 Inclusive Design Research Centre, OCAD University

Licensed under the New BSD license. You may not use this file except in
compliance with this License.

You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/

#r "System.Management.dll"

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Dynamic;
using System.Management;

public class ProcInfo
{
    public string command = "";
    public int pid = -1;
    public string fullPath = "";
    public string[] argv;
    public string state = "";
}

public class Startup
{
    public async Task<object> Invoke(dynamic input)
    {
        Process[] processes;
        Process aProcess;
        ProcessModule mainModule;
        ProcInfo aProcInfo;
        ArrayList result;

        // Either get a list of processes with the given name, or a list of
        // all processes.
        if (input != null) {
            processes = Process.GetProcessesByName(input);
        }
        else {
            processes = Process.GetProcesses();
        }

        result = new ArrayList(processes.Length);
        for (int i = 0; i < processes.Length; i++) {
            aProcess = processes[i];
            aProcInfo = new ProcInfo();
            aProcInfo.pid = aProcess.Id;
            aProcInfo.command = aProcess.ProcessName;

            // Get the command line and its arguments.  Based on:
            // https://stackoverflow.com/questions/2633628/can-i-get-command-line-arguments-of-other-processes-from-net-c
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " +
                aProcess.Id
            );
            StringBuilder commandLine = new StringBuilder();
            foreach (ManagementObject mo in searcher.Get()) {
                commandLine.Append(mo["CommandLine"]);
                commandLine.Append(" ");
            }
            aProcInfo.argv = makeArgv(commandLine.ToString());

            // Get full path via the process's MainModule.  If that fails, use
            // aProcInfo.argv[0].  If it succeeds, insure that argv[0] is the
            // full path.
            try {
                mainModule = aProcess.MainModule;
                aProcInfo.fullPath = mainModule.FileName;
                aProcInfo.argv[0] = aProcInfo.fullPath;
            }
            catch (Exception e) {
                mainModule = null;
                if (aProcInfo.argv.Length > 0) {
                    aProcInfo.fullPath = aProcInfo.argv[0];
                }
            }
            // Use only the main thread's state (the first one).
            // Also, map Windows thread states to Linux-y process states
            // This may not be correct thing to do:  revisit.
            ProcessThreadCollection threads = aProcess.Threads;
            switch (aProcess.Threads[0].ThreadState) {
                case ThreadState.Running:
                case ThreadState.Wait:
                    aProcInfo.state = "Running";
                    break;

                case ThreadState.Initialized:
                case ThreadState.Ready:
                case ThreadState.Standby:
                case ThreadState.Transition:
                    aProcInfo.state = "Sleeping";
                    break;

                case ThreadState.Terminated:
                case ThreadState.Unknown:
                    aProcInfo.state = "Zombie";
                    break;
            }
            result.Add(aProcInfo);
        }
        return result.ToArray();
{'fraction_non_alphanumeric': 0.08460038986354776, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 1.708905315029919, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '4225240', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1904, 'n_words': 558}