-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_xauth.py
40 lines (29 loc) · 1.06 KB
/
create_xauth.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
# based on https://github.com/luiscarlosgph/dockerx/tree/main
import os
import tempfile
import subprocess
import shlex
import pathlib
def run_shell(cmd):
cmd_list = shlex.split(cmd, posix=False)
proc = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = proc.communicate()
return out
def main():
# Create an empty Xauthority file if it does not exist
xauth_path = os.path.join(tempfile.gettempdir(), '.docker.xauth')
if os.path.isdir(xauth_path):
pathlib.Path(xauth_path).rmdir()
if not os.path.isfile(xauth_path):
pathlib.Path(xauth_path).touch()
# Hack X11 cookie
out = run_shell('xauth nlist ' + os.environ['DISPLAY'])
cookie = 'ffff' + out.decode('ascii')[4:]
# Save X11 cookie in a temporary file
cookie_path = os.path.join(tempfile.gettempdir(), '.myx11cookie')
with open(cookie_path, 'w') as f:
f.write(cookie)
# Merge cookie in Xauthority file
run_shell('xauth -f ' + xauth_path + ' nmerge ' + cookie_path)
if __name__ == "__main__":
main()