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

1165.5.7 by William Grant
Add an ivle-refreshfilesystem script, which currently just rewrites svn(-group).conf.
1
#!/usr/bin/env python
2
# IVLE - Informatics Virtual Learning Environment
3
# Copyright (C) 2007-2009 The University of Melbourne
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19
"""Refresh parts of the filesystem that are generated from the database.
20
1344.1.1 by William Grant
ivle-refreshfilesystem now refreshes jails, and removes extra jails.
21
In particular:
22
 - missing user jails are created
23
 - missing user and group Subversion repositories are created
24
 - the Subversion password file is updated
25
 - the Subversion authorisation files are rewritten
1165.5.7 by William Grant
Add an ivle-refreshfilesystem script, which currently just rewrites svn(-group).conf.
26
"""
27
1344.1.3 by William Grant
Move junked directories elsewhere -- don't delete them.
28
import datetime
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
29
import logging
1344.1.1 by William Grant
ivle-refreshfilesystem now refreshes jails, and removes extra jails.
30
import os
1344.1.3 by William Grant
Move junked directories elsewhere -- don't delete them.
31
import os.path
32
import shutil
1165.5.7 by William Grant
Add an ivle-refreshfilesystem script, which currently just rewrites svn(-group).conf.
33
34
from ivle.config import Config
1344.1.2 by William Grant
Create and remove Subversion repos too.
35
from ivle.database import get_store, ProjectGroup, User
1165.5.7 by William Grant
Add an ivle-refreshfilesystem script, which currently just rewrites svn(-group).conf.
36
import ivle.makeuser
1349.1.1 by William Grant
Refactor into a function.
37
38
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
39
logging.basicConfig(
40
    format='%(asctime)s %(levelname)s %(message)s',
41
    level=logging.INFO)
42
43
1349.1.1 by William Grant
Refactor into a function.
44
JUNK_DIRECTORY_SUFFIX = (
1344.1.3 by William Grant
Move junked directories elsewhere -- don't delete them.
45
    '-removed-%s' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))
46
1349.1.1 by William Grant
Refactor into a function.
47
1344.1.3 by William Grant
Move junked directories elsewhere -- don't delete them.
48
def get_junk_dir(path):
1349.1.1 by William Grant
Refactor into a function.
49
    return os.path.normpath(path) + JUNK_DIRECTORY_SUFFIX
50
1344.1.3 by William Grant
Move junked directories elsewhere -- don't delete them.
51
52
def junk(parent, name):
53
    """Move the named directory into a junk directory alongside the parent."""
1347 by William Grant
Don't attempt to create the junk dir if it already exists.
54
    if not os.path.exists(get_junk_dir(parent)):
55
        os.makedirs(get_junk_dir(parent))
1344.1.3 by William Grant
Move junked directories elsewhere -- don't delete them.
56
    shutil.move(
57
        os.path.join(parent, name),
58
        os.path.join(get_junk_dir(parent), name))
59
60
1349.1.1 by William Grant
Refactor into a function.
61
def refresh_filesystem(config, store):
62
    active_users = store.find(User, state=u'enabled').order_by(User.login)
63
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
64
    logging.info("Refreshing active user jails.")
1349.1.1 by William Grant
Refactor into a function.
65
    for user in active_users:
66
        ivle.makeuser.make_jail(user, config)
67
68
    present_jails = set(
69
        login for login in os.listdir(config['paths']['jails']['src'])
70
        if not (login.startswith('__') and login.endswith('__')))
71
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
72
    logging.info("Junking extra user jails...")
1349.1.1 by William Grant
Refactor into a function.
73
    for jail in present_jails - set(user.login for user in active_users):
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
74
        logging.info(' - %s' % jail)
1349.1.1 by William Grant
Refactor into a function.
75
        junk(config['paths']['jails']['src'], jail)
76
77
    repo_root = config['paths']['svn']['repo_path']
78
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
79
    logging.info("Creating missing Subversion user repositories.")
1349.1.3 by William Grant
Add missing space.
80
    present_user_repos = set(
1349.1.1 by William Grant
Refactor into a function.
81
        login for login in os.listdir(os.path.join(repo_root, 'users')))
82
83
    for repo in set(user.login for user in active_users) - present_user_repos:
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
84
        logging.info(' - %s' % repo)
1349.1.1 by William Grant
Refactor into a function.
85
        ivle.makeuser.make_svn_repo(
86
            os.path.join(repo_root, 'users', repo), throw_on_error=True)
87
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
88
    logging.info("Junking extra Subversion user repositories.")
1349.1.1 by William Grant
Refactor into a function.
89
    for repo in present_user_repos - set(user.login for user in active_users):
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
90
        logging.info(' - %s' % repo)
1349.1.1 by William Grant
Refactor into a function.
91
        junk(os.path.join(repo_root, 'users'), repo)
92
93
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
94
    logging.info("Creating missing Subversion group repositories.")
1349.1.1 by William Grant
Refactor into a function.
95
    present_group_repos = set(
96
        group for group in os.listdir(os.path.join(repo_root, 'groups')))
97
98
    active_group_identifiers = set("_".join(
99
        [group.project_set.offering.subject.short_name,
100
         group.project_set.offering.semester.year,
101
         group.project_set.offering.semester.semester,
102
         group.name]) for group in store.find(ProjectGroup))
103
104
    for repo in active_group_identifiers - present_group_repos:
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
105
        logging.info(' - %s' % repo)
1349.1.1 by William Grant
Refactor into a function.
106
        ivle.makeuser.make_svn_repo(
107
            os.path.join(repo_root, 'groups', repo), throw_on_error=True)
108
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
109
    logging.info("Junking extra Subversion user repositories.")
1349.1.1 by William Grant
Refactor into a function.
110
    for repo in present_group_repos - active_group_identifiers:
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
111
        logging.info(' - %s' % repo)
1349.1.1 by William Grant
Refactor into a function.
112
        junk(os.path.join(repo_root, 'groups'), repo)
113
114
1351 by William Grant
ivle-refreshfilesystem will now rebuild the Subversion htpasswd file.
115
    logging.info("Rebuild Subversion password file.")
116
    for user in store.find(User, state=u'enabled'):
117
        ivle.makeuser.make_svn_auth(
118
            store, user.login, config, throw_on_error=True)
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
119
    logging.info("Rebuilding Subversion user configuration.")
1349.1.1 by William Grant
Refactor into a function.
120
    ivle.makeuser.rebuild_svn_config(store, config)
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
121
    logging.info("Rebuilding Subversion group configuration.")
1349.1.1 by William Grant
Refactor into a function.
122
    ivle.makeuser.rebuild_svn_group_config(store, config)
123
124
125
if __name__ == '__main__':
126
    config = Config()
127
    store = get_store(config)
1349.1.2 by William Grant
Use logging, rather than printing to stderr directly.
128
    refresh_filesystem(config, store)