*: Run update-copyright.py
[pyassuan.git] / bin / get-info.py
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
4 #
5 # This file is part of pyassuan.
6 #
7 # pyassuan is free software: you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation, either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # pyassuan is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # pyassuan.  If not, see <http://www.gnu.org/licenses/>.
18
19 """Simple pinentry program for getting server info.
20 """
21
22 from pyassuan import __version__
23 from pyassuan import client as _client
24 from pyassuan import common as _common
25 from pyassuan import error as _error
26
27
28 if __name__ == '__main__':
29     import argparse
30     import logging
31
32     parser = argparse.ArgumentParser(description=__doc__)
33     parser.add_argument(
34         '-v', '--version', action='version',
35         version='%(prog)s {}'.format(__version__))
36     parser.add_argument(
37         '-V', '--verbose', action='count', default=0,
38         help='increase verbosity')
39     parser.add_argument(
40         'filename',
41         help="path to server's unix socket")
42
43     args = parser.parse_args()
44
45     client = _client.AssuanClient(name='get-info', close_on_disconnect=True)
46
47     if args.verbose:
48         client.logger.setLevel(max(
49                 logging.DEBUG, client.logger.level - 10*args.verbose))
50
51     client.connect(socket_path=args.filename)
52     try:
53         response = client.read_response()
54         assert response.type == 'OK', response
55         client.make_request(_common.Request('HELP'))
56         client.make_request(_common.Request('HELP GETINFO'))
57         for attribute in ['version', 'pid', 'socket_name', 'ssh_socket_name']:
58             try:
59                 client.make_request(_common.Request('GETINFO', attribute))
60             except _error.AssuanError as e:
61                 if e.message.startswith('No data'):
62                     pass
63                 else:
64                     raise
65     finally:
66         client.make_request(_common.Request('BYE'))
67         client.disconnect()