1 """The su (switch user) module allows you to execute commands as root.
5 proxy_maker = SuProxyMaker('I need root access to change /etc/fstab')
6 yield proxy_maker.blocker
7 root = proxy_maker.get_root()
9 call = root.open('/etc/fstab')
22 from rox
import g
, _
, master_proxy
, tasks
26 _my_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
27 _child_script
= os
.path
.join(_my_dir
, 'suchild.sh')
29 class SuProxyMaker(tasks
.Blocker
):
32 def __init__(self
, message
):
33 """Displays a box prompting the user for a password.
34 Creates a new master_proxy.MasterObject and starts the child
35 process. The user is prompted for the root
43 to_child
.writeable
.close()
44 from_child
.readable
.close()
52 from_child
.writeable
.close()
53 to_child
.readable
.close()
55 self
._root
= master_proxy
.MasterProxy(to_child
.writeable
,
56 from_child
.readable
).root
58 self
.blocker
= self
._root
.getuid()
61 """Raises UserAbort if the user cancels."""
63 uid
= self
.blocker
.result
64 except master_proxy
.LostConnection
:
65 raise rox
.UserAbort("Failed to become root (cancelled "
66 "at user's request?)")
71 def _exec_child(message
, to_parent
, from_parent
):
72 fcntl
.fcntl(to_parent
, fcntl
.F_SETFD
, 0)
73 fcntl
.fcntl(from_parent
, fcntl
.F_SETFD
, 0)
74 os
.execlp('xterm', 'xterm',
76 '-title', 'Enter password',
81 str(to_parent
.fileno()),
82 str(from_parent
.fileno()))
85 """Contains Python file objects for two pipe ends.
86 Wrapping the FDs in this way ensures that they will
94 self
.readable
= os
.fdopen(r
, 'r')
99 self
.writeable
= os
.fdopen(w
, 'w')