File: update-configlet-frontends

package info (click to toggle)
configlet 1.8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,044 kB
  • ctags: 211
  • sloc: python: 1,417; xml: 709; makefile: 139; sh: 44
file content (314 lines) | stat: -rwxr-xr-x 9,489 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/python

# update-configlets --Script for updating configlet information

# $Progeny: update-configlet-frontends,v 1.38 2002/02/12 16:22:03 jlicquia Exp $

# Copyright (C) 2001  Progeny Linux Systems, Inc.
# AUTHORS: Jeff Licquia <jlicquia@progeny.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,  as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# TODO: A third mode, similar to Jeff's original mode, when the
# configlet package is first installed there may already be configlets
# on the system.  So update-configlets will need a mode to scan for
# all existing configlets and do the setup work.  It also needs to
# tear down that stuff upon removal.

import os
import string
import sys
import getopt
import configlet

desktop_template = [ "[Desktop Entry]",
                     "Name=<configlet-title>",
                     "Comment=<configlet-desc>",
                     "Exec=<configlet-dirname>-configlet-capplet",
                     "Terminal=0",
                     "Type=Application" ]

desktop_directories = ["/usr/share/control-center/Debian",
                       "/usr/share/gnome/apps/Settings/Debian"]

base_config_capplet = "/usr/bin/configlet-capplet"
default_configlet_path = "/usr/share/configlets"

do_install = 0
do_remove = 0
do_all = 0
configlet_name = ""

configlet_list_filename = "/var/cache/configlet-frontends/configlet-capplet.list"
configlet_list = []

def debug(msg):
    if os.environ.has_key("DEBUG"):
        sys.stderr.write("update-configlet-frontends: %s\n" % (msg,))

def warn(msg):
    sys.stderr.write("%s\n" % (msg,))

def fatal(msg):
    warn(msg)
    warn("Terminating")
    sys.exit(-1)

def usage():
    sys.stderr.write("Usage: update-configlets " +
                     "{--install|--remove|--reinstall} <configlet>\n")
    sys.stderr.write("                         " +
                     "{--install-all|--remove-all|--update}\n")
    sys.stderr.write("Where <configlet> is the directory name under " +
                     "%s\n" % (default_configlet_path,))

def parse_opts():
    global do_install
    global do_remove
    global do_all
    global configlet_name

    try:
        opts, args = getopt.getopt(sys.argv[1:], "h",
                                   ['install=', 'remove=', 'reinstall=',
                                    'install-all', 'remove-all', 'update',
                                    'help'])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    sys.argv = [sys.argv[0]]

    if len(opts) > 1:
        usage()
        sys.exit(2)

    if len(opts) == 0:
        # Default to "--update".
        do_install = 1
        do_remove = 1
        do_all = 1
        return
    else:
        for o, a in opts:
            if o in ('-h', '--help'):
                usage()
                sys.exit(0)
            elif o in ('--install-all', '--remove-all', '--update'):
                if o == '--install-all':
                    do_install = 1
                    do_remove = 0
                elif o == '--remove-all':
                    do_install = 0
                    do_remove = 1
                else:
                    do_install = 1
                    do_remove = 1

                do_all = 1
                return

            configlet_name = a;
            do_all = 0
            if o in ('--install',):
                do_install = 1
                do_remove = 0
            elif o in ('--remove',):
                do_install = 0
                do_remove = 1
            elif o in ('--reinstall',):
                do_install = 1
                do_remove = 1
            else:
                usage()
                sys.exit(2)

def find_old_configlet_entries():
    global configlet_list

    sysdir = None
    for dir in desktop_directories:
        if os.path.exists(dir):
            sysdir = dir
            break
    if sysdir is None:
        return

    for sysfn in os.listdir(sysdir):
        if sysfn[-8:] != ".desktop":
            continue

        sysfile = open("%s/%s" % (desktop_directories[0], sysfn))
        syslines = sysfile.readlines()
        sysfile.close()

        configlet_found = 0
        for sysline in syslines:
            if sysline[:4] != "Exec":
                continue
            sysline = sysline[5:]
            if sysline.rstrip()[-18:] == "-configlet-capplet":
                configlet_list.append(sysline.rstrip()[:-18])
            break

def load_configlet_list():
    global configlet_list

    if os.path.exists(configlet_list_filename):
        configlet_list_file = open(configlet_list_filename)
        configlet_lines = configlet_list_file.readlines()
        configlet_list_file.close()

        for line in configlet_lines:
            configlet_list.append(line.rstrip())
    else:
        find_old_configlet_entries()

def save_configlet_list():
    if len(configlet_list) < 1:
        if os.path.exists(configlet_list_filename):
            os.unlink(configlet_list_filename)
    else:
        configlet_list_file = open(configlet_list_filename, "w")
        for configlet in configlet_list:
            configlet_list_file.write("%s\n" % (configlet,))

        configlet_list_file.close()

def remove_file(file):
    if os.path.exists(file):
        os.unlink(file)
    else:
        debug("No such file: %s" % (file,))


def initialize_system():
    global desktop_directories
    
    for sysdir in desktop_directories:
        if not os.path.exists(sysdir):
            os.mkdir(sysdir, 0755)

def remove_configlet(configlet):
    global desktop_directories

    if configlet in configlet_list:
        for sysdir in desktop_directories:
            file = "%s/%s.desktop" % (sysdir, configlet)
            remove_file(file)
        
        file = "/usr/bin/%s-configlet-capplet" % (configlet,)
        remove_file(file)

        configlet_list.remove(configlet)
    
def install_configlet(configlet_directory):
    global desktop_directories
    global base_config_capplet
    global desktop_template
    global configlet_list
    
    # Make certain we have everything set up (e.g. directories)
    initialize_system()

    # Load the configlet.
    if configlet_directory[0] != '/':
        cfglet_path = "%s/%s" % (default_configlet_path, configlet_directory)
    else:
        cfglet_path = configlet_directory
    
    real_configlet = configlet.start_configlet(cfglet_path)
    if real_configlet is None:
        return

    # Get the configlet's information.
    cfglet_title = real_configlet.get_display_title()
    cfglet_name = real_configlet.get_name()
    cfglet_desc = real_configlet.get_description()

    # Have we already added this configlet?  Don't add it twice.
    if cfglet_name in configlet_list:
        return
    
    # Define the list of new files to create. The first file must be the
    # configlet-capplet script/link.
    files = ["/usr/bin/%s-configlet-capplet" % (configlet_directory,),]

    for sysdir in desktop_directories:
        files.append("%s/%s.desktop" % (sysdir, configlet_directory))

    # Make certain those files don't exist yet
    for file in files:
        if os.path.exists(file):
            debug("%s already exists, overwriting" % (file,))
            os.unlink(file)

    # Grab the /usr/bin filename and create a symlink
    file = files.pop(0)
    os.symlink(base_config_capplet, file)

    # Now, create the remaining (.desktop) files

    # Create the contents for the files
    full_contents = ""
    for line in desktop_template:
        contents = string.replace(line, "<configlet-name>", cfglet_name)
        contents = string.replace(contents, "<configlet-desc>", cfglet_desc)
        contents = string.replace(contents, "<configlet-title>", cfglet_title)
        contents = string.replace(contents, "<configlet-path>", cfglet_path)
        contents = string.replace(contents, "<configlet-dirname>",
                                  configlet_directory)
        contents = contents + "\n"
        full_contents += contents

    for file in files:
        real_file = open(file, "w")
        real_file.write(full_contents)
        real_file.close()

    configlet_list.append(configlet_directory)

def remove_all_configlets():
    for configlet in configlet_list:
        remove_configlet(configlet)

def install_all_configlets():
    for dir in os.listdir(default_configlet_path):
        if not os.path.isdir("%s/%s" % (default_configlet_path, dir)):
            continue

        install_configlet(dir)
    
if not os.path.exists("/usr/share/control-center"):
    sys.exit(0)

parse_opts()
if not do_all and configlet_name == "":
    usage()
    sys.exit(2)

load_configlet_list()

if do_all:
    if (do_remove == 1):
        remove_all_configlets()
    if (do_install == 1):
        install_all_configlets()
else:
    if (do_remove == 1):
        remove_configlet(configlet_name)
    if (do_install == 1):
        install_configlet(configlet_name)

save_configlet_list()