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

« back to all changes in this revision

Viewing changes to www/common/makeuser.py

  • Committer: mattgiuca
  • Date: 2008-01-12 15:35:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:201
Added "test" directory.
Added make_date_test.py, a short script I wrote to test the date format
algorithm committed in the previous revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
20
# Date:   9/1/2008
 
21
 
 
22
# Allows creation of users. This sets up the user's jail and home directory
 
23
# within the jail.
 
24
 
 
25
import os
 
26
import shutil
 
27
 
 
28
import conf
 
29
 
 
30
def makeuser(username):
 
31
    """Creates a new user's jail space, in the jail directory as configured in
 
32
    conf.py.
 
33
 
 
34
    This expects there to be a "template" directory within the jail root which
 
35
    contains all the files for a sample student jail. It creates the student's
 
36
    directory in the jail root, by making a hard-link copy of every file in the
 
37
    template directory, recursively.
 
38
    """
 
39
    templatedir = os.path.join(conf.jail_base, 'template')
 
40
    if not os.path.isdir(templatedir):
 
41
        raise Exception("Template jail directory does not exist: " +
 
42
            templatedir)
 
43
    userdir = os.path.join(conf.jail_base, username)
 
44
    if os.path.exists(userdir):
 
45
        raise Exception("User's jail directory already exists: " +
 
46
            userdir)
 
47
 
 
48
    # Hard-link (copy aliasing) the entire tree over
 
49
    linktree(templatedir, userdir)
 
50
 
 
51
    # Set up the user's home directory
 
52
    os.mkdir(os.path.join(userdir, 'home', username))
 
53
 
 
54
def linktree(src, dst):
 
55
    """Recursively hard-link a directory tree using os.link().
 
56
 
 
57
    The destination directory must not already exist.
 
58
    If exception(s) occur, an Error is raised with a list of reasons.
 
59
 
 
60
    Symlinks are preserved (in fact, hard links are created which point to the
 
61
    symlinks).
 
62
 
 
63
    Code heavily based upon shutil.copytree from Python 2.5 library.
 
64
    """
 
65
    names = os.listdir(src)
 
66
    os.makedirs(dst)
 
67
    errors = []
 
68
    for name in names:
 
69
        srcname = os.path.join(src, name)
 
70
        dstname = os.path.join(dst, name)
 
71
        try:
 
72
            if os.path.isdir(srcname):
 
73
                linktree(srcname, dstname)
 
74
            else:
 
75
                os.link(srcname, dstname)
 
76
            # XXX What about devices, sockets etc.?
 
77
        except (IOError, os.error), why:
 
78
            errors.append((srcname, dstname, str(why)))
 
79
        # catch the Error from the recursive copytree so that we can
 
80
        # continue with other files
 
81
        except Exception, err:
 
82
            errors.append(err.args[0])
 
83
    try:
 
84
        shutil.copystat(src, dst)
 
85
    except WindowsError:
 
86
        # can't copy file access times on Windows
 
87
        pass
 
88
    except OSError, why:
 
89
        errors.extend((src, dst, str(why)))
 
90
    if errors:
 
91
        raise Exception, errors