Skip to content

Instantly share code, notes, and snippets.

@ymasory
Created April 21, 2011 02:03
Show Gist options
  • Save ymasory/933531 to your computer and use it in GitHub Desktop.
Save ymasory/933531 to your computer and use it in GitHub Desktop.
my gnome customization script
#!/usr/bin/python
# GNOME customization script.
#
# Tested on Ubuntu 10.10, Debian Squeeze, Debian Sid (circa April 2011).
# Run as root.
#
# @author Yuvi Masory
import getpass
import os
import os.path
import socket
import subprocess
import sys
class SysInfo:
def __init__(self):
#On which user's behalf is this script running?
self.uname = 'yuvi'
#What's that user's home directory?
self.home = '/home/yuvi'
#Does this computer have a media keyboard (i.e. with volume buttons)
self.xf86comp = True
#Is this computer a laptop?
self.laptop = True
#What's this computer's name?
self.hostname = socket.gethostname()
#Which distro?
ubuntu_release_file = '/etc/lsb-release'
ubuntu_id = 'DISTRIB_ID=Ubuntu'
if os.path.exists(ubuntu_release_file):
self.ubuntu = ubuntu_id in open(ubuntu_release_file).read()
else:
self.ubuntu = False
self.debian = not self.ubuntu
#Debian: stable or sid?
if self.debian:
if self.hostname in []: #add the names of your unstable comps
self.stable = True
else:
self.stable = False
self.unstable = not self.stable
class GConfEditor:
def __init__(self, info):
self.info = info
machineid = open('/var/lib/dbus/machine-id').read().strip()
dotdbus = self.info.home + '/.dbus/session-bus/' + machineid + '-0'
dotdbus_lines = [l.strip() for l in open(dotdbus).readlines()]
dbus_asgns = list(
filter(lambda l: l.startswith('DBUS_SESSION_BUS_ADDRESS='),
dotdbus_lines))
assert len(dbus_asgns) == 1
self.dbus_args = [dbus_asgns[0]]
def _gset(self, key, val, valType):
args = self.dbus_args + ['gconftool-2', '--set', key,
'--type', valType, val]
Util.ccall(args, user=self.info.uname)
def _gset_list(self, key, list_type, val):
args = self.dbus_args + ['gconftool-2', '--set', key, '--type', 'list',
'--list-type', list_type, val]
Util.ccall(args, user=self.info.uname)
def _gset_bool(self, key, cond):
if cond:
val = 'true'
else:
val = 'false'
self._gset(key, val, 'bool')
def _gset_string(self, key, val):
self._gset(key, val, 'string')
def _gset_int(self, key, val):
self._gset(key, str(val), 'int')
def set_screensaver_delay(self):
self._gset_int('/desktop/gnome/session/idle_delay', 30)
def set_keybindings(self):
xf86bindings = { 'mute' : 'XF86AudioMute',
'vup' : 'XF86AudioRaiseVolume',
'vdown' : 'XF86AudioLowerVolume' }
if self.info.xf86comp:
mute = xf86bindings['mute']
else:
mute = '<Mod4>BackSpace'
if self.info.xf86comp:
vdown = xf86bindings['vdown']
else:
vdown = '<Mod4>minus'
if self.info.xf86comp:
vup = xf86bindings['vup']
else:
vup = '<Mod4>equal'
self._gset_string('/apps/gnome_settings_daemon/keybindings/' +
'volume_mute',
mute)
self._gset_string('/apps/gnome_settings_daemon/keybindings/' +
'volume_down',
vdown)
self._gset_string('/apps/gnome_settings_daemon/keybindings/' +
'volume_up',
vup)
self._gset_string('/apps/gnome_settings_daemon/keybindings/www',
'<Mod4>period')
self._gset_string('/apps/gnome_settings_daemon/keybindings/' +
'screensaver',
'<Mod4>l')
self._gset_string('/apps/metacity/global_keybindings/' +
'switch_to_workspace_left',
'<Mod4>Left')
self._gset_string('/apps/metacity/global_keybindings/' +
'switch_to_workspace_right',
'<Mod4>Right')
self._gset_string('/apps/metacity/global_keybindings/' +
'run_command_terminal',
'<Mod4>9')
def terminal_tweaks(self):
self._gset_bool('/apps/gnome-terminal/global/use_menu_accelerators',
False)
self._gset_bool('/apps/gnome-terminal/global/use_mnemonics',
False)
self._gset_bool('/apps/gnome-terminal/global/confirm_window_close',
False)
self._gset_bool('/apps/gnome-terminal/profiles/Default/silent_bell',
True)
self._gset_string('/apps/gnome-terminal/keybindings/set_window_title',
'<Shift><Control>t')
self._gset_string('/apps/gnome-terminal/keybindings/new_tab',
'disabled')
self._gset_string('/apps/gnome-terminal/profiles/Default/font',
'Monospace 10')
self._gset_bool(
'/apps/gnome-terminal/profiles/Default/use_system_font', False)
def nautilus_tweaks(self):
self._gset_string('/apps/nautilus/preferences/default_folder_viewer',
'list_view')
self._gset_bool('/apps/nautilus/desktop/volumes_visible', True)
self._gset_bool('/apps/nautilus/desktop/trash_icon_visible', True)
self._gset_bool('/apps/nautilus/desktop/network_icon_visible', True)
self._gset_bool('/apps/nautilus/desktop/home_icon_visible', False)
self._gset_bool('/apps/nautilus/desktop/computer_icon_visible', False)
self._gset_bool('/apps/nautilus/preferences/always_use_location_entry',
True)
def disable_sounds(self):
self._gset_bool('/desktop/gnome/sound/event_sounds', False)
def window_tweaks(self):
self._gset_string(
'/apps/metacity/general/action_middle_click_titlebar',
'toggle_maximize_vertically')
self._gset_string(
'/apps/metacity/general/action_right_click_titlebar',
'menu')
compiz_plugs = (
'[core,ccp,move,resize,place,decoration,dbus,' +
'mousepoll,session,text,workarounds,neg,vpswitch,' +
'gnomecompat,svg,imgjpeg,png,wall,commands,' +
'regex,resizeinfo,snap,animation,fade,expo,' +
'ezoom,scale,switcher,scaleaddon]')
self._gset_list(
'/apps/compiz/general/allscreens/options/active_plugins',
'string',
compiz_plugs)
def panel_tweaks(self):
self._gset_bool('/apps/panel/applets/clock_screen0/prefs/show_seconds',
True)
def keyboard_options(self):
self._gset_list('/desktop/gnome/peripherals/keyboard/kbd/options',
'string',
('[' +
'ctrl' + '\t' +
'ctrl:nocaps,' +
'terminate' + '\t' + 'terminate:ctrl_alt_bksp' +
']'))
def make_chromium_default(self):
if self.info.ubuntu or (self.info.debian and self.info.stable):
chromium = 'chromium-browser'
else:
chromium = 'chromium'
self._gset_string('/desktop/gnome/applications/browser/exec',
'/usr/bin/' + chromium)
self._gset_string('/desktop/gnome/applications/browser/exec',
'/usr/bin/' + chromium)
self._gset_string('/desktop/gnome/url-handlers/about/command',
'/usr/bin/' + chromium + ' %s')
self._gset_string('/desktop/gnome/url-handlers/http/command',
'/usr/bin/' + chromium + ' %s')
self._gset_string('/desktop/gnome/url-handlers/https/command',
'/usr/bin/' + chromium + ' %s')
self._gset_string('/desktop/gnome/url-handlers/unknown/command',
'/usr/bin/' + chromium + ' %s')
self._gset_string('/desktop/gnome/url-handlers/chrome/command',
'/usr/bin/' + chromium + ' %s')
Util.ccall(['update-alternatives',
'--quiet',
'--set',
'x-www-browser',
'/usr/bin/' + chromium])
def set_power_prefs(self):
self._gset_int('/apps/gnome-power-manager/timeout/sleep_display_ac',
1800)
if self.info.laptop:
lidsetting = 'blank'
else:
lidsetting = 'suspend'
self._gset_string('/apps/gnome-power-manager/buttons/lid_battery',
lidsetting)
self._gset_string('/apps/gnome-power-manager/buttons/lid_ac',
'blank')
class Util:
@staticmethod
def ccall(args, cwd=None, careful=True, user=None):
'''
"Careful" blocking process call.
Takes a string like 'ls -l'.
Throws exception on non-zero return code, unless careful=False.
cwd is directory from which command is run, defaulting to current
working directory.
'''
if user != None:
args = ['sudo', '-u', user] + args
p = subprocess.Popen(args, close_fds=True, cwd=cwd)
p.wait()
ret = p.returncode
if (careful and ret != 0):
raise Exception('non-zero return from: ' + str(args))
else:
return ret
@staticmethod
def scall(args, cwd=None, user=None):
'''
"Silent" blocking process call.
Takes an list of strings like ['ls', '-l'] and returns the 3-tuple
(stdout, stderr, returncode).
'''
if user != None:
args = ['sudo', '-u', user] + args
p = subprocess.Popen(args, stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
close_fds=True, shell=False, cwd=cwd)
o, e = p.communicate()
return o, e, p.returncode
if __name__ == '__main__':
if getpass.getuser() != 'root':
sys.stderr.write('please run as root\n')
sys.exit(1)
info = SysInfo()
gconf = GConfEditor(info)
gconf.set_screensaver_delay()
gconf.set_keybindings()
gconf.terminal_tweaks()
gconf.nautilus_tweaks()
gconf.disable_sounds()
gconf.window_tweaks()
gconf.panel_tweaks()
gconf.keyboard_options()
gconf.make_chromium_default()
gconf.set_power_prefs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment