~ubuntu-branches/ubuntu/oneiric/gconf/oneiric

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
134
135
136
#! /usr/bin/python
#
# copyright (c) 2006 Josselin Mouette <joss@debian.org>
# Licensed under the GNU Lesser General Public License, version 2.1
# See COPYING for details

from optparse import OptionParser
import sys,os,os.path,shutil,tempfile,subprocess,re

def get_valid_languages():
    '''Return set of valid languages.'''

    langs = set()
    cmd = subprocess.Popen(['locale', '-a'], stdout=subprocess.PIPE)
    for l in cmd.communicate()[0].split():
        if l == 'POSIX':
            continue
        langs.add(l.split('_')[0])

    return langs

def trim(filename, valid_languages):
    '''Remove redundancy from given %gconf-tree.xml, for faster loading'''

    infile = open(filename)
    outfile = open(filename + '.new', 'w')
    local_schema_re = re.compile('<local_schema locale="([a-zA-Z0-9@._-]+)">')
    try:
        in_bad_locale_block = False
        for l in infile:
            if in_bad_locale_block:
                if '</local_schema>' in l:
                    in_bad_locale_block = False
                continue

            m = local_schema_re.search(l)
            if m:
                if m.group(1) not in valid_languages:
                    in_bad_locale_block = True
                    continue

            l = l.replace(' short_desc=""', '')
            l = l.lstrip('\t')

            outfile.write(l)

        os.rename(filename + '.new', filename)
    except:
        os.unlink(filename + '.new')
        raise

parser = OptionParser(usage="usage: %prog --[un]register file1.schemas [file2.schemas [...]]")

parser.add_option("--register", action="store_true", dest="register",
                  help="register schemas to the GConf database",
                  default=None)
parser.add_option("--unregister", action="store_false", dest="register",
                  help="unregister schemas from the GConf database",
                  default=None)
parser.add_option("--register-all", action="store_true", dest="register_all",
                   help="clean up the GConf database and register all schemas again",
                   default=False)
parser.add_option("--no-signal", action="store_false", default=True, dest="signal",
                  help="do not send SIGHUP the running gconfd-2 processes")
(options, args) = parser.parse_args()

if options.register==None and not options.register_all:
  parser.error("You need to specify --register or --unregister.")

if 'DPKG_RUNNING_VERSION' in os.environ and not options.register_all:
    # This is what happens when we are called in an obsolete postinst/prerm script
    # Do nothing, it will be done in the trigger
    sys.exit(0)

schema_location="/usr/share/gconf/schemas"
defaults_dest="/var/lib/gconf/defaults"

schemas = [ ]
if options.register_all:
  for f in os.listdir(schema_location):
    if f.endswith(".schemas"):
      schemas.append(os.path.join(schema_location,f))
else:
  for schema in args:
    if not os.path.isabs(schema):
      schema=os.path.join(schema_location,schema)
    if os.path.isfile(schema):
      schemas.append(schema)
    else:
      sys.stderr.write('Warning: %s could not be found.\n'%schema)

if os.geteuid():
  parser.error("You must be root to launch this program.")

if options.register_all:
  options.register=True
  for f in os.listdir(defaults_dest):
    os.remove(os.path.join(defaults_dest,f))
  open(os.path.join(defaults_dest,"%gconf-tree.xml"),"w").close()

if schemas:
    tmp_home=tempfile.mkdtemp(prefix='gconf-')
    env={'HOME': tmp_home,
         'GCONF_CONFIG_SOURCE': 'xml:readwrite:'+defaults_dest}
    if options.register:
      arg='--makefile-install-rule'
    else:
      arg='--makefile-uninstall-rule'

    fd = os.open("/dev/null",os.O_WRONLY)
    save_stdout=os.dup(1)
    os.dup2(fd,1)
    os.close(fd)
    res=os.spawnvpe(os.P_WAIT,'gconftool-2',['gconftool-2',arg]+schemas,env)
    os.dup2(save_stdout,1)
    os.close(save_stdout)

    shutil.rmtree(tmp_home)

    trim(os.path.join(defaults_dest,"%gconf-tree.xml"), get_valid_languages())

    if(res):
      sys.exit(res)

if options.register and options.signal:
  # tell running processes to re-read the GConf database
  import signal
  try:
    pids=os.popen('pidof gconfd-2').readlines()[0].split()
    for pid in pids:
      try:
        os.kill(int(pid),signal.SIGHUP)
      except OSError:
        pass
  except IndexError:
    pass