imip-agent

tools/set_quota_limits.py

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