Skip to content

Commit

Permalink
remove all logic handling output redirection since its no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyaGomaa committed Sep 2, 2024
1 parent 1395003 commit d602fa9
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 61 deletions.
1 change: 0 additions & 1 deletion managers/process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def is_pbar_supported(self) -> bool:

def start_output_process(self, current_stdout, stderr, slips_logfile):
output_process = Output(
stdout=current_stdout,
stderr=stderr,
slips_logfile=slips_logfile,
verbose=self.main.args.verbose or 0,
Expand Down
4 changes: 1 addition & 3 deletions slips/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,8 @@ def start(self):
self.print_version()
print("https://stratosphereips.org")
print("-" * 27)

self.setup_print_levels()

self.stdout: str = self.checker.check_stdout_redirection()
self.stdout = ""
stderr: str = self.get_slips_error_file()
slips_logfile: str = self.get_slips_logfile()
# if stdout is redirected to a file,
Expand Down
34 changes: 0 additions & 34 deletions slips_files/core/helpers/checker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import subprocess
import sys

import psutil
Expand Down Expand Up @@ -201,37 +200,4 @@ def input_module_exists(self, module):
f"Stopping Slips."
)
return False

return True

def check_stdout_redirection(self) -> str:
"""
Determine if the stdout is redirected to a file
return the current_stdout
current_stdout will be '' if it's not redirected to a file
"""
print("@@@@@@@@@@@@@@@@ check_output_redirection is called!")
# lsof will provide a list of all open fds belonging to slips
# -a: Combines the conditions.
# -d 1,2: Filters by file descriptors 1 (stdout) and 2 (stderr).
command = f"lsof -p {self.main.pid} -a -d 1"
result = subprocess.run(command.split(), capture_output=True)
# Get command output
output = result.stdout.decode("utf-8")
# if stdout is being redirected we'll find '1w' in one of the lines
# 1 means stdout, w means write mode
# by default, stdout is not redirected
current_stdout = ""
for line in output.splitlines():
# /dev/pts means we're running in a terminal, if redirection is
# used, the files name will be there instead
# Command is the header line
if "/dev/pts/" not in line and "COMMAND" not in line:
# stdout is redirected, get the file
current_stdout: str = line.split(" ")[-1]
break
print(
f"@@@@@@@@@@@@@@@@ check_stdout_redirection: current_stdout"
f" {current_stdout}"
)
return current_stdout
23 changes: 0 additions & 23 deletions slips_files/core/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from multiprocessing.connection import Connection
from multiprocessing import Event
import sys
import io
from pathlib import Path
from datetime import datetime
import os
Expand Down Expand Up @@ -46,7 +45,6 @@ def __init__(
self,
verbose=1,
debug=0,
stdout="",
stderr="output/errors.log",
slips_logfile="output/slips.log",
input_type=False,
Expand Down Expand Up @@ -85,10 +83,6 @@ def __init__(
utils.change_logfiles_ownership(
self.slips_logfile, self.UID, self.GID
)
self.stdout = stdout
if stdout != "":
self.change_stdout()

if self.verbose > 2:
print(f"Verbosity: {self.verbose}. Debugging: {self.debug}")

Expand Down Expand Up @@ -149,23 +143,6 @@ def log_line(self, msg: dict):
slips_logfile.write(f"{date_time} [{sender}] {msg}\n")
self.slips_logfile_lock.release()

def change_stdout(self):
"""
to be able to print the stats to the output file
"""
# io.TextIOWrapper creates a file object of this file
# Pass 0 to open() to switch output buffering off
# (only allowed in binary mode)
# write_through= True, to flush the buffer to disk, from there the
# file can read it.
# without it, the file writer keeps the information in a local buffer
# that's not accessible to the file.
stdout = io.TextIOWrapper(
open(self.stdout, "wb", 0), write_through=True
)
sys.stdout = stdout
return stdout

def print(self, sender: str, txt: str, end="\n"):
"""
prints the given txt whether using tqdm or using print()
Expand Down

0 comments on commit d602fa9

Please sign in to comment.