File: fjdisplay.py

package info (click to toggle)
firejail 0.9.78-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,336 kB
  • sloc: ansic: 47,244; exp: 10,586; sh: 1,499; makefile: 684; python: 505; awk: 32
file content (49 lines) | stat: -rwxr-xr-x 1,360 bytes parent folder | download
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
#!/usr/bin/env python3
# This file is part of Firejail project
# Copyright (C) 2014-2026 Firejail Authors
# License GPL v2

import re
import sys
import subprocess

usage = """fjdisplay.py name-of-firejail
returns the display in the form of ':NNN'
"""


def getfirejails():
    output = subprocess.check_output(['firemon', '--x11'])
    firejails = {}
    name = ''
    for line in output.split('\n'):
        namematch = re.search('--name=(\w+\S*)', line)
        if namematch:
            name = namematch.group(1)
        displaymatch = re.search('DISPLAY (:\d+)', line)
        if displaymatch:
            firejails[name] = displaymatch.group(1)
    return firejails


def getdisplay(name):
    firejails = getfirejails()
    fjlist = '\n'.join(firejails.keys())
    namere = re.compile('^' + name + '.*', re.MULTILINE)
    matchingjails = namere.findall(fjlist)
    if len(matchingjails) == 1:
        return firejails[matchingjails[0]]
    if len(matchingjails) == 0:
        raise NameError("firejail {} does not exist".format(name))
    else:
        raise NameError("ambiguous firejail name")


if __name__ == '__main__':
    if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) > 2:
        print(usage)
        exit()
    if len(sys.argv) == 1:
        print(getfirejails())
    if len(sys.argv) == 2:
        print(getdisplay(sys.argv[1]))