Contents

  1. gin.py

gin.py

#!/usr/bin/env python
#
# GIN - GNOME Instant Nationalization
# Written by Paul Flint <flint@flint.com>,
#            Nick Wheeler <dragoncow2@ubuntu.com>,
#            Kevin Cole <kjcole@ubuntu.com>
#
# Last modified by Kevin Cole <kjcole@ubuntu.com> 2006.05.13
#
# 2006.04.22 KJC - Converted to Python.
# 2006.04.22 KJC - Added choice of language.
# 2006.04.26 KJC - Added caution in changing locales for certain users.
# 2006.04.27 KJC - Opted for triple quotes to eliminate backslash quotes.
# 2006.04.27 KJC - Moved development notes to external TODO file.
# 2006.05.13 KJC - Modified xsession environment variables.

import sys                              # Provides command line arguments
import shutil                           # Provides cp, mv, rm...
import os                               # Provides chmod and chown
import getpass                          # Provides current username
import pwd                              # Provided NUMERIC uid, gid

# Supported locales/languages
locales   = {"spanish":("es","es_SV"),
             "english":("en","en_US")}
languages = locales.keys()
languages.sort()

dmrc  = """[Desktop]\n"""
dmrc += """Session=default\n"""
dmrc += """Language=%s.UTF-8\n"""

xsession  = """LANGUAGE="%s:%s"\n"""
xsession += """LANG="%s.UTF-8"\n""" 
xsession += """gnome-session\n"""

errors = {}
errors[0]  = """%s arguments\n"""
errors[0] += """Usage: gin <username> {"english" | "spanish"}\n"""
errors[1]  = """%s arguments\n"""
errors[1] += """Usage: gin {"english" | "spanish"}\n"""
errors[2]  = """No such user: %s\n"""
errors[3]  = """%s is a non-standard user...\n"""
errors[4]  = """Language must be one of:\n"""
for language in languages:
  errors[4] += "  %s\n" % (language)

if os.geteuid() == 0:                   # We are Superuser. 
  if len(sys.argv) < 3:                 # Two args, please...
    print errors[0] % ("Not enough")    # ...Bartender, another round!
    sys.exit()                          # ...B'bye
  elif len(sys.argv) > 3:               # Two args, please...
    print errors[0] % ("Too many")      # ...God, I'm wasted!
    sys.exit()                          # ...B'bye
  LUSER = sys.argv[1]                   # User to be set
  LANG  = sys.argv[2]                   # Language to be set

else:                                   # Nope, we're just Clark Kent.
  if len(sys.argv) < 2:                 # One arg, please....
    print errors[1] % ("Not enough")    # ...Bartender, another round!
    sys.exit()                          # ...B'bye
  elif len(sys.argv) > 2:               # One arg, please....
    print errors[1] % ("Too many")      # ...God, I'm wasted!
    sys.exit()                          # ...B'bye
  LUSER = getpass.getuser()             # User to be set is meself.
  LANG  = sys.argv[1]                   # Language to be set

try:
  userinfo = pwd.getpwnam(LUSER)        # Get user info from /etc/passwd
except KeyError:                        # If the user doesn't exist...
  print errors[2] % (LUSER)             # ...You're seeing pink elephants
  sys.exit()                            # ...B'bye
uid  = userinfo[2]                      # Extract the user's numeric user  ID
gid  = userinfo[3]                      # Extract the user's numeric group ID
home = userinfo[5]                      # Extract the user's home directory
if uid < 100 or home[0:5] != "/home":   # If user is "strange and special"...
  print errors[3]                       # ...Don't drink and drive
  sys.exit()                            # ...B'bye

if LANG not in languages:               # If we do not speak this language...
  print errors[4]                       # ...Your speech is getting slurry
  sys.exit()                            # ...B'bye

print "Switching %s to %s!" % (LUSER, LANG)

# display manager resource file
#
path = "%s/.dmrc" % (home)              # Destination filename
tmp = open("/tmp/new.dmrc","w")         # Write a /tmp file to be safe
tmp.write(dmrc % (locales[LANG][1]))
tmp.close()
shutil.move("/tmp/new.dmrc",path)       # Move the the temp file to reality
os.chown(path,uid,gid)                  # Make sure the user owns it.

# xsession script
#
path = "%s/.xsession" % (home)          # Destination filename
tmp = open("/tmp/new.xsession","w")     # Write a /tmp file to be safe
tmp.write(xsession % (locales[LANG][1],locales[LANG][0],locales[LANG][1]))
tmp.close()
shutil.move("/tmp/new.xsession",path)   # Move the the temp file to reality
os.chown(path,uid,gid)                  # Make sure the user owns it.
os.chmod(path,00755)                    # Make it executable.

print "Success!\n"                      # Success!

Generated by GNU enscript 1.6.4.