1#! /usr/bin/env python3 2# 3# Copyright (C) 2018 Garmin Ltd. 4# 5# SPDX-License-Identifier: GPL-2.0-only 6# 7 8import os 9import sys 10import logging 11import argparse 12import sqlite3 13 14sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib')) 15 16import hashserv 17 18VERSION = "1.0.0" 19 20DEFAULT_BIND = 'unix://./hashserve.sock' 21 22 23def main(): 24 parser = argparse.ArgumentParser(description='Hash Equivalence Reference Server. Version=%s' % VERSION, 25 epilog='''The bind address is the path to a unix domain socket if it is 26 prefixed with "unix://". Otherwise, it is an IP address 27 and port in form ADDRESS:PORT. To bind to all addresses, leave 28 the ADDRESS empty, e.g. "--bind :8686". To bind to a specific 29 IPv6 address, enclose the address in "[]", e.g. 30 "--bind [::1]:8686"''' 31 ) 32 33 parser.add_argument('-b', '--bind', default=DEFAULT_BIND, help='Bind address (default "%(default)s")') 34 parser.add_argument('-d', '--database', default='./hashserv.db', help='Database file (default "%(default)s")') 35 parser.add_argument('-l', '--log', default='WARNING', help='Set logging level') 36 parser.add_argument('-u', '--upstream', help='Upstream hashserv to pull hashes from') 37 parser.add_argument('-r', '--read-only', action='store_true', help='Disallow write operations from clients') 38 39 args = parser.parse_args() 40 41 logger = logging.getLogger('hashserv') 42 43 level = getattr(logging, args.log.upper(), None) 44 if not isinstance(level, int): 45 raise ValueError('Invalid log level: %s' % args.log) 46 47 logger.setLevel(level) 48 console = logging.StreamHandler() 49 console.setLevel(level) 50 logger.addHandler(console) 51 52 server = hashserv.create_server(args.bind, args.database, upstream=args.upstream, read_only=args.read_only) 53 server.serve_forever() 54 return 0 55 56 57if __name__ == '__main__': 58 try: 59 ret = main() 60 except Exception: 61 ret = 1 62 import traceback 63 traceback.print_exc() 64 sys.exit(ret) 65