Hello,
I'm trying to run a python log file summary and classification tool using python as a base with windows Forms C# GUI. MY code is :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;
namespace Sepa
{
public partial class Form1 : Form
{
string LogFileLocation;
string DataPreprocessScript;
readonly string SummaryScript;
readonly string ClassificationScript;
readonly string PythonFilePath;
public string DataPreprocessScript1 { get => DataPreprocessScript; set => DataPreprocessScript = value; }
public Form1()
{
PythonFilePath = LocatePython();
DataPreprocessScript1 = Directory.GetCurrentDirectory() + "\\Scripts\\data_process.py";
SummaryScript = Directory.GetCurrentDirectory() + "\\Scripts\\Summary_code.py";
ClassificationScript = Directory.GetCurrentDirectory() + "\\Scripts\\Classify.py";
InitializeComponent();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "CSV Files |* .csv",
Title = "Open CSV"
};
if (ofd.ShowDialog() == DialogResult.OK)
{
FilePath.AppendText(ofd.FileName);
LogFileLocation = ofd.FileName;
}
}
private void SubmitButton_Click(object sender, EventArgs e)
{
var psi = new ProcessStartInfo
{
FileName = PythonFilePath
};
var errors = "";
if (string.IsNullOrEmpty(psi.FileName))
{
errors += "Python Not detected on PC";
}
else if(Summary.Checked == false && Classification.Checked == false)
{
errors += "Please Pick an Output";
}
else if (LogFileLocation == null)
{
errors += "No CSV File Submitted";
}
else
{
string script;
if (Summary.Checked)
{
script = SummaryScript;
}
else
{
script = ClassificationScript;
}
psi.Arguments = $"{script} {LogFileLocation} ";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
RunPython(psi, errors);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Clear_Click(object sender, EventArgs e)
{
Output.Text = "";
}
private void RunPython(ProcessStartInfo psi, string errors)
{
var results = "";
using (var process = Process.Start(psi))
{
errors += process.StandardError.ReadToEnd();
results += process.StandardOutput.ReadToEnd();
}
if (!string.IsNullOrEmpty(results))
{
Output.Text += "Results: " + results + System.Environment.NewLine;
}
if (!string.IsNullOrEmpty(errors))
{
Output.Text += "Errors: " + errors + System.Environment.NewLine;
}
}
private string LocatePython()
{
string Path = Environment.GetEnvironmentVariable("Path");
string[] paths = Path.Split(';');
for (int i = 0; i < paths.Length; i++)
{
string p = paths[i];
if (p.IndexOf("Python") > -1)
{
return p;
}
continue;
}
return "";
}
}
}
And the Python code is:
import pandas as pd
import numpy as np
import re
import os
import wikipedia as wk
import wikipediaapi
import sys
import requests
from bs4 import BeautifulSoup
try:
filename = sys.argv[1]
df = pd.read_csv(filename)
except IOError:
print("Error: File cannot be found/ Data cannot be read")
sys.exit()
else:
print("Successful upload of file.")
savefile = filename.rsplit('.')
savefilepath = savefile[0]
try:
df = df[['Time of Day','Process Name','PID','Command Line']]
except KeyError:
df = df[['Process Name','PID','Command Line']]
df.fillna('N/A', inplace = True)
df.fillna(0, inplace = True)
try:
df1 = df[['Time of Day','Process Name','PID','Command Line']]
except KeyError:
df1 = df[['Process Name','PID','Command Line']]
df2 = df[['Command Line']]
df1 = df1.rename({'Command Line': 'Command_Line'}, axis='columns')
df2 = df2.rename({'Command Line': 'Command_Line'}, axis='columns')
print("Data preprocessing is complete.")
df2[['Command_Path','Command_Line']] = df2.Command_Line.str.split(" ",n=1,expand=True)
charreplace = {
"\\\\": " ",
"\"": " ",
"\'": " "
}
df_fixed = df2.replace({'Command_Line':charreplace, 'Command_Path':charreplace}, regex=True)
df2[['Command_Path']] = df_fixed[['Command_Path']]
df2[['Command_Line']] = df_fixed[['Command_Line']]
del df_fixed
df2[['path','file']] = df2.Command_Path.str.rsplit(" ",n=1,expand=True)
translation = []
cleanr = re.compile('<.*?>')
executable = list(df2["file"])
for index in executable:
if index is not None:
a = re.sub('.exe' ,'.html', index.lower())
url = "https://ss64.com/nt/" + a
response = requests.get(url)
if url == "https://ss64.com/nt/":
title = "[Page not found]"
else:
soup = BeautifulSoup(response.text, "html.parser")
title = re.sub(cleanr, '', str(soup.findAll('title')))
if title != "[Page not found]":
htmlcontent = soup.findAll('p')
y = False
for x in htmlcontent:
if y is False:
print(re.sub(cleanr,'',str(x)))
y = True
else:
print("Manual Not Found")
I'm using the
An A-Z Index of Windows CMD commands - SS64.com[
^] manual page to get my information via web scraping but the GUI cant seem to handle dynamically finding creating the proper URL by adding a file .exe to the back and will just indefinitely freeze. The program currently prints it onto a terminal but it will eventually be saved into a excel document. When running this code via python normally, it out puts everything perfectly fine, but will just freeze from my GUI. I'm not even sure where to start looking for the issues.
What I have tried:
Ive tried manually setting the URL to specific pages and get information and that information properly outputs, but will freeze when the GUI tries to create the URL via the log file even if the created URL should be the same as the manually inputted one.