import streamlit as st import os from subprocess import Popen, PIPE import re # Function to upload and save files def save_uploadedfile(uploadedfile): with open(os.path.join("uploads", uploadedfile.name), "wb") as f: f.write(uploadedfile.getbuffer()) return uploadedfile.name # Function to analyze shell script with shellcheck def analyze_shell_script(script_path): command = f'shellcheck {script_path}' process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() return stdout.decode(), stderr.decode() # Function to fix common shell script issues (simple example) def fix_shell_script_errors(script_content): # Example: Remove unused variables warning (SC2034) fixed_content = re.sub(r'\b\w+=["\'].+["\']\s*# SC2034', '', script_content) return fixed_content # Streamlit app layout st.title("Shell Script Investigation and Fixing App") # Upload the file uploaded_file = st.file_uploader("Upload your shell script", type=["sh"]) if uploaded_file is not None: # Save uploaded file file_name = save_uploadedfile(uploaded_file) st.write(f"File '{file_name}' uploaded successfully!") # Show the content of the uploaded file st.subheader("Uploaded Shell Script") script_content = uploaded_file.read().decode("utf-8") st.code(script_content, language='bash') # Provide button to run investigation if st.button("Investigate"): stdout, stderr = analyze_shell_script(os.path.join("uploads", file_name)) if stderr: st.error(f"Error during analysis: {stderr}") else: st.subheader("Investigation Results") st.text(stdout) # Provide button to fix errors if st.button("Fix Issues"): fixed_content = fix_shell_script_errors(script_content) st.subheader("Fixed Shell Script") st.code(fixed_content, language='bash') st.download_button("Download Fixed Script", data=fixed_content, file_name=f"fixed_{file_name}")