~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
#!/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

"""Refresh parts of the filesystem that are generated from the database.

In particular:
 - missing user jails are created
 - missing user and group Subversion repositories are created
 - the Subversion password file is updated
 - the Subversion authorisation files are rewritten
"""

import datetime
import os
import os.path
import shutil
import sys

from ivle.config import Config
from ivle.database import get_store, ProjectGroup, User
import ivle.makeuser
# Python 2.5's shutil.rmtree follows symlinks!
from ivle.util import safe_rmtree

junk_directory_suffix = (
    '-removed-%s' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))

def get_junk_dir(path):
    return os.path.normpath(path) + junk_directory_suffix

def junk(parent, name):
    """Move the named directory into a junk directory alongside the parent."""
    if not os.path.exists(get_junk_dir(parent)):
        os.makedirs(get_junk_dir(parent))
    shutil.move(
        os.path.join(parent, name),
        os.path.join(get_junk_dir(parent), name))


config = Config()
store = get_store(config)

active_users = store.find(User, state=u'enabled').order_by(User.login)

print >>sys.stderr, "Refreshing active user jails..."
for user in active_users:
    ivle.makeuser.make_jail(user, config)

present_jails = set(
    login for login in os.listdir(config['paths']['jails']['src'])
    if not (login.startswith('__') and login.endswith('__')))

print >>sys.stderr, "Junking extra user jails..."
for jail in present_jails - set(user.login for user in active_users):
    print ' - %s' % jail
    junk(config['paths']['jails']['src'], jail)

repo_root = config['paths']['svn']['repo_path']

print >>sys.stderr, "Creating missing Subversion user repositories..."
present_user_repos= set(
    login for login in os.listdir(os.path.join(repo_root, 'users')))

for repo in set(user.login for user in active_users) - present_user_repos:
    print ' - %s' % repo
    ivle.makeuser.make_svn_repo(
        os.path.join(repo_root, 'users', repo), throw_on_error=True)

print >>sys.stderr, "Junking extra Subversion user repositories..."
for repo in present_user_repos - set(user.login for user in active_users):
    print ' - %s' % repo
    junk(os.path.join(repo_root, 'users'), repo)


print >>sys.stderr, "Creating missing Subversion group repositories..."
present_group_repos = set(
    group for group in os.listdir(os.path.join(repo_root, 'groups')))

active_group_identifiers = set("_".join(
    [group.project_set.offering.subject.short_name,
     group.project_set.offering.semester.year,
     group.project_set.offering.semester.semester,
     group.name]) for group in store.find(ProjectGroup))

for repo in active_group_identifiers - present_group_repos:
    print ' - %s' % repo
    ivle.makeuser.make_svn_repo(
        os.path.join(repo_root, 'groups', repo), throw_on_error=True)

print >>sys.stderr, "Junking extra Subversion user repositories..."
for repo in present_group_repos - active_group_identifiers:
    print ' - %s' % repo
    junk(os.path.join(repo_root, 'groups'), repo)


print >>sys.stderr, "Rebuilding Subversion user configuration..."
ivle.makeuser.rebuild_svn_config(store, config)
print >>sys.stderr, "Rebuilding Subversion group configuration..."
ivle.makeuser.rebuild_svn_group_config(store, config)