-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicfork.py
46 lines (35 loc) · 1.16 KB
/
magicfork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/python
# coding:utf-8
__author__ = 'Readm'
import __builtin__
import subprocess
import sys
from os import fork
def magicfork(terminal='gnome'):
_fork = fork()
if _fork != 0:
return _fork
else:
if terminal in ['gnome', 'gnome-terminal']:
bash_exec = "gnome-terminal -e \"bash -c 'mkfifo tmp; tty>tmp; exec sleep 99999999'\"; cat tmp; rm tmp"
elif terminal == 'xterm':
bash_exec = "xterm -e 'tty >&3; exec sleep 99999999' 3>&1"
else:
raise NameError('Unrecognized terminal name.')
_r_i = __builtin__.raw_input
_i = __builtin__.input
p = subprocess.Popen(bash_exec, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
tty_path = p.stdout.readline().strip()
tty = open(tty_path, 'r+')
sys.stdout = tty
sys.stderr = tty
sys.stdin = tty
def raw_input(prompt):
sys.stdout.write(prompt)
return _r_i('')
__builtin__.raw_input = raw_input
def input(prompt):
sys.stdout.write(prompt)
return _i('')
__builtin__.input = input
return 0