1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
22
# Allows creation of users. This sets up the user's jail and home directory
30
def makeuser(username):
31
"""Creates a new user's jail space, in the jail directory as configured in
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.
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: " +
43
userdir = os.path.join(conf.jail_base, username)
44
if os.path.exists(userdir):
45
raise Exception("User's jail directory already exists: " +
48
# Hard-link (copy aliasing) the entire tree over
49
linktree(templatedir, userdir)
51
# Set up the user's home directory
52
os.mkdir(os.path.join(userdir, 'home', username))
54
def linktree(src, dst):
55
"""Recursively hard-link a directory tree using os.link().
57
The destination directory must not already exist.
58
If exception(s) occur, an Error is raised with a list of reasons.
60
Symlinks are preserved (in fact, hard links are created which point to the
63
Code heavily based upon shutil.copytree from Python 2.5 library.
65
names = os.listdir(src)
69
srcname = os.path.join(src, name)
70
dstname = os.path.join(dst, name)
72
if os.path.isdir(srcname):
73
linktree(srcname, dstname)
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])
84
shutil.copystat(src, dst)
86
# can't copy file access times on Windows
89
errors.extend((src, dst, str(why)))
91
raise Exception, errors