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

« back to all changes in this revision

Viewing changes to setup.py

  • Committer: dcoles
  • Date: 2008-07-02 04:34:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:800
Docs: Updated docs to show debootstrap dependency and relect changes to jail 
building.

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
 
# This program is a frontend for the modules in the setup packages that 
25
 
# build and install IVLE in separate steps.
 
24
# This program configures, builds and installs IVLE in three separate steps.
26
25
# It is called with at least one argument, which specifies which operation to
27
26
# take.
28
27
 
 
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
 
 
34
# setup.py config [args]
 
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.
 
38
# Creates lib/conf/conf.py and trampoline/conf.h.
 
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 __staging__ directory (unless --nojail specified).
 
59
 
 
60
import os
 
61
import stat
 
62
import shutil
29
63
import sys
30
 
import setup.build
31
 
import setup.install
 
64
import getopt
 
65
import string
 
66
import errno
 
67
import mimetypes
 
68
import compileall
 
69
import getopt
 
70
import hashlib
 
71
import uuid
 
72
import pysvn
 
73
 
 
74
# Import modules from the website is tricky since they're in the www
 
75
# directory.
 
76
sys.path.append(os.path.join(os.getcwd(), 'lib'))
 
77
import conf
 
78
import common.makeuser
 
79
 
 
80
# Determine which Python version (2.4 or 2.5, for example) we are running,
 
81
# and use that as the filename to the Python directory.
 
82
# Just get the first 3 characters of sys.version.
 
83
PYTHON_VERSION = sys.version[0:3]
 
84
 
 
85
class ConfigOption:
 
86
    """A configuration option; one of the things written to conf.py."""
 
87
    def __init__(self, option_name, default, prompt, comment):
 
88
        """Creates a configuration option.
 
89
        option_name: Name of the variable in conf.py. Also name of the
 
90
            command-line argument to setup.py conf.
 
91
        default: Default value for this variable.
 
92
        prompt: (Short) string presented during the interactive prompt in
 
93
            setup.py conf.
 
94
        comment: (Long) comment string stored in conf.py. Each line of this
 
95
            string should begin with a '#'.
 
96
        """
 
97
        self.option_name = option_name
 
98
        self.default = default
 
99
        self.prompt = prompt
 
100
        self.comment = comment
 
101
 
 
102
# Configuration options, defaults and descriptions
 
103
config_options = []
 
104
config_options.append(ConfigOption("root_dir", "/",
 
105
    """Root directory where IVLE is located (in URL space):""",
 
106
    """
 
107
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
 
108
# with this).
 
109
# eg. "/" or "/ivle"."""))
 
110
config_options.append(ConfigOption("ivle_install_dir", "/opt/ivle",
 
111
    'Root directory where IVLE will be installed (on the local file '
 
112
    'system):',
 
113
    """
 
114
# In the local file system, where IVLE is actually installed.
 
115
# This directory should contain the "www" and "bin" directories."""))
 
116
config_options.append(ConfigOption("jail_base", "/home/informatics/jails",
 
117
    """Location of Directories
 
118
=======================
 
119
Root directory where the jails (containing user files) are stored
 
120
(on the local file system):""",
 
121
    """
 
122
# In the local file system, where are the student/user file spaces located.
 
123
# The user jails are expected to be located immediately in subdirectories of
 
124
# this location."""))
 
125
config_options.append(ConfigOption("subjects_base",
 
126
    "/home/informatics/subjects",
 
127
    """Root directory where the subject directories (containing worksheets
 
128
and other per-subject files) are stored (on the local file system):""",
 
129
    """
 
130
# In the local file system, where are the per-subject file spaces located.
 
131
# The individual subject directories are expected to be located immediately
 
132
# in subdirectories of this location."""))
 
133
config_options.append(ConfigOption("exercises_base",
 
134
    "/home/informatics/exercises",
 
135
    """Root directory where the exercise directories (containing
 
136
subject-independent exercise sheets) are stored (on the local file
 
137
system):""",
 
138
    """
 
139
# In the local file system, where are the subject-independent exercise sheet
 
140
# file spaces located."""))
 
141
config_options.append(ConfigOption("tos_path",
 
142
    "/home/informatics/tos.html",
 
143
    """Location where the Terms of Service document is stored (on the local
 
144
    file system):""",
 
145
    """
 
146
# In the local file system, where is the Terms of Service document located."""))
 
147
config_options.append(ConfigOption("motd_path",
 
148
    "/home/informatics/motd.html",
 
149
    """Location where the Message of the Day document is stored (on the local
 
150
    file system):""",
 
151
    """
 
152
# In the local file system, where is the Message of the Day document
 
153
# located. This is an HTML file (just the body fragment), which will
 
154
# be displayed on the login page. It is optional."""))
 
155
config_options.append(ConfigOption("public_host", "public.localhost",
 
156
    """Hostname which will cause the server to go into "public mode",
 
157
providing login-free access to student's published work:""",
 
158
    """
 
159
# The server goes into "public mode" if the browser sends a request with this
 
160
# host. This is for security reasons - we only serve public student files on a
 
161
# separate domain to the main IVLE site.
 
162
# Public mode does not use cookies, and serves only public content.
 
163
# Private mode (normal mode) requires login, and only serves files relevant to
 
164
# the logged-in user."""))
 
165
config_options.append(ConfigOption("allowed_uids", "33",
 
166
    """UID of the web server process which will run IVLE.
 
167
Only this user may execute the trampoline. May specify multiple users as
 
168
a comma-separated list.
 
169
    (eg. "1002,78")""",
 
170
    """
 
171
# The User-ID of the web server process which will run IVLE, and any other
 
172
# users who are allowed to run the trampoline. This is stores as a string of
 
173
# comma-separated integers, simply because it is not used within Python, only
 
174
# used by the setup program to write to conf.h (see setup.py config)."""))
 
175
config_options.append(ConfigOption("db_host", "localhost",
 
176
    """PostgreSQL Database config
 
177
==========================
 
178
Hostname of the DB server:""",
 
179
    """
 
180
### PostgreSQL Database config ###
 
181
# Database server hostname"""))
 
182
config_options.append(ConfigOption("db_port", "5432",
 
183
    """Port of the DB server:""",
 
184
    """
 
185
# Database server port"""))
 
186
config_options.append(ConfigOption("db_dbname", "ivle",
 
187
    """Database name:""",
 
188
    """
 
189
# Database name"""))
 
190
config_options.append(ConfigOption("db_forumdbname", "ivle_forum",
 
191
    """Forum Database name:""",
 
192
    """
 
193
# Forum Database name"""))
 
194
config_options.append(ConfigOption("db_user", "postgres",
 
195
    """Username for DB server login:""",
 
196
    """
 
197
# Database username"""))
 
198
config_options.append(ConfigOption("db_password", "",
 
199
    """Password for DB server login:
 
200
    (Caution: This password is stored in plaintext in lib/conf/conf.py)""",
 
201
    """
 
202
# Database password"""))
 
203
config_options.append(ConfigOption("auth_modules", "ldap_auth",
 
204
    """Authentication config
 
205
=====================
 
206
Comma-separated list of authentication modules. Only "ldap" is available
 
207
by default.""",
 
208
    """
 
209
# Comma-separated list of authentication modules.
 
210
# These refer to importable Python modules in the www/auth directory.
 
211
# Modules "ldap" and "guest" are available in the source tree, but
 
212
# other modules may be plugged in to auth against organisation-specific
 
213
# auth backends."""))
 
214
config_options.append(ConfigOption("ldap_url", "ldaps://www.example.com",
 
215
    """(LDAP options are only relevant if "ldap" is included in the list of
 
216
auth modules).
 
217
URL for LDAP authentication server:""",
 
218
    """
 
219
# URL for LDAP authentication server"""))
 
220
config_options.append(ConfigOption("ldap_format_string",
 
221
    "uid=%s,ou=users,o=example",
 
222
    """Format string for LDAP auth request:
 
223
    (Must contain a single "%s" for the user's login name)""",
 
224
    """
 
225
# Format string for LDAP auth request
 
226
# (Must contain a single "%s" for the user's login name)"""))
 
227
config_options.append(ConfigOption("svn_addr", "http://svn.localhost/",
 
228
    """Subversion config
 
229
=================
 
230
The base url for accessing subversion repositories:""",
 
231
    """
 
232
# The base url for accessing subversion repositories."""))
 
233
config_options.append(ConfigOption("svn_conf", "/opt/ivle/svn/svn.conf",
 
234
    """The location of the subversion configuration file used by apache
 
235
to host the user repositories:""",
 
236
    """
 
237
# The location of the subversion configuration file used by
 
238
# apache to host the user repositories."""))
 
239
config_options.append(ConfigOption("svn_repo_path", "/home/informatics/repositories",
 
240
    """The root directory for the subversion repositories:""",
 
241
    """
 
242
# The root directory for the subversion repositories."""))
 
243
config_options.append(ConfigOption("svn_auth_ivle", "/opt/ivle/svn/ivle.auth",
 
244
    """The location of the password file used to authenticate users
 
245
of the subversion repository from the ivle server:""",
 
246
    """
 
247
# The location of the password file used to authenticate users
 
248
# of the subversion repository from the ivle server."""))
 
249
config_options.append(ConfigOption("svn_auth_local", "/opt/ivle/svn/local.auth",
 
250
    """The location of the password file used to authenticate local users
 
251
of the subversion repository:""",
 
252
    """
 
253
# The location of the password file used to authenticate local users
 
254
# of the subversion repository."""))
 
255
config_options.append(ConfigOption("usrmgt_host", "localhost",
 
256
    """User Management Server config
 
257
============================
 
258
The hostname where the usrmgt-server runs:""",
 
259
    """
 
260
# The hostname where the usrmgt-server runs."""))
 
261
config_options.append(ConfigOption("usrmgt_port", "2178",
 
262
    """The port where the usrmgt-server runs:""",
 
263
    """
 
264
# The port where the usrmgt-server runs."""))
 
265
config_options.append(ConfigOption("usrmgt_magic", "",
 
266
    """The password for the usrmgt-server:""",
 
267
    """
 
268
# The password for the usrmgt-server."""))
 
269
 
 
270
# Try importing existing conf, but if we can't just set up defaults
 
271
# The reason for this is that these settings are used by other phases
 
272
# of setup besides conf, so we need to know them.
 
273
# Also this allows you to hit Return to accept the existing value.
 
274
try:
 
275
    confmodule = __import__("lib/conf/conf")
 
276
    for opt in config_options:
 
277
        try:
 
278
            globals()[opt.option_name] = confmodule.__dict__[opt.option_name]
 
279
        except:
 
280
            globals()[opt.option_name] = opt.default
 
281
except ImportError:
 
282
    # Just set reasonable defaults
 
283
    for opt in config_options:
 
284
        globals()[opt.option_name] = opt.default
 
285
 
 
286
# Try importing install_list, but don't fail if we can't, because listmake can
 
287
# function without it.
 
288
try:
 
289
    import install_list
 
290
except:
 
291
    pass
 
292
 
 
293
# Mime types which will automatically be placed in the list by listmake.
 
294
# Note that listmake is not intended to be run by the final user (the system
 
295
# administrator who installs this), so the developers can customize the list
 
296
# as necessary, and include it in the distribution.
 
297
listmake_mimetypes = ['text/x-python', 'text/html',
 
298
    'application/x-javascript', 'application/javascript',
 
299
    'text/css', 'image/png', 'image/gif', 'application/xml']
 
300
 
 
301
# Main function skeleton from Guido van Rossum
 
302
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
32
303
 
33
304
def main(argv=None):
34
305
    if argv is None:
37
308
    # Print the opening spiel including the GPL notice
38
309
 
39
310
    print """IVLE - Informatics Virtual Learning Environment Setup
40
 
Copyright (C) 2007-2009 The University of Melbourne
 
311
Copyright (C) 2007-2008 The University of Melbourne
41
312
IVLE comes with ABSOLUTELY NO WARRANTY.
42
313
This is free software, and you are welcome to redistribute it
43
314
under certain conditions. See LICENSE.txt for details.
53
324
        help([])
54
325
        return 1
55
326
 
56
 
    oper_func = call_operator(operation)
57
 
    return oper_func(argv[2:])
58
 
 
59
 
def help(args):
60
 
    if len(args)!=1:
61
 
        print """Usage: python setup.py operation [options]
62
 
Operation can be:
63
 
    help [operation]
64
 
    build
65
 
    install
66
 
 
67
 
    For help and options for a specific operation use 'help [operation]'."""
68
 
    else:
69
 
        operator = args[0]
70
 
        oper_func = call_operator(operator)
71
 
        oper_func(['operator','--help'])
72
 
 
73
 
def call_operator(operation):
 
327
    # Disallow run as root unless installing
 
328
    if (operation != 'install' and operation != 'updatejails' and operation != 
 
329
    'build'
 
330
        and os.geteuid() == 0):
 
331
        print >>sys.stderr, "I do not want to run this stage as root."
 
332
        print >>sys.stderr, "Please run as a normal user."
 
333
        return 1
74
334
    # Call the requested operation's function
75
335
    try:
76
336
        oper_func = {
77
337
            'help' : help,
78
 
            'build' : setup.build.build,
79
 
            'install' : setup.install.install,
 
338
            'config' : conf,
 
339
            'build' : build,
 
340
            'listmake' : listmake,
 
341
            'install' : install,
 
342
            'updatejails' : updatejails,
80
343
        }[operation]
81
344
    except KeyError:
82
345
        print >>sys.stderr, (
83
346
            """Invalid operation '%s'. Try python setup.py help."""
84
347
            % operation)
85
 
        sys.exit(1)
86
 
    return oper_func
 
348
        return 1
 
349
    return oper_func(argv[2:])
 
350
 
 
351
# Operation functions
 
352
 
 
353
def help(args):
 
354
    if args == []:
 
355
        print """Usage: python setup.py operation [args]
 
356
Operation (and args) can be:
 
357
    help [operation]
 
358
    listmake (developer use only)
 
359
    config [args]
 
360
    build
 
361
    install [--nojail] [--nosubjects] [-n|--dry]
 
362
"""
 
363
        return 1
 
364
    elif len(args) != 1:
 
365
        print """Usage: python setup.py help [operation]"""
 
366
        return 2
 
367
    else:
 
368
        operation = args[0]
 
369
 
 
370
    if operation == 'help':
 
371
        print """python setup.py help [operation]
 
372
Prints the usage message or detailed help on an operation, then exits."""
 
373
    elif operation == 'listmake':
 
374
        print """python setup.py listmake
 
375
(For developer use only)
 
376
Recurses through the source tree and builds a list of all files which should
 
377
be copied upon installation. This should be run by the developer before
 
378
cutting a distribution, and the listfile it generates should be included in
 
379
the distribution, avoiding the administrator having to run it."""
 
380
    elif operation == 'config':
 
381
        print """python setup.py config [args]
 
382
Configures IVLE with machine-specific details, most notably, various paths.
 
383
Either prompts the administrator for these details or accepts them as
 
384
command-line args. Will be interactive only if there are no arguments given.
 
385
Takes defaults from existing conf file if it exists.
 
386
 
 
387
To run IVLE out of the source directory (allowing development without having
 
388
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
 
389
directory, and run build/install one time.
 
390
 
 
391
Creates lib/conf/conf.py and trampoline/conf.h.
 
392
 
 
393
Args are:"""
 
394
        for opt in config_options:
 
395
            print "    --" + opt.option_name
 
396
        print """As explained in the interactive prompt or conf.py.
 
397
"""
 
398
    elif operation == 'build':
 
399
        print """python -O setup.py build [--dry|-n]
 
400
Compiles all files and sets up a jail template in the source directory.
 
401
-O is recommended to cause compilation to be optimised.
 
402
Details:
 
403
Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
 
404
Creates jail/.
 
405
Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
 
406
Copies console/ to a location within the jail.
 
407
Copies OS programs and files to corresponding locations within the jail
 
408
  (eg. python and Python libs, ld.so, etc).
 
409
Generates .pyc or .pyo files for all the IVLE .py files.
 
410
 
 
411
--dry | -n  Print out the actions but don't do anything."""
 
412
    elif operation == 'install':
 
413
        print """sudo python setup.py install [--nojail] [--nosubjects][--dry|-n]
 
414
(Requires root)
 
415
Create target install directory ($target).
 
416
Create $target/bin.
 
417
Copy trampoline/trampoline to $target/bin.
 
418
chown and chmod the installed trampoline.
 
419
Copy www/ to $target.
 
420
Copy jail/ to jails __staging__ directory (unless --nojail specified).
 
421
Copy subjects/ to subjects directory (unless --nosubjects specified).
 
422
 
 
423
--nojail        Do not copy the jail.
 
424
--nosubjects    Do not copy the subjects and exercises directories.
 
425
--dry | -n  Print out the actions but don't do anything."""
 
426
    elif operation == 'updatejails':
 
427
        print """sudo python setup.py updatejails [--dry|-n]
 
428
(Requires root)
 
429
Copy jail/ to each subdirectory in jails directory.
 
430
 
 
431
--dry | -n  Print out the actions but don't do anything."""
 
432
    else:
 
433
        print >>sys.stderr, (
 
434
            """Invalid operation '%s'. Try python setup.py help."""
 
435
            % operation)
 
436
    return 1
 
437
 
 
438
def listmake(args):
 
439
    # We build two separate lists, by walking www and console
 
440
    list_www = build_list_py_files('www')
 
441
    list_lib = build_list_py_files('lib')
 
442
    list_subjects = build_list_py_files('subjects', no_top_level=True)
 
443
    list_exercises = build_list_py_files('exercises', no_top_level=True)
 
444
    list_scripts = [
 
445
        "scripts/python-console",
 
446
        "scripts/fileservice",
 
447
        "scripts/serveservice",
 
448
        "scripts/usrmgt-server",
 
449
        "scripts/diffservice",
 
450
    ]
 
451
    # Make sure that the files generated by conf are in the list
 
452
    # (since listmake is typically run before conf)
 
453
    if "lib/conf/conf.py" not in list_lib:
 
454
        list_lib.append("lib/conf/conf.py")
 
455
    # Write these out to a file
 
456
    cwd = os.getcwd()
 
457
    # the files that will be created/overwritten
 
458
    listfile = os.path.join(cwd, "install_list.py")
 
459
 
 
460
    try:
 
461
        file = open(listfile, "w")
 
462
 
 
463
        file.write("""# IVLE Configuration File
 
464
# install_list.py
 
465
# Provides lists of all files to be installed by `setup.py install' from
 
466
# certain directories.
 
467
# Note that any files with the given filename plus 'c' or 'o' (that is,
 
468
# compiled .pyc or .pyo files) will be copied as well.
 
469
 
 
470
# List of all installable files in www directory.
 
471
list_www = """)
 
472
        writelist_pretty(file, list_www)
 
473
        file.write("""
 
474
# List of all installable files in lib directory.
 
475
list_lib = """)
 
476
        writelist_pretty(file, list_lib)
 
477
        file.write("""
 
478
# List of all installable files in scripts directory.
 
479
list_scripts = """)
 
480
        writelist_pretty(file, list_scripts)
 
481
        file.write("""
 
482
# List of all installable files in subjects directory.
 
483
# This is to install sample subjects and material.
 
484
list_subjects = """)
 
485
        writelist_pretty(file, list_subjects)
 
486
        file.write("""
 
487
# List of all installable files in exercises directory.
 
488
# This is to install sample exercise material.
 
489
list_exercises = """)
 
490
        writelist_pretty(file, list_exercises)
 
491
 
 
492
        file.close()
 
493
    except IOError, (errno, strerror):
 
494
        print "IO error(%s): %s" % (errno, strerror)
 
495
        sys.exit(1)
 
496
 
 
497
    print "Successfully wrote install_list.py"
 
498
 
 
499
    print
 
500
    print ("You may modify the set of installable files before cutting the "
 
501
            "distribution:")
 
502
    print listfile
 
503
    print
 
504
 
 
505
    return 0
 
506
 
 
507
def build_list_py_files(dir, no_top_level=False):
 
508
    """Builds a list of all py files found in a directory and its
 
509
    subdirectories. Returns this as a list of strings.
 
510
    no_top_level=True means the file paths will not include the top-level
 
511
    directory.
 
512
    """
 
513
    pylist = []
 
514
    for (dirpath, dirnames, filenames) in os.walk(dir):
 
515
        # Exclude directories beginning with a '.' (such as '.svn')
 
516
        filter_mutate(lambda x: x[0] != '.', dirnames)
 
517
        # All *.py files are added to the list
 
518
        pylist += [os.path.join(dirpath, item) for item in filenames
 
519
            if mimetypes.guess_type(item)[0] in listmake_mimetypes]
 
520
    if no_top_level:
 
521
        for i in range(0, len(pylist)):
 
522
            _, pylist[i] = pylist[i].split(os.sep, 1)
 
523
    return pylist
 
524
 
 
525
def writelist_pretty(file, list):
 
526
    """Writes a list one element per line, to a file."""
 
527
    if list == []:
 
528
        file.write("[]\n")
 
529
    else:
 
530
        file.write('[\n')
 
531
        for elem in list:
 
532
            file.write('    %s,\n' % repr(elem))
 
533
        file.write(']\n')
 
534
 
 
535
def conf(args):
 
536
    global db_port, usrmgt_port
 
537
    # Set up some variables
 
538
 
 
539
    cwd = os.getcwd()
 
540
    # the files that will be created/overwritten
 
541
    conffile = os.path.join(cwd, "lib/conf/conf.py")
 
542
    jailconffile = os.path.join(cwd, "lib/conf/jailconf.py")
 
543
    conf_hfile = os.path.join(cwd, "trampoline/conf.h")
 
544
    phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
 
545
    usrmgtserver_initdfile = os.path.join(cwd, "doc/setup/usrmgt-server.init")
 
546
 
 
547
    # Get command-line arguments to avoid asking questions.
 
548
 
 
549
    optnames = []
 
550
    for opt in config_options:
 
551
        optnames.append(opt.option_name + "=")
 
552
    (opts, args) = getopt.gnu_getopt(args, "", optnames)
 
553
 
 
554
    if args != []:
 
555
        print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
 
556
        return 2
 
557
 
 
558
    if opts == []:
 
559
        # Interactive mode. Prompt the user for all the values.
 
560
 
 
561
        print """This tool will create the following files:
 
562
    %s
 
563
    %s
 
564
    %s
 
565
    %s
 
566
    %s
 
567
prompting you for details about your configuration. The file will be
 
568
overwritten if it already exists. It will *not* install or deploy IVLE.
 
569
 
 
570
Please hit Ctrl+C now if you do not wish to do this.
 
571
""" % (conffile, jailconffile, conf_hfile, phpBBconffile, usrmgtserver_initdfile)
 
572
 
 
573
        # Get information from the administrator
 
574
        # If EOF is encountered at any time during the questioning, just exit
 
575
        # silently
 
576
 
 
577
        for opt in config_options:
 
578
            globals()[opt.option_name] = \
 
579
                query_user(globals()[opt.option_name], opt.prompt)
 
580
    else:
 
581
        opts = dict(opts)
 
582
        # Non-interactive mode. Parse the options.
 
583
        for opt in config_options:
 
584
            if '--' + opt.option_name in opts:
 
585
                globals()[opt.option_name] = opts['--' + opt.option_name]
 
586
 
 
587
    # Error handling on input values
 
588
    try:
 
589
        allowed_uids_list = map(int, allowed_uids.split(','))
 
590
    except ValueError:
 
591
        print >>sys.stderr, (
 
592
        "Invalid UID list (%s).\n"
 
593
        "Must be a comma-separated list of integers." % allowed_uids)
 
594
        return 1
 
595
    try:
 
596
        db_port = int(db_port)
 
597
        if db_port < 0 or db_port >= 65536: raise ValueError()
 
598
    except ValueError:
 
599
        print >>sys.stderr, (
 
600
        "Invalid DB port (%s).\n"
 
601
        "Must be an integer between 0 and 65535." % repr(db_port))
 
602
        return 1
 
603
    try:
 
604
        usrmgt_port = int(usrmgt_port)
 
605
        if usrmgt_port < 0 or usrmgt_port >= 65536: raise ValueError()
 
606
    except ValueError:
 
607
        print >>sys.stderr, (
 
608
        "Invalid user management port (%s).\n"
 
609
        "Must be an integer between 0 and 65535." % repr(usrmgt_port))
 
610
        return 1
 
611
 
 
612
    # Generate the forum secret
 
613
    forum_secret = hashlib.md5(uuid.uuid4().bytes).hexdigest()
 
614
 
 
615
    # Write lib/conf/conf.py
 
616
 
 
617
    try:
 
618
        conf = open(conffile, "w")
 
619
 
 
620
        conf.write("""# IVLE Configuration File
 
621
# conf.py
 
622
# Miscellaneous application settings
 
623
 
 
624
""")
 
625
        for opt in config_options:
 
626
            conf.write('%s\n%s = %s\n' % (opt.comment, opt.option_name,
 
627
                repr(globals()[opt.option_name])))
 
628
 
 
629
        # Add the forum secret to the config file (regenerated each config)
 
630
        conf.write('forum_secret = "%s"\n' % (forum_secret))
 
631
 
 
632
        conf.close()
 
633
    except IOError, (errno, strerror):
 
634
        print "IO error(%s): %s" % (errno, strerror)
 
635
        sys.exit(1)
 
636
 
 
637
    print "Successfully wrote lib/conf/conf.py"
 
638
 
 
639
    # Write conf/jailconf.py
 
640
 
 
641
    try:
 
642
        conf = open(jailconffile, "w")
 
643
 
 
644
        # In the "in-jail" version of conf, we don't need MOST of the details
 
645
        # (it would be a security risk to have them here).
 
646
        # So we just write root_dir, and jail_base is "/".
 
647
        # (jail_base being "/" means "jail-relative" paths are relative to "/"
 
648
        # when inside the jail.)
 
649
        conf.write("""# IVLE Configuration File
 
650
# conf.py
 
651
# Miscellaneous application settings
 
652
# (User jail version)
 
653
 
 
654
 
 
655
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
 
656
# with this).
 
657
# eg. "/" or "/ivle".
 
658
root_dir = %s
 
659
 
 
660
# In the local file system, where are the student/user file spaces located.
 
661
# The user jails are expected to be located immediately in subdirectories of
 
662
# this location.
 
663
jail_base = '/'
 
664
 
 
665
# The hostname for serving publicly accessible pages
 
666
public_host = %s
 
667
""" % (repr(root_dir),repr(public_host)))
 
668
 
 
669
        conf.close()
 
670
    except IOError, (errno, strerror):
 
671
        print "IO error(%s): %s" % (errno, strerror)
 
672
        sys.exit(1)
 
673
 
 
674
    print "Successfully wrote lib/conf/jailconf.py"
 
675
 
 
676
    # Write trampoline/conf.h
 
677
 
 
678
    try:
 
679
        conf = open(conf_hfile, "w")
 
680
 
 
681
        conf.write("""/* IVLE Configuration File
 
682
 * conf.h
 
683
 * Administrator settings required by trampoline.
 
684
 * Note: trampoline will have to be rebuilt in order for changes to this file
 
685
 * to take effect.
 
686
 */
 
687
 
 
688
/* In the local file system, where are the jails located.
 
689
 * The trampoline does not allow the creation of a jail anywhere besides
 
690
 * jail_base or a subdirectory of jail_base.
 
691
 */
 
692
static const char* jail_base = "%s";
 
693
 
 
694
/* Which user IDs are allowed to run the trampoline.
 
695
 * This list should be limited to the web server user.
 
696
 * (Note that root is an implicit member of this list).
 
697
 */
 
698
static const int allowed_uids[] = { %s };
 
699
""" % (repr(jail_base)[1:-1], repr(allowed_uids_list)[1:-1]))
 
700
    # Note: The above uses PYTHON reprs, not C reprs
 
701
    # However they should be the same with the exception of the outer
 
702
    # characters, which are stripped off and replaced
 
703
 
 
704
        conf.close()
 
705
    except IOError, (errno, strerror):
 
706
        print "IO error(%s): %s" % (errno, strerror)
 
707
        sys.exit(1)
 
708
 
 
709
    print "Successfully wrote trampoline/conf.h"
 
710
 
 
711
    # Write www/php/phpBB3/config.php
 
712
 
 
713
    try:
 
714
        conf = open(phpBBconffile, "w")
 
715
        
 
716
        # php-pg work around
 
717
        if db_host == 'localhost':
 
718
            forumdb_host = '127.0.0.1'
 
719
        else:
 
720
            forumdb_host = db_host
 
721
 
 
722
        conf.write( """<?php
 
723
// phpBB 3.0.x auto-generated configuration file
 
724
// Do not change anything in this file!
 
725
$dbms = 'postgres';
 
726
$dbhost = '""" + forumdb_host + """';
 
727
$dbport = '""" + str(db_port) + """';
 
728
$dbname = '""" + db_forumdbname + """';
 
729
$dbuser = '""" + db_user + """';
 
730
$dbpasswd = '""" + db_password + """';
 
731
 
 
732
$table_prefix = 'phpbb_';
 
733
$acm_type = 'file';
 
734
$load_extensions = '';
 
735
@define('PHPBB_INSTALLED', true);
 
736
// @define('DEBUG', true);
 
737
//@define('DEBUG_EXTRA', true);
 
738
 
 
739
$forum_secret = '""" + forum_secret +"""';
 
740
?>"""   )
 
741
    
 
742
        conf.close()
 
743
    except IOError, (errno, strerror):
 
744
        print "IO error(%s): %s" % (errno, strerror)
 
745
        sys.exit(1)
 
746
 
 
747
    print "Successfully wrote www/php/phpBB3/config.php"
 
748
 
 
749
    # Write lib/conf/usrmgt-server.init
 
750
 
 
751
    try:
 
752
        conf = open(usrmgtserver_initdfile, "w")
 
753
 
 
754
        conf.write( '''#! /bin/sh
 
755
 
 
756
# Works for Ubuntu. Check before using on other distributions
 
757
 
 
758
### BEGIN INIT INFO
 
759
# Provides:          usrmgt-server
 
760
# Required-Start:    $syslog $networking $urandom
 
761
# Required-Stop:     $syslog
 
762
# Default-Start:     2 3 4 5
 
763
# Default-Stop:      1
 
764
# Short-Description: IVLE user management server
 
765
# Description:       Daemon connecting to the IVLE user management database.
 
766
### END INIT INFO
 
767
 
 
768
PATH=/sbin:/bin:/usr/sbin:/usr/bin
 
769
DESC="IVLE user management server"
 
770
NAME=usrmgt-server
 
771
DAEMON=/opt/ivle/scripts/$NAME
 
772
DAEMON_ARGS="''' + str(usrmgt_port) + ''' ''' + usrmgt_magic + '''"
 
773
PIDFILE=/var/run/$NAME.pid
 
774
SCRIPTNAME=/etc/init.d/usrmgt-server
 
775
 
 
776
# Exit if the daemon does not exist 
 
777
test -f $DAEMON || exit 0
 
778
 
 
779
# Load the VERBOSE setting and other rcS variables
 
780
[ -f /etc/default/rcS ] && . /etc/default/rcS
 
781
 
 
782
# Define LSB log_* functions.
 
783
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
 
784
. /lib/lsb/init-functions
 
785
 
 
786
#
 
787
# Function that starts the daemon/service
 
788
#
 
789
do_start()
 
790
{
 
791
        # Return
 
792
        #   0 if daemon has been started
 
793
        #   1 if daemon was already running
 
794
        #   2 if daemon could not be started
 
795
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
 
796
                || return 1
 
797
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
 
798
                $DAEMON_ARGS \
 
799
                || return 2
 
800
        # Add code here, if necessary, that waits for the process to be ready
 
801
        # to handle requests from services started subsequently which depend
 
802
        # on this one.  As a last resort, sleep for some time.
 
803
}
 
804
 
 
805
#
 
806
# Function that stops the daemon/service
 
807
#
 
808
do_stop()
 
809
{
 
810
        # Return
 
811
        #   0 if daemon has been stopped
 
812
        #   1 if daemon was already stopped
 
813
        #   2 if daemon could not be stopped
 
814
        #   other if a failure occurred
 
815
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
 
816
        RETVAL="$?"
 
817
        [ "$RETVAL" = 2 ] && return 2
 
818
        # Wait for children to finish too if this is a daemon that forks
 
819
        # and if the daemon is only ever run from this initscript.
 
820
        # If the above conditions are not satisfied then add some other code
 
821
        # that waits for the process to drop all resources that could be
 
822
        # needed by services started subsequently.  A last resort is to
 
823
        # sleep for some time.
 
824
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
 
825
        [ "$?" = 2 ] && return 2
 
826
        # Many daemons don't delete their pidfiles when they exit.
 
827
        rm -f $PIDFILE
 
828
        return "$RETVAL"
 
829
}
 
830
 
 
831
#
 
832
# Function that sends a SIGHUP to the daemon/service
 
833
#
 
834
do_reload() {
 
835
        #
 
836
        # If the daemon can reload its configuration without
 
837
        # restarting (for example, when it is sent a SIGHUP),
 
838
        # then implement that here.
 
839
        #
 
840
        start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
 
841
        return 0
 
842
}
 
843
 
 
844
case "$1" in
 
845
  start)
 
846
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
 
847
        do_start
 
848
        case "$?" in
 
849
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
 
850
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
 
851
        esac
 
852
        ;;
 
853
  stop)
 
854
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
 
855
        do_stop
 
856
        case "$?" in
 
857
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
 
858
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
 
859
        esac
 
860
        ;;
 
861
  #reload|force-reload)
 
862
        #
 
863
        # If do_reload() is not implemented then leave this commented out
 
864
        # and leave 'force-reload' as an alias for 'restart'.
 
865
        #
 
866
        #log_daemon_msg "Reloading $DESC" "$NAME"
 
867
        #do_reload
 
868
        #log_end_msg $?
 
869
        #;;
 
870
  restart|force-reload)
 
871
        #
 
872
        # If the "reload" option is implemented then remove the
 
873
        # 'force-reload' alias
 
874
        #
 
875
        log_daemon_msg "Restarting $DESC" "$NAME"
 
876
        do_stop
 
877
        case "$?" in
 
878
          0|1)
 
879
                do_start
 
880
                case "$?" in
 
881
                        0) log_end_msg 0 ;;
 
882
                        1) log_end_msg 1 ;; # Old process is still running
 
883
                        *) log_end_msg 1 ;; # Failed to start
 
884
                esac
 
885
                ;;
 
886
          *)
 
887
                # Failed to stop
 
888
                log_end_msg 1
 
889
                ;;
 
890
        esac
 
891
        ;;
 
892
  *)
 
893
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
 
894
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
 
895
        exit 3
 
896
        ;;
 
897
esac
 
898
 
 
899
:
 
900
''')
 
901
        
 
902
        conf.close()
 
903
    except IOError, (errno, strerror):
 
904
        print "IO error(%s): %s" % (errno, strerror)
 
905
        sys.exit(1)
 
906
 
 
907
    # fix permissions as the file contains the database password
 
908
    try:
 
909
        os.chmod('doc/setup/usrmgt-server.init', 0600)
 
910
    except OSError, (errno, strerror):
 
911
        print "WARNING: Couldn't chmod doc/setup/usrmgt-server.init:"
 
912
        print "OS error(%s): %s" % (errno, strerror)
 
913
 
 
914
    print "Successfully wrote lib/conf/usrmgt-server.init"
 
915
 
 
916
    print
 
917
    print "You may modify the configuration at any time by editing"
 
918
    print conffile
 
919
    print jailconffile
 
920
    print conf_hfile
 
921
    print phpBBconffile
 
922
    print usrmgtserver_initdfile
 
923
    print
 
924
    return 0
 
925
 
 
926
def build(args):
 
927
    # Get "dry" variable from command line
 
928
    (opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
 
929
    opts = dict(opts)
 
930
    dry = '-n' in opts or '--dry' in opts
 
931
 
 
932
    if dry:
 
933
        print "Dry run (no actions will be executed\n"
 
934
    
 
935
    if not dry and os.geteuid() != 0:
 
936
        print >>sys.stderr, "Must be root to run build"
 
937
        print >>sys.stderr, "(I need to chroot)."
 
938
        return 1
 
939
    
 
940
    # Find out the revison number
 
941
    revnum = get_svn_revision()
 
942
    print "Building Revision %s"%str(revnum)
 
943
    if not dry:
 
944
        vfile = open('BUILD-VERSION','w')
 
945
        vfile.write(str(revnum) + '\n')
 
946
        vfile.close()
 
947
 
 
948
    # Compile the trampoline
 
949
    curdir = os.getcwd()
 
950
    os.chdir('trampoline')
 
951
    action_runprog('make', [], dry)
 
952
    os.chdir(curdir)
 
953
 
 
954
    # Create the jail and its subdirectories
 
955
    # Note: Other subdirs will be made by copying files
 
956
    action_runprog('./buildjail.sh', [], dry)
 
957
 
 
958
    # Copy all console and operating system files into the jail
 
959
    action_copylist(install_list.list_scripts, 'jail/opt/ivle', dry)
 
960
    # Chmod the python console
 
961
    action_chmod_x('jail/opt/ivle/scripts/python-console', dry)
 
962
    action_chmod_x('jail/opt/ivle/scripts/fileservice', dry)
 
963
    action_chmod_x('jail/opt/ivle/scripts/serveservice', dry)
 
964
    
 
965
    # Also copy the IVLE lib directory into the jail
 
966
    # This is necessary for running certain scripts
 
967
    action_copylist(install_list.list_lib, 'jail/opt/ivle', dry)
 
968
    # IMPORTANT: The file jail/opt/ivle/lib/conf/conf.py contains details
 
969
    # which could compromise security if left in the jail (such as the DB
 
970
    # password).
 
971
    # The "safe" version is in jailconf.py. Delete conf.py and replace it with
 
972
    # jailconf.py.
 
973
    action_copyfile('lib/conf/jailconf.py',
 
974
        'jail/opt/ivle/lib/conf/conf.py', dry)
 
975
 
 
976
    # Compile .py files into .pyc or .pyo files
 
977
    compileall.compile_dir('www', quiet=True)
 
978
    compileall.compile_dir('lib', quiet=True)
 
979
    compileall.compile_dir('scripts', quiet=True)
 
980
    compileall.compile_dir('jail/opt/ivle/lib', quiet=True)
 
981
 
 
982
    # Set up ivle.pth inside the jail
 
983
    # Need to set /opt/ivle/lib to be on the import path
 
984
    ivle_pth = \
 
985
        "jail/usr/lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION
 
986
    f = open(ivle_pth, 'w')
 
987
    f.write('/opt/ivle/lib\n')
 
988
    f.close()
 
989
 
 
990
    return 0
 
991
 
 
992
def copy_file_to_jail(src, dry):
 
993
    """Copies a single file from an absolute location into the same location
 
994
    within the jail. src must begin with a '/'. The jail will be located
 
995
    in a 'jail' subdirectory of the current path."""
 
996
    action_copyfile(src, 'jail' + src, dry)
 
997
 
 
998
def install(args):
 
999
    # Get "dry" and "nojail" variables from command line
 
1000
    (opts, args) = getopt.gnu_getopt(args, "n",
 
1001
        ['dry', 'nojail', 'nosubjects'])
 
1002
    opts = dict(opts)
 
1003
    dry = '-n' in opts or '--dry' in opts
 
1004
    nojail = '--nojail' in opts
 
1005
    nosubjects = '--nosubjects' in opts
 
1006
 
 
1007
    if dry:
 
1008
        print "Dry run (no actions will be executed\n"
 
1009
 
 
1010
    if not dry and os.geteuid() != 0:
 
1011
        print >>sys.stderr, "Must be root to run install"
 
1012
        print >>sys.stderr, "(I need to chown some files)."
 
1013
        return 1
 
1014
 
 
1015
    # Create the target (install) directory
 
1016
    action_mkdir(ivle_install_dir, dry)
 
1017
 
 
1018
    # Create bin and copy the compiled files there
 
1019
    action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
 
1020
    tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
 
1021
    action_copyfile('trampoline/trampoline', tramppath, dry)
 
1022
    # chown trampoline to root and set setuid bit
 
1023
    action_chown_setuid(tramppath, dry)
 
1024
 
 
1025
    # Create a scripts directory to put the usrmgt-server in.
 
1026
    action_mkdir(os.path.join(ivle_install_dir, 'scripts'), dry)
 
1027
    usrmgtpath = os.path.join(ivle_install_dir, 'scripts/usrmgt-server')
 
1028
    action_copyfile('scripts/usrmgt-server', usrmgtpath, dry)
 
1029
    action_chmod_x(usrmgtpath, dry)
 
1030
 
 
1031
    # Copy the www and lib directories using the list
 
1032
    action_copylist(install_list.list_www, ivle_install_dir, dry)
 
1033
    action_copylist(install_list.list_lib, ivle_install_dir, dry)
 
1034
    
 
1035
    # Copy the php directory
 
1036
    forum_dir = "www/php/phpBB3"
 
1037
    forum_path = os.path.join(ivle_install_dir, forum_dir)
 
1038
    action_copytree(forum_dir, forum_path, dry)
 
1039
    print "chown -R www-data:www-data %s" % forum_path
 
1040
    if not dry:
 
1041
        os.system("chown -R www-data:www-data %s" % forum_path)
 
1042
 
 
1043
    if not nojail:
 
1044
        # Copy the local jail directory built by the build action
 
1045
        # to the jails __staging__ directory (it will be used to help build
 
1046
        # all the students' jails).
 
1047
        action_copytree('jail', os.path.join(jail_base, '__staging__'), dry)
 
1048
    if not nosubjects:
 
1049
        # Copy the subjects and exercises directories across
 
1050
        action_copylist(install_list.list_subjects, subjects_base, dry,
 
1051
            srcdir="./subjects")
 
1052
        action_copylist(install_list.list_exercises, exercises_base, dry,
 
1053
            srcdir="./exercises")
 
1054
 
 
1055
    # Append IVLE path to ivle.pth in python site packages
 
1056
    # (Unless it's already there)
 
1057
    ivle_pth = os.path.join(sys.prefix,
 
1058
        "lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION)
 
1059
    ivle_www = os.path.join(ivle_install_dir, "www")
 
1060
    ivle_lib = os.path.join(ivle_install_dir, "lib")
 
1061
    write_ivle_pth = True
 
1062
    write_ivle_lib_pth = True
 
1063
    try:
 
1064
        file = open(ivle_pth, 'r')
 
1065
        for line in file:
 
1066
            if line.strip() == ivle_www:
 
1067
                write_ivle_pth = False
 
1068
            elif line.strip() == ivle_lib:
 
1069
                write_ivle_lib_pth = False
 
1070
        file.close()
 
1071
    except (IOError, OSError):
 
1072
        pass
 
1073
    if write_ivle_pth:
 
1074
        action_append(ivle_pth, ivle_www)
 
1075
    if write_ivle_lib_pth:
 
1076
        action_append(ivle_pth, ivle_lib)
 
1077
 
 
1078
 
 
1079
    # Create the ivle working revision record file
 
1080
    action_mkdir(os.path.join(ivle_install_dir, 'version'), dry)
 
1081
    ivle_revision_record_file = os.path.join(ivle_install_dir, 'version/ivle-revision.txt')
 
1082
    if not dry:
 
1083
        try:
 
1084
            conf = open(ivle_revision_record_file, "w")
 
1085
 
 
1086
            conf.write( "# IVLE code revision listing generated by running 'svn status -v ..' from " + os.getcwd() + "\n#\n\n")
 
1087
 
 
1088
            conf.close()
 
1089
        except IOError, (errno, strerror):
 
1090
            print "IO error(%s): %s" % (errno, strerror)
 
1091
            sys.exit(1)
 
1092
 
 
1093
        os.system("svn status -v .. >> %s" % ivle_revision_record_file)
 
1094
 
 
1095
    print "Wrote IVLE code revision status to %s" % ivle_revision_record_file
 
1096
 
 
1097
    return 0
 
1098
 
 
1099
def updatejails(args):
 
1100
    # Get "dry" variable from command line
 
1101
    (opts, args) = getopt.gnu_getopt(args, "n", ['dry'])
 
1102
    opts = dict(opts)
 
1103
    dry = '-n' in opts or '--dry' in opts
 
1104
 
 
1105
    if dry:
 
1106
        print "Dry run (no actions will be executed\n"
 
1107
 
 
1108
    if not dry and os.geteuid() != 0:
 
1109
        print >>sys.stderr, "Must be root to run install"
 
1110
        print >>sys.stderr, "(I need to chown some files)."
 
1111
        return 1
 
1112
 
 
1113
    # Update the staging jail directory in case it hasn't been installed
 
1114
    # recently.
 
1115
    action_copytree('jail', os.path.join(jail_base, '__staging__'), dry)
 
1116
 
 
1117
    # Re-link all the files in all students jails.
 
1118
    for dir in os.listdir(jail_base):
 
1119
        if dir == '__staging__': continue
 
1120
        # First back up the student's home directory
 
1121
        temp_home = os.tmpnam()
 
1122
        action_rename(os.path.join(jail_base, dir, 'home'), temp_home, dry)
 
1123
        # Delete the student's jail and relink the jail files
 
1124
        action_linktree(os.path.join(jail_base, '__staging__'),
 
1125
            os.path.join(jail_base, dir), dry)
 
1126
        # Restore the student's home directory
 
1127
        action_rename(temp_home, os.path.join(jail_base, dir, 'home'), dry)
 
1128
        # Set up the user's home directory just in case they don't have a
 
1129
        # directory for this yet
 
1130
        action_mkdir(os.path.join(jail_base, dir, 'home', dir), dry)
 
1131
 
 
1132
    return 0
 
1133
 
 
1134
# The actions call Python os functions but print actions and handle dryness.
 
1135
# May still throw os exceptions if errors occur.
 
1136
 
 
1137
class RunError:
 
1138
    """Represents an error when running a program (nonzero return)."""
 
1139
    def __init__(self, prog, retcode):
 
1140
        self.prog = prog
 
1141
        self.retcode = retcode
 
1142
    def __str__(self):
 
1143
        return str(self.prog) + " returned " + repr(self.retcode)
 
1144
 
 
1145
def action_runprog(prog, args, dry):
 
1146
    """Runs a unix program. Searches in $PATH. Synchronous (waits for the
 
1147
    program to return). Runs in the current environment. First prints the
 
1148
    action as a "bash" line.
 
1149
 
 
1150
    Throws a RunError with a retcode of the return value of the program,
 
1151
    if the program did not return 0.
 
1152
 
 
1153
    prog: String. Name of the program. (No path required, if in $PATH).
 
1154
    args: [String]. Arguments to the program.
 
1155
    dry: Bool. If True, prints but does not execute.
 
1156
    """
 
1157
    print prog, string.join(args, ' ')
 
1158
    if dry: return
 
1159
    ret = os.spawnvp(os.P_WAIT, prog, args)
 
1160
    if ret != 0:
 
1161
        raise RunError(prog, ret)
 
1162
 
 
1163
def action_remove(path, dry):
 
1164
    """Calls rmtree, deleting the target file if it exists."""
 
1165
    try:
 
1166
        print "rm -r", path
 
1167
        if not dry:
 
1168
            shutil.rmtree(path, True)
 
1169
    except OSError, (err, msg):
 
1170
        if err != errno.EEXIST:
 
1171
            raise
 
1172
        # Otherwise, didn't exist, so we don't care
 
1173
 
 
1174
def action_rename(src, dst, dry):
 
1175
    """Calls rename. Deletes the target if it already exists."""
 
1176
    action_remove(dst, dry)
 
1177
    print "mv ", src, dst
 
1178
    if dry: return
 
1179
    try:
 
1180
        os.rename(src, dst)
 
1181
    except OSError, (err, msg):
 
1182
        if err != errno.EEXIST:
 
1183
            raise
 
1184
 
 
1185
def action_mkdir(path, dry):
 
1186
    """Calls mkdir. Silently ignored if the directory already exists.
 
1187
    Creates all parent directories as necessary."""
 
1188
    print "mkdir -p", path
 
1189
    if dry: return
 
1190
    try:
 
1191
        os.makedirs(path)
 
1192
    except OSError, (err, msg):
 
1193
        if err != errno.EEXIST:
 
1194
            raise
 
1195
 
 
1196
def action_copytree(src, dst, dry):
 
1197
    """Copies an entire directory tree. Symlinks are seen as normal files and
 
1198
    copies of the entire file (not the link) are made. Creates all parent
 
1199
    directories as necessary.
 
1200
 
 
1201
    See shutil.copytree."""
 
1202
    # Allow copying over itself
 
1203
    if (os.path.normpath(os.path.join(os.getcwd(),src)) ==
 
1204
        os.path.normpath(os.path.join(os.getcwd(),dst))):
 
1205
        return
 
1206
    action_remove(dst, dry)
 
1207
    print "cp -r", src, dst
 
1208
    if dry: return
 
1209
    shutil.copytree(src, dst, True)
 
1210
 
 
1211
def action_linktree(src, dst, dry):
 
1212
    """Hard-links an entire directory tree. Same as copytree but the created
 
1213
    files are hard-links not actual copies. Removes the existing destination.
 
1214
    """
 
1215
    action_remove(dst, dry)
 
1216
    print "<cp with hardlinks> -r", src, dst
 
1217
    if dry: return
 
1218
    common.makeuser.linktree(src, dst)
 
1219
 
 
1220
def action_copylist(srclist, dst, dry, srcdir="."):
 
1221
    """Copies all files in a list to a new location. The files in the list
 
1222
    are read relative to the current directory, and their destinations are the
 
1223
    same paths relative to dst. Creates all parent directories as necessary.
 
1224
    srcdir is "." by default, can be overridden.
 
1225
    """
 
1226
    for srcfile in srclist:
 
1227
        dstfile = os.path.join(dst, srcfile)
 
1228
        srcfile = os.path.join(srcdir, srcfile)
 
1229
        dstdir = os.path.split(dstfile)[0]
 
1230
        if not os.path.isdir(dstdir):
 
1231
            action_mkdir(dstdir, dry)
 
1232
        print "cp -f", srcfile, dstfile
 
1233
        if not dry:
 
1234
            try:
 
1235
                shutil.copyfile(srcfile, dstfile)
 
1236
                shutil.copymode(srcfile, dstfile)
 
1237
            except shutil.Error:
 
1238
                pass
 
1239
 
 
1240
def action_copyfile(src, dst, dry):
 
1241
    """Copies one file to a new location. Creates all parent directories
 
1242
    as necessary.
 
1243
    Warn if file not found.
 
1244
    """
 
1245
    dstdir = os.path.split(dst)[0]
 
1246
    if not os.path.isdir(dstdir):
 
1247
        action_mkdir(dstdir, dry)
 
1248
    print "cp -f", src, dst
 
1249
    if not dry:
 
1250
        try:
 
1251
            shutil.copyfile(src, dst)
 
1252
            shutil.copymode(src, dst)
 
1253
        except (shutil.Error, IOError), e:
 
1254
            print "Warning: " + str(e)
 
1255
 
 
1256
def action_symlink(src, dst, dry):
 
1257
    """Creates a symlink in a given location. Creates all parent directories
 
1258
    as necessary.
 
1259
    """
 
1260
    dstdir = os.path.split(dst)[0]
 
1261
    if not os.path.isdir(dstdir):
 
1262
        action_mkdir(dstdir, dry)
 
1263
    # Delete existing file
 
1264
    if os.path.exists(dst):
 
1265
        os.remove(dst)
 
1266
    print "ln -fs", src, dst
 
1267
    if not dry:
 
1268
        os.symlink(src, dst)
 
1269
 
 
1270
def action_append(ivle_pth, ivle_www):
 
1271
    file = open(ivle_pth, 'a+')
 
1272
    file.write(ivle_www + '\n')
 
1273
    file.close()
 
1274
 
 
1275
def action_chown_setuid(file, dry):
 
1276
    """Chowns a file to root, and sets the setuid bit on the file.
 
1277
    Calling this function requires the euid to be root.
 
1278
    The actual mode of path is set to: rws--s--s
 
1279
    """
 
1280
    print "chown root:root", file
 
1281
    if not dry:
 
1282
        os.chown(file, 0, 0)
 
1283
    print "chmod a+xs", file
 
1284
    print "chmod u+rw", file
 
1285
    if not dry:
 
1286
        os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
 
1287
            | stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
 
1288
 
 
1289
def action_chmod_x(file, dry):
 
1290
    """Chmod 755 a file (sets permissions to rwxr-xr-x)."""
 
1291
    print "chmod 755", file
 
1292
    if not dry:
 
1293
        os.chmod(file, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
 
1294
            | stat.S_IXGRP | stat.S_IRGRP | stat.S_IXOTH | stat.S_IROTH)
 
1295
 
 
1296
def query_user(default, prompt):
 
1297
    """Prompts the user for a string, which is read from a line of stdin.
 
1298
    Exits silently if EOF is encountered. Returns the string, with spaces
 
1299
    removed from the beginning and end.
 
1300
 
 
1301
    Returns default if a 0-length line (after spaces removed) was read.
 
1302
    """
 
1303
    sys.stdout.write('%s\n    (default: "%s")\n>' % (prompt, default))
 
1304
    try:
 
1305
        val = sys.stdin.readline()
 
1306
    except KeyboardInterrupt:
 
1307
        # Ctrl+C
 
1308
        sys.stdout.write("\n")
 
1309
        sys.exit(1)
 
1310
    sys.stdout.write("\n")
 
1311
    # If EOF, exit
 
1312
    if val == '': sys.exit(1)
 
1313
    # If empty line, return default
 
1314
    val = val.strip()
 
1315
    if val == '': return default
 
1316
    return val
 
1317
 
 
1318
def filter_mutate(function, list):
 
1319
    """Like built-in filter, but mutates the given list instead of returning a
 
1320
    new one. Returns None."""
 
1321
    i = len(list)-1
 
1322
    while i >= 0:
 
1323
        # Delete elements which do not match
 
1324
        if not function(list[i]):
 
1325
            del list[i]
 
1326
        i -= 1
 
1327
 
 
1328
def get_svn_revision():
 
1329
    """Returns either the current SVN revision of this build, or None"""
 
1330
    try:
 
1331
        svn = pysvn.Client()
 
1332
        entry = svn.info('.')
 
1333
        revnum = entry.revision.number
 
1334
    except pysvn.ClientError, e:
 
1335
        revnum = None
 
1336
    return revnum
87
1337
 
88
1338
if __name__ == "__main__":
89
1339
    sys.exit(main())