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

« back to all changes in this revision

Viewing changes to bin/ivle-loadsampledata

  • Committer: Matt Giuca
  • Date: 2010-02-12 04:00:39 UTC
  • Revision ID: matt.giuca@gmail.com-20100212040039-vw9yf8p4s98g6nu9
Added an argument 'config' to every single get_permissions method throughout the program. All calls to get_permissions pass a config. This is to allow per-site policy configurations on permissions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import sys
25
25
import os
 
26
import shutil
26
27
import optparse
27
28
import subprocess
28
29
import logging
29
30
import readline
 
31
import warnings
 
32
 
 
33
import psycopg2
30
34
 
31
35
from ivle.config import Config
 
36
import ivle.svn
32
37
 
33
38
logging.basicConfig(
34
39
    format='%(asctime)s %(levelname)s %(message)s',
39
44
    Return None if the program had a 0 return code.
40
45
    Return the string stderr if the program failed.
41
46
    """
42
 
    proc = subprocess.Popen(*popenargs, stderr=subprocess.PIPE, **kwargs)
 
47
    kwargs['stderr'] = subprocess.PIPE
 
48
 
 
49
    proc = subprocess.Popen(*popenargs, **kwargs)
43
50
    _, stderr = proc.communicate()
44
51
    if proc.returncode == 0:
45
52
        return None
98
105
 
99
106
        # Unmount all the jails
100
107
        logging.info("Unmounting all users.")
101
 
        subprocess.check_call(["bin/ivle-mountallusers", "-u"])
 
108
        subprocess.check_call(["ivle-mountallusers", "-u"])
102
109
 
103
110
        # Drop database
104
111
        logging.info("Dropping database \"%s\"." % dbconfig["name"])
120
127
 
121
128
# Populate with database schema
122
129
logging.info("Populating database with schema.")
123
 
file = open("userdb/users.sql")
124
 
proc = subprocess.Popen(["psql", "-h", "localhost", "-W", dbconfig["name"],
125
 
                           dbconfig["username"]], stdin=file)
126
 
if proc.wait() != 0:
127
 
    file.close()
128
 
    sys.exit(1)
129
 
file.close()
 
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()
130
138
 
131
139
# Build or rebuild all of the users' filesystems and subversion repos
132
140
logging.info("Creating data directories.")
133
 
subprocess.check_call(["bin/ivle-createdatadirs"])
 
141
subprocess.check_call(["ivle-createdatadirs"])
134
142
 
135
143
# Move all of the users' filesystems and subversion repos out of the way
136
144
# (This will clean out the user dirs because there are no users in the DB.)
137
145
logging.info("Moving existing user filesystems and repos out of the way.")
138
 
subprocess.check_call(["bin/ivle-refreshfilesystem"])
 
146
subprocess.check_call(["ivle-refreshfilesystem"])
139
147
 
140
148
# Populate with sample data
141
149
logging.info("Populating database with sample data.")
150
158
# Build all of the users' filesystems and subversion repos
151
159
# (This will create fresh user dirs and repos because the jails were empty.)
152
160
logging.info("Building sample users' filesystems and repos.")
153
 
subprocess.check_call(["bin/ivle-refreshfilesystem"])
 
161
subprocess.check_call(["ivle-refreshfilesystem"])
 
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")
 
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")