summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2012-08-15 01:12:14 +0200
committerhut <hut@lavabit.com>2012-08-15 01:18:04 +0200
commit98cc0a97832e1bd6a896ebdaeb9162d109fac4b9 (patch)
treed54fe6a622487de33b2e1866260492c5179f21ac
parent5622e8a811951fca5e48685a1495371920e50bd4 (diff)
downloadranger-98cc0a97832e1bd6a896ebdaeb9162d109fac4b9.tar.gz
ext.popen_forked: separated Popen_forked to a library
-rw-r--r--ranger/ext/popen_forked.py26
-rwxr-xr-xranger/ext/rifle.py35
2 files changed, 47 insertions, 14 deletions
diff --git a/ranger/ext/popen_forked.py b/ranger/ext/popen_forked.py
new file mode 100644
index 00000000..0dd52252
--- /dev/null
+++ b/ranger/ext/popen_forked.py
@@ -0,0 +1,26 @@
+# Copyright (C) 2012  Roman Zimbelmann <romanz@lavabit.com>
+# This software is distributed under the terms of the GNU GPL version 3.
+
+import os
+import subprocess
+
+def Popen_forked(*args, **kwargs):
+	"""
+	Forks process and runs Popen with the given args and kwargs.
+
+	If os.fork() is not supported, runs Popen without forking and returns the
+	process object returned by Popen.
+	Otherwise, returns None.
+	"""
+	try:
+		pid = os.fork()
+	except:
+		# fall back to not forking if os.fork() is not supported
+		return subprocess.Popen(*args, **kwargs)
+	else:
+		if pid == 0:
+			os.setsid()
+			kwargs['stdin'] = open(os.devnull, 'r')
+			kwargs['stdout'] = kwargs['stderr'] = open(os.devnull, 'w')
+			subprocess.Popen(*args, **kwargs)
+			os._exit(0)
diff --git a/ranger/ext/rifle.py b/ranger/ext/rifle.py
index d5c40a0e..9b5fe2c9 100755
--- a/ranger/ext/rifle.py
+++ b/ranger/ext/rifle.py
@@ -65,6 +65,25 @@ except ImportError:
 		return _cached_executables
 
 
+try:
+	from ranger.ext.popen_forked import Popen_forked
+except ImportError:
+	def Popen_forked(*args, **kwargs):
+		"""Forks process and runs Popen with the given args and kwargs."""
+		try:
+			pid = os.fork()
+		except:
+			# fall back to not forking if os.fork() is not supported
+			return Popen(*args, **kwargs)
+		else:
+			if pid == 0:
+				os.setsid()
+				kwargs['stdin'] = open(os.devnull, 'r')
+				kwargs['stdout'] = kwargs['stderr'] = open(os.devnull, 'w')
+				Popen(*args, **kwargs)
+				os._exit(0)
+
+
 def _is_terminal():
 	# Check if stdin (file descriptor 0), stdout (fd 1) and
 	# stderr (fd 2) are connected to a terminal
@@ -313,20 +332,8 @@ class Rifle(object):
 						os.environ['TERMCMD'] = term
 					cmd = [os.environ['TERMCMD'], '-e'] + cmd
 				if 'f' in flags or 't' in flags:
-					devnull_r = open(os.devnull, 'r')
-					devnull_w = open(os.devnull, 'w')
-					try:
-						pid = os.fork()
-					except:
-						# fall back to not detaching if fork() is not supported
-						p = Popen(cmd, env=self.hook_environment(os.environ))
-						p.wait()
-					else:
-						if pid == 0:
-							os.setsid()
-							p = Popen(cmd, env=self.hook_environment(os.environ),
-								stdin=devnull_r, stdout=devnull_w, stderr=devnull_w)
-							os._exit(0)
+					from ranger.ext.run_forked import Popen_forked
+					Popen_forked(cmd, env=self.hook_environment(os.environ))
 				else:
 					p = Popen(cmd, env=self.hook_environment(os.environ))
 					p.wait()
-09 02:17:11 -0400 test config init using sync.Once' href='/gbmor/getwtxt/commit/svc/init_test.go?h=v0.4.15&id=5e92b610a5f8832e6a389b1acb01fd0cc866780e'>5e92b61 ^
c050730 ^
e10fc64 ^
6753171 ^
c050730 ^

e10fc64 ^

7f76158 ^
e10fc64 ^
5e92b61 ^
a6c8162 ^

e10fc64 ^





a6c8162 ^





979e1f9 ^
a6c8162 ^
6753171 ^
6753171 ^
6753171 ^

bc6811a ^
6753171 ^
bc6811a ^



6753171 ^
6753171 ^


bc6811a ^

6753171 ^






7f76158 ^
6753171 ^

e10fc64 ^
6753171 ^
6753171 ^
6753171 ^
6753171 ^
6753171 ^
45d6723 ^
6753171 ^




6753171 ^
2fd6b1a ^
bc6811a ^

2fd6b1a ^
e539d23 ^
2fd6b1a ^



bc6811a ^














f56b111 ^












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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126