1 #!/usr/bin/env python 2 3 """ 4 Set delegates for a particular quota. 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> <delegate>... [ <options> ] 67 68 General options: 69 70 -j Indicates the journal directory location 71 -T Indicates the store type (the configured value if omitted) 72 """ % split(sys.argv[0])[1] 73 sys.exit(1) 74 75 # Override defaults if indicated. 76 77 getvalue = lambda value, default=None: value and value[0] or default 78 79 store_type = getvalue(store_type, settings["STORE_TYPE"]) 80 journal_dir = getvalue(journal_dir) 81 82 # Obtain store-related objects. 83 84 journal = get_journal(store_type, journal_dir) 85 f = getreader("utf-8")(sys.stdin) 86 delegates = get_table_from_stream(f, tab_separated=False) 87 journal.set_delegates(quota, [value for (value,) in delegates]) 88 89 # vim: tabstop=4 expandtab shiftwidth=4