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

134 by mattgiuca
Added module: common/makeuser.py. Creates a new user jail by hardlinking the
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Module: MakeUser
19
# Author: Matt Giuca
364 by mattgiuca
setup.py: Fixed config command-line args (forgot to make general).
20
# Date:   1/2/2008
134 by mattgiuca
Added module: common/makeuser.py. Creates a new user jail by hardlinking the
21
364 by mattgiuca
setup.py: Fixed config command-line args (forgot to make general).
22
# Allows creation of users. This sets up the following:
23
# * User's jail and home directory within the jail.
24
# * Subversion repository (TODO)
25
# * Check out Subversion workspace into jail (TODO)
26
# * Database details for user (TODO)
27
# * Unix user account (TODO)
134 by mattgiuca
Added module: common/makeuser.py. Creates a new user jail by hardlinking the
28
29
import os
30
import shutil
31
32
import conf
33
34
def makeuser(username):
35
    """Creates a new user's jail space, in the jail directory as configured in
36
    conf.py.
37
38
    This expects there to be a "template" directory within the jail root which
39
    contains all the files for a sample student jail. It creates the student's
40
    directory in the jail root, by making a hard-link copy of every file in the
41
    template directory, recursively.
42
    """
43
    templatedir = os.path.join(conf.jail_base, 'template')
44
    if not os.path.isdir(templatedir):
45
        raise Exception("Template jail directory does not exist: " +
46
            templatedir)
47
    userdir = os.path.join(conf.jail_base, username)
48
    if os.path.exists(userdir):
49
        raise Exception("User's jail directory already exists: " +
50
            userdir)
51
52
    # Hard-link (copy aliasing) the entire tree over
53
    linktree(templatedir, userdir)
54
55
    # Set up the user's home directory
56
    os.mkdir(os.path.join(userdir, 'home', username))
57
58
def linktree(src, dst):
59
    """Recursively hard-link a directory tree using os.link().
60
61
    The destination directory must not already exist.
62
    If exception(s) occur, an Error is raised with a list of reasons.
63
64
    Symlinks are preserved (in fact, hard links are created which point to the
65
    symlinks).
66
67
    Code heavily based upon shutil.copytree from Python 2.5 library.
68
    """
69
    names = os.listdir(src)
70
    os.makedirs(dst)
71
    errors = []
72
    for name in names:
73
        srcname = os.path.join(src, name)
74
        dstname = os.path.join(dst, name)
75
        try:
76
            if os.path.isdir(srcname):
77
                linktree(srcname, dstname)
78
            else:
79
                os.link(srcname, dstname)
80
            # XXX What about devices, sockets etc.?
81
        except (IOError, os.error), why:
82
            errors.append((srcname, dstname, str(why)))
83
        # catch the Error from the recursive copytree so that we can
84
        # continue with other files
85
        except Exception, err:
86
            errors.append(err.args[0])
87
    try:
88
        shutil.copystat(src, dst)
89
    except WindowsError:
90
        # can't copy file access times on Windows
91
        pass
92
    except OSError, why:
93
        errors.extend((src, dst, str(why)))
94
    if errors:
95
        raise Exception, errors