/
cadmium
executable file
·133 lines (104 loc) · 4.48 KB
/
cadmium
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import glob
import json
import argparse
import distutils.spawn
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
_CADMIUM_WRAPPERS = {
'Incognito':
{
'enabled': True,
'params': ['--incognito'],
'wrapper': [],
},
'SELinux sandbox':
{
'enabled': os.path.exists('/sys/fs/selinux') and distutils.spawn.find_executable('sandbox'),
'params': ['--incognito', '--no-sandbox', '--disable-infobars', '--no-first-run'],
'wrapper': ['sandbox', '-X', '-t', 'sandbox_web_t'],
},
}
class CadmiumWindow(Gtk.Window):
def __init__(self, profiles, args):
Gtk.Window.__init__(self, title="Cadmium")
self.profiles = profiles
self.url = args.url
self.chrome_binary = args.chrome_binary
self.set_border_width(10)
vbox = Gtk.VBox(spacing=6)
self.add(vbox)
for profile in self.profiles:
button = Gtk.Button.new_with_label(profile)
button.connect("clicked", self.click_profile_button)
vbox.pack_start(button, True, True, 0)
for wrapper in _CADMIUM_WRAPPERS:
if _CADMIUM_WRAPPERS[wrapper]['enabled']:
button = Gtk.Button.new_with_label(wrapper)
button.connect("clicked", self.click_wrapper_button)
vbox.pack_start(button, True, True, 0)
def click_profile_button(self, button):
button_label = button.get_label()
self.run_chrome(button_label, ["--profile-directory={0}".format(self.profiles[button_label])])
def click_wrapper_button(self, button):
button_label = button.get_label()
params = _CADMIUM_WRAPPERS[button_label]['params']
wrapper = _CADMIUM_WRAPPERS[button_label]['wrapper']
self.run_chrome(button_label, params, wrapper)
def run_chrome(self, profile_name, params=[], wrapper=[]):
if self.url:
params.append(self.url)
cmd = wrapper + [self.chrome_binary] + params
print("Starting Chrome with '{0}' profile: {1}".format(profile_name, cmd))
Gio.Subprocess.new(cmd, Gio.SubprocessFlags.NONE)
Gtk.main_quit()
def get_xdg_config_home():
return os.environ.get('XDG_CONFIG_HOME', '~/.config/')
def find_chrome():
for chrome in ['google-chrome', 'chromium']:
chrome_executable = distutils.spawn.find_executable(chrome)
if chrome_executable:
chrome_config_path = os.path.expanduser(os.path.join(get_xdg_config_home(), chrome))
if not os.path.exists(chrome_config_path):
chrome_config_path = None
return (chrome_executable, chrome_config_path)
return (None, None)
def find_chrome_profiles(chrome_config_path):
profiles = {}
if not chrome_config_path:
return profiles
for profile_preferences in glob.glob(os.path.join(chrome_config_path, '*', 'Preferences')):
(profile_path, _) = os.path.split(profile_preferences)
(_, profile_directory) = os.path.split(profile_path)
if profile_directory == 'System Profile':
continue
with open(profile_preferences, 'r') as preferences_file:
preferences = json.load(preferences_file)
profiles[preferences['profile']['name']] = profile_directory
return profiles
def get_cadmium_parser():
(chrome_executable, chrome_config_path) = find_chrome()
parser = argparse.ArgumentParser()
parser.add_argument('url', nargs='?', help='The URL to open in Chrome')
parser.add_argument('--chrome-binary', help='Which Chrome binary to use (default: %(default)s)', default=chrome_executable)
parser.add_argument('--chrome-config-path', help='Which Chrome config path to use (default: %(default)s)', default=chrome_config_path)
return parser
def cadmium():
parser = get_cadmium_parser()
args = parser.parse_args()
if not args.chrome_binary:
print('Could not find any Chrome or Chromium binary and you did not pass one via the command line. Exiting')
sys.exit(127)
if not args.chrome_config_path:
print('Could not find any Chrome or Chromium configs and you did not pass one via the command line.')
profiles = find_chrome_profiles(args.chrome_config_path)
cwin = CadmiumWindow(profiles, args)
cwin.connect("delete-event", Gtk.main_quit)
cwin.show_all()
Gtk.main()
if __name__ == '__main__':
cadmium()