~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
#!/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 logging
import os
import os.path
import shutil

from ivle.config import Config
from ivle.database import get_store, ProjectGroup, User
import ivle.makeuser


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


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))


def refresh_filesystem(config, store):
    active_users = store.find(User, state=u'enabled').order_by(User.login)

    logging.info("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('__')))

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

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

    logging.info("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:
        logging.info(' - %s' % repo)
        ivle.makeuser.make_svn_repo(
            os.path.join(repo_root, 'users', repo), throw_on_error=True)

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


    logging.info("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:
        logging.info(' - %s' % repo)
        ivle.makeuser.make_svn_repo(
            os.path.join(repo_root, 'groups', repo), throw_on_error=True)

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


    logging.info("Rebuild Subversion password file.")
    for user in store.find(User, state=u'enabled'):
        ivle.makeuser.make_svn_auth(
            store, user.login, config, throw_on_error=True)
    logging.info("Rebuilding Subversion user configuration.")
    ivle.makeuser.rebuild_svn_config(store, config)
    logging.info("Rebuilding Subversion group configuration.")
    ivle.makeuser.rebuild_svn_group_config(store, config)


if __name__ == '__main__':
    config = Config()
    store = get_store(config)
    refresh_filesystem(config, store)