import inspect import sys import subprocess import os from sys import platform import time as t import uuid win32 = platform == "win32" linux = platform == "linux" or platform == "linux2" macos = platform == "darwin" datafile = "" if win32: sysid = hex(uuid.getnode()) datafile += sysid datafile += "gendata.csv" # Python is running as Administrator (so netstat can get filename, to block, etc), # so we use this to see who is actually logged in # it's very hacky startupinfo = subprocess.STARTUPINFO() #if not getattr(sys, "frozen", False): startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # hide powershell window res = subprocess.check_output(["WMIC", "ComputerSystem", "GET", "UserName"], universal_newlines=True, startupinfo=startupinfo) _, username = res.strip().rsplit("\n", 1) userid, sysdom = username.rsplit("\\", 1) def time(): return int(t.time()) def fprint(msg): #if not getattr(sys, "frozen", False): setup_child() try: frm = inspect.stack()[1] mod = inspect.getmodule(frm[0]) print('[' + mod.__name__ + ":" + frm.function + ']:', msg) except Exception as e: try: print('[????:' + frm.function + ']:', msg) except: print('[????]:', msg) # else: #print(msg) def find_data_file(filename): if getattr(sys, "frozen", False): # The application is frozen datadir = os.path.dirname(sys.executable) else: # The application is not frozen # Change this bit to match where you store your data files: datadir = os.path.dirname(__file__) return os.path.join(datadir, filename) def run_ps(cmd): fprint("init PS") if win32: startupinfo = subprocess.STARTUPINFO() #print("DICKS") #if not getattr(sys, "frozen", False): # print("test") # #completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True, startupinfo=startupinfo) #else: # print("alt") startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # , "-WindowStyle", "hidden" fprint("running PS command: " + cmd) completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True, startupinfo=startupinfo) fprint("ran PS command sucessfully") #completed = subprocess.run(["powershell", "-WindowStyle", "hidden", "-Command", cmd], capture_output=True, startupinfo=startupinfo) return completed def setup_child(): if not getattr(sys, "frozen", False): sys.stdout = Logger(filename=find_data_file("output.log")) sys.stderr = Logger(filename=find_data_file("output.log")) class Logger(object): def __init__(self, filename="output.log"): self.log = open(filename, "a") self.terminal = sys.stdout def write(self, message): self.log.write(message) #close(filename) #self.log = open(filename, "a") try: self.terminal.write(message) except: sleep(0) def flush(self): print("")