forked from otommod/browser-mpris2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-chrome.py
62 lines (48 loc) · 1.89 KB
/
install-chrome.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import os
import sys
XDG_CONFIG_HOME = os.environ.get("XDG_CONFIG_HOME",
os.path.expanduser("~/.config"))
MESSAGE_HOSTS = os.path.join(XDG_CONFIG_HOME,
"chromium",
"NativeMessagingHosts")
def die(msg):
print(msg, file=sys.stderr)
sys.exit(1)
def main(args):
if len(args) < 1:
die("You must provide at least the extension ID")
elif len(args) < 2:
args.append(os.path.expanduser("~/bin/chrome-mpris2"))
ext_id = args[0]
prog_path = args[1]
# Chrome's extension IDs are in hexadecimal but using a-p, referred
# internally as "mpdecimal". https://stackoverflow.com/a/2050916
if not all(97 <= ord(c) <= 112 for c in ext_id):
die("Not valid extension ID")
# Check that python-gobject is available. This is done because it's hard
# to see if chrome-mpris2 fails with an import error; you'd need to check
# the X log. Of course it's possible that any dependencies met during
# installation are unavailable later but what can you do.
try:
from gi.repository import GLib, Gio
except ImportError:
die("Required dependency python-gobject not found")
# Before that, Gio couldn't publish DBus objects (introspection bug)
if (GLib.MAJOR_VERSION, GLib.MINOR_VERSION) < (2, 46):
die("Your GLib version is too old")
manifest = {
"name": "org.mpris.browser_host",
"description": "A DBus service",
"path": prog_path,
"type": "stdio",
"allowed_origins": [
"chrome-extension://" + ext_id + "/",
]
}
manifest_path = os.path.join(MESSAGE_HOSTS, "org.mpris.browser_host.json")
os.makedirs(MESSAGE_HOSTS, exist_ok=True)
with open(manifest_path, "w") as f:
json.dump(manifest, f)
if __name__ == "__main__":
main(sys.argv[1:])