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

« back to all changes in this revision

Viewing changes to setup/install.py

  • Committer: William Grant
  • Date: 2010-02-25 03:18:21 UTC
  • Revision ID: grantw@unimelb.edu.au-20100225031821-mi9a2tm5679fht4d
Shuffle things around so that req.user and req.store only construct when actually retrieved, and ensure they're not retrieved for media files. Saves 50ms of DB connection time per request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# Author: Matt Giuca, Refactored by David Coles
20
20
# Date:   03/07/2008
21
21
 
22
 
# setup/build.py
23
 
# Compiles all files and sets up a jail template in the source directory.
24
 
# Details:
25
 
# Compiles (GCC) trampoline/trampoline.c to trampoline/trampoline.
26
 
# Creates jail/.
27
 
# Creates standard subdirs inside the jail, eg bin, opt, home, tmp.
28
 
# Copies console/ to a location within the jail.
29
 
# Copies OS programs and files to corresponding locations within the jail
30
 
#   (eg. python and Python libs, ld.so, etc).
31
 
# Generates .pyc files for all the IVLE .py files.
32
 
 
33
22
import optparse
34
23
import os
35
 
from setuputil import *
 
24
import sys
 
25
import functools
 
26
import distutils.sysconfig
 
27
 
 
28
from setup import util
36
29
 
37
30
def install(args):
38
31
    usage = """usage: %prog install [options]
39
32
(Requires root)
40
33
Create target install directory ($target).
41
34
Create $target/bin.
42
 
Copy trampoline/trampoline to $target/bin.
43
 
Copy timount/timount to $target/bin.
 
35
Copy bin/trampoline/trampoline to $target/bin.
 
36
Copy bin/timount/timount to $target/bin.
44
37
chown and chmod the installed trampoline.
45
 
Copy www/ to $target.
46
 
Copy jail/ to jail_system directory (unless --nojail specified).
47
 
Copy subjects/ to subjects directory (unless --nosubjects specified).
48
38
"""
49
39
 
50
40
    # Parse arguments
52
42
    parser.add_option("-n", "--dry",
53
43
        action="store_true", dest="dry",
54
44
        help="Print out the actions but don't do anything.")
55
 
    parser.add_option("-J", "--nojail",
56
 
        action="store_true", dest="nojail",
57
 
        help="Don't copy jail/ to jail_system directory")
58
 
    parser.add_option("-S", "--nosubjects",
59
 
        action="store_true", dest="nosubjects",
60
 
        help="Don't copy subject/ to subjects directory.")
 
45
    parser.add_option("--root",
 
46
        action="store", dest="rootdir",
 
47
        help="Install into a different root directory.",
 
48
        default='/')
 
49
    parser.add_option("--prefix",
 
50
        action="store", dest="prefix",
 
51
        help="Base prefix to install IVLE into (default: /usr/local).",
 
52
        default='/usr/local')
 
53
    parser.add_option("--python-site-packages",
 
54
        action="store", dest="python_site_packages",
 
55
        help="Path to Python site packages directory.",
 
56
        default=None)
61
57
    (options, args) = parser.parse_args(args)
62
58
 
 
59
    # Prefix must be absolute (not really necessary, but since a relative
 
60
    # prefix will be taken relative to *root* not working directory, it is
 
61
    # confusing if we allow it).
 
62
    if options.prefix[:1] not in (os.path.sep, os.path.altsep):
 
63
        print >>sys.stderr, """prefix must be an absolute path.
 
64
    (This will be interpreted relative to root, so provide --root=. if you
 
65
    want a path relative to the working directory)."""
 
66
        return 1
 
67
 
 
68
    # Calculate python_site_packages using the supplied prefix
 
69
    if options.python_site_packages is None:
 
70
        options.python_site_packages = distutils.sysconfig.get_python_lib(
 
71
            prefix=options.prefix)
 
72
 
63
73
    # Call the real function
64
 
    __install(options.dry, options.nojail, options.nosubjects)
65
 
 
66
 
def __install(dry=False,nojail=False,nosubjects=False):
67
 
    # Importing configuration is a little tricky
68
 
    sys.path.append(os.path.join(os.pardir, 'lib'))
69
 
    sys.path.append(os.pardir)
70
 
    import conf.conf
71
 
    import install_list
72
 
 
73
 
    # Pull the required varibles out of the config
74
 
    ivle_install_dir = conf.conf.ivle_install_dir
75
 
    jail_base = conf.conf.jail_base
76
 
    jail_system = conf.conf.jail_system
77
 
    subjects_base = conf.conf.subjects_base
78
 
    exercises_base = conf.conf.exercises_base
 
74
    return __install(prefix=options.prefix,
 
75
                     python_site_packages=options.python_site_packages,
 
76
                     dry=options.dry, rootdir=options.rootdir)
 
77
 
 
78
def __install(prefix, python_site_packages, dry=False, rootdir=None):
 
79
    install_list = util.InstallList()
 
80
 
 
81
    # We need to apply make_install_path with the rootdir to an awful lot of
 
82
    # config variables, so make it easy:
 
83
    mip = functools.partial(util.make_install_path, rootdir)
 
84
 
 
85
    # Compute the lib_path, share_path and bin_path (copied from
 
86
    # ivle/conf/conf.py).
 
87
    lib_path = mip(os.path.join(prefix, 'lib/ivle'))
 
88
    share_path = mip(os.path.join(prefix, 'share/ivle'))
 
89
    bin_path = mip(os.path.join(prefix, 'bin'))
 
90
    python_site_packages = mip(python_site_packages)
79
91
 
80
92
    # Must be run as root or a dry run  
81
93
    if dry:
82
94
        print "Dry run (no actions will be executed)\n"
83
95
    
84
96
    if not dry and os.geteuid() != 0:
85
 
        print >>sys.stderr, "Must be root to run build"
86
 
        print >>sys.stderr, "(I need to chown)."
 
97
        print >>sys.stderr, "Must be root to run install"
87
98
        return 1
88
 
    
89
 
    # Create the target (install) directory
90
 
    action_mkdir(ivle_install_dir, dry)
91
 
 
92
 
    # Create bin and copy the compiled files there
93
 
    action_mkdir(os.path.join(ivle_install_dir, 'bin'), dry)
94
 
    tramppath = os.path.join(ivle_install_dir, 'bin/trampoline')
95
 
    action_copyfile('trampoline/trampoline', tramppath, dry)
 
99
 
 
100
    # Create the config directory.
 
101
    util.action_mkdir(mip('/etc/ivle/plugins.d'), dry)
 
102
 
 
103
    # Create lib and copy the compiled files there
 
104
    util.action_mkdir(lib_path, dry)
 
105
 
 
106
    tramppath = os.path.join(lib_path, 'trampoline')
 
107
    util.action_copyfile('bin/trampoline/trampoline', tramppath, dry)
96
108
    # chown trampoline to root and set setuid bit
97
 
    action_chown_setuid(tramppath, dry)
98
 
 
99
 
    timountpath = os.path.join(ivle_install_dir, 'bin/timount')
100
 
    action_copyfile('timount/timount', timountpath, dry)
101
 
 
102
 
    # Create a scripts directory to put the usrmgt-server in.
103
 
    action_mkdir(os.path.join(ivle_install_dir, 'scripts'), dry)
104
 
    usrmgtpath = os.path.join(ivle_install_dir, 'scripts/usrmgt-server')
105
 
    action_copyfile('scripts/usrmgt-server', usrmgtpath, dry)
106
 
    action_chmod_x(usrmgtpath, dry)
107
 
 
108
 
    # Copy the www and lib directories using the list
109
 
    action_copylist(install_list.list_www, ivle_install_dir, dry)
110
 
    action_copylist(install_list.list_lib, ivle_install_dir, dry)
111
 
    
112
 
    # Copy the php directory
113
 
    forum_dir = "www/php/phpBB3"
114
 
    forum_path = os.path.join(ivle_install_dir, forum_dir)
115
 
    action_copytree(forum_dir, forum_path, dry)
116
 
    print "chown -R www-data:www-data %s" % forum_path
117
 
    if not dry:
118
 
        os.system("chown -R www-data:www-data %s" % forum_path)
119
 
 
120
 
    if not nojail:
121
 
        # Copy the local jail directory built by the build action
122
 
        # to the jail_system directory (it will be used to help build
123
 
        # all the students' jails).
124
 
        action_copytree('jail', jail_system, dry)
125
 
    if not nosubjects:
126
 
        # Copy the subjects and exercises directories across
127
 
        action_copylist(install_list.list_subjects, subjects_base, dry,
128
 
            srcdir="./subjects")
129
 
        action_copylist(install_list.list_exercises, exercises_base, dry,
130
 
            srcdir="./exercises")
131
 
 
132
 
    # Append IVLE path to ivle.pth in python site packages
133
 
    # (Unless it's already there)
134
 
    ivle_pth = os.path.join(sys.prefix,
135
 
        "lib/python%s/site-packages/ivle.pth" % PYTHON_VERSION)
136
 
    ivle_www = os.path.join(ivle_install_dir, "www")
137
 
    ivle_lib = os.path.join(ivle_install_dir, "lib")
138
 
    write_ivle_pth = True
139
 
    write_ivle_lib_pth = True
140
 
    try:
141
 
        file = open(ivle_pth, 'r')
142
 
        for line in file:
143
 
            if line.strip() == ivle_www:
144
 
                write_ivle_pth = False
145
 
            elif line.strip() == ivle_lib:
146
 
                write_ivle_lib_pth = False
147
 
        file.close()
148
 
    except (IOError, OSError):
149
 
        pass
150
 
    if write_ivle_pth:
151
 
        action_append(ivle_pth, ivle_www)
152
 
    if write_ivle_lib_pth:
153
 
        action_append(ivle_pth, ivle_lib)
154
 
 
155
 
 
156
 
    # Create the ivle working revision record file
157
 
    action_mkdir(os.path.join(ivle_install_dir, 'version'), dry)
158
 
    ivle_revision_record_file = os.path.join(ivle_install_dir, 'version/ivle-revision.txt')
159
 
    if not dry:
160
 
        try:
161
 
            conf = open(ivle_revision_record_file, "w")
162
 
 
163
 
            conf.write( "# IVLE code revision listing generated by running 'svn status -v ..' from " + os.getcwd() + "\n#\n\n")
164
 
 
165
 
            conf.close()
166
 
        except IOError, (errno, strerror):
167
 
            print "IO error(%s): %s" % (errno, strerror)
168
 
            sys.exit(1)
169
 
 
170
 
        os.system("svn status -v . >> %s" % ivle_revision_record_file)
171
 
 
172
 
    print "Wrote IVLE code revision status to %s" % ivle_revision_record_file
 
109
    util.action_chown_setuid(tramppath, dry)
 
110
 
 
111
    timountpath = os.path.join(lib_path, 'timount')
 
112
    util.action_copyfile('bin/timount/timount', timountpath, dry)
 
113
 
 
114
    # Copy in the services (only usrmgt-server is needed on the host, but
 
115
    # the jail build requires the rest).
 
116
    util.action_copylist(install_list.list_services, share_path, dry)
 
117
    usrmgtpath = os.path.join(share_path, 'services/usrmgt-server')
 
118
    util.action_chmod_x(usrmgtpath, dry)
 
119
 
 
120
    # Copy the user-executable binaries using the list.
 
121
    util.action_copylist(install_list.list_user_binaries, bin_path, dry,
 
122
                         onlybasename=True)
 
123
 
 
124
    # Copy the lib directory (using the list)
 
125
    util.action_copylist(install_list.list_ivle_lib, python_site_packages, dry)
173
126
 
174
127
    return 0
175
128