1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/set_quota_groups.py Fri May 13 01:10:08 2016 +0200
1.3 @@ -0,0 +1,100 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Set a mapping of users to groups for a quota.
1.8 +
1.9 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from codecs import getreader
1.26 +from os.path import abspath, split
1.27 +import sys
1.28 +
1.29 +# Find the modules.
1.30 +
1.31 +try:
1.32 + import imiptools
1.33 +except ImportError:
1.34 + parent = abspath(split(split(__file__)[0])[0])
1.35 + if split(parent)[1] == "imip-agent":
1.36 + sys.path.append(parent)
1.37 +
1.38 +from imiptools import config
1.39 +from imiptools.stores import get_journal
1.40 +from imiptools.text import get_table_from_stream
1.41 +
1.42 +# Main program.
1.43 +
1.44 +if __name__ == "__main__":
1.45 +
1.46 + # Interpret the command line arguments.
1.47 +
1.48 + args = []
1.49 + store_type = []
1.50 + journal_dir = []
1.51 +
1.52 + # Collect quota details first, switching to other arguments when encountering
1.53 + # switches.
1.54 +
1.55 + l = args
1.56 +
1.57 + for arg in sys.argv[1:]:
1.58 + if arg == "-T":
1.59 + l = store_type
1.60 + elif arg == "-j":
1.61 + l = journal_dir
1.62 + else:
1.63 + l.append(arg)
1.64 +
1.65 + try:
1.66 + quota, = args
1.67 + except ValueError:
1.68 + print >>sys.stderr, """\
1.69 +Usage: %s <quota> [ <options> ]
1.70 +
1.71 +Read from standard input a list of user-to-group mappings of the following
1.72 +form:
1.73 +
1.74 +<user> <user or group>
1.75 +
1.76 +For example:
1.77 +
1.78 +* all
1.79 +
1.80 +The values may be separated using any whitespace characters.
1.81 +
1.82 +General options:
1.83 +
1.84 +-j Indicates the journal directory location
1.85 +-T Indicates the store type (the configured value if omitted)
1.86 +""" % split(sys.argv[0])[1]
1.87 + sys.exit(1)
1.88 +
1.89 + # Override defaults if indicated.
1.90 +
1.91 + getvalue = lambda value, default=None: value and value[0] or default
1.92 +
1.93 + store_type = getvalue(store_type, config.STORE_TYPE)
1.94 + journal_dir = getvalue(journal_dir)
1.95 +
1.96 + # Obtain store-related objects.
1.97 +
1.98 + journal = get_journal(store_type, journal_dir)
1.99 + f = getreader("utf-8")(sys.stdin)
1.100 + groups = dict(get_table_from_stream(f, tab_separated=False))
1.101 + journal.set_groups(quota, groups)
1.102 +
1.103 +# vim: tabstop=4 expandtab shiftwidth=4