169
139
if operation == 'help':
170
140
print """python setup.py help [operation]
171
141
Prints the usage message or detailed help on an operation, then exits."""
172
elif operation == 'listmake':
173
print """python setup.py listmake
174
(For developer use only)
175
Recurses through the source tree and builds a list of all files which should
176
be copied upon installation. This should be run by the developer before
177
cutting a distribution, and the listfile it generates should be included in
178
the distribution, avoiding the administrator having to run it."""
179
142
elif operation == 'conf':
180
143
print """python setup.py conf [args]
181
144
Configures IVLE with machine-specific details, most notably, various paths.
182
145
Either prompts the administrator for these details or accepts them as
183
command-line args. Will be interactive only if there are no arguments given.
184
Takes defaults from existing conf file if it exists.
186
To run IVLE out of the source directory (allowing development without having
187
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
188
directory, and run build/install one time.
190
147
Creates www/conf/conf.py and trampoline/conf.h.
197
As explained in the interactive prompt or conf.py.
199
150
elif operation == 'build':
200
print """python -O setup.py build [--dry|-n]
151
print """python setup.py build
201
152
Compiles all files and sets up a jail template in the source directory.
202
-O is recommended to cause compilation to be optimised.
204
154
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
232
# We build two separate lists, by walking www and console
233
list_www = build_list_py_files('www')
234
list_console = build_list_py_files('console')
235
# Make sure that the files generated by conf are in the list
236
# (since listmake is typically run before conf)
237
if "www/conf/conf.py" not in list_www:
238
list_www.append("www/conf/conf.py")
239
# Make sure that console/python-console is in the list
240
if "console/python-console" not in list_console:
241
list_console.append("console/python-console")
242
# Write these out to a file
244
# the files that will be created/overwritten
245
listfile = os.path.join(cwd, "install_list.py")
248
file = open(listfile, "w")
250
file.write("""# IVLE Configuration File
252
# Provides lists of all files to be installed by `setup.py install' from
253
# certain directories.
254
# Note that any files with the given filename plus 'c' or 'o' (that is,
255
# compiled .pyc or .pyo files) will be copied as well.
257
# List of all installable files in www directory.
259
writelist_pretty(file, list_www)
261
# List of all installable files in console directory.
263
writelist_pretty(file, list_console)
266
except IOError, (errno, strerror):
267
print "IO error(%s): %s" % (errno, strerror)
270
print "Successfully wrote install_list.py"
273
print ("You may modify the set of installable files before cutting the "
280
def build_list_py_files(dir):
281
"""Builds a list of all py files found in a directory and its
282
subdirectories. Returns this as a list of strings."""
284
for (dirpath, dirnames, filenames) in os.walk(dir):
285
# Exclude directories beginning with a '.' (such as '.svn')
286
filter_mutate(lambda x: x[0] != '.', dirnames)
287
# All *.py files are added to the list
288
pylist += [os.path.join(dirpath, item) for item in filenames
289
if mimetypes.guess_type(item)[0] in listmake_mimetypes]
292
def writelist_pretty(file, list):
293
"""Writes a list one element per line, to a file."""
299
file.write(' %s,\n' % repr(elem))
303
global root_dir, ivle_install_dir, jail_base, allowed_uids
304
187
# Set up some variables
306
189
cwd = os.getcwd()
332
204
Please hit Ctrl+C now if you do not wish to do this.
333
205
""" % (conffile, conf_hfile)
335
# Get information from the administrator
336
# If EOF is encountered at any time during the questioning, just exit
339
root_dir = query_user(root_dir,
340
"""Root directory where IVLE is located (in URL space):""")
341
ivle_install_dir = query_user(ivle_install_dir,
342
'Root directory where IVLE will be installed (on the local file '
344
jail_base = query_user(jail_base,
345
"""Root directory where the jails (containing user files) are stored
346
(on the local file system):""")
347
allowed_uids = query_user(allowed_uids,
348
"""UID of the web server process which will run IVLE.
349
Only this user may execute the trampoline. May specify multiple users as
350
a comma-separated list.
355
# Non-interactive mode. Parse the options.
356
if '--root_dir' in opts:
357
root_dir = opts['--root_dir']
358
if '--ivle_install_dir' in opts:
359
ivle_install_dir = opts['--ivle_install_dir']
360
if '--jail_base' in opts:
361
jail_base = opts['--jail_base']
362
if '--allowed_uids' in opts:
363
allowed_uids = opts['--allowed_uids']
365
# Error handling on input values
367
allowed_uids = map(int, allowed_uids.split(','))
369
print >>sys.stderr, (
370
"Invalid UID list (%s).\n"
371
"Must be a comma-separated list of integers." % allowed_uids)
207
# Get information from the administrator
208
# If EOF is encountered at any time during the questioning, just exit
211
root_dir = query_user(
212
"""Root directory where IVLE is located (in URL space):
213
(eg. "/" or "/ivle")""")
214
ivle_install_dir = query_user(
215
'Root directory where IVLE is located (on the local file system):\n'
216
'(eg. "/home/informatics/ivle")')
217
jail_base = query_user(
218
"""Root directory where user files are stored (on the local file system):
219
(eg. "/home/informatics/jails")""")
374
221
# Write www/conf/conf.py
452
# Get "dry" variable from command line
453
(opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
455
dry = '-n' in opts or '--dry' in opts
458
print "Dry run (no actions will be executed\n"
460
# Compile the trampoline
461
action_runprog('gcc', ['-Wall', '-o', 'trampoline/trampoline',
462
'trampoline/trampoline.c'], dry)
464
# Create the jail and its subdirectories
465
# Note: Other subdirs will be made by copying files
466
action_mkdir('jail', dry)
467
action_mkdir('jail/home', dry)
468
action_mkdir('jail/tmp', dry)
470
# Copy all console and operating system files into the jail
471
action_copylist(install_list.list_console, 'jail/opt/ivle', dry)
472
copy_os_files_jail(dry)
474
# Compile .py files into .pyc or .pyo files
475
compileall.compile_dir('www', quiet=True)
476
compileall.compile_dir('console', quiet=True)
480
def copy_os_files_jail(dry):
481
"""Copies necessary Operating System files from their usual locations
482
into the jail/ directory of the cwd."""
483
# Currently source paths are configured for Ubuntu.
484
copy_file_to_jail('/lib/ld-linux.so.2', dry)
485
copy_file_to_jail('/lib/tls/i686/cmov/libc.so.6', dry)
486
copy_file_to_jail('/lib/tls/i686/cmov/libdl.so.2', dry)
487
copy_file_to_jail('/lib/tls/i686/cmov/libm.so.6', dry)
488
copy_file_to_jail('/lib/tls/i686/cmov/libpthread.so.0', dry)
489
copy_file_to_jail('/lib/tls/i686/cmov/libutil.so.1', dry)
490
copy_file_to_jail('/usr/bin/python2.5', dry)
491
action_symlink('python2.5', 'jail/usr/bin/python', dry)
492
action_copytree('/usr/lib/python2.5', 'jail/usr/lib/python2.5', dry)
494
def copy_file_to_jail(src, dry):
495
"""Copies a single file from an absolute location into the same location
496
within the jail. src must begin with a '/'. The jail will be located
497
in a 'jail' subdirectory of the current path."""
498
action_copyfile(src, 'jail' + src, dry)
500
300
def install(args):
501
# Get "dry" and "nojail" variables from command line
502
(opts, args) = getopt.gnu_getopt(args, "n", ['dry', 'nojail'])
504
dry = '-n' in opts or '--dry' in opts
505
nojail = '--nojail' in opts
508
print "Dry run (no actions will be executed\n"
510
if not dry and os.geteuid() != 0:
511
print >>sys.stderr, "Must be root to run install"
512
print >>sys.stderr, "(I need to chown some files)."
515
# Create the target (install) directory
516
action_mkdir(ivle_install_dir, dry)
518
# Create bin and copy the compiled files there
519
action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
520
tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
521
action_copyfile('trampoline/trampoline', tramppath, dry)
522
# chown trampoline to root and set setuid bit
523
action_chown_setuid(tramppath, dry)
525
# Copy the www directory using the list
526
action_copylist(install_list.list_www, ivle_install_dir, dry)
529
# Copy the local jail directory built by the build action
530
# to the jails template directory (it will be used as a template
531
# for all the students' jails).
532
action_copytree('jail', os.path.join(jail_base, 'template'), dry)
536
# The actions call Python os functions but print actions and handle dryness.
537
# May still throw os exceptions if errors occur.
540
"""Represents an error when running a program (nonzero return)."""
541
def __init__(self, prog, retcode):
543
self.retcode = retcode
545
return str(self.prog) + " returned " + repr(self.retcode)
547
def action_runprog(prog, args, dry):
548
"""Runs a unix program. Searches in $PATH. Synchronous (waits for the
549
program to return). Runs in the current environment. First prints the
550
action as a "bash" line.
552
Throws a RunError with a retcode of the return value of the program,
553
if the program did not return 0.
555
prog: String. Name of the program. (No path required, if in $PATH).
556
args: [String]. Arguments to the program.
557
dry: Bool. If True, prints but does not execute.
559
print prog, string.join(args, ' ')
561
ret = os.spawnvp(os.P_WAIT, prog, args)
563
raise RunError(prog, ret)
565
def action_mkdir(path, dry):
566
"""Calls mkdir. Silently ignored if the directory already exists.
567
Creates all parent directories as necessary."""
568
print "mkdir -p", path
572
except OSError, (err, msg):
573
if err != errno.EEXIST:
576
def action_copytree(src, dst, dry):
577
"""Copies an entire directory tree. Symlinks are seen as normal files and
578
copies of the entire file (not the link) are made. Creates all parent
579
directories as necessary.
581
See shutil.copytree."""
582
if os.access(dst, os.F_OK):
585
shutil.rmtree(dst, True)
586
print "cp -r", src, dst
588
shutil.copytree(src, dst, True)
590
def action_copylist(srclist, dst, dry):
591
"""Copies all files in a list to a new location. The files in the list
592
are read relative to the current directory, and their destinations are the
593
same paths relative to dst. Creates all parent directories as necessary.
595
for srcfile in srclist:
596
dstfile = os.path.join(dst, srcfile)
597
dstdir = os.path.split(dstfile)[0]
598
if not os.path.isdir(dstdir):
599
action_mkdir(dstdir, dry)
600
print "cp -f", srcfile, dstfile
603
shutil.copyfile(srcfile, dstfile)
604
shutil.copymode(srcfile, dstfile)
608
def action_copyfile(src, dst, dry):
609
"""Copies one file to a new location. Creates all parent directories
612
dstdir = os.path.split(dst)[0]
613
if not os.path.isdir(dstdir):
614
action_mkdir(dstdir, dry)
615
print "cp -f", src, dst
618
shutil.copyfile(src, dst)
619
shutil.copymode(src, dst)
623
def action_symlink(src, dst, dry):
624
"""Creates a symlink in a given location. Creates all parent directories
627
dstdir = os.path.split(dst)[0]
628
if not os.path.isdir(dstdir):
629
action_mkdir(dstdir, dry)
630
# Delete existing file
631
if os.path.exists(dst):
633
print "ln -fs", src, dst
637
def action_chown_setuid(file, dry):
638
"""Chowns a file to root, and sets the setuid bit on the file.
639
Calling this function requires the euid to be root.
640
The actual mode of path is set to: rws--s--s
642
print "chown root:root", file
645
print "chmod a+xs", file
646
print "chmod u+rw", file
648
os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
649
| stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
651
def query_user(default, prompt):
304
def query_user(prompt):
652
305
"""Prompts the user for a string, which is read from a line of stdin.
653
306
Exits silently if EOF is encountered. Returns the string, with spaces
654
307
removed from the beginning and end.
656
Returns default if a 0-length line (after spaces removed) was read.
658
sys.stdout.write('%s\n (default: "%s")\n>' % (prompt, default))
309
sys.stdout.write(prompt)
310
sys.stdout.write("\n>")
660
312
val = sys.stdin.readline()
661
313
except KeyboardInterrupt: