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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2009 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""
Script to load the sample data into a fresh IVLE instance, for testing or
experimentation.
"""

import sys
import os
import shutil
import optparse
import subprocess
import logging
import readline
import warnings

import psycopg2

from ivle.config import Config
import ivle.svn

logging.basicConfig(
    format='%(asctime)s %(levelname)s %(message)s',
    level=logging.INFO)

def runprog_stderr(*popenargs, **kwargs):
    """Run a program, using subprocess.Popen.
    Return None if the program had a 0 return code.
    Return the string stderr if the program failed.
    """
    kwargs['stderr'] = subprocess.PIPE

    proc = subprocess.Popen(*popenargs, **kwargs)
    _, stderr = proc.communicate()
    if proc.returncode == 0:
        return None
    else:
        return stderr

if os.getuid() != 0:
    print "Must run %s as root." % os.path.basename(sys.argv[0])
    sys.exit(1)

usage = """usage: %prog [OPTIONS] <SQL-FILE>
Loads the sample data into the installed IVLE instance."""

parser = optparse.OptionParser(usage)
parser.add_option("-f", "--force",
    action="store_true", dest="force",
    help="destroy all data without prompting",
    default=False
    )
parser.add_option("--pg-user",
    action="store", dest="pg_user",
    help="database super-user (for dropping and creating db) "
        "(default: postgres)",
    default="postgres"
    )

(options, args) = parser.parse_args()

if len(args) != 1:
    parser.error("incorrect number of arguments")

sqlfile = args[0]

dbconfig = Config()['database']

# Try creating the database (if it succeeds, no harm)
logging.info("Creating database \"%s\"." % dbconfig["name"])
errmsg = runprog_stderr(["sudo", "-u", options.pg_user, "createdb", "-O",
                         dbconfig["username"], dbconfig["name"]])
if errmsg is not None:
    # Couldn't create the DB
    if errmsg.strip().endswith("already exists"):
        logging.info("Database already exists.")
        # The database already exists (most common error)
        # Drop and re-create the database, if the user is absolutely sure
        if not options.force:
            try:
                drop = raw_input("Do you want to delete all existing data? "
                            "THIS WILL DROP YOUR DATABASE!\n[yes/no]> ")
            except (KeyboardInterrupt, EOFError):
                print
                sys.exit(1)
            if drop.strip().lower() != "yes":
                sys.exit(1)
        # OK, here we go

        # Unmount all the jails
        logging.info("Unmounting all users.")
        subprocess.check_call(["ivle-mountallusers", "-u"])

        # Drop database
        logging.info("Dropping database \"%s\"." % dbconfig["name"])
        errmsg = subprocess.check_call(["sudo", "-u", options.pg_user,
                                        "dropdb", dbconfig["name"]])
        # Re-create database
        logging.info("Creating database \"%s\"." % dbconfig["name"])
        errmsg = subprocess.check_call(["sudo", "-u", options.pg_user,
                                        "createdb", "-O",dbconfig["username"],
                                        dbconfig["name"]])
    else:
        logging.error(errmsg.strip())
        sys.exit(1)

# Create "plpgsql" language
logging.info("Creating language plpgsql.")
errmsg = subprocess.check_call(["sudo", "-u", options.pg_user, "createlang",
                         "plpgsql", dbconfig["name"]])

# Populate with database schema
logging.info("Populating database with schema.")
ivleconn = psycopg2.connect(
    host='localhost', database=dbconfig['name'], user=dbconfig['username'],
    password=dbconfig['password'])
ivlecursor = ivleconn.cursor()
logging.info("Populating database with schema.")
schemafile = open("userdb/users.sql")
ivlecursor.execute(schemafile.read())
schemafile.close()

# Build or rebuild all of the users' filesystems and subversion repos
logging.info("Creating data directories.")
subprocess.check_call(["ivle-createdatadirs"])

# Move all of the users' filesystems and subversion repos out of the way
# (This will clean out the user dirs because there are no users in the DB.)
logging.info("Moving existing user filesystems and repos out of the way.")
subprocess.check_call(["ivle-refreshfilesystem"])

# Populate with sample data
logging.info("Populating database with sample data.")
file = open("examples/db/sample.sql")
proc = subprocess.Popen(["sudo", "-u", "postgres", "psql", dbconfig["name"]],
                        stdin=file)
if proc.wait() != 0:
    file.close()
    sys.exit(1)
file.close()

# Build all of the users' filesystems and subversion repos
# (This will create fresh user dirs and repos because the jails were empty.)
logging.info("Building sample users' filesystems and repos.")
subprocess.check_call(["ivle-refreshfilesystem"])

config = Config()
dbconfig = config['database']

# Populate some of the users' Subversion repos
def temp_checkout(svnclient, username):
    """Checkout user `username`'s repo to a temporary directory.
    @return: The temporary workspace directory.
    """
    # Do the checkout over HTTP, since we then use the user's own credentials
    repourl = config['urls']['svn_addr'] + '/users/' + username
    # Ignore warnings about the use of tempnam
    warnings.simplefilter('ignore')
    tempdir = os.tempnam()
    warnings.resetwarnings()
    svnclient.checkout(repourl, tempdir)
    return tempdir

logging.info("Populating student repositories")
def svnload(dumpfile, repo):
    """Run svnadmin load.
    @param dumpfile: Dump file to load from, relative to examples/userrepos.
    @param repo: Repo to write to, relative to /var/lib/ivle/svn/repositories.
    """
    f = open(os.path.join("examples/userrepos", dumpfile), "rb")
    repo = os.path.join(config['paths']['svn']['repo_path'], repo)
    errmsg = runprog_stderr(["sudo", "-u", "www-data", "svnadmin", "load",
                             repo], stdin=f)
    if errmsg is not None:
        logging.error(errmsg)
    f.close()

# Load the known SVN dump files
svnload("studenta.dump", "users/studenta")
svnload("group1.dump", "groups/ivle-102_2009_2_group1")