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

« back to all changes in this revision

Viewing changes to setup.py

  • Committer: mattgiuca
  • Date: 2007-12-20 22:35:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:104
setup.py: Replaced the simple script with a full options processing script
    which has multiple operations (help, conf, build, listmake, install).
    conf does what setup.py used to do.
    Wrote help operation, and explained all of the other operations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
# Date:   12/12/2007
22
22
 
23
23
# This is a command-line application, for use by the administrator.
24
 
# Prompts the administrator to enter machine-specific details and builds the
25
 
# file conf/conf.py.
26
 
# (This file is not included with the distribution precisely because it
27
 
# contains machine-specific settings).
 
24
# This program configures, builds and installs IVLE in three separate steps.
 
25
# It is called with at least one argument, which specifies which operation to
 
26
# take.
 
27
 
 
28
# setup.py conf [args]
 
29
# Configures IVLE with machine-specific details, most notably, various paths.
 
30
# Either prompts the administrator for these details or accepts them as
 
31
# command-line args.
 
32
# Creates www/conf/conf.py and trampoline/conf.h.
 
33
 
 
34
# setup.py build
 
35
# Compiles all files and sets up a jail template in the source directory.
 
36
# Details:
 
37
# Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
 
38
# Creates jail/.
 
39
# Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
 
40
# Copies console/ to a location within the jail.
 
41
# Copies OS programs and files to corresponding locations within the jail
 
42
#   (eg. python and Python libs, ld.so, etc).
 
43
# Generates .pyc files for all the IVLE .py files.
 
44
 
 
45
# setup.py listmake (for developer use only)
 
46
# Recurses through the source tree and builds a list of all files which should
 
47
# be copied upon installation. This should be run by the developer before
 
48
# cutting a distribution, and the listfile it generates should be included in
 
49
# the distribution, avoiding the administrator having to run it.
 
50
 
 
51
# setup.py install [--nojail] [--dry|n]
 
52
# (Requires root)
 
53
# Create target install directory ($target).
 
54
# Create $target/bin.
 
55
# Copy trampoline/trampoline to $target/bin.
 
56
# chown and chmod the installed trampoline.
 
57
# Copy www/ to $target.
 
58
# Copy jail/ to jails template directory (unless --nojail specified).
28
59
 
29
60
import os
30
61
import sys
31
 
 
32
 
def query_user(prompt):
33
 
    """Prompts the user for a string, which is read from a line of stdin.
34
 
    Exits silently if EOF is encountered. Returns the string, with spaces
35
 
    removed from the beginning and end.
36
 
    """
37
 
    sys.stdout.write(prompt)
38
 
    sys.stdout.write("\n>")
39
 
    try:
40
 
        val = sys.stdin.readline()
41
 
    except KeyboardInterrupt:
42
 
        # Ctrl+C
43
 
        sys.stdout.write("\n")
44
 
        sys.exit(1)
45
 
    sys.stdout.write("\n")
46
 
    if val == '': sys.exit(1)
47
 
    return val.strip()
48
 
 
49
 
# MAIN SCRIPT
50
 
 
51
 
# Set up some variables
52
 
 
53
 
cwd = os.getcwd()
54
 
# the files that will be created/overwritten
55
 
conffile = os.path.join(cwd, "www/conf/conf.py")
56
 
conf_hfile = os.path.join(cwd, "trampoline/conf.h")
57
 
 
58
 
# Fixed config options that we don't ask the admin
59
 
 
60
 
default_app = "dummy"
61
 
 
62
 
# Print the opening spiel including the GPL notice
63
 
 
64
 
print """IVLE - Informatics Virtual Learning Environment Setup
 
62
import getopt
 
63
 
 
64
# Main function skeleton from Guido van Rossum
 
65
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
 
66
 
 
67
class Usage(Exception):
 
68
    def __init__(self, msg):
 
69
        self.msg = msg
 
70
 
 
71
def main(argv=None):
 
72
    if argv is None:
 
73
        argv = sys.argv
 
74
 
 
75
    # Print the opening spiel including the GPL notice
 
76
 
 
77
    print """IVLE - Informatics Virtual Learning Environment Setup
65
78
Copyright (C) 2007-2008 The University of Melbourne
66
79
IVLE comes with ABSOLUTELY NO WARRANTY.
67
80
This is free software, and you are welcome to redistribute it
68
81
under certain conditions. See LICENSE.txt for details.
69
 
"""
70
 
 
71
 
print """IVLE Setup
72
 
This tool will create the following files:
 
82
 
 
83
IVLE Setup
 
84
"""
 
85
 
 
86
    # First argument is the name of the setup operation
 
87
    try:
 
88
        operation = argv[1]
 
89
    except IndexError:
 
90
        # Print usage message and exit
 
91
        help([])
 
92
        return 1
 
93
 
 
94
    # Call the requested operation's function
 
95
    try:
 
96
        return {
 
97
            'help' : help,
 
98
            'conf' : conf,
 
99
            'build' : build,
 
100
            'listmake' : listmake,
 
101
            'install' : install,
 
102
        }[operation](argv[2:])
 
103
    except KeyError:
 
104
        print >>sys.stderr, (
 
105
            """Invalid operation '%s'. Try python setup.py help."""
 
106
            % operation)
 
107
 
 
108
    try:
 
109
        try:
 
110
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
 
111
        except getopt.error, msg:
 
112
            raise Usage(msg)
 
113
        # more code, unchanged
 
114
    except Usage, err:
 
115
        print >>sys.stderr, err.msg
 
116
        print >>sys.stderr, "for help use --help"
 
117
        return 2
 
118
 
 
119
# Operation functions
 
120
 
 
121
def help(args):
 
122
    if args == []:
 
123
        print """Usage: python setup.py operation [args]
 
124
Operation (and args) can be:
 
125
    help [operation]
 
126
    conf [args]
 
127
    build
 
128
    install [--nojail] [-n|--dry]
 
129
"""
 
130
        return 1
 
131
    elif len(args) != 1:
 
132
        print """Usage: python setup.py help [operation]"""
 
133
        return 2
 
134
    else:
 
135
        operation = args[0]
 
136
 
 
137
    if operation == 'help':
 
138
        print """python setup.py help [operation]
 
139
Prints the usage message or detailed help on an operation, then exits."""
 
140
    elif operation == 'conf':
 
141
        print """python setup.py conf [args]
 
142
Configures IVLE with machine-specific details, most notably, various paths.
 
143
Either prompts the administrator for these details or accepts them as
 
144
command-line args.
 
145
Creates www/conf/conf.py and trampoline/conf.h.
 
146
Args are:
 
147
"""
 
148
    elif operation == 'build':
 
149
        print """python setup.py build
 
150
Compiles all files and sets up a jail template in the source directory.
 
151
Details:
 
152
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
 
153
Creates jail/.
 
154
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
 
155
Copies console/ to a location within the jail.
 
156
Copies OS programs and files to corresponding locations within the jail
 
157
  (eg. python and Python libs, ld.so, etc).
 
158
Generates .pyc files for all the IVLE .py files."""
 
159
    elif operation == 'listmake':
 
160
        print """python setup.py listmake
 
161
(For developer use only)
 
162
Recurses through the source tree and builds a list of all files which should
 
163
be copied upon installation. This should be run by the developer before
 
164
cutting a distribution, and the listfile it generates should be included in
 
165
the distribution, avoiding the administrator having to run it."""
 
166
    elif operation == 'install':
 
167
        print """sudo python setup.py install [--nojail] [--dry|-n]
 
168
(Requires root)
 
169
Create target install directory ($target).
 
170
Create $target/bin.
 
171
Copy trampoline/trampoline to $target/bin.
 
172
chown and chmod the installed trampoline.
 
173
Copy www/ to $target.
 
174
Copy jail/ to jails template directory (unless --nojail specified).
 
175
 
 
176
--nojail    Do not copy the jail.
 
177
--dry | -n  Print out the actions but don't do anything."""
 
178
    else:
 
179
        print >>sys.stderr, (
 
180
            """Invalid operation '%s'. Try python setup.py help."""
 
181
            % operation)
 
182
    return 1
 
183
 
 
184
def conf(args):
 
185
    # Set up some variables
 
186
 
 
187
    cwd = os.getcwd()
 
188
    # the files that will be created/overwritten
 
189
    conffile = os.path.join(cwd, "www/conf/conf.py")
 
190
    conf_hfile = os.path.join(cwd, "trampoline/conf.h")
 
191
 
 
192
    # Fixed config options that we don't ask the admin
 
193
 
 
194
    default_app = "dummy"
 
195
 
 
196
    print """This tool will create the following files:
73
197
    %s
74
198
    %s
75
199
prompting you for details about your configuration. The file will be
78
202
Please hit Ctrl+C now if you do not wish to do this.
79
203
""" % (conffile, conf_hfile)
80
204
 
81
 
# Get information from the administrator
82
 
# If EOF is encountered at any time during the questioning, just exit silently
 
205
    # Get information from the administrator
 
206
    # If EOF is encountered at any time during the questioning, just exit
 
207
    # silently
83
208
 
84
 
root_dir = query_user("""Root directory where IVLE is located (in URL space):
85
 
(eg. "/" or "/ivle")""")
86
 
ivle_install_dir = query_user('Root directory where IVLE is located (on the '
87
 
'local file system):\n'
88
 
'(eg. "/home/informatics/ivle")')
89
 
student_dir = query_user(
 
209
    root_dir = query_user(
 
210
    """Root directory where IVLE is located (in URL space):
 
211
    (eg. "/" or "/ivle")""")
 
212
    ivle_install_dir = query_user(
 
213
    'Root directory where IVLE is located (on the local file system):\n'
 
214
    '(eg. "/home/informatics/ivle")')
 
215
    student_dir = query_user(
90
216
    """Root directory where user files are stored (on the local file system):
91
 
(eg. "/home/informatics/jails")""")
92
 
 
93
 
# Write www/conf.py
94
 
 
95
 
try:
96
 
    conf = open(conffile, "w")
97
 
 
98
 
    conf.write("""# IVLE Configuration File
 
217
    (eg. "/home/informatics/jails")""")
 
218
 
 
219
    # Write www/conf/conf.py
 
220
 
 
221
    try:
 
222
        conf = open(conffile, "w")
 
223
 
 
224
        conf.write("""# IVLE Configuration File
99
225
# conf.py
100
226
# Miscellaneous application settings
101
227
 
120
246
# presented with the login screen.
121
247
default_app = "%s"
122
248
""" % (root_dir, ivle_install_dir, student_dir, default_app))
123
 
    
124
 
    conf.close()
125
 
except IOError, (errno, strerror):
126
 
    print "IO error(%s): %s" % (errno, strerror)
127
 
    sys.exit(1)
128
 
 
129
 
print "Successfully wrote www/conf.py"
130
 
 
131
 
# Write trampoline/conf.h
132
 
 
133
 
try:
134
 
    conf = open(conf_hfile, "w")
135
 
 
136
 
    conf.write("""/* IVLE Configuration File
 
249
        
 
250
        conf.close()
 
251
    except IOError, (errno, strerror):
 
252
        print "IO error(%s): %s" % (errno, strerror)
 
253
        sys.exit(1)
 
254
 
 
255
    print "Successfully wrote www/conf/conf.py"
 
256
 
 
257
    # Write trampoline/conf.h
 
258
 
 
259
    try:
 
260
        conf = open(conf_hfile, "w")
 
261
 
 
262
        conf.write("""/* IVLE Configuration File
137
263
 * conf.h
138
264
 * Administrator settings required by trampoline.
139
265
 * Note: trampoline will have to be rebuilt in order for changes to this file
147
273
static const char* jail_base = "%s";
148
274
""" % (student_dir))
149
275
 
150
 
    conf.close()
151
 
except IOError, (errno, strerror):
152
 
    print "IO error(%s): %s" % (errno, strerror)
153
 
    sys.exit(1)
154
 
 
155
 
print "Successfully wrote trampoline/conf.h"
156
 
 
157
 
print
158
 
print "You may modify the configuration at any time by editing"
159
 
print conffile
160
 
print conf_hfile
161
 
print
162
 
 
 
276
        conf.close()
 
277
    except IOError, (errno, strerror):
 
278
        print "IO error(%s): %s" % (errno, strerror)
 
279
        sys.exit(1)
 
280
 
 
281
    print "Successfully wrote trampoline/conf.h"
 
282
 
 
283
    print
 
284
    print "You may modify the configuration at any time by editing"
 
285
    print conffile
 
286
    print conf_hfile
 
287
    print
 
288
    return 0
 
289
 
 
290
def build(args):
 
291
    print "Build"
 
292
    return 0
 
293
 
 
294
def listmake(args):
 
295
    print "Listmake"
 
296
    return 0
 
297
 
 
298
def install(args):
 
299
    print "Install"
 
300
    return 0
 
301
 
 
302
def query_user(prompt):
 
303
    """Prompts the user for a string, which is read from a line of stdin.
 
304
    Exits silently if EOF is encountered. Returns the string, with spaces
 
305
    removed from the beginning and end.
 
306
    """
 
307
    sys.stdout.write(prompt)
 
308
    sys.stdout.write("\n>")
 
309
    try:
 
310
        val = sys.stdin.readline()
 
311
    except KeyboardInterrupt:
 
312
        # Ctrl+C
 
313
        sys.stdout.write("\n")
 
314
        sys.exit(1)
 
315
    sys.stdout.write("\n")
 
316
    if val == '': sys.exit(1)
 
317
    return val.strip()
 
318
 
 
319
if __name__ == "__main__":
 
320
    sys.exit(main())