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

1359 by Matt Giuca
Added a script ivle-loadsampledata.
1
#!/usr/bin/env python
2
# IVLE - Informatics Virtual Learning Environment
3
# Copyright (C) 2007-2009 The University of Melbourne
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19
"""
20
Script to load the sample data into a fresh IVLE instance, for testing or
21
experimentation.
22
"""
23
24
import sys
25
import os
1437 by Matt Giuca
ivle-loadsampledata: Checks out a user's Subversion repository, adds a few files, then commits.
26
import shutil
1359 by Matt Giuca
Added a script ivle-loadsampledata.
27
import optparse
28
import subprocess
29
import logging
30
import readline
1437 by Matt Giuca
ivle-loadsampledata: Checks out a user's Subversion repository, adds a few files, then commits.
31
import warnings
1359 by Matt Giuca
Added a script ivle-loadsampledata.
32
1403 by William Grant
Don't ask for the 'ivle' user password in loadsampledata; get it from the DB.
33
import psycopg2
34
1359 by Matt Giuca
Added a script ivle-loadsampledata.
35
from ivle.config import Config
1437 by Matt Giuca
ivle-loadsampledata: Checks out a user's Subversion repository, adds a few files, then commits.
36
import ivle.svn
1359 by Matt Giuca
Added a script ivle-loadsampledata.
37
38
logging.basicConfig(
39
    format='%(asctime)s %(levelname)s %(message)s',
40
    level=logging.INFO)
41
42
def runprog_stderr(*popenargs, **kwargs):
43
    """Run a program, using subprocess.Popen.
44
    Return None if the program had a 0 return code.
45
    Return the string stderr if the program failed.
46
    """
1461 by William Grant
Fix ivle-loadsampledata to work with Python 2.5.
47
    kwargs['stderr'] = subprocess.PIPE
48
49
    proc = subprocess.Popen(*popenargs, **kwargs)
1359 by Matt Giuca
Added a script ivle-loadsampledata.
50
    _, stderr = proc.communicate()
51
    if proc.returncode == 0:
52
        return None
53
    else:
54
        return stderr
55
56
if os.getuid() != 0:
57
    print "Must run %s as root." % os.path.basename(sys.argv[0])
58
    sys.exit(1)
59
60
usage = """usage: %prog [OPTIONS] <SQL-FILE>
61
Loads the sample data into the installed IVLE instance."""
62
63
parser = optparse.OptionParser(usage)
64
parser.add_option("-f", "--force",
65
    action="store_true", dest="force",
66
    help="destroy all data without prompting",
67
    default=False
68
    )
69
parser.add_option("--pg-user",
70
    action="store", dest="pg_user",
71
    help="database super-user (for dropping and creating db) "
72
        "(default: postgres)",
73
    default="postgres"
74
    )
75
76
(options, args) = parser.parse_args()
77
78
if len(args) != 1:
79
    parser.error("incorrect number of arguments")
80
81
sqlfile = args[0]
82
83
dbconfig = Config()['database']
84
85
# Try creating the database (if it succeeds, no harm)
86
logging.info("Creating database \"%s\"." % dbconfig["name"])
87
errmsg = runprog_stderr(["sudo", "-u", options.pg_user, "createdb", "-O",
88
                         dbconfig["username"], dbconfig["name"]])
89
if errmsg is not None:
90
    # Couldn't create the DB
91
    if errmsg.strip().endswith("already exists"):
92
        logging.info("Database already exists.")
93
        # The database already exists (most common error)
94
        # Drop and re-create the database, if the user is absolutely sure
95
        if not options.force:
96
            try:
97
                drop = raw_input("Do you want to delete all existing data? "
98
                            "THIS WILL DROP YOUR DATABASE!\n[yes/no]> ")
99
            except (KeyboardInterrupt, EOFError):
100
                print
101
                sys.exit(1)
102
            if drop.strip().lower() != "yes":
103
                sys.exit(1)
104
        # OK, here we go
105
106
        # Unmount all the jails
107
        logging.info("Unmounting all users.")
1470 by Matt Giuca
ivle-loadsampledata: Access commands through the PATH, rather than using bin/ relative paths.
108
        subprocess.check_call(["ivle-mountallusers", "-u"])
1359 by Matt Giuca
Added a script ivle-loadsampledata.
109
110
        # Drop database
111
        logging.info("Dropping database \"%s\"." % dbconfig["name"])
112
        errmsg = subprocess.check_call(["sudo", "-u", options.pg_user,
113
                                        "dropdb", dbconfig["name"]])
114
        # Re-create database
115
        logging.info("Creating database \"%s\"." % dbconfig["name"])
116
        errmsg = subprocess.check_call(["sudo", "-u", options.pg_user,
117
                                        "createdb", "-O",dbconfig["username"],
118
                                        dbconfig["name"]])
119
    else:
120
        logging.error(errmsg.strip())
121
        sys.exit(1)
122
123
# Create "plpgsql" language
124
logging.info("Creating language plpgsql.")
125
errmsg = subprocess.check_call(["sudo", "-u", options.pg_user, "createlang",
126
                         "plpgsql", dbconfig["name"]])
127
128
# Populate with database schema
129
logging.info("Populating database with schema.")
1403 by William Grant
Don't ask for the 'ivle' user password in loadsampledata; get it from the DB.
130
ivleconn = psycopg2.connect(
131
    host='localhost', database=dbconfig['name'], user=dbconfig['username'],
132
    password=dbconfig['password'])
133
ivlecursor = ivleconn.cursor()
134
logging.info("Populating database with schema.")
135
schemafile = open("userdb/users.sql")
136
ivlecursor.execute(schemafile.read())
137
schemafile.close()
1359 by Matt Giuca
Added a script ivle-loadsampledata.
138
1362 by Matt Giuca
ivle-loadsampledata: Runs ivle-refreshfilesystem both before and after loading sample data into the database, so that existing sample users are moved out of the way. This is explained in the docs.
139
# Build or rebuild all of the users' filesystems and subversion repos
140
logging.info("Creating data directories.")
1470 by Matt Giuca
ivle-loadsampledata: Access commands through the PATH, rather than using bin/ relative paths.
141
subprocess.check_call(["ivle-createdatadirs"])
1362 by Matt Giuca
ivle-loadsampledata: Runs ivle-refreshfilesystem both before and after loading sample data into the database, so that existing sample users are moved out of the way. This is explained in the docs.
142
143
# Move all of the users' filesystems and subversion repos out of the way
144
# (This will clean out the user dirs because there are no users in the DB.)
145
logging.info("Moving existing user filesystems and repos out of the way.")
1470 by Matt Giuca
ivle-loadsampledata: Access commands through the PATH, rather than using bin/ relative paths.
146
subprocess.check_call(["ivle-refreshfilesystem"])
1362 by Matt Giuca
ivle-loadsampledata: Runs ivle-refreshfilesystem both before and after loading sample data into the database, so that existing sample users are moved out of the way. This is explained in the docs.
147
1359 by Matt Giuca
Added a script ivle-loadsampledata.
148
# Populate with sample data
149
logging.info("Populating database with sample data.")
150
file = open("examples/db/sample.sql")
151
proc = subprocess.Popen(["sudo", "-u", "postgres", "psql", dbconfig["name"]],
152
                        stdin=file)
153
if proc.wait() != 0:
154
    file.close()
155
    sys.exit(1)
156
file.close()
157
1362 by Matt Giuca
ivle-loadsampledata: Runs ivle-refreshfilesystem both before and after loading sample data into the database, so that existing sample users are moved out of the way. This is explained in the docs.
158
# Build all of the users' filesystems and subversion repos
159
# (This will create fresh user dirs and repos because the jails were empty.)
160
logging.info("Building sample users' filesystems and repos.")
1470 by Matt Giuca
ivle-loadsampledata: Access commands through the PATH, rather than using bin/ relative paths.
161
subprocess.check_call(["ivle-refreshfilesystem"])
1437 by Matt Giuca
ivle-loadsampledata: Checks out a user's Subversion repository, adds a few files, then commits.
162
163
config = Config()
164
dbconfig = config['database']
165
166
# Populate some of the users' Subversion repos
167
def temp_checkout(svnclient, username):
168
    """Checkout user `username`'s repo to a temporary directory.
169
    @return: The temporary workspace directory.
170
    """
171
    # Do the checkout over HTTP, since we then use the user's own credentials
172
    repourl = config['urls']['svn_addr'] + '/users/' + username
173
    # Ignore warnings about the use of tempnam
174
    warnings.simplefilter('ignore')
175
    tempdir = os.tempnam()
176
    warnings.resetwarnings()
177
    svnclient.checkout(repourl, tempdir)
178
    return tempdir
179
180
logging.info("Populating student repositories")
1478 by Matt Giuca
Added subversion dumps as part of sample data. ivle-loadsampledata loads these dumps, so the sample data now comes with sample files (requiring a checkout).
181
def svnload(dumpfile, repo):
182
    """Run svnadmin load.
183
    @param dumpfile: Dump file to load from, relative to examples/userrepos.
184
    @param repo: Repo to write to, relative to /var/lib/ivle/svn/repositories.
185
    """
186
    f = open(os.path.join("examples/userrepos", dumpfile), "rb")
187
    repo = os.path.join(config['paths']['svn']['repo_path'], repo)
188
    errmsg = runprog_stderr(["sudo", "-u", "www-data", "svnadmin", "load",
189
                             repo], stdin=f)
190
    if errmsg is not None:
191
        logging.error(errmsg)
192
    f.close()
193
194
# Load the known SVN dump files
195
svnload("studenta.dump", "users/studenta")
196
svnload("group1.dump", "groups/ivle-102_2009_2_group1")