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

« back to all changes in this revision

Viewing changes to setup/install.py

  • Committer: dcoles
  • Date: 2008-07-03 04:38:30 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:804
Setup: To go with last revision - Now just a front end for the setup package

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