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

« back to all changes in this revision

Viewing changes to setup.py

  • Committer: dcoles
  • Date: 2008-07-03 04:20:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:803
Setup: Modularised setup.py so it is now no longer over 1000 lines. This should 
allow us to get in there and tidy up each module much easier. Also removed 
updatejails since this functionality seems to be duplicated with remakeuser.py 
and remakealluser.py scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
# cutting a distribution, and the listfile it generates should be included in
32
32
# the distribution, avoiding the administrator having to run it.
33
33
 
34
 
# setup.py conf [args]
 
34
# setup.py config [args]
35
35
# Configures IVLE with machine-specific details, most notably, various paths.
36
36
# Either prompts the administrator for these details or accepts them as
37
37
# command-line args.
38
 
# Creates www/conf/conf.py and trampoline/conf.h.
 
38
# Creates lib/conf/conf.py and trampoline/conf.h.
39
39
 
40
40
# setup.py build
41
41
# Compiles all files and sets up a jail template in the source directory.
55
55
# Copy trampoline/trampoline to $target/bin.
56
56
# chown and chmod the installed trampoline.
57
57
# Copy www/ to $target.
58
 
# Copy jail/ to jails template directory (unless --nojail specified).
 
58
# Copy jail/ to jails __staging__ directory (unless --nojail specified).
59
59
 
60
60
import os
61
61
import stat
67
67
import mimetypes
68
68
import compileall
69
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."""))
70
269
 
71
270
# Try importing existing conf, but if we can't just set up defaults
72
271
# The reason for this is that these settings are used by other phases
73
272
# of setup besides conf, so we need to know them.
74
273
# Also this allows you to hit Return to accept the existing value.
75
274
try:
76
 
    confmodule = __import__("www/conf/conf")
77
 
    root_dir = confmodule.root_dir
78
 
    ivle_install_dir = confmodule.ivle_install_dir
79
 
    jail_base = confmodule.jail_base
 
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
80
281
except ImportError:
81
282
    # Just set reasonable defaults
82
 
    root_dir = "/ivle"
83
 
    ivle_install_dir = "/opt/ivle"
84
 
    jail_base = "/home/informatics/jails"
85
 
# Always defaults
86
 
allowed_uids = "0"
 
283
    for opt in config_options:
 
284
        globals()[opt.option_name] = opt.default
87
285
 
88
286
# Try importing install_list, but don't fail if we can't, because listmake can
89
287
# function without it.
98
296
# as necessary, and include it in the distribution.
99
297
listmake_mimetypes = ['text/x-python', 'text/html',
100
298
    'application/x-javascript', 'application/javascript',
101
 
    'text/css', 'image/png']
 
299
    'text/css', 'image/png', 'image/gif', 'application/xml']
102
300
 
103
301
# Main function skeleton from Guido van Rossum
104
302
# http://www.artima.com/weblogs/viewpost.jsp?thread=4829
127
325
        return 1
128
326
 
129
327
    # Disallow run as root unless installing
130
 
    if operation != 'install' and os.geteuid() == 0:
 
328
    if (operation != 'install' and operation != 'updatejails' and operation != 
 
329
    'build'
 
330
        and os.geteuid() == 0):
131
331
        print >>sys.stderr, "I do not want to run this stage as root."
132
332
        print >>sys.stderr, "Please run as a normal user."
133
333
        return 1
135
335
    try:
136
336
        oper_func = {
137
337
            'help' : help,
138
 
            'conf' : conf,
 
338
            'config' : conf,
139
339
            'build' : build,
140
340
            'listmake' : listmake,
141
341
            'install' : install,
 
342
            'updatejails' : updatejails,
142
343
        }[operation]
143
344
    except KeyError:
144
345
        print >>sys.stderr, (
155
356
Operation (and args) can be:
156
357
    help [operation]
157
358
    listmake (developer use only)
158
 
    conf [args]
 
359
    config [args]
159
360
    build
160
 
    install [--nojail] [-n|--dry]
 
361
    install [--nojail] [--nosubjects] [-n|--dry]
161
362
"""
162
363
        return 1
163
364
    elif len(args) != 1:
176
377
be copied upon installation. This should be run by the developer before
177
378
cutting a distribution, and the listfile it generates should be included in
178
379
the distribution, avoiding the administrator having to run it."""
179
 
    elif operation == 'conf':
180
 
        print """python setup.py conf [args]
 
380
    elif operation == 'config':
 
381
        print """python setup.py config [args]
181
382
Configures IVLE with machine-specific details, most notably, various paths.
182
383
Either prompts the administrator for these details or accepts them as
183
384
command-line args. Will be interactive only if there are no arguments given.
187
388
to rebuild/install), just provide ivle_install_dir as the IVLE trunk
188
389
directory, and run build/install one time.
189
390
 
190
 
Creates www/conf/conf.py and trampoline/conf.h.
 
391
Creates lib/conf/conf.py and trampoline/conf.h.
191
392
 
192
 
Args are:
193
 
    --root_dir
194
 
    --ivle_install_dir
195
 
    --jail_base
196
 
    --allowed_uids
197
 
As explained in the interactive prompt or conf.py.
 
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.
198
397
"""
199
398
    elif operation == 'build':
200
399
        print """python -O setup.py build [--dry|-n]
211
410
 
212
411
--dry | -n  Print out the actions but don't do anything."""
213
412
    elif operation == 'install':
214
 
        print """sudo python setup.py install [--nojail] [--dry|-n]
 
413
        print """sudo python setup.py install [--nojail] [--nosubjects][--dry|-n]
215
414
(Requires root)
216
415
Create target install directory ($target).
217
416
Create $target/bin.
218
417
Copy trampoline/trampoline to $target/bin.
219
418
chown and chmod the installed trampoline.
220
419
Copy www/ to $target.
221
 
Copy jail/ to jails template directory (unless --nojail specified).
222
 
 
223
 
--nojail    Do not copy the jail.
 
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
 
224
431
--dry | -n  Print out the actions but don't do anything."""
225
432
    else:
226
433
        print >>sys.stderr, (
231
438
def listmake(args):
232
439
    # We build two separate lists, by walking www and console
233
440
    list_www = build_list_py_files('www')
234
 
    list_console = build_list_py_files('console')
 
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
    ]
235
451
    # Make sure that the files generated by conf are in the list
236
452
    # (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")
 
453
    if "lib/conf/conf.py" not in list_lib:
 
454
        list_lib.append("lib/conf/conf.py")
242
455
    # Write these out to a file
243
456
    cwd = os.getcwd()
244
457
    # the files that will be created/overwritten
258
471
list_www = """)
259
472
        writelist_pretty(file, list_www)
260
473
        file.write("""
261
 
# List of all installable files in console directory.
262
 
list_console = """)
263
 
        writelist_pretty(file, list_console)
 
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)
264
491
 
265
492
        file.close()
266
493
    except IOError, (errno, strerror):
277
504
 
278
505
    return 0
279
506
 
280
 
def build_list_py_files(dir):
 
507
def build_list_py_files(dir, no_top_level=False):
281
508
    """Builds a list of all py files found in a directory and its
282
 
    subdirectories. Returns this as a list of strings."""
 
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
    """
283
513
    pylist = []
284
514
    for (dirpath, dirnames, filenames) in os.walk(dir):
285
515
        # Exclude directories beginning with a '.' (such as '.svn')
287
517
        # All *.py files are added to the list
288
518
        pylist += [os.path.join(dirpath, item) for item in filenames
289
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)
290
523
    return pylist
291
524
 
292
525
def writelist_pretty(file, list):
300
533
        file.write(']\n')
301
534
 
302
535
def conf(args):
303
 
    global root_dir, ivle_install_dir, jail_base, allowed_uids
 
536
    global db_port, usrmgt_port
304
537
    # Set up some variables
305
538
 
306
539
    cwd = os.getcwd()
307
540
    # the files that will be created/overwritten
308
 
    conffile = os.path.join(cwd, "www/conf/conf.py")
 
541
    conffile = os.path.join(cwd, "lib/conf/conf.py")
 
542
    jailconffile = os.path.join(cwd, "lib/conf/jailconf.py")
309
543
    conf_hfile = os.path.join(cwd, "trampoline/conf.h")
310
 
 
311
 
    # Fixed config options that we don't ask the admin
312
 
    default_app = "dummy"
 
544
    phpBBconffile = os.path.join(cwd, "www/php/phpBB3/config.php")
 
545
    usrmgtserver_initdfile = os.path.join(cwd, "doc/setup/usrmgt-server.init")
313
546
 
314
547
    # Get command-line arguments to avoid asking questions.
315
548
 
316
 
    (opts, args) = getopt.gnu_getopt(args, "", ['root_dir=',
317
 
                    'ivle_install_dir=', 'jail_base=', 'allowed_uids='])
 
549
    optnames = []
 
550
    for opt in config_options:
 
551
        optnames.append(opt.option_name + "=")
 
552
    (opts, args) = getopt.gnu_getopt(args, "", optnames)
318
553
 
319
554
    if args != []:
320
555
        print >>sys.stderr, "Invalid arguments:", string.join(args, ' ')
326
561
        print """This tool will create the following files:
327
562
    %s
328
563
    %s
 
564
    %s
 
565
    %s
 
566
    %s
329
567
prompting you for details about your configuration. The file will be
330
568
overwritten if it already exists. It will *not* install or deploy IVLE.
331
569
 
332
570
Please hit Ctrl+C now if you do not wish to do this.
333
 
""" % (conffile, conf_hfile)
 
571
""" % (conffile, jailconffile, conf_hfile, phpBBconffile, usrmgtserver_initdfile)
334
572
 
335
573
        # Get information from the administrator
336
574
        # If EOF is encountered at any time during the questioning, just exit
337
575
        # silently
338
576
 
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 '
343
 
        'system):')
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.
351
 
    (eg. "1002,78")""")
352
 
 
 
577
        for opt in config_options:
 
578
            globals()[opt.option_name] = \
 
579
                query_user(globals()[opt.option_name], opt.prompt)
353
580
    else:
354
581
        opts = dict(opts)
355
582
        # 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']
 
583
        for opt in config_options:
 
584
            if '--' + opt.option_name in opts:
 
585
                globals()[opt.option_name] = opts['--' + opt.option_name]
364
586
 
365
587
    # Error handling on input values
366
588
    try:
367
 
        allowed_uids = map(int, allowed_uids.split(','))
 
589
        allowed_uids_list = map(int, allowed_uids.split(','))
368
590
    except ValueError:
369
591
        print >>sys.stderr, (
370
592
        "Invalid UID list (%s).\n"
371
593
        "Must be a comma-separated list of integers." % allowed_uids)
372
594
        return 1
373
 
 
374
 
    # Write www/conf/conf.py
 
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
375
616
 
376
617
    try:
377
618
        conf = open(conffile, "w")
380
621
# conf.py
381
622
# Miscellaneous application settings
382
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
 
383
654
 
384
655
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
385
656
# with this).
386
657
# eg. "/" or "/ivle".
387
 
root_dir = "%s"
388
 
 
389
 
# In the local file system, where IVLE is actually installed.
390
 
# This directory should contain the "www" and "bin" directories.
391
 
ivle_install_dir = "%s"
 
658
root_dir = %s
392
659
 
393
660
# In the local file system, where are the student/user file spaces located.
394
661
# The user jails are expected to be located immediately in subdirectories of
395
662
# this location.
396
 
jail_base = "%s"
 
663
jail_base = '/'
397
664
 
398
 
# Which application to load by default (if the user navigates to the top level
399
 
# of the site). This is the app's URL name.
400
 
# Note that if this app requires authentication, the user will first be
401
 
# presented with the login screen.
402
 
default_app = "%s"
403
 
""" % (root_dir, ivle_install_dir, jail_base, default_app))
 
665
# The hostname for serving publicly accessible pages
 
666
public_host = %s
 
667
""" % (repr(root_dir),repr(public_host)))
404
668
 
405
669
        conf.close()
406
670
    except IOError, (errno, strerror):
407
671
        print "IO error(%s): %s" % (errno, strerror)
408
672
        sys.exit(1)
409
673
 
410
 
    print "Successfully wrote www/conf/conf.py"
 
674
    print "Successfully wrote lib/conf/jailconf.py"
411
675
 
412
676
    # Write trampoline/conf.h
413
677
 
432
696
 * (Note that root is an implicit member of this list).
433
697
 */
434
698
static const int allowed_uids[] = { %s };
435
 
""" % (jail_base, repr(allowed_uids)[1:-1]))
 
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
436
703
 
437
704
        conf.close()
438
705
    except IOError, (errno, strerror):
441
708
 
442
709
    print "Successfully wrote trampoline/conf.h"
443
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
 
444
916
    print
445
917
    print "You may modify the configuration at any time by editing"
446
918
    print conffile
 
919
    print jailconffile
447
920
    print conf_hfile
 
921
    print phpBBconffile
 
922
    print usrmgtserver_initdfile
448
923
    print
449
924
    return 0
450
925
 
456
931
 
457
932
    if dry:
458
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()
459
947
 
460
948
    # Compile the trampoline
461
 
    action_runprog('gcc', ['-Wall', '-o', 'trampoline/trampoline',
462
 
        'trampoline/trampoline.c'], dry)
 
949
    curdir = os.getcwd()
 
950
    os.chdir('trampoline')
 
951
    action_runprog('make', [], dry)
 
952
    os.chdir(curdir)
463
953
 
464
954
    # Create the jail and its subdirectories
465
955
    # 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)
 
956
    action_runprog('./buildjail.sh', [], dry)
469
957
 
470
958
    # 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)
 
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)
473
975
 
474
976
    # Compile .py files into .pyc or .pyo files
475
977
    compileall.compile_dir('www', quiet=True)
476
 
    compileall.compile_dir('console', 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()
477
989
 
478
990
    return 0
479
991
 
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)
493
 
 
494
992
def copy_file_to_jail(src, dry):
495
993
    """Copies a single file from an absolute location into the same location
496
994
    within the jail. src must begin with a '/'. The jail will be located
499
997
 
500
998
def install(args):
501
999
    # Get "dry" and "nojail" variables from command line
502
 
    (opts, args) = getopt.gnu_getopt(args, "n", ['dry', 'nojail'])
 
1000
    (opts, args) = getopt.gnu_getopt(args, "n",
 
1001
        ['dry', 'nojail', 'nosubjects'])
503
1002
    opts = dict(opts)
504
1003
    dry = '-n' in opts or '--dry' in opts
505
1004
    nojail = '--nojail' in opts
 
1005
    nosubjects = '--nosubjects' in opts
506
1006
 
507
1007
    if dry:
508
1008
        print "Dry run (no actions will be executed\n"
522
1022
    # chown trampoline to root and set setuid bit
523
1023
    action_chown_setuid(tramppath, dry)
524
1024
 
525
 
    # Copy the www directory using the list
 
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
526
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)
527
1042
 
528
1043
    if not nojail:
529
1044
        # 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)
 
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)
533
1131
 
534
1132
    return 0
535
1133
 
562
1160
    if ret != 0:
563
1161
        raise RunError(prog, ret)
564
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
 
565
1185
def action_mkdir(path, dry):
566
1186
    """Calls mkdir. Silently ignored if the directory already exists.
567
1187
    Creates all parent directories as necessary."""
579
1199
    directories as necessary.
580
1200
 
581
1201
    See shutil.copytree."""
582
 
    if os.access(dst, os.F_OK):
583
 
        print "rm -r", dst
584
 
        if not dry:
585
 
            shutil.rmtree(dst, True)
 
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)
586
1207
    print "cp -r", src, dst
587
1208
    if dry: return
588
1209
    shutil.copytree(src, dst, True)
589
1210
 
590
 
def action_copylist(srclist, dst, dry):
 
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="."):
591
1221
    """Copies all files in a list to a new location. The files in the list
592
1222
    are read relative to the current directory, and their destinations are the
593
1223
    same paths relative to dst. Creates all parent directories as necessary.
 
1224
    srcdir is "." by default, can be overridden.
594
1225
    """
595
1226
    for srcfile in srclist:
596
1227
        dstfile = os.path.join(dst, srcfile)
 
1228
        srcfile = os.path.join(srcdir, srcfile)
597
1229
        dstdir = os.path.split(dstfile)[0]
598
1230
        if not os.path.isdir(dstdir):
599
1231
            action_mkdir(dstdir, dry)
608
1240
def action_copyfile(src, dst, dry):
609
1241
    """Copies one file to a new location. Creates all parent directories
610
1242
    as necessary.
 
1243
    Warn if file not found.
611
1244
    """
612
1245
    dstdir = os.path.split(dst)[0]
613
1246
    if not os.path.isdir(dstdir):
617
1250
        try:
618
1251
            shutil.copyfile(src, dst)
619
1252
            shutil.copymode(src, dst)
620
 
        except shutil.Error:
621
 
            pass
 
1253
        except (shutil.Error, IOError), e:
 
1254
            print "Warning: " + str(e)
622
1255
 
623
1256
def action_symlink(src, dst, dry):
624
1257
    """Creates a symlink in a given location. Creates all parent directories
634
1267
    if not dry:
635
1268
        os.symlink(src, dst)
636
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
 
637
1275
def action_chown_setuid(file, dry):
638
1276
    """Chowns a file to root, and sets the setuid bit on the file.
639
1277
    Calling this function requires the euid to be root.
648
1286
        os.chmod(file, stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
649
1287
            | stat.S_ISUID | stat.S_IRUSR | stat.S_IWUSR)
650
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
 
651
1296
def query_user(default, prompt):
652
1297
    """Prompts the user for a string, which is read from a line of stdin.
653
1298
    Exits silently if EOF is encountered. Returns the string, with spaces
680
1325
            del list[i]
681
1326
        i -= 1
682
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
 
1337
 
683
1338
if __name__ == "__main__":
684
1339
    sys.exit(main())
 
1340