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

97 by mattgiuca
Moved template.py and setup.py to better places.
1
#!/usr/bin/env python
2
# IVLE - Informatics Virtual Learning Environment
3
# Copyright (C) 2007-2008 The University of Melbourne
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19
# Module: setup
20
# Author: Matt Giuca
21
# Date:   12/12/2007
22
23
# This is a command-line application, for use by the administrator.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
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
118 by mattgiuca
setup.py: Added copytree and copylist actions.
28
# setup.py listmake (for developer use only)
29
# Recurses through the source tree and builds a list of all files which should
30
# be copied upon installation. This should be run by the developer before
31
# cutting a distribution, and the listfile it generates should be included in
32
# the distribution, avoiding the administrator having to run it.
33
316 by drtomc
setup.py - name the configuration command "config" to bring it into line with
34
# setup.py config [args]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
35
# Configures IVLE with machine-specific details, most notably, various paths.
36
# Either prompts the administrator for these details or accepts them as
37
# command-line args.
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
38
# Creates lib/conf/conf.py and trampoline/conf.h.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
39
40
# setup.py build
41
# Compiles all files and sets up a jail template in the source directory.
42
# Details:
43
# Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
44
# Creates jail/.
45
# Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
46
# Copies console/ to a location within the jail.
47
# Copies OS programs and files to corresponding locations within the jail
48
#   (eg. python and Python libs, ld.so, etc).
49
# Generates .pyc files for all the IVLE .py files.
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).
97 by mattgiuca
Moved template.py and setup.py to better places.
59
60
import os
119 by mattgiuca
setup.py: Added install action. Completely works!
61
import stat
118 by mattgiuca
setup.py: Added copytree and copylist actions.
62
import shutil
97 by mattgiuca
Moved template.py and setup.py to better places.
63
import sys
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
64
import getopt
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
65
import string
66
import errno
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
67
import mimetypes
116 by mattgiuca
setup.py: mkdir now properly obeys "dry".
68
import compileall
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
69
import getopt
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
70
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
71
# Import modules from the website is tricky since they're in the www
72
# directory.
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
73
sys.path.append(os.path.join(os.getcwd(), 'lib'))
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
74
import conf
75
import common.makeuser
76
317 by mattgiuca
doc/dependencies: Added dependency on matplotlib.
77
# Determine which Python version (2.4 or 2.5, for example) we are running,
78
# and use that as the filename to the Python directory.
79
# Just get the first 3 characters of sys.version.
80
PYTHON_VERSION = sys.version[0:3]
81
251 by mattgiuca
setup.py: Worked the "files to copy into jail" out into a separate list
82
# Operating system files to copy over into the jail.
83
# These will be copied from the given place on the OS file system into the
84
# same place within the jail.
85
JAIL_FILES = [
86
    '/lib/ld-linux.so.2',
87
    '/lib/tls/i686/cmov/libc.so.6',
88
    '/lib/tls/i686/cmov/libdl.so.2',
89
    '/lib/tls/i686/cmov/libm.so.6',
90
    '/lib/tls/i686/cmov/libpthread.so.0',
91
    '/lib/tls/i686/cmov/libutil.so.1',
253 by mattgiuca
setup.py: chmods python-console when building.
92
    '/etc/ld.so.conf',
93
    '/etc/ld.so.cache',
94
    # These 2 files do not exist in Ubuntu
95
    #'/etc/ld.so.preload',
96
    #'/etc/ld.so.nohwcap',
97
    # UNIX commands
98
    '/usr/bin/strace',
99
    '/bin/ls',
100
    '/bin/echo',
101
    # Needed by python
317 by mattgiuca
doc/dependencies: Added dependency on matplotlib.
102
    '/usr/bin/python%s' % PYTHON_VERSION,
253 by mattgiuca
setup.py: chmods python-console when building.
103
    # Needed by matplotlib
104
    '/usr/lib/i686/cmov/libssl.so.0.9.8',
105
    '/usr/lib/i686/cmov/libcrypto.so.0.9.8',
106
    '/lib/tls/i686/cmov/libnsl.so.1',
107
    '/usr/lib/libz.so.1',
108
    '/usr/lib/atlas/liblapack.so.3',
109
    '/usr/lib/atlas/libblas.so.3',
110
    '/usr/lib/libg2c.so.0',
111
    '/usr/lib/libstdc++.so.6',
112
    '/usr/lib/libfreetype.so.6',
113
    '/usr/lib/libpng12.so.0',
114
    '/usr/lib/libBLT.2.4.so.8.4',
115
    '/usr/lib/libtk8.4.so.0',
116
    '/usr/lib/libtcl8.4.so.0',
117
    '/usr/lib/tcl8.4/init.tcl',
118
    '/usr/lib/libX11.so.6',
119
    '/usr/lib/libXau.so.6',
120
    '/usr/lib/libXdmcp.so.6',
121
    '/lib/libgcc_s.so.1',
122
    '/etc/matplotlibrc',
251 by mattgiuca
setup.py: Worked the "files to copy into jail" out into a separate list
123
]
124
# Symlinks to make within the jail. Src mapped to dst.
125
JAIL_LINKS = {
317 by mattgiuca
doc/dependencies: Added dependency on matplotlib.
126
    'python%s' % PYTHON_VERSION: 'jail/usr/bin/python',
251 by mattgiuca
setup.py: Worked the "files to copy into jail" out into a separate list
127
}
128
# Trees to copy. Src mapped to dst (these will be passed to action_copytree).
129
JAIL_COPYTREES = {
317 by mattgiuca
doc/dependencies: Added dependency on matplotlib.
130
    '/usr/lib/python%s' % PYTHON_VERSION:
131
        'jail/usr/lib/python%s' % PYTHON_VERSION,
253 by mattgiuca
setup.py: chmods python-console when building.
132
    '/usr/share/matplotlib': 'jail/usr/share/matplotlib',
133
    '/etc/ld.so.conf.d': 'jail/etc/ld.so.conf.d',
251 by mattgiuca
setup.py: Worked the "files to copy into jail" out into a separate list
134
}
135
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
136
class ConfigOption:
137
    """A configuration option; one of the things written to conf.py."""
138
    def __init__(self, option_name, default, prompt, comment):
139
        """Creates a configuration option.
140
        option_name: Name of the variable in conf.py. Also name of the
141
            command-line argument to setup.py conf.
142
        default: Default value for this variable.
143
        prompt: (Short) string presented during the interactive prompt in
144
            setup.py conf.
145
        comment: (Long) comment string stored in conf.py. Each line of this
146
            string should begin with a '#'.
147
        """
148
        self.option_name = option_name
149
        self.default = default
150
        self.prompt = prompt
151
        self.comment = comment
152
153
# Configuration options, defaults and descriptions
154
config_options = []
155
config_options.append(ConfigOption("root_dir", "/ivle",
156
    """Root directory where IVLE is located (in URL space):""",
157
    """
158
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
159
# with this).
160
# eg. "/" or "/ivle"."""))
161
config_options.append(ConfigOption("ivle_install_dir", "/opt/ivle",
162
    'Root directory where IVLE will be installed (on the local file '
163
    'system):',
164
    """
165
# In the local file system, where IVLE is actually installed.
166
# This directory should contain the "www" and "bin" directories."""))
167
config_options.append(ConfigOption("jail_base", "/home/informatics/jails",
168
    """Root directory where the jails (containing user files) are stored
169
(on the local file system):""",
170
    """
171
# In the local file system, where are the student/user file spaces located.
172
# The user jails are expected to be located immediately in subdirectories of
173
# this location."""))
174
config_options.append(ConfigOption("subjects_base",
175
    "/home/informatics/subjects",
176
    """Root directory where the subject directories (containing worksheets
177
and other per-subject files) are stored (on the local file system):""",
178
    """
179
# In the local file system, where are the per-subject file spaces located.
180
# The individual subject directories are expected to be located immediately
181
# in subdirectories of this location."""))
395 by mattgiuca
Tutorial: split subjects directory into subjects and problems.
182
config_options.append(ConfigOption("problems_base",
183
    "/home/informatics/problems",
184
    """Root directory where the problem directories (containing
185
subject-independent problem sheets) are stored (on the local file
186
system):""",
187
    """
188
# In the local file system, where are the subject-independent problem sheet
189
# file spaces located."""))
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
190
config_options.append(ConfigOption("public_host", "public.localhost",
191
    """Hostname which will cause the server to go into "public mode",
192
providing login-free access to student's published work:""",
193
    """
194
# The server goes into "public mode" if the browser sends a request with this
195
# host. This is for security reasons - we only serve public student files on a
196
# separate domain to the main IVLE site.
197
# Public mode does not use cookies, and serves only public content.
198
# Private mode (normal mode) requires login, and only serves files relevant to
199
# the logged-in user."""))
200
config_options.append(ConfigOption("allowed_uids", "33",
201
    """UID of the web server process which will run IVLE.
202
Only this user may execute the trampoline. May specify multiple users as
203
a comma-separated list.
204
    (eg. "1002,78")""",
205
    """
206
# The User-ID of the web server process which will run IVLE, and any other
207
# users who are allowed to run the trampoline. This is stores as a string of
208
# comma-separated integers, simply because it is not used within Python, only
209
# used by the setup program to write to conf.h (see setup.py config)."""))
359 by mattgiuca
setup.py: Added config options for database settings.
210
config_options.append(ConfigOption("db_host", "localhost",
211
    """PostgreSQL Database config
212
==========================
213
Hostname of the DB server:""",
214
    """
215
### PostgreSQL Database config ###
216
# Database server hostname"""))
217
config_options.append(ConfigOption("db_port", "5432",
218
    """Port of the DB server:""",
219
    """
220
# Database server port"""))
363 by mattgiuca
setup.py: Added new config option - "database name"
221
config_options.append(ConfigOption("db_dbname", "ivle",
222
    """Database name:""",
223
    """
224
# Database name"""))
359 by mattgiuca
setup.py: Added config options for database settings.
225
config_options.append(ConfigOption("db_user", "postgres",
226
    """Username for DB server login:""",
227
    """
228
# Database username"""))
229
config_options.append(ConfigOption("db_password", "",
230
    """Password for DB server login:
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
231
    (Caution: This password is stored in plaintext in lib/conf/conf.py)""",
359 by mattgiuca
setup.py: Added config options for database settings.
232
    """
233
# Database password"""))
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
234
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
235
# Try importing existing conf, but if we can't just set up defaults
236
# The reason for this is that these settings are used by other phases
237
# of setup besides conf, so we need to know them.
238
# Also this allows you to hit Return to accept the existing value.
239
try:
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
240
    confmodule = __import__("lib/conf/conf")
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
241
    for opt in config_options:
242
        try:
243
            globals()[opt.option_name] = confmodule.__dict__[opt.option_name]
244
        except:
245
            globals()[opt.option_name] = opt.default
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
246
except ImportError:
247
    # Just set reasonable defaults
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
248
    for opt in config_options:
249
        globals()[opt.option_name] = opt.default
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
250
118 by mattgiuca
setup.py: Added copytree and copylist actions.
251
# Try importing install_list, but don't fail if we can't, because listmake can
252
# function without it.
253
try:
254
    import install_list
255
except:
256
    pass
257
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
258
# Mime types which will automatically be placed in the list by listmake.
259
# Note that listmake is not intended to be run by the final user (the system
260
# administrator who installs this), so the developers can customize the list
261
# as necessary, and include it in the distribution.
262
listmake_mimetypes = ['text/x-python', 'text/html',
263
    'application/x-javascript', 'application/javascript',
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
264
    'text/css', 'image/png', 'application/xml']
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
265
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
266
# Main function skeleton from Guido van Rossum
267
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
268
269
def main(argv=None):
270
    if argv is None:
271
        argv = sys.argv
272
273
    # Print the opening spiel including the GPL notice
274
275
    print """IVLE - Informatics Virtual Learning Environment Setup
97 by mattgiuca
Moved template.py and setup.py to better places.
276
Copyright (C) 2007-2008 The University of Melbourne
277
IVLE comes with ABSOLUTELY NO WARRANTY.
278
This is free software, and you are welcome to redistribute it
279
under certain conditions. See LICENSE.txt for details.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
280
281
IVLE Setup
282
"""
283
284
    # First argument is the name of the setup operation
285
    try:
286
        operation = argv[1]
287
    except IndexError:
288
        # Print usage message and exit
289
        help([])
290
        return 1
291
131 by mattgiuca
setup.py:
292
    # Disallow run as root unless installing
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
293
    if (operation != 'install' and operation != 'updatejails'
294
        and os.geteuid() == 0):
131 by mattgiuca
setup.py:
295
        print >>sys.stderr, "I do not want to run this stage as root."
296
        print >>sys.stderr, "Please run as a normal user."
297
        return 1
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
298
    # Call the requested operation's function
299
    try:
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
300
        oper_func = {
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
301
            'help' : help,
316 by drtomc
setup.py - name the configuration command "config" to bring it into line with
302
            'config' : conf,
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
303
            'build' : build,
304
            'listmake' : listmake,
305
            'install' : install,
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
306
            'updatejails' : updatejails,
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
307
        }[operation]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
308
    except KeyError:
309
        print >>sys.stderr, (
310
            """Invalid operation '%s'. Try python setup.py help."""
311
            % operation)
129 by mattgiuca
setup.py: Minor fix, exits cleanly if arguments are invalid.
312
        return 1
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
313
    return oper_func(argv[2:])
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
314
315
# Operation functions
316
317
def help(args):
318
    if args == []:
319
        print """Usage: python setup.py operation [args]
320
Operation (and args) can be:
321
    help [operation]
131 by mattgiuca
setup.py:
322
    listmake (developer use only)
316 by drtomc
setup.py - name the configuration command "config" to bring it into line with
323
    config [args]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
324
    build
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
325
    install [--nojail] [--nosubjects] [-n|--dry]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
326
"""
327
        return 1
328
    elif len(args) != 1:
329
        print """Usage: python setup.py help [operation]"""
330
        return 2
331
    else:
332
        operation = args[0]
333
334
    if operation == 'help':
335
        print """python setup.py help [operation]
336
Prints the usage message or detailed help on an operation, then exits."""
118 by mattgiuca
setup.py: Added copytree and copylist actions.
337
    elif operation == 'listmake':
338
        print """python setup.py listmake
339
(For developer use only)
340
Recurses through the source tree and builds a list of all files which should
341
be copied upon installation. This should be run by the developer before
342
cutting a distribution, and the listfile it generates should be included in
343
the distribution, avoiding the administrator having to run it."""
316 by drtomc
setup.py - name the configuration command "config" to bring it into line with
344
    elif operation == 'config':
345
        print """python setup.py config [args]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
346
Configures IVLE with machine-specific details, most notably, various paths.
347
Either prompts the administrator for these details or accepts them as
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
348
command-line args. Will be interactive only if there are no arguments given.
349
Takes defaults from existing conf file if it exists.
133 by mattgiuca
setup.py: Now allows IVLE to be installed over itself, in order to allow
350
351
To run IVLE out of the source directory (allowing development without having
352
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
353
directory, and run build/install one time.
354
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
355
Creates lib/conf/conf.py and trampoline/conf.h.
133 by mattgiuca
setup.py: Now allows IVLE to be installed over itself, in order to allow
356
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
357
Args are:"""
358
        for opt in config_options:
359
            print "    --" + opt.option_name
360
        print """As explained in the interactive prompt or conf.py.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
361
"""
362
    elif operation == 'build':
118 by mattgiuca
setup.py: Added copytree and copylist actions.
363
        print """python -O setup.py build [--dry|-n]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
364
Compiles all files and sets up a jail template in the source directory.
116 by mattgiuca
setup.py: mkdir now properly obeys "dry".
365
-O is recommended to cause compilation to be optimised.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
366
Details:
367
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
368
Creates jail/.
369
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
370
Copies console/ to a location within the jail.
371
Copies OS programs and files to corresponding locations within the jail
372
  (eg. python and Python libs, ld.so, etc).
118 by mattgiuca
setup.py: Added copytree and copylist actions.
373
Generates .pyc or .pyo files for all the IVLE .py files.
374
375
--dry | -n  Print out the actions but don't do anything."""
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
376
    elif operation == 'install':
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
377
        print """sudo python setup.py install [--nojail] [--nosubjects][--dry|-n]
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
378
(Requires root)
379
Create target install directory ($target).
380
Create $target/bin.
381
Copy trampoline/trampoline to $target/bin.
382
chown and chmod the installed trampoline.
383
Copy www/ to $target.
384
Copy jail/ to jails template directory (unless --nojail specified).
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
385
Copy subjects/ to subjects directory (unless --nosubjects specified).
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
386
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
387
--nojail        Do not copy the jail.
395 by mattgiuca
Tutorial: split subjects directory into subjects and problems.
388
--nosubjects    Do not copy the subjects and problems directories.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
389
--dry | -n  Print out the actions but don't do anything."""
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
390
    elif operation == 'updatejails':
391
        print """sudo python setup.py updatejails [--dry|-n]
392
(Requires root)
393
Copy jail/ to each subdirectory in jails directory.
394
395
--dry | -n  Print out the actions but don't do anything."""
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
396
    else:
397
        print >>sys.stderr, (
398
            """Invalid operation '%s'. Try python setup.py help."""
399
            % operation)
400
    return 1
401
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
402
def listmake(args):
403
    # We build two separate lists, by walking www and console
404
    list_www = build_list_py_files('www')
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
405
    list_lib = build_list_py_files('lib')
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
406
    list_console = build_list_py_files('console')
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
407
    list_subjects = build_list_py_files('subjects', no_top_level=True)
395 by mattgiuca
Tutorial: split subjects directory into subjects and problems.
408
    list_problems = build_list_py_files('problems', no_top_level=True)
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
409
    # Make sure that the files generated by conf are in the list
410
    # (since listmake is typically run before conf)
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
411
    if "lib/conf/conf.py" not in list_lib:
412
        list_www.append("lib/conf/conf.py")
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
413
    # Make sure that console/python-console is in the list
414
    if "console/python-console" not in list_console:
415
        list_console.append("console/python-console")
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
416
    # Write these out to a file
417
    cwd = os.getcwd()
418
    # the files that will be created/overwritten
419
    listfile = os.path.join(cwd, "install_list.py")
420
421
    try:
422
        file = open(listfile, "w")
423
424
        file.write("""# IVLE Configuration File
425
# install_list.py
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
426
# Provides lists of all files to be installed by `setup.py install' from
427
# certain directories.
116 by mattgiuca
setup.py: mkdir now properly obeys "dry".
428
# Note that any files with the given filename plus 'c' or 'o' (that is,
429
# compiled .pyc or .pyo files) will be copied as well.
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
430
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
431
# List of all installable files in www directory.
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
432
list_www = """)
433
        writelist_pretty(file, list_www)
434
        file.write("""
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
435
# List of all installable files in lib directory.
436
list_lib = """)
437
        writelist_pretty(file, list_lib)
438
        file.write("""
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
439
# List of all installable files in console directory.
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
440
list_console = """)
441
        writelist_pretty(file, list_console)
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
442
        file.write("""
443
# List of all installable files in subjects directory.
444
# This is to install sample subjects and material.
445
list_subjects = """)
446
        writelist_pretty(file, list_subjects)
395 by mattgiuca
Tutorial: split subjects directory into subjects and problems.
447
        file.write("""
448
# List of all installable files in problems directory.
449
# This is to install sample exercise material.
450
list_problems = """)
451
        writelist_pretty(file, list_problems)
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
452
453
        file.close()
454
    except IOError, (errno, strerror):
455
        print "IO error(%s): %s" % (errno, strerror)
456
        sys.exit(1)
457
458
    print "Successfully wrote install_list.py"
459
460
    print
461
    print ("You may modify the set of installable files before cutting the "
462
            "distribution:")
463
    print listfile
464
    print
465
466
    return 0
467
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
468
def build_list_py_files(dir, no_top_level=False):
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
469
    """Builds a list of all py files found in a directory and its
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
470
    subdirectories. Returns this as a list of strings.
471
    no_top_level=True means the file paths will not include the top-level
472
    directory.
473
    """
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
474
    pylist = []
475
    for (dirpath, dirnames, filenames) in os.walk(dir):
476
        # Exclude directories beginning with a '.' (such as '.svn')
477
        filter_mutate(lambda x: x[0] != '.', dirnames)
478
        # All *.py files are added to the list
479
        pylist += [os.path.join(dirpath, item) for item in filenames
117 by mattgiuca
setup.py: listmake uses mimetypes to select all files with certain mime types,
480
            if mimetypes.guess_type(item)[0] in listmake_mimetypes]
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
481
    if no_top_level:
482
        for i in range(0, len(pylist)):
483
            _, pylist[i] = pylist[i].split(os.sep, 1)
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
484
    return pylist
485
486
def writelist_pretty(file, list):
487
    """Writes a list one element per line, to a file."""
488
    if list == []:
489
        file.write("[]\n")
490
    else:
491
        file.write('[\n')
492
        for elem in list:
493
            file.write('    %s,\n' % repr(elem))
494
        file.write(']\n')
495
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
496
def conf(args):
364 by mattgiuca
setup.py: Fixed config command-line args (forgot to make general).
497
    global db_port
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
498
    # Set up some variables
499
500
    cwd = os.getcwd()
501
    # the files that will be created/overwritten
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
502
    conffile = os.path.join(cwd, "lib/conf/conf.py")
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
503
    conf_hfile = os.path.join(cwd, "trampoline/conf.h")
504
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
505
    # Get command-line arguments to avoid asking questions.
506
364 by mattgiuca
setup.py: Fixed config command-line args (forgot to make general).
507
    optnames = []
508
    for opt in config_options:
509
        optnames.append(opt.option_name + "=")
510
    (opts, args) = getopt.gnu_getopt(args, "", optnames)
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
511
512
    if args != []:
513
        print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
514
        return 2
515
516
    if opts == []:
517
        # Interactive mode. Prompt the user for all the values.
518
519
        print """This tool will create the following files:
100 by mattgiuca
setup.py: Added a new config variable, ivle_install_dir.
520
    %s
521
    %s
97 by mattgiuca
Moved template.py and setup.py to better places.
522
prompting you for details about your configuration. The file will be
523
overwritten if it already exists. It will *not* install or deploy IVLE.
524
525
Please hit Ctrl+C now if you do not wish to do this.
100 by mattgiuca
setup.py: Added a new config variable, ivle_install_dir.
526
""" % (conffile, conf_hfile)
97 by mattgiuca
Moved template.py and setup.py to better places.
527
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
528
        # Get information from the administrator
529
        # If EOF is encountered at any time during the questioning, just exit
530
        # silently
97 by mattgiuca
Moved template.py and setup.py to better places.
531
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
532
        for opt in config_options:
533
            globals()[opt.option_name] = \
534
                query_user(globals()[opt.option_name], opt.prompt)
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
535
    else:
536
        opts = dict(opts)
537
        # Non-interactive mode. Parse the options.
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
538
        for opt in config_options:
539
            if '--' + opt.option_name in opts:
540
                globals()[opt.option_name] = opts['--' + opt.option_name]
120 by mattgiuca
setup.py: Added command-line argument mode for conf. This completely works!
541
110 by mattgiuca
setup.py: Added to trampoline/conf.h an "allowed_uids" array. Asks the user
542
    # Error handling on input values
543
    try:
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
544
        allowed_uids_list = map(int, allowed_uids.split(','))
110 by mattgiuca
setup.py: Added to trampoline/conf.h an "allowed_uids" array. Asks the user
545
    except ValueError:
112 by mattgiuca
setup.py: A few comment changes.
546
        print >>sys.stderr, (
547
        "Invalid UID list (%s).\n"
548
        "Must be a comma-separated list of integers." % allowed_uids)
110 by mattgiuca
setup.py: Added to trampoline/conf.h an "allowed_uids" array. Asks the user
549
        return 1
364 by mattgiuca
setup.py: Fixed config command-line args (forgot to make general).
550
    try:
551
        db_port = int(db_port)
552
        if db_port < 0 or db_port >= 65536: raise ValueError()
553
    except ValueError:
554
        print >>sys.stderr, (
555
        "Invalid DB port (%s).\n"
556
        "Must be an integer between 0 and 65535." % repr(db_port))
557
        return 1
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
558
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
559
    # Write lib/conf/conf.py
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
560
561
    try:
562
        conf = open(conffile, "w")
563
564
        conf.write("""# IVLE Configuration File
97 by mattgiuca
Moved template.py and setup.py to better places.
565
# conf.py
566
# Miscellaneous application settings
567
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
568
""")
569
        for opt in config_options:
364 by mattgiuca
setup.py: Fixed config command-line args (forgot to make general).
570
            conf.write('%s\n%s = %s\n' % (opt.comment, opt.option_name,
571
                repr(globals()[opt.option_name])))
110 by mattgiuca
setup.py: Added to trampoline/conf.h an "allowed_uids" array. Asks the user
572
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
573
        conf.close()
574
    except IOError, (errno, strerror):
575
        print "IO error(%s): %s" % (errno, strerror)
576
        sys.exit(1)
577
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
578
    print "Successfully wrote lib/conf/conf.py"
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
579
580
    # Write trampoline/conf.h
581
582
    try:
583
        conf = open(conf_hfile, "w")
584
585
        conf.write("""/* IVLE Configuration File
100 by mattgiuca
setup.py: Added a new config variable, ivle_install_dir.
586
 * conf.h
587
 * Administrator settings required by trampoline.
588
 * Note: trampoline will have to be rebuilt in order for changes to this file
589
 * to take effect.
590
 */
591
592
/* In the local file system, where are the jails located.
593
 * The trampoline does not allow the creation of a jail anywhere besides
594
 * jail_base or a subdirectory of jail_base.
595
 */
596
static const char* jail_base = "%s";
110 by mattgiuca
setup.py: Added to trampoline/conf.h an "allowed_uids" array. Asks the user
597
598
/* Which user IDs are allowed to run the trampoline.
599
 * This list should be limited to the web server user.
600
 * (Note that root is an implicit member of this list).
601
 */
112 by mattgiuca
setup.py: A few comment changes.
602
static const int allowed_uids[] = { %s };
358 by mattgiuca
setup.py: Gutted out the config options code. It was getting so there were
603
""" % (jail_base, repr(allowed_uids_list)[1:-1]))
100 by mattgiuca
setup.py: Added a new config variable, ivle_install_dir.
604
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
605
        conf.close()
606
    except IOError, (errno, strerror):
607
        print "IO error(%s): %s" % (errno, strerror)
608
        sys.exit(1)
609
610
    print "Successfully wrote trampoline/conf.h"
611
612
    print
613
    print "You may modify the configuration at any time by editing"
614
    print conffile
615
    print conf_hfile
616
    print
617
    return 0
618
619
def build(args):
121 by mattgiuca
setup: build and install now read command line options
620
    # Get "dry" variable from command line
621
    (opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
622
    opts = dict(opts)
623
    dry = '-n' in opts or '--dry' in opts
624
625
    if dry:
626
        print "Dry run (no actions will be executed\n"
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
627
628
    # Compile the trampoline
349 by mattgiuca
setup.py: Changed so instead of directly calling gcc on the trampoline, calls
629
    curdir = os.getcwd()
630
    os.chdir('trampoline')
631
    action_runprog('make', [], dry)
632
    os.chdir(curdir)
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
633
634
    # Create the jail and its subdirectories
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
635
    # Note: Other subdirs will be made by copying files
116 by mattgiuca
setup.py: mkdir now properly obeys "dry".
636
    action_mkdir('jail', dry)
637
    action_mkdir('jail/home', dry)
638
    action_mkdir('jail/tmp', dry)
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
639
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
640
    # Copy all console and operating system files into the jail
118 by mattgiuca
setup.py: Added copytree and copylist actions.
641
    action_copylist(install_list.list_console, 'jail/opt/ivle', dry)
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
642
    copy_os_files_jail(dry)
253 by mattgiuca
setup.py: chmods python-console when building.
643
    # Chmod the python console
644
    action_chmod_x('jail/opt/ivle/console/python-console', dry)
645
    
118 by mattgiuca
setup.py: Added copytree and copylist actions.
646
116 by mattgiuca
setup.py: mkdir now properly obeys "dry".
647
    # Compile .py files into .pyc or .pyo files
648
    compileall.compile_dir('www', quiet=True)
649
    compileall.compile_dir('console', quiet=True)
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
650
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
651
    return 0
652
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
653
def copy_os_files_jail(dry):
654
    """Copies necessary Operating System files from their usual locations
655
    into the jail/ directory of the cwd."""
656
    # Currently source paths are configured for Ubuntu.
251 by mattgiuca
setup.py: Worked the "files to copy into jail" out into a separate list
657
    for filename in JAIL_FILES:
658
        copy_file_to_jail(filename, dry)
659
    for src, dst in JAIL_LINKS.items():
660
        action_symlink(src, dst, dry)
661
    for src, dst in JAIL_COPYTREES.items():
662
        action_copytree(src, dst, dry)
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
663
664
def copy_file_to_jail(src, dry):
665
    """Copies a single file from an absolute location into the same location
666
    within the jail. src must begin with a '/'. The jail will be located
667
    in a 'jail' subdirectory of the current path."""
668
    action_copyfile(src, 'jail' + src, dry)
669
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
670
def install(args):
121 by mattgiuca
setup: build and install now read command line options
671
    # Get "dry" and "nojail" variables from command line
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
672
    (opts, args) = getopt.gnu_getopt(args, "n",
673
        ['dry', 'nojail', 'nosubjects'])
121 by mattgiuca
setup: build and install now read command line options
674
    opts = dict(opts)
675
    dry = '-n' in opts or '--dry' in opts
676
    nojail = '--nojail' in opts
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
677
    nosubjects = '--nosubjects' in opts
121 by mattgiuca
setup: build and install now read command line options
678
679
    if dry:
680
        print "Dry run (no actions will be executed\n"
119 by mattgiuca
setup.py: Added install action. Completely works!
681
682
    if not dry and os.geteuid() != 0:
683
        print >>sys.stderr, "Must be root to run install"
684
        print >>sys.stderr, "(I need to chown some files)."
685
        return 1
686
687
    # Create the target (install) directory
688
    action_mkdir(ivle_install_dir, dry)
689
690
    # Create bin and copy the compiled files there
691
    action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
692
    tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
693
    action_copyfile('trampoline/trampoline', tramppath, dry)
694
    # chown trampoline to root and set setuid bit
695
    action_chown_setuid(tramppath, dry)
696
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
697
    # Copy the www and lib directories using the list
119 by mattgiuca
setup.py: Added install action. Completely works!
698
    action_copylist(install_list.list_www, ivle_install_dir, dry)
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
699
    action_copylist(install_list.list_lib, ivle_install_dir, dry)
119 by mattgiuca
setup.py: Added install action. Completely works!
700
701
    if not nojail:
702
        # Copy the local jail directory built by the build action
703
        # to the jails template directory (it will be used as a template
704
        # for all the students' jails).
705
        action_copytree('jail', os.path.join(jail_base, 'template'), dry)
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
706
    if not nosubjects:
395 by mattgiuca
Tutorial: split subjects directory into subjects and problems.
707
        # Copy the subjects and problems directories across
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
708
        action_copylist(install_list.list_subjects, subjects_base, dry,
709
            srcdir="./subjects")
395 by mattgiuca
Tutorial: split subjects directory into subjects and problems.
710
        action_copylist(install_list.list_problems, problems_base, dry,
711
            srcdir="./problems")
119 by mattgiuca
setup.py: Added install action. Completely works!
712
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
713
    # Append IVLE path to ivle.pth in python site packages
714
    # (Unless it's already there)
715
    ivle_pth = os.path.join(sys.prefix,
317 by mattgiuca
doc/dependencies: Added dependency on matplotlib.
716
        "lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION)
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
717
    ivle_www = os.path.join(ivle_install_dir, "www")
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
718
    ivle_lib = os.path.join(ivle_install_dir, "lib")
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
719
    write_ivle_pth = True
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
720
    write_ivle_lib_pth = True
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
721
    try:
722
        file = open(ivle_pth, 'r')
723
        for line in file:
724
            if line.strip() == ivle_www:
725
                write_ivle_pth = False
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
726
            elif line.strip() == ivle_lib:
727
                write_ivle_lib_pth = False
728
        file.close()
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
729
    except (IOError, OSError):
730
        pass
731
    if write_ivle_pth:
732
        action_append(ivle_pth, ivle_www)
409 by mattgiuca
Moved www/conf and www/common to a new directory lib. This separates the "web"
733
    if write_ivle_lib_pth:
734
        action_append(ivle_pth, ivle_lib)
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
735
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
736
    return 0
737
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
738
def updatejails(args):
739
    # Get "dry" variable from command line
740
    (opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
741
    opts = dict(opts)
742
    dry = '-n' in opts or '--dry' in opts
743
744
    if dry:
745
        print "Dry run (no actions will be executed\n"
746
747
    if not dry and os.geteuid() != 0:
748
        print >>sys.stderr, "Must be root to run install"
749
        print >>sys.stderr, "(I need to chown some files)."
750
        return 1
751
752
    # Update the template jail directory in case it hasn't been installed
753
    # recently.
754
    action_copytree('jail', os.path.join(jail_base, 'template'), dry)
755
756
    # Re-link all the files in all students jails.
757
    for dir in os.listdir(jail_base):
758
        if dir == 'template': continue
759
        # First back up the student's home directory
760
        temp_home = os.tmpnam()
761
        action_rename(os.path.join(jail_base, dir, 'home'), temp_home, dry)
762
        # Delete the student's jail and relink the jail files
763
        action_linktree(os.path.join(jail_base, 'template'),
764
            os.path.join(jail_base, dir), dry)
765
        # Restore the student's home directory
766
        action_rename(temp_home, os.path.join(jail_base, dir, 'home'), dry)
767
        # Set up the user's home directory just in case they don't have a
768
        # directory for this yet
769
        action_mkdir(os.path.join(jail_base, dir, 'home', dir), dry)
770
771
    return 0
772
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
773
# The actions call Python os functions but print actions and handle dryness.
774
# May still throw os exceptions if errors occur.
775
108 by mattgiuca
setup.py: Added RunError class. action_runprog now throws a RunError if the
776
class RunError:
777
    """Represents an error when running a program (nonzero return)."""
778
    def __init__(self, prog, retcode):
779
        self.prog = prog
780
        self.retcode = retcode
781
    def __str__(self):
782
        return str(self.prog) + " returned " + repr(self.retcode)
783
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
784
def action_runprog(prog, args, dry):
785
    """Runs a unix program. Searches in $PATH. Synchronous (waits for the
786
    program to return). Runs in the current environment. First prints the
787
    action as a "bash" line.
788
108 by mattgiuca
setup.py: Added RunError class. action_runprog now throws a RunError if the
789
    Throws a RunError with a retcode of the return value of the program,
790
    if the program did not return 0.
791
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
792
    prog: String. Name of the program. (No path required, if in $PATH).
793
    args: [String]. Arguments to the program.
794
    dry: Bool. If True, prints but does not execute.
795
    """
796
    print prog, string.join(args, ' ')
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
797
    if dry: return
798
    ret = os.spawnvp(os.P_WAIT, prog, args)
799
    if ret != 0:
800
        raise RunError(prog, ret)
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
801
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
802
def action_rename(src, dst, dry):
803
    """Calls rename. Deletes the target if it already exists."""
804
    if os.access(dst, os.F_OK):
805
        print "rm -r", dst
806
        if not dry:
807
            shutil.rmtree(dst, True)
808
    print "mv ", src, dst
809
    if dry: return
810
    try:
811
        os.rename(src, dst)
812
    except OSError, (err, msg):
813
        if err != errno.EEXIST:
814
            raise
815
116 by mattgiuca
setup.py: mkdir now properly obeys "dry".
816
def action_mkdir(path, dry):
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
817
    """Calls mkdir. Silently ignored if the directory already exists.
818
    Creates all parent directories as necessary."""
819
    print "mkdir -p", path
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
820
    if dry: return
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
821
    try:
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
822
        os.makedirs(path)
107 by mattgiuca
setup.py: Added "action" functions which encapsulate calling OS functions.
823
    except OSError, (err, msg):
824
        if err != errno.EEXIST:
825
            raise
826
118 by mattgiuca
setup.py: Added copytree and copylist actions.
827
def action_copytree(src, dst, dry):
828
    """Copies an entire directory tree. Symlinks are seen as normal files and
829
    copies of the entire file (not the link) are made. Creates all parent
830
    directories as necessary.
831
832
    See shutil.copytree."""
833
    if os.access(dst, os.F_OK):
834
        print "rm -r", dst
835
        if not dry:
836
            shutil.rmtree(dst, True)
837
    print "cp -r", src, dst
838
    if dry: return
132 by mattgiuca
setup.py: File copying is more robust - preserves symlinks and permissions
839
    shutil.copytree(src, dst, True)
118 by mattgiuca
setup.py: Added copytree and copylist actions.
840
252 by mattgiuca
setup.py: Added action "updatejails" which wipes all student jails, replacing
841
def action_linktree(src, dst, dry):
842
    """Hard-links an entire directory tree. Same as copytree but the created
843
    files are hard-links not actual copies. Removes the existing destination.
844
    """
845
    if os.access(dst, os.F_OK):
846
        print "rm -r", dst
847
        if not dry:
848
            shutil.rmtree(dst, True)
849
    print "<cp with hardlinks> -r", src, dst
850
    if dry: return
851
    common.makeuser.linktree(src, dst)
852
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
853
def action_copylist(srclist, dst, dry, srcdir="."):
118 by mattgiuca
setup.py: Added copytree and copylist actions.
854
    """Copies all files in a list to a new location. The files in the list
855
    are read relative to the current directory, and their destinations are the
856
    same paths relative to dst. Creates all parent directories as necessary.
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
857
    srcdir is "." by default, can be overridden.
118 by mattgiuca
setup.py: Added copytree and copylist actions.
858
    """
859
    for srcfile in srclist:
860
        dstfile = os.path.join(dst, srcfile)
313 by mattgiuca
test/test_framework: Updated examples, a bit of better descriptions, sample
861
        srcfile = os.path.join(srcdir, srcfile)
118 by mattgiuca
setup.py: Added copytree and copylist actions.
862
        dstdir = os.path.split(dstfile)[0]
863
        if not os.path.isdir(dstdir):
864
            action_mkdir(dstdir, dry)
865
        print "cp -f", srcfile, dstfile
866
        if not dry:
133 by mattgiuca
setup.py: Now allows IVLE to be installed over itself, in order to allow
867
            try:
868
                shutil.copyfile(srcfile, dstfile)
869
                shutil.copymode(srcfile, dstfile)
870
            except shutil.Error:
871
                pass
118 by mattgiuca
setup.py: Added copytree and copylist actions.
872
119 by mattgiuca
setup.py: Added install action. Completely works!
873
def action_copyfile(src, dst, dry):
874
    """Copies one file to a new location. Creates all parent directories
875
    as necessary.
318 by mattgiuca
setup.py: action_copyfile now handles errors as warnings, instead of quitting
876
    Warn if file not found.
119 by mattgiuca
setup.py: Added install action. Completely works!
877
    """
878
    dstdir = os.path.split(dst)[0]
879
    if not os.path.isdir(dstdir):
880
        action_mkdir(dstdir, dry)
881
    print "cp -f", src, dst
882
    if not dry:
133 by mattgiuca
setup.py: Now allows IVLE to be installed over itself, in order to allow
883
        try:
884
            shutil.copyfile(src, dst)
885
            shutil.copymode(src, dst)
318 by mattgiuca
setup.py: action_copyfile now handles errors as warnings, instead of quitting
886
        except (shutil.Error, IOError), e:
887
            print "Warning: " + str(e)
119 by mattgiuca
setup.py: Added install action. Completely works!
888
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
889
def action_symlink(src, dst, dry):
890
    """Creates a symlink in a given location. Creates all parent directories
891
    as necessary.
892
    """
893
    dstdir = os.path.split(dst)[0]
894
    if not os.path.isdir(dstdir):
895
        action_mkdir(dstdir, dry)
131 by mattgiuca
setup.py:
896
    # Delete existing file
897
    if os.path.exists(dst):
898
        os.remove(dst)
899
    print "ln -fs", src, dst
122 by mattgiuca
setup.py: build action now copies all operating system files into the jail.
900
    if not dry:
901
        os.symlink(src, dst)
902
256 by mattgiuca
Changed the way IVLE's path is loaded into Python's sys.path. Now a file
903
def action_append(ivle_pth, ivle_www):
904
    file = open(ivle_pth, 'a+')
905
    file.write(ivle_www + '\n')
906
    file.close()
907
119 by mattgiuca
setup.py: Added install action. Completely works!
908
def action_chown_setuid(file, dry):
909
    """Chowns a file to root, and sets the setuid bit on the file.
910
    Calling this function requires the euid to be root.
911
    The actual mode of path is set to: rws--s--s
912
    """
913
    print "chown root:root", file
914
    if not dry:
915
        os.chown(file, 0, 0)
916
    print "chmod a+xs", file
917
    print "chmod u+rw", file
918
    if not dry:
919
        os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
920
            | stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
921
253 by mattgiuca
setup.py: chmods python-console when building.
922
def action_chmod_x(file, dry):
371 by mattgiuca
setup.py: Fixed chmodding python-console. (Turns out this was a bug in setup,
923
    """Chmod 755 a file (sets permissions to rwxr-xr-x)."""
924
    print "chmod 755", file
253 by mattgiuca
setup.py: chmods python-console when building.
925
    if not dry:
371 by mattgiuca
setup.py: Fixed chmodding python-console. (Turns out this was a bug in setup,
926
        os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
927
            | stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)
253 by mattgiuca
setup.py: chmods python-console when building.
928
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
929
def query_user(default, prompt):
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
930
    """Prompts the user for a string, which is read from a line of stdin.
931
    Exits silently if EOF is encountered. Returns the string, with spaces
932
    removed from the beginning and end.
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
933
934
    Returns default if a 0-length line (after spaces removed) was read.
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
935
    """
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
936
    sys.stdout.write('%s\n    (default: "%s")\n>' % (prompt, default))
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
937
    try:
938
        val = sys.stdin.readline()
939
    except KeyboardInterrupt:
940
        # Ctrl+C
941
        sys.stdout.write("\n")
942
        sys.exit(1)
943
    sys.stdout.write("\n")
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
944
    # If EOF, exit
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
945
    if val == '': sys.exit(1)
113 by mattgiuca
setup.py: Now loads defaults, if possible, from the existing conf.py.
946
    # If empty line, return default
947
    val = val.strip()
948
    if val == '': return default
949
    return val
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
950
114 by mattgiuca
setup.py: Wrote listmake (and ancillary functions).
951
def filter_mutate(function, list):
952
    """Like built-in filter, but mutates the given list instead of returning a
953
    new one. Returns None."""
954
    i = len(list)-1
955
    while i >= 0:
956
        # Delete elements which do not match
957
        if not function(list[i]):
958
            del list[i]
959
        i -= 1
960
104 by mattgiuca
setup.py: Replaced the simple script with a full options processing script
961
if __name__ == "__main__":
962
    sys.exit(main())