imip-agent

Annotated tools/set_quota_limits.py

1465:0c623c86704f
2020-08-03 Paul Boddie Changed diagram font to sans-serif.
paul@1089 1
#!/usr/bin/env python
paul@1089 2
paul@1089 3
"""
paul@1176 4
Set quota limits for a collection of user groups.
paul@1089 5
paul@1215 6
Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
paul@1089 7
paul@1089 8
This program is free software; you can redistribute it and/or modify it under
paul@1089 9
the terms of the GNU General Public License as published by the Free Software
paul@1089 10
Foundation; either version 3 of the License, or (at your option) any later
paul@1089 11
version.
paul@1089 12
paul@1089 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@1089 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@1089 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@1089 16
details.
paul@1089 17
paul@1089 18
You should have received a copy of the GNU General Public License along with
paul@1089 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@1089 20
"""
paul@1089 21
paul@1176 22
from codecs import getreader
paul@1140 23
from os.path import abspath, split
paul@1089 24
import sys
paul@1089 25
paul@1089 26
# Find the modules.
paul@1089 27
paul@1089 28
try:
paul@1089 29
    import imiptools
paul@1089 30
except ImportError:
paul@1140 31
    parent = abspath(split(split(__file__)[0])[0])
paul@1089 32
    if split(parent)[1] == "imip-agent":
paul@1089 33
        sys.path.append(parent)
paul@1089 34
paul@1215 35
from imiptools.config import settings
paul@1089 36
from imiptools.stores import get_journal
paul@1176 37
from imiptools.text import get_table_from_stream
paul@1089 38
paul@1089 39
# Main program.
paul@1089 40
paul@1089 41
if __name__ == "__main__":
paul@1089 42
paul@1089 43
    # Interpret the command line arguments.
paul@1089 44
paul@1089 45
    args = []
paul@1089 46
    store_type = []
paul@1089 47
    journal_dir = []
paul@1089 48
paul@1089 49
    # Collect quota details first, switching to other arguments when encountering
paul@1089 50
    # switches.
paul@1089 51
paul@1089 52
    l = args
paul@1089 53
paul@1089 54
    for arg in sys.argv[1:]:
paul@1089 55
        if arg == "-T":
paul@1089 56
            l = store_type
paul@1089 57
        elif arg == "-j":
paul@1089 58
            l = journal_dir
paul@1089 59
        else:
paul@1089 60
            l.append(arg)
paul@1089 61
paul@1089 62
    try:
paul@1176 63
        quota, = args
paul@1089 64
    except ValueError:
paul@1089 65
        print >>sys.stderr, """\
paul@1176 66
Usage: %s <quota> [ <options> ]
paul@1176 67
paul@1176 68
Read from standard input a list of group-to-limit mappings of the following
paul@1176 69
form:
paul@1176 70
paul@1176 71
<user or group> <limit>
paul@1176 72
paul@1176 73
For example:
paul@1176 74
paul@1176 75
* PT1H
paul@1176 76
paul@1176 77
The values may be separated using any whitespace characters.
paul@1089 78
paul@1089 79
General options:
paul@1089 80
paul@1089 81
-j  Indicates the journal directory location
paul@1089 82
-T  Indicates the store type (the configured value if omitted)
paul@1089 83
""" % split(sys.argv[0])[1]
paul@1089 84
        sys.exit(1)
paul@1089 85
paul@1089 86
    # Override defaults if indicated.
paul@1089 87
paul@1089 88
    getvalue = lambda value, default=None: value and value[0] or default
paul@1089 89
paul@1215 90
    store_type = getvalue(store_type, settings["STORE_TYPE"])
paul@1089 91
    journal_dir = getvalue(journal_dir)
paul@1089 92
paul@1089 93
    # Obtain store-related objects.
paul@1089 94
paul@1089 95
    journal = get_journal(store_type, journal_dir)
paul@1338 96
    if not journal:
paul@1338 97
        print >>sys.stderr, "Journal not present."
paul@1338 98
        sys.exit(1)
paul@1338 99
paul@1176 100
    f = getreader("utf-8")(sys.stdin)
paul@1176 101
    limits = dict(get_table_from_stream(f, tab_separated=False))
paul@1176 102
    journal.set_limits(quota, limits)
paul@1089 103
paul@1089 104
# vim: tabstop=4 expandtab shiftwidth=4