Mercurial > hg > ltpdarepo
annotate src/ltpdarepo/admin.py @ 202:10801d55c5d5
Reorganize command line interface code.
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Wed, 16 Nov 2011 19:08:45 +0100 |
parents | ac0a27a72b9e |
children | f8f898b39058 |
rev | line source |
---|---|
188
fbab144c296c
Add license information.
Daniele Nicolodi <daniele@grinta.net>
parents:
173
diff
changeset
|
1 # Copyright 2011 Daniele Nicolodi <nicolodi@science.unitn.it> |
fbab144c296c
Add license information.
Daniele Nicolodi <daniele@grinta.net>
parents:
173
diff
changeset
|
2 # |
fbab144c296c
Add license information.
Daniele Nicolodi <daniele@grinta.net>
parents:
173
diff
changeset
|
3 # This software may be used and distributed according to the terms of |
fbab144c296c
Add license information.
Daniele Nicolodi <daniele@grinta.net>
parents:
173
diff
changeset
|
4 # the GNU Affero General Public License version 3 or any later version. |
fbab144c296c
Add license information.
Daniele Nicolodi <daniele@grinta.net>
parents:
173
diff
changeset
|
5 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
6 import argparse |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
7 import logging |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
8 import sys |
42
5dfa71aadde8
Silence MySQLdb warnings in admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
37
diff
changeset
|
9 import warnings |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
10 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
11 from contextlib import contextmanager |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
12 from string import upper |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
13 |
0 | 14 import MySQLdb as mysql |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
15 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
16 import ltpdarepo |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
17 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
18 from .database import Database |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
19 from .install import install |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
20 from .tests.populate import populate |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
21 from .upgrade import upgrade |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
22 from .user import User |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
23 |
173
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
24 try: |
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
25 import sqlalchemy |
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
26 HAS_SQL_ALCHEMY = True |
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
27 except ImportError: |
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
28 HAS_SQL_ALCHEMY = False |
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
29 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
30 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
31 class verbosity(argparse.Action): |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
32 def __call__(self, *args, **kwargs): |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
33 # increse logging level |
125
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
34 logger = logging.getLogger('ltpdarepo') |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
35 logger.setLevel(logger.level - 10) |
0 | 36 |
37 | |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
38 def _bool(value): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
39 # parse string into boolean value |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
40 if value not in ('yes', 'no', 'true', 'false'): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
41 raise argparse.ArgumentTypeError('invalid value for bool parameter: %s' % value) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
42 return value in ('yes', 'true') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
43 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
44 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
45 class Commands(object): |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
46 def __init__(self): |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
47 self.parser = argparse.ArgumentParser(add_help=False) |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
48 self.commands = self.parser.add_subparsers(metavar='command') |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
49 options = self.parser.add_argument_group('options') |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
50 options.add_argument('-v', '--verbose', action=verbosity, |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
51 nargs=0, dest=argparse.SUPPRESS, |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
52 help='increase verbosity') |
0 | 53 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
54 def add(self, func, name=None): |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
55 name = name or func.__name__ |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
56 desc = func.__doc__ or ' ' |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
57 parser = self.commands.add_parser(name, |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
58 help=desc.splitlines()[0], |
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
59 description=desc, |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
60 add_help=False) |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
61 parser.set_defaults(command=func) |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
62 return parser |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
63 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
64 def parse(self, *args): |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
65 return self.parser.parse_args(*args) |
0 | 66 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
67 def dispatch(self): |
125
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
68 args = vars(self.parser.parse_args()) |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
69 command = args.pop('command') |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
70 try: |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
71 command(**args) |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
72 except Exception: |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
73 import traceback |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
74 sys.stderr.write(traceback.format_exc()) |
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
75 sys.exit(1) |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
76 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
77 def __iter__(self): |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
78 return iter(sorted(self.commands.choices.keys())) |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
79 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
80 def __getitem__(self, name): |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
81 return self.commands.choices[name] |
0 | 82 |
83 | |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
84 class Application(ltpdarepo.Application): |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
85 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
86 commands = Commands() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
87 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
88 def __init__(self, *args, **kwargs): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
89 super(Application, self).__init__(*args, **kwargs) |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
90 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
91 # database connection parameters that may be |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
92 # overridden by command line arguments |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
93 self._hostname = self.config['HOSTNAME'] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
94 self._database = self.config['DATABASE'] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
95 self._username = self.config['USERNAME'] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
96 self._password = self.config['PASSWORD'] |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
97 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
98 def __getattr__(self, name): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
99 if name in self.config: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
100 return self.config[name] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
101 return self[name] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
102 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
103 def connect(self, **kwargs): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
104 # open connection to the database |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
105 return mysql.connect(host=self._hostname, user=self._username, |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
106 passwd=self._password, charset='utf8', **kwargs) |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
107 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
108 @contextmanager |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
109 def interact(self): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
110 # fake request |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
111 ctx = self.test_request_context('') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
112 ctx.push() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
113 # execute before request handlers |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
114 self.preprocess_request() |
173
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
115 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
116 yield |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
117 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
118 # execute after request handlers |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
119 self.process_response(self.response_class()) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
120 ctx.pop() |
173
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
121 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
122 def dispatch(self): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
123 args = vars(self.commands.parser.parse_args()) |
173
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
124 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
125 # common parameters |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
126 username = args.pop('_username', None) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
127 if username: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
128 self._username = username |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
129 password = args.pop('_password', None) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
130 if username or password: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
131 self._password = password |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
132 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
133 # run command |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
134 command = args.pop('command') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
135 command(self, **args) |
173
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
136 |
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
137 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
138 def help(self, name=None, out=sys.stderr): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
139 """provide commands help""" |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
140 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
141 if name is not None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
142 print >>out, self.commands[name].format_help().strip() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
143 return |
0 | 144 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
145 print >>out, "LTPDA Repository administration tool" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
146 print >>out, self.commands.parser.format_usage().strip() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
147 print >>out, "" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
148 print >>out, "commands:" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
149 for cmd in self.commands: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
150 doc = self.commands[cmd].description or ' ' |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
151 print >>out, " %-15s %s" % (cmd, doc.splitlines()[0]) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
152 print >>out, "" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
153 print >>out, "options:" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
154 print >>out, " %-15s %s" % ("-v, --verbose", "increase verbosity. may be specified multiple times") |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
155 print >>out, "" |
0 | 156 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
157 cmd = commands.add(help) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
158 cmd.add_argument('name', nargs='?', help='command', metavar='NAME') |
0 | 159 |
160 | |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
161 def user(self, username): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
162 """show user""" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
163 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
164 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
165 user = User.load(username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
166 if user is None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
167 raise Exception("user '%s' not found" % username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
168 for name in user.__slots__: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
169 print '%12s: %s' % (name, getattr(user, name)) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
170 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
171 cmd = commands.add(user) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
172 cmd.add_argument('username') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
173 |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
174 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
175 def useradd(self, username, **kwargs): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
176 """create user account""" |
0 | 177 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
178 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
179 user = User.load(username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
180 if user is not None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
181 raise Exception("user '%s' exists" % username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
182 user = User(username, **kwargs) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
183 user.create() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
184 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
185 cmd = commands.add(useradd) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
186 cmd.add_argument('username', metavar='USERNAME') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
187 cmd.add_argument('-a', '--admin', default=argparse.SUPPRESS, type=_bool) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
188 cmd.add_argument('-n', '--name', default=argparse.SUPPRESS) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
189 cmd.add_argument('-s', '--surname', default=argparse.SUPPRESS) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
190 cmd.add_argument('-e', '--email', default=argparse.SUPPRESS) |
0 | 191 |
192 | |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
193 def userdel(self, username): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
194 """delete user account""" |
0 | 195 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
196 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
197 user = User.load(username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
198 if user is None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
199 raise Exception("user '%s' not found" % username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
200 user.delete() |
0 | 201 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
202 cmd = commands.add(userdel) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
203 cmd.add_argument('username', metavar='USERNAME') |
0 | 204 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
205 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
206 def usermod(self, username, **kwargs): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
207 """modify user account""" |
125
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
208 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
209 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
210 user = User.load(username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
211 if user is None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
212 raise Exception("user '%s' not found" % username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
213 # update user |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
214 for name, value in kwargs.iteritems(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
215 setattr(user, name, value) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
216 user.save() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
217 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
218 cmd = commands.add(usermod) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
219 cmd.add_argument('username', metavar='username') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
220 cmd.add_argument('-a', '--admin', default=argparse.SUPPRESS, type=_bool) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
221 cmd.add_argument('-n', '--name', default=argparse.SUPPRESS) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
222 cmd.add_argument('-s', '--surname', default=argparse.SUPPRESS) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
223 cmd.add_argument('-e', '--email', default=argparse.SUPPRESS) |
0 | 224 |
125
ab38e8a0eb8d
Minor tweaks to the admin command line interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
109
diff
changeset
|
225 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
226 def passwd(self, username, password): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
227 """change password for a given user""" |
109
598f84f0db4e
Extend test to exercise the permissions management interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
89
diff
changeset
|
228 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
229 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
230 user = User.load(username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
231 if user is None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
232 raise Exception("user '%s' not found" % username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
233 user.passwd(password) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
234 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
235 cmd = commands.add(passwd) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
236 cmd.add_argument('username', metavar='USERNAME') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
237 cmd.add_argument('password', metavar='PASSWORD') |
0 | 238 |
239 | |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
240 def grant(self, username, database, **privs): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
241 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
242 conn = self.connect() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
243 curs = conn.cursor() |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
244 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
245 for priv in privs: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
246 curs.execute('''GRANT %s ON %s.* TO %%s@%%s''' % (priv, database), (username, '%')) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
247 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
248 conn.commit() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
249 conn.close() |
0 | 250 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
251 def _grant(self, username, database, privs): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
252 """grant permissions to users""" |
0 | 253 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
254 privs = dict((priv, True) for priv in privs) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
255 return self.grant(username, database, **privs) |
30
1b37af4b842c
Fix help display in command line tool and add minimal documentation.
Daniele Nicolodi <daniele@grinta.net>
parents:
26
diff
changeset
|
256 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
257 cmd = commands.add(_grant, name='grant') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
258 cmd.add_argument('username') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
259 cmd.add_argument('database') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
260 cmd.add_argument('privs', metavar='PRIV', nargs='+', type=upper, |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
261 choices=frozenset(['SELECT', 'INSERT', 'UPDATE', 'DELETE'])) |
0 | 262 |
263 | |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
264 def privileges(self, username, database=None): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
265 """show privileges for given user""" |
0 | 266 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
267 conn = self.connect() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
268 curs = conn.cursor() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
269 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
270 from collections import defaultdict |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
271 privs = defaultdict(lambda: {'select': False, 'insert': False, 'update': False, 'delete': False}) |
0 | 272 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
273 curs.execute('''SELECT DISTINCT Db, Select_priv, Insert_priv, |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
274 Update_priv, Delete_priv FROM mysql.db WHERE User=%s |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
275 ORDER BY Db''', username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
276 for row in curs.fetchall(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
277 privs[row[0]] = {'select': row[1] == 'Y', |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
278 'insert': row[2] == 'Y', |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
279 'update': row[3] == 'Y', |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
280 'delete': row[4] == 'Y'} |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
281 conn.close() |
0 | 282 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
283 if database is not None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
284 return privs[database] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
285 return privs |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
286 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
287 def _privileges(self, username): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
288 """show user privileges""" |
0 | 289 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
290 privs = self.privileges(username) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
291 if privs: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
292 headers = tuple('database select insert update delete'.split()) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
293 print '%-20s %7s %7s %7s %7s' % headers |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
294 for db, priv in privs.iteritems(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
295 values = [priv[x] and 'Y' or 'N' for x in 'select insert update delete'.split()] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
296 print '%-20s' % db, '%7s %7s %7s %7s' % tuple(values) |
0 | 297 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
298 cmd = commands.add(_privileges, name='privs') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
299 cmd.add_argument('username', metavar='USERNAME') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
300 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
301 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
302 def createdb(self, database, name='', description=''): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
303 """create database""" |
0 | 304 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
305 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
306 db = Database.load(database) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
307 if db is not None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
308 raise Exception("database '%s' exists" % database) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
309 db = Database(database, name=name, description=description) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
310 db.create() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
311 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
312 cmd = commands.add(createdb) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
313 cmd.add_argument('database', help='database name') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
314 cmd.add_argument('-n', '--name', default='') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
315 cmd.add_argument('-d', '--description', default='') |
0 | 316 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
317 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
318 def dropdb(self, database): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
319 """delete database""" |
0 | 320 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
321 with self.interact(): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
322 db = Database.load(database) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
323 if db is None: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
324 raise Exception('database "%s" not found' % database) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
325 db.drop() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
326 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
327 cmd = commands.add(dropdb) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
328 cmd.add_argument('database', help='database name') |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
329 |
0 | 330 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
331 # install |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
332 install = install |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
333 cmd = commands.add(install) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
334 cmd.add_argument('--user', '-u', dest='_username', metavar='USER') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
335 cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='') |
0 | 336 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
337 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
338 # upgrade |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
339 upgrade = upgrade |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
340 cmd = commands.add(upgrade) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
341 cmd.add_argument('--user', '-u', dest='_username', metavar='USER') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
342 cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
343 cmd.add_argument('--from', type=float, dest='_from', metavar='REV') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
344 cmd.add_argument('--to', type=float, dest='_to', metavar='REV') |
54
99db71455ec2
Create valid 'tsdata' test objects when populating the database.
Daniele Nicolodi <daniele@grinta.net>
parents:
43
diff
changeset
|
345 |
173
e2a9b0c3d83e
Expose the database structure dump utility through the admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
167
diff
changeset
|
346 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
347 # populate |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
348 populate = populate |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
349 cmd = commands.add(populate) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
350 cmd.add_argument('--user', '-u', dest='_username', metavar='USER') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
351 cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
352 cmd.add_argument('database', help='database') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
353 cmd.add_argument('nobjs', nargs='?', default=30, type=int, help='number of obejcts') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
354 |
141
2827664bb785
Randomize test objects submission time.
Daniele Nicolodi <daniele@grinta.net>
parents:
132
diff
changeset
|
355 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
356 # dump |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
357 if HAS_SQL_ALCHEMY: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
358 def dump(self, database, tables=None, out=sys.stdout): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
359 """dump database structure""" |
0 | 360 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
361 from .tests import schema |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
362 schema.dump(self.USERNAME, self.PASSWORD, self.HOSTNAME, |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
363 database, tables=tables, out=out) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
364 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
365 cmd = commands.add(dump) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
366 cmd.add_argument('database') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
367 cmd.add_argument('--user', '-u', dest='_username', metavar='USER') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
368 cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
369 cmd.add_argument('--tables', nargs='+') |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
370 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
371 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
372 def wipe(self, yes=True): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
373 """delete all database content""" |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
374 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
375 # connect to the database |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
376 conn = self.connect() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
377 curs = conn.cursor() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
378 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
379 # databases list |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
380 curs.execute("""SHOW DATABASES""") |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
381 databases = [row[0] for row in curs.fetchall()] |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
382 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
383 # delete databases |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
384 for db in databases: |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
385 if db not in ('mysql', 'information_schema'): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
386 curs.execute("""DROP DATABASE `%s`""" % db) |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
387 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
388 # delete users |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
389 curs.execute("""DELETE FROM mysql.user |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
390 WHERE user <> 'root' and user <> ''""") |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
391 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
392 # delete privileges |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
393 curs.execute("""DELETE FROM mysql.db""") |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
394 curs.execute("""DELETE FROM mysql.tables_priv""") |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
395 curs.execute("""DELETE FROM mysql.columns_priv""") |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
396 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
397 # flush privileges |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
398 curs.execute("""FLUSH PRIVILEGES""") |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
399 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
400 conn.commit() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
401 conn.close() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
402 |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
403 cmd = commands.add(wipe) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
404 cmd.add_argument('--user', '-u', dest='_username', metavar='USER') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
405 cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
406 cmd.add_argument('--yes', action='store_true', required=True) |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
407 |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
408 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
409 def setup(self): |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
410 """setup test environment""" |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
411 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
412 self.install() |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
413 self.useradd('u1', admin=True) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
414 self.passwd('u1', 'u1') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
415 self.createdb('db1', description=u'Test database One') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
416 self.createdb('db2', description=u'Test database Tw\u00F6') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
417 self.populate('db1', 30) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
418 self.grant('u1', 'db1', select=True, insert=True, update=True, delete=True) |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
419 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
420 cmd = commands.add(setup) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
421 cmd.add_argument('--user', '-u', dest='_username', metavar='USER') |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
422 cmd.add_argument('--passwd', '-p', dest='_password', metavar='PASSWORD', default='') |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
423 |
0 | 424 |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
425 def main(conf=None): |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
426 # setup logging |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
427 handler = logging.StreamHandler() |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
428 handler.setLevel(logging.DEBUG) |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
429 logger = logging.getLogger('ltpdarepo') |
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
430 logger.addHandler(handler) |
34
d87ede392d48
Decrease default verbosity level to 'warning'.
Daniele Nicolodi <daniele@grinta.net>
parents:
30
diff
changeset
|
431 logger.setLevel(logging.WARNING) |
0 | 432 |
42
5dfa71aadde8
Silence MySQLdb warnings in admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
37
diff
changeset
|
433 # silence annoying mysql warnings |
167
28debb8307a5
Really silence mysql warnings.
Daniele Nicolodi <daniele@grinta.net>
parents:
163
diff
changeset
|
434 warnings.filterwarnings('ignore', category=mysql.Warning) |
42
5dfa71aadde8
Silence MySQLdb warnings in admin interface.
Daniele Nicolodi <daniele@grinta.net>
parents:
37
diff
changeset
|
435 |
25
04b93e15bb71
Rework command line administration tool.
Daniele Nicolodi <daniele@grinta.net>
parents:
0
diff
changeset
|
436 # dispatch command line |
202
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
437 app = Application(conf) |
10801d55c5d5
Reorganize command line interface code.
Daniele Nicolodi <daniele@grinta.net>
parents:
201
diff
changeset
|
438 app.dispatch() |