Skip to content

Instantly share code, notes, and snippets.

@cscutcher
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscutcher/da6a0d694ab6f32dfa29 to your computer and use it in GitHub Desktop.
Save cscutcher/da6a0d694ab6f32dfa29 to your computer and use it in GitHub Desktop.
My setup for tmux. Its all terrible hacked. Need to make a python module.
#!/bin/bash
tmux -S /tmp/tmux-shared $*
#!/usr/bin/env python
"""
Share allow current session to be shared
"""
from home import tmux
if __name__ == "__main__":
server_socket = tmux.get_server_socket()
tmux.enable_screen_sharing_on_server(server_socket, "tmux-share")
#!/usr/bin/env python
"""
Disable write access to shared thinger
"""
from home import tmux
if __name__ == "__main__":
server_socket = tmux.get_server_socket()
tmux.disable_write_sharing_on_server(server_socket)
#!/usr/bin/env python
"""
Enable write access to shared thinger
"""
from home import tmux
if __name__ == "__main__":
server_socket = tmux.get_server_socket()
tmux.enable_write_sharing_on_server(server_socket)
#!/usr/bin/env python
"""
Deny current session to be shared
"""
from home import tmux
if __name__ == "__main__":
server_socket = tmux.get_server_socket()
tmux.disable_screen_sharing_on_server(server_socket)
# -*- coding: utf-8 -*-
"""
Utility functions for tmux
"""
import grp
import logging
import os
import os.path
import stat
DEV_LOGGER = logging.getLogger(__name__)
def get_server_socket():
"""
Get server socket from current environment
"""
tmux_env = os.environ["TMUX"]
tmux_path, _, _ = tmux_env.split(",")
return tmux_path
def enable_screen_sharing_on_server(socket_path, share_group_name):
"""
Enable sharing on server by socket_path
"""
group = grp.getgrnam(share_group_name)
os.chown(socket_path, -1, group.gr_gid)
stat_result = os.stat(socket_path)
mode = stat_result.st_mode
new_mode = mode | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
os.chmod(socket_path, new_mode)
def disable_screen_sharing_on_server(socket_path):
"""
Disable sharing on server by socket path
"""
stat_result = os.stat(socket_path)
mode = stat_result.st_mode
new_mode = mode & ~(stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP)
os.chmod(socket_path, new_mode)
disable_write_sharing_on_server(socket_path)
def enable_write_sharing_on_server(socket_path):
"""
Enable write access to sharing
"""
file(socket_path + "-rw", 'w').write(" ")
def disable_write_sharing_on_server(socket_path):
"""
Disable write sharing
"""
path = socket_path + "-rw"
if os.path.isfile(path):
os.unlink(path)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Attach to shared tmux session
"""
import logging
import os
import sys
import tmuxp
import tmuxp.formats
import tmuxp.exc
DEV_LOGGER = logging.getLogger(__name__)
SOCKET_PATH = "/tmp/tmux-shared"
SOCKET_RW_PATH = SOCKET_PATH + "-rw"
def session_info_string(session):
"""
Turn tmuxp Session to nice string
"""
return ", ".join(
["%s=%s" % (key.replace("session_", ""), value) for key, value in session.iteritems()])
def rogue_attach(server, session_id):
"""
Create rogue sessoin
"""
sformats = tmuxp.formats.SESSION_FORMATS
tmux_formats = ['#{%s}' % f for f in sformats]
env = os.environ.get('TMUX')
if env:
del os.environ['TMUX']
tmux_args = (
'-d',
'-t', session_id,
'-P', '-F%s' % '\t'.join(tmux_formats),
)
proc = server.tmux(
'new-session',
*tmux_args
)
if proc.stderr:
raise tmuxp.exc.TmuxpException(proc.stderr)
session = proc.stdout[0]
if env:
os.environ['TMUX'] = env
# combine format keys with values returned from ``tmux list-windows``
session = dict(zip(sformats, session.split('\t')))
# clear up empty dict
session = dict((k, v) for k, v in session.items() if v)
session = tmuxp.Session(server=server, **session)
return session
def is_readwrite():
"""
Is read write allowed
"""
return os.path.isfile(SOCKET_RW_PATH) and (
os.stat(SOCKET_RW_PATH).st_uid == os.stat(SOCKET_PATH).st_uid)
def read_only_attach(server, session):
"""
Attach tosesison read only
"""
proc = server.tmux("attach-session", "-t", session["session_id"], "-r")
if proc.stderr:
raise tmuxp.exc.TmuxpException(proc.stderr)
def main():
try:
server = tmuxp.Server(socket_path=SOCKET_PATH)
sessions = server.list_sessions()
except:
print "Sharing disabled!"
sys.exit(2)
if len(sessions) < 1:
print "NO SESSIONS!"
sys.exit(1)
elif len(sessions) == 1:
session_index = 0
else:
print "Available sessions:"
for index, session in enumerate(sessions):
print "%d) %s (%s)" % (index, session["session_name"], session_info_string(session))
session_index = int(raw_input("Select session [0-%d]: " % (len(sessions) - 1,)))
read_write = is_readwrite()
if read_write:
attach_func = lambda _server, session: session.attach_session()
rogue_user_input = raw_input("Use independent control of windows? [y or n] (default n) ")
if len(rogue_user_input.strip()) == 0:
rogue_user = False
else:
rogue_user = rogue_user_input.strip().startswith("y")
else:
attach_func = read_only_attach
rogue_user = False
if rogue_user:
new_session = rogue_attach(server, sessions[session_index]["session_id"])
attach_func(server, new_session)
new_session.kill_session()
else:
attach_func(server, sessions[session_index])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment