~azzar1/unity/add-show-desktop-key

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
315
316
317
318
319
320
#!/usr/bin/env python
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Module: setup
# Author: Matt Giuca
# Date:   12/12/2007

# This is a command-line application, for use by the administrator.
# This program configures, builds and installs IVLE in three separate steps.
# It is called with at least one argument, which specifies which operation to
# take.

# setup.py conf [args]
# Configures IVLE with machine-specific details, most notably, various paths.
# Either prompts the administrator for these details or accepts them as
# command-line args.
# Creates www/conf/conf.py and trampoline/conf.h.

# setup.py build
# Compiles all files and sets up a jail template in the source directory.
# Details:
# Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
# Creates jail/.
# Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
# Copies console/ to a location within the jail.
# Copies OS programs and files to corresponding locations within the jail
#   (eg. python and Python libs, ld.so, etc).
# Generates .pyc files for all the IVLE .py files.

# setup.py listmake (for developer use only)
# Recurses through the source tree and builds a list of all files which should
# be copied upon installation. This should be run by the developer before
# cutting a distribution, and the listfile it generates should be included in
# the distribution, avoiding the administrator having to run it.

# setup.py install [--nojail] [--dry|n]
# (Requires root)
# Create target install directory ($target).
# Create $target/bin.
# Copy trampoline/trampoline to $target/bin.
# chown and chmod the installed trampoline.
# Copy www/ to $target.
# Copy jail/ to jails template directory (unless --nojail specified).

import os
import sys
import getopt

# Main function skeleton from Guido van Rossum
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def main(argv=None):
    if argv is None:
        argv = sys.argv

    # Print the opening spiel including the GPL notice

    print """IVLE - Informatics Virtual Learning Environment Setup
Copyright (C) 2007-2008 The University of Melbourne
IVLE comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See LICENSE.txt for details.

IVLE Setup
"""

    # First argument is the name of the setup operation
    try:
        operation = argv[1]
    except IndexError:
        # Print usage message and exit
        help([])
        return 1

    # Call the requested operation's function
    try:
        return {
            'help' : help,
            'conf' : conf,
            'build' : build,
            'listmake' : listmake,
            'install' : install,
        }[operation](argv[2:])
    except KeyError:
        print >>sys.stderr, (
            """Invalid operation '%s'. Try python setup.py help."""
            % operation)

    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.error, msg:
            raise Usage(msg)
        # more code, unchanged
    except Usage, err:
        print >>sys.stderr, err.msg
        print >>sys.stderr, "for help use --help"
        return 2

# Operation functions

def help(args):
    if args == []:
        print """Usage: python setup.py operation [args]
Operation (and args) can be:
    help [operation]
    conf [args]
    build
    install [--nojail] [-n|--dry]
"""
        return 1
    elif len(args) != 1:
        print """Usage: python setup.py help [operation]"""
        return 2
    else:
        operation = args[0]

    if operation == 'help':
        print """python setup.py help [operation]
Prints the usage message or detailed help on an operation, then exits."""
    elif operation == 'conf':
        print """python setup.py conf [args]
Configures IVLE with machine-specific details, most notably, various paths.
Either prompts the administrator for these details or accepts them as
command-line args.
Creates www/conf/conf.py and trampoline/conf.h.
Args are:
"""
    elif operation == 'build':
        print """python setup.py build
Compiles all files and sets up a jail template in the source directory.
Details:
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
Creates jail/.
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
Copies console/ to a location within the jail.
Copies OS programs and files to corresponding locations within the jail
  (eg. python and Python libs, ld.so, etc).
Generates .pyc files for all the IVLE .py files."""
    elif operation == 'listmake':
        print """python setup.py listmake
(For developer use only)
Recurses through the source tree and builds a list of all files which should
be copied upon installation. This should be run by the developer before
cutting a distribution, and the listfile it generates should be included in
the distribution, avoiding the administrator having to run it."""
    elif operation == 'install':
        print """sudo python setup.py install [--nojail] [--dry|-n]
(Requires root)
Create target install directory ($target).
Create $target/bin.
Copy trampoline/trampoline to $target/bin.
chown and chmod the installed trampoline.
Copy www/ to $target.
Copy jail/ to jails template directory (unless --nojail specified).

--nojail    Do not copy the jail.
--dry | -n  Print out the actions but don't do anything."""
    else:
        print >>sys.stderr, (
            """Invalid operation '%s'. Try python setup.py help."""
            % operation)
    return 1

def conf(args):
    # Set up some variables

    cwd = os.getcwd()
    # the files that will be created/overwritten
    conffile = os.path.join(cwd, "www/conf/conf.py")
    conf_hfile = os.path.join(cwd, "trampoline/conf.h")

    # Fixed config options that we don't ask the admin

    default_app = "dummy"

    print """This tool will create the following files:
    %s
    %s
prompting you for details about your configuration. The file will be
overwritten if it already exists. It will *not* install or deploy IVLE.

Please hit Ctrl+C now if you do not wish to do this.
""" % (conffile, conf_hfile)

    # Get information from the administrator
    # If EOF is encountered at any time during the questioning, just exit
    # silently

    root_dir = query_user(
    """Root directory where IVLE is located (in URL space):
    (eg. "/" or "/ivle")""")
    ivle_install_dir = query_user(
    'Root directory where IVLE is located (on the local file system):\n'
    '(eg. "/home/informatics/ivle")')
    student_dir = query_user(
    """Root directory where user files are stored (on the local file system):
    (eg. "/home/informatics/jails")""")

    # Write www/conf/conf.py

    try:
        conf = open(conffile, "w")

        conf.write("""# IVLE Configuration File
# conf.py
# Miscellaneous application settings


# In URL space, where in the site is IVLE located. (All URLs will be prefixed
# with this).
# eg. "/" or "/ivle".
root_dir = "%s"

# In the local file system, where IVLE is actually installed.
# This directory should contain the "www" and "bin" directories.
ivle_install_dir = "%s"

# In the local file system, where are the student/user file spaces located.
# The user jails are expected to be located immediately in subdirectories of
# this location.
student_dir = "%s"

# Which application to load by default (if the user navigates to the top level
# of the site). This is the app's URL name.
# Note that if this app requires authentication, the user will first be
# presented with the login screen.
default_app = "%s"
""" % (root_dir, ivle_install_dir, student_dir, default_app))
        
        conf.close()
    except IOError, (errno, strerror):
        print "IO error(%s): %s" % (errno, strerror)
        sys.exit(1)

    print "Successfully wrote www/conf/conf.py"

    # Write trampoline/conf.h

    try:
        conf = open(conf_hfile, "w")

        conf.write("""/* IVLE Configuration File
 * conf.h
 * Administrator settings required by trampoline.
 * Note: trampoline will have to be rebuilt in order for changes to this file
 * to take effect.
 */

/* In the local file system, where are the jails located.
 * The trampoline does not allow the creation of a jail anywhere besides
 * jail_base or a subdirectory of jail_base.
 */
static const char* jail_base = "%s";
""" % (student_dir))

        conf.close()
    except IOError, (errno, strerror):
        print "IO error(%s): %s" % (errno, strerror)
        sys.exit(1)

    print "Successfully wrote trampoline/conf.h"

    print
    print "You may modify the configuration at any time by editing"
    print conffile
    print conf_hfile
    print
    return 0

def build(args):
    print "Build"
    return 0

def listmake(args):
    print "Listmake"
    return 0

def install(args):
    print "Install"
    return 0

def query_user(prompt):
    """Prompts the user for a string, which is read from a line of stdin.
    Exits silently if EOF is encountered. Returns the string, with spaces
    removed from the beginning and end.
    """
    sys.stdout.write(prompt)
    sys.stdout.write("\n>")
    try:
        val = sys.stdin.readline()
    except KeyboardInterrupt:
        # Ctrl+C
        sys.stdout.write("\n")
        sys.exit(1)
    sys.stdout.write("\n")
    if val == '': sys.exit(1)
    return val.strip()

if __name__ == "__main__":
    sys.exit(main())