Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. # Electron Cash - lightweight Bitcoin client
  2. # Copyright (C) 2019 Axel Gembe <derago@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person
  5. # obtaining a copy of this software and associated documentation files
  6. # (the "Software"), to deal in the Software without restriction,
  7. # including without limitation the rights to use, copy, modify, merge,
  8. # publish, distribute, sublicense, and/or sell copies of the Software,
  9. # and to permit persons to whom the Software is furnished to do so,
  10. # subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23.  
  24. """
  25. what's up doc?
  26. """
  27.  
  28. import sys
  29. import os
  30. import ctypes
  31. import psutil
  32.  
  33. STD_OUTPUT_HANDLE = -11
  34. FILE_TYPE_DISK = 1
  35. MB_ICONERROR = 0x10
  36. MB_OK = 0
  37.  
  38. def parent_process_pids() -> int:
  39.     """
  40.    Returns all parent process PIDs, starting with the closest parent
  41.    """
  42.     try:
  43.         pid = os.getpid()
  44.         while pid > 0:
  45.             pid = psutil.Process(pid).ppid()
  46.             yield pid
  47.     except psutil.NoSuchProcess:
  48.         # Parent process not found, likely terminated, nothing we can do
  49.         pass
  50.  
  51. def create_or_attach_console(attach: bool = True, create: bool = False, title: str = None) -> bool:
  52.     """
  53.    First this checks if output is redirected to a file and does nothing if it is. Then it tries
  54.    to attach to the console of any parent process and if not successful it optionally creates a
  55.    console or fails.
  56.    If a console was found or created, it will redirect current output handles to this console.
  57.    """
  58.     std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
  59.     if std_out_handle > 0:
  60.         if ctypes.windll.kernel32.GetFileType(std_out_handle) == FILE_TYPE_DISK:
  61.             # Output is being redirected to a file, do nothing
  62.             return
  63.  
  64.     has_console = False
  65.  
  66.     if not has_console and attach:
  67.         # Try to attach to a parent console
  68.         for pid in parent_process_pids():
  69.             if ctypes.windll.kernel32.AttachConsole(pid):
  70.                 has_console = True
  71.                 break
  72.  
  73.     if not has_console and create:
  74.         # Try to allocate a new console
  75.         if ctypes.windll.kernel32.AllocConsole():
  76.             has_console = True
  77.  
  78.     if not has_console:
  79.         return False
  80.  
  81.     if title:
  82.         # Set the console title
  83.         ctypes.windll.kernel32.SetConsoleTitleW(title)
  84.  
  85.     conout = open('CONOUT$', 'w')
  86.     sys.stdout = conout
  87.     sys.stderr = conout
  88.     sys.stdin = open('CONIN$', 'r')
  89.  
  90.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement