From 171c746c7e30995a3af1307915e47c8494c89c5e Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 4 Jun 2016 09:36:48 +0300 Subject: [PATCH] gpg: move agent main code to __main__ --- trezor_agent/gpg/__main__.py | 27 ++++++++++++++++++++------- trezor_agent/gpg/agent.py | 19 +------------------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/trezor_agent/gpg/__main__.py b/trezor_agent/gpg/__main__.py index 19f861b..7b00cc1 100755 --- a/trezor_agent/gpg/__main__.py +++ b/trezor_agent/gpg/__main__.py @@ -7,7 +7,8 @@ import os import sys import time -from . import encode, keyring, proto +from . import agent, encode, keyring, proto +from .. import server log = logging.getLogger(__name__) @@ -28,6 +29,15 @@ def run_create(args): sys.stdout.write(proto.armor(result, 'PUBLIC KEY BLOCK')) +def run_agent(_): + """Run a simple GPG-agent server.""" + sock_path = os.path.expanduser('~/.gnupg/S.gpg-agent') + with server.unix_domain_socket_server(sock_path) as sock: + for conn in agent.yield_connections(sock): + with contextlib.closing(conn): + agent.handle_connection(conn) + + def main(): """Main function.""" p = argparse.ArgumentParser() @@ -36,12 +46,15 @@ def main(): subparsers.required = True subparsers.dest = 'command' - create = subparsers.add_parser('create') - create.add_argument('-s', '--subkey', action='store_true', default=False) - create.add_argument('--ecdh', action='store_true', default=False) - create.add_argument('-e', '--ecdsa-curve', default='nist256p1') - create.add_argument('-t', '--time', type=int, default=int(time.time())) - create.set_defaults(run=run_create) + create_cmd = subparsers.add_parser('create') + create_cmd.add_argument('-s', '--subkey', action='store_true', default=False) + create_cmd.add_argument('--ecdh', action='store_true', default=False) + create_cmd.add_argument('-e', '--ecdsa-curve', default='nist256p1') + create_cmd.add_argument('-t', '--time', type=int, default=int(time.time())) + create_cmd.set_defaults(run=run_create) + + agent_cmd = subparsers.add_parser('agent') + agent_cmd.set_defaults(run=run_agent) args = p.parse_args() logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO, diff --git a/trezor_agent/gpg/agent.py b/trezor_agent/gpg/agent.py index 47513ea..3a017b1 100644 --- a/trezor_agent/gpg/agent.py +++ b/trezor_agent/gpg/agent.py @@ -2,10 +2,9 @@ import binascii import contextlib import logging -import os from . import decode, encode, keyring -from .. import server, util +from .. import util log = logging.getLogger(__name__) @@ -141,19 +140,3 @@ def handle_connection(conn): return keyring.sendline(conn, b'OK') - - -def main(): - """Run a simple GPG-agent server.""" - logging.basicConfig(level=logging.INFO, - format='%(asctime)s %(levelname)-10s %(message)s') - - sock_path = os.path.expanduser('~/.gnupg/S.gpg-agent') - with server.unix_domain_socket_server(sock_path) as sock: - for conn in yield_connections(sock): - with contextlib.closing(conn): - handle_connection(conn) - - -if __name__ == '__main__': - main()