File: set_delegates.py

package info (click to toggle)
imip-agent 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,056 kB
  • sloc: python: 9,888; sh: 4,480; sql: 144; makefile: 8
file content (89 lines) | stat: -rwxr-xr-x 2,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python

"""
Set delegates for a particular quota.

Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from codecs import getreader
from os.path import abspath, split
import sys

# Find the modules.

try:
    import imiptools
except ImportError:
    parent = abspath(split(split(__file__)[0])[0])
    if split(parent)[1] == "imip-agent":
        sys.path.append(parent)

from imiptools.config import settings
from imiptools.stores import get_journal
from imiptools.text import get_table_from_stream

# Main program.

if __name__ == "__main__":

    # Interpret the command line arguments.

    args = []
    store_type = []
    journal_dir = []

    # Collect quota details first, switching to other arguments when encountering
    # switches.

    l = args

    for arg in sys.argv[1:]:
        if arg == "-T":
            l = store_type
        elif arg == "-j":
            l = journal_dir
        else:
            l.append(arg)

    try:
        quota, = args
    except ValueError:
        print >>sys.stderr, """\
Usage: %s <quota> <delegate>... [ <options> ]

General options:

-j  Indicates the journal directory location
-T  Indicates the store type (the configured value if omitted)
""" % split(sys.argv[0])[1]
        sys.exit(1)

    # Override defaults if indicated.

    getvalue = lambda value, default=None: value and value[0] or default

    store_type = getvalue(store_type, settings["STORE_TYPE"])
    journal_dir = getvalue(journal_dir)

    # Obtain store-related objects.

    journal = get_journal(store_type, journal_dir)
    f = getreader("utf-8")(sys.stdin)
    delegates = get_table_from_stream(f, tab_separated=False)
    journal.set_delegates(quota, [value for (value,) in delegates])

# vim: tabstop=4 expandtab shiftwidth=4