def test_init_redis_values(self, mocked_render_template): config.init("kinto.ini", backend="redis", cache_backend="redis") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) redis_url = "redis://localhost:6379" self.maxDiff = None # See the full diff in case of error self.assertDictEqual( kwargs, { "host": "127.0.0.1", "secret": kwargs["secret"], "bucket_id_salt": kwargs["bucket_id_salt"], "storage_backend": "kinto_redis.storage", "cache_backend": "kinto_redis.cache", "permission_backend": "kinto_redis.permission", "storage_url": redis_url + "/1", "cache_url": redis_url + "/2", "permission_url": redis_url + "/3", "kinto_version": __version__, "config_file_timestamp": mock.ANY, }, ) self._assertTimestampStringsAlmostEqual( strftime("%a, %d %b %Y %H:%M:%S %z"), # expected kwargs["config_file_timestamp"], # actual )
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto commands") parser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=CONFIG_FILE) parser.add_argument('--backend', help='Specify backend', dest='backend', required=False, default=None) subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands', help='init/start/migrate') parser_init = subparsers.add_parser('init') parser_init.set_defaults(which='init') parser_migrate = subparsers.add_parser('migrate') parser_migrate.set_defaults(which='migrate') parser_start = subparsers.add_parser('start') parser_start.set_defaults(which='start') args = vars(parser.parse_args()) config_file = args['ini_file'] if args['which'] == 'init': if os.path.exists(config_file): print("%s already exist." % config_file, file=sys.stderr) sys.exit(1) backend = args['backend'] if not backend: while True: prompt = ("Which backend to use? " "(1 - postgresql, 2 - redis, default - memory) ") answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass init(config_file, backend) elif args['which'] == 'migrate': env = bootstrap(config_file) cliquet.init_schema(env) elif args['which'] == 'start': pserve_argv = ['pserve', config_file, '--reload'] pserve.main(pserve_argv)
def test_init_postgresql_redis_values(self, mocked_render_template): config.init("kinto.ini", backend="postgresql", cache_backend="redis") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) redis_url = "redis://*****:*****@localhost/postgres", "cache_url": redis_url + "/2", "permission_url": "postgresql://*****:*****@localhost/postgres", "kinto_version": __version__, "config_file_timestamp": mock.ANY, }, )
def test_init_memory_values(self, mocked_render_template): config.init("kinto.ini", backend="memory", cache_backend="memory") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) self.assertDictEqual( kwargs, { "host": "127.0.0.1", "secret": kwargs["secret"], "storage_backend": "kinto.core.storage.memory", "cache_backend": "kinto.core.cache.memory", "permission_backend": "kinto.core.permission.memory", "storage_url": "", "cache_url": "", "permission_url": "", "kinto_version": __version__, "config_file_timestamp": mock.ANY, }, ) self._assertTimestampStringsAlmostEqual( strftime("%a, %d %b %Y %H:%M:%S %z"), # expected kwargs["config_file_timestamp"], # actual )
def test_init_postgresql_memcached_values(self, mocked_render_template): config.init("kinto.ini", backend="postgresql", cache_backend="memcached") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) postgresql_url = "postgres://*****:*****@localhost/postgres" cache_url = "127.0.0.1:11211 127.0.0.2:11211" self.assertDictEqual( kwargs, { "host": "127.0.0.1", "secret": kwargs["secret"], "storage_backend": "kinto.core.storage.postgresql", "cache_backend": "kinto.core.cache.memcached", "permission_backend": "kinto.core.permission.postgresql", "storage_url": postgresql_url, "cache_url": cache_url, "permission_url": postgresql_url, "kinto_version": __version__, "config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"), }, )
def test_init_postgresql_values(self, mocked_render_template): self.maxDiff = None config.init("kinto.ini", backend="postgresql", cache_backend="postgresql") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) postgresql_url = "postgresql://*****:*****@localhost/postgres" self.assertDictEqual( kwargs, { "host": "127.0.0.1", "secret": kwargs["secret"], "bucket_id_salt": kwargs["bucket_id_salt"], "storage_backend": "kinto.core.storage.postgresql", "cache_backend": "kinto.core.cache.postgresql", "permission_backend": "kinto.core.permission.postgresql", "storage_url": postgresql_url, "cache_url": postgresql_url, "permission_url": postgresql_url, "kinto_version": __version__, "config_file_timestamp": mock.ANY, }, ) self._assertTimestampStringsAlmostEqual( strftime("%a, %d %b %Y %H:%M:%S %z"), # expected kwargs["config_file_timestamp"], # actual )
def test_init_memory_values(self, mocked_render_template): config.init('kinto.ini', 'memory') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.memory', 'cache_backend': 'cliquet.cache.memory', 'permission_backend': 'cliquet.permission.memory', 'storage_url': '', 'cache_url': '', 'permission_url': '' })
def test_init_memory_values(self, mocked_render_template): init('kinto.ini', 'memory') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) self.assertDictEqual( kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.memory', 'cache_backend': 'cliquet.cache.memory', 'permission_backend': 'cliquet.permission.memory', 'storage_url': '', 'cache_url': '', 'permission_url': '' })
def test_init_postgresql_values(self, mocked_render_template): config.init('kinto.ini', 'postgresql') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) postgresql_url = "postgres://*****:*****@localhost/postgres" self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.postgresql', 'cache_backend': 'cliquet.cache.postgresql', 'permission_backend': 'cliquet.permission.postgresql', 'storage_url': postgresql_url, 'cache_url': postgresql_url, 'permission_url': postgresql_url })
def test_init_redis_values(self, mocked_render_template): config.init('kinto.ini', 'redis') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) redis_url = "redis://localhost:6379" self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.redis', 'cache_backend': 'cliquet.cache.redis', 'permission_backend': 'cliquet.permission.redis', 'storage_url': redis_url + '/1', 'cache_url': redis_url + '/2', 'permission_url': redis_url + '/3' })
def test_init_memory_values(self, mocked_render_template): config.init('kinto.ini', 'memory') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'kinto.core.storage.memory', 'cache_backend': 'kinto.core.cache.memory', 'permission_backend': 'kinto.core.permission.memory', 'storage_url': '', 'cache_url': '', 'permission_url': '', 'kinto_version': __version__, 'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z') })
def test_init_postgresql_values(self, mocked_render_template): init('kinto.ini', 'postgresql') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) postgresql_url = "postgres://*****:*****@localhost/postgres" self.assertDictEqual( kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.postgresql', 'cache_backend': 'cliquet.cache.postgresql', 'permission_backend': 'cliquet.permission.postgresql', 'storage_url': postgresql_url, 'cache_url': postgresql_url, 'permission_url': postgresql_url })
def test_init_redis_values(self, mocked_render_template): init('kinto.ini', 'redis') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) redis_url = "redis://localhost:6379" self.assertDictEqual( kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.redis', 'cache_backend': 'cliquet.cache.redis', 'permission_backend': 'cliquet.permission.redis', 'storage_url': redis_url + '/1', 'cache_url': redis_url + '/2', 'permission_url': redis_url + '/3' })
def test_init_redis_values(self, mocked_render_template): config.init('kinto.ini', 'redis') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) redis_url = "redis://localhost:6379" self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'kinto.core.storage.redis', 'cache_backend': 'kinto.core.cache.redis', 'permission_backend': 'kinto.core.permission.redis', 'storage_url': redis_url + '/1', 'cache_url': redis_url + '/2', 'permission_url': redis_url + '/3', 'kinto_version': __version__, 'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z') })
def test_init_postgresql_values(self, mocked_render_template): config.init('kinto.ini', 'postgresql') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) postgresql_url = "postgres://*****:*****@localhost/postgres" self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'kinto.core.storage.postgresql', 'cache_backend': 'kinto.core.cache.postgresql', 'permission_backend': 'kinto.core.permission.postgresql', 'storage_url': postgresql_url, 'cache_url': postgresql_url, 'permission_url': postgresql_url, 'kinto_version': __version__, 'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z') })
def test_init_redis_values(self, mocked_render_template): config.init('kinto.ini', 'redis') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) redis_url = "redis://localhost:6379" self.assertDictEqual(kwargs, { 'secret': kwargs['secret'], 'storage_backend': 'cliquet.storage.redis', 'cache_backend': 'cliquet.cache.redis', 'permission_backend': 'cliquet.permission.redis', 'storage_url': redis_url + '/1', 'cache_url': redis_url + '/2', 'permission_url': redis_url + '/3', 'kinto_version': __version__, 'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z') })
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto commands") parser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=CONFIG_FILE) subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands', help='init/start/migrate') parser_init = subparsers.add_parser('init') parser_init.set_defaults(which='init') parser_migrate = subparsers.add_parser('migrate') parser_migrate.set_defaults(which='migrate') parser_start = subparsers.add_parser('start') parser_start.set_defaults(which='start') args = vars(parser.parse_args()) config_file = args['ini_file'] if args['which'] == 'init': if not os.path.exists(config_file): init(config_file) else: print("%s already exist." % config_file, file=sys.stderr) sys.exit(1) elif args['which'] == 'migrate': env = bootstrap(config_file) cliquet.init_schema(env) elif args['which'] == 'start': pserve_argv = ['pserve', config_file, '--reload'] pserve.main(pserve_argv)
def test_init_redis_values(self, mocked_render_template): config.init('kinto.ini', backend='redis', cache_backend='redis') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(args, ('kinto.tpl', 'kinto.ini')) redis_url = "redis://localhost:6379" self.maxDiff = None # See the full diff in case of error self.assertDictEqual(kwargs, { 'host': '127.0.0.1', 'secret': kwargs['secret'], 'storage_backend': 'kinto_redis.storage', 'cache_backend': 'kinto_redis.cache', 'permission_backend': 'kinto_redis.permission', 'storage_url': redis_url + '/1', 'cache_url': redis_url + '/2', 'permission_url': redis_url + '/3', 'kinto_version': __version__, 'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z') })
def test_init_redis_values(self, mocked_render_template): config.init('kinto.ini', backend='redis', cache_backend='redis') args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ('kinto.tpl', 'kinto.ini')) redis_url = "redis://localhost:6379" self.maxDiff = None # See the full diff in case of error self.assertDictEqual(kwargs, { 'host': '127.0.0.1', 'secret': kwargs['secret'], 'storage_backend': 'kinto_redis.storage', 'cache_backend': 'kinto_redis.cache', 'permission_backend': 'kinto_redis.permission', 'storage_url': redis_url + '/1', 'cache_url': redis_url + '/2', 'permission_url': redis_url + '/3', 'kinto_version': __version__, 'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z') })
def test_init_memory_values(self, mocked_render_template): config.init("kinto.ini", backend="memory", cache_backend="memory") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) self.assertDictEqual( kwargs, { "host": "127.0.0.1", "secret": kwargs["secret"], "storage_backend": "kinto.core.storage.memory", "cache_backend": "kinto.core.cache.memory", "permission_backend": "kinto.core.permission.memory", "storage_url": "", "cache_url": "", "permission_url": "", "kinto_version": __version__, "config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"), }, )
def test_init_redis_values(self, mocked_render_template): config.init("kinto.ini", backend="redis", cache_backend="redis") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(args, ("kinto.tpl", "kinto.ini")) redis_url = "redis://localhost:6379" self.maxDiff = None # See the full diff in case of error self.assertDictEqual( kwargs, { "host": "127.0.0.1", "secret": kwargs["secret"], "storage_backend": "kinto_redis.storage", "cache_backend": "kinto_redis.cache", "permission_backend": "kinto_redis.permission", "storage_url": redis_url + "/1", "cache_url": redis_url + "/2", "permission_url": redis_url + "/3", "kinto_version": __version__, "config_file_timestamp": strftime("%a, %d %b %Y %H:%M:%S %z"), }, )
def initstep(args,config_file): backend = args['backend'] if not backend: while True: prompt = ("Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) ") answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass init(config_file, backend) # Install postgresql libraries if necessary if backend == "postgresql": try: import psycopg2 # NOQA except ImportError: import pip pip.main(['install', "cliquet[postgresql]"])
def test_hmac_secret_is_text(self, mocked_render_template): config.init('kinto.ini', 'postgresql') args, kwargs = list(mocked_render_template.call_args) self.assertEquals(type(kwargs['secret']), six.text_type)
def test_hmac_secret_is_text(self, mocked_render_template): config.init("kinto.ini", backend="postgresql", cache_backend="postgresql") args, kwargs = list(mocked_render_template.call_args) self.assertEqual(type(kwargs["secret"]), str)
def test_hmac_secret_is_text(self, mocked_render_template): config.init('kinto.ini', backend='postgresql', cache_backend='postgresql') args, kwargs = list(mocked_render_template.call_args) self.assertEqual(type(kwargs['secret']), str)
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto Command-Line " "Interface") commands = ( "init", "start", "migrate", "delete-collection", "version", "rebuild-quotas", "create-user", ) subparsers = parser.add_subparsers( title="subcommands", description="Main Kinto CLI commands", dest="subcommand", help="Choose and run with --help", ) subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) subparser.add_argument( "--ini", help="Application configuration file", dest="ini_file", required=False, default=DEFAULT_CONFIG_FILE, ) subparser.add_argument( "-q", "--quiet", action="store_const", const=logging.CRITICAL, dest="verbosity", help="Show only critical errors.", ) subparser.add_argument( "-v", "--debug", action="store_const", const=logging.DEBUG, dest="verbosity", help="Show all messages, including debug messages.", ) if command == "init": subparser.add_argument( "--backend", help="{memory,redis,postgresql}", dest="backend", required=False, default=None, ) subparser.add_argument( "--cache-backend", help="{memory,redis,postgresql,memcached}", dest="cache-backend", required=False, default=None, ) subparser.add_argument( "--host", help="Host to listen() on.", dest="host", required=False, default="127.0.0.1", ) elif command == "migrate": subparser.add_argument( "--dry-run", action="store_true", help="Simulate the migration operations " "and show information", dest="dry_run", required=False, default=False, ) elif command == "delete-collection": subparser.add_argument( "--bucket", help="The bucket where the collection " "belongs to.", required=True ) subparser.add_argument("--collection", help="The collection to remove.", required=True) elif command == "rebuild-quotas": subparser.add_argument( "--dry-run", action="store_true", help="Simulate the rebuild operation " "and show information", dest="dry_run", required=False, default=False, ) elif command == "start": subparser.add_argument( "--reload", action="store_true", help="Restart when code or config changes", required=False, default=False, ) subparser.add_argument( "--port", type=int, help="Listening port number", required=False, default=DEFAULT_PORT, ) elif command == "create-user": subparser.add_argument( "-u", "--username", help="Superuser username", required=False, default=None ) subparser.add_argument( "-p", "--password", help="Superuser password", required=False, default=None ) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args["ini_file"] which_command = parsed_args["which"] # Initialize logging from level = parsed_args.get("verbosity") or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == "init": if os.path.exists(config_file): print("{} already exists.".format(config_file), file=sys.stderr) return 1 backend = parsed_args["backend"] cache_backend = parsed_args["cache-backend"] if not backend: while True: prompt = ( "Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) " ) answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass if not cache_backend: while True: prompt = ( "Select the cache backend you would like to use: " "(1 - postgresql, 2 - redis, 3 - memcached, default - memory) " ) answer = input(prompt).strip() try: cache_backends = { "1": "postgresql", "2": "redis", "3": "memcached", "": "memory", } cache_backend = cache_backends[answer] break except KeyError: pass init(config_file, backend, cache_backend, parsed_args["host"]) # Install postgresql libraries if necessary if backend == "postgresql" or cache_backend == "postgresql": try: import psycopg2 # NOQA except ImportError: subprocess.check_call( [sys.executable, "-m", "pip", "install", "kinto[postgresql]"] ) elif backend == "redis" or cache_backend == "redis": try: import kinto_redis # NOQA except ImportError: subprocess.check_call([sys.executable, "-m", "pip", "install", "kinto[redis]"]) elif cache_backend == "memcached": try: import memcache # NOQA except ImportError: subprocess.check_call([sys.executable, "-m", "pip", "install", "kinto[memcached]"]) elif which_command == "migrate": dry_run = parsed_args["dry_run"] env = bootstrap(config_file, options={"command": "migrate"}) scripts.migrate(env, dry_run=dry_run) elif which_command == "delete-collection": env = bootstrap(config_file, options={"command": "delete-collection"}) return scripts.delete_collection(env, parsed_args["bucket"], parsed_args["collection"]) elif which_command == "rebuild-quotas": dry_run = parsed_args["dry_run"] env = bootstrap(config_file, options={"command": "rebuild-quotas"}) return scripts.rebuild_quotas(env, dry_run=dry_run) elif which_command == "create-user": username = parsed_args["username"] password = parsed_args["password"] env = bootstrap(config_file, options={"command": "create-user"}) return create_user(env, username=username, password=password) elif which_command == "start": pserve_argv = ["pserve"] if parsed_args["reload"]: pserve_argv.append("--reload") if level == logging.DEBUG: pserve_argv.append("-v") if level == logging.CRITICAL: pserve_argv.append("-q") pserve_argv.append(config_file) pserve_argv.append("http_port={}".format(parsed_args["port"])) pserve.main(argv=pserve_argv) else: print(__version__) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto Command-Line " "Interface") # XXX: deprecate this option, unnatural as first argument. parser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=DEFAULT_CONFIG_FILE) parser.add_argument('-q', '--quiet', action='store_const', const=logging.CRITICAL, dest='verbosity', help='Show only critical errors.') parser.add_argument('--debug', action='store_const', const=logging.DEBUG, dest='verbosity', help='Show all messages, including debug messages.') commands = ('init', 'start', 'migrate', 'delete-collection', 'version') subparsers = parser.add_subparsers(title='subcommands', description='Main Kinto CLI commands', dest='subcommand', help="Choose and run with --help") subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) if command == 'init': subparser.add_argument('--backend', help='{memory,redis,postgresql}', dest='backend', required=False, default=None) elif command == 'migrate': subparser.add_argument('--dry-run', action='store_true', help='Simulate the migration operations ' 'and show information', dest='dry_run', required=False, default=False) elif command == 'delete-collection': subparser.add_argument('--bucket', help='The bucket where the collection ' 'belongs to.', required=True) subparser.add_argument('--collection', help='The collection to remove.', required=True) elif command == 'start': subparser.add_argument('--reload', action='store_true', help='Restart when code or config changes', required=False, default=False) subparser.add_argument('--port', type=int, help='Listening port number', required=False, default=DEFAULT_PORT) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args['ini_file'] which_command = parsed_args['which'] # Initialize logging from level = parsed_args.get('verbosity') or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == 'init': if os.path.exists(config_file): print("%s already exists." % config_file, file=sys.stderr) return 1 backend = parsed_args['backend'] if not backend: while True: prompt = ("Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) ") answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass init(config_file, backend) # Install postgresql libraries if necessary if backend == "postgresql": try: import psycopg2 # NOQA except ImportError: import pip pip.main(['install', "kinto[postgresql]"]) elif which_command == 'migrate': dry_run = parsed_args['dry_run'] env = bootstrap(config_file) scripts.migrate(env, dry_run=dry_run) elif which_command == 'delete-collection': env = bootstrap(config_file) return scripts.delete_collection(env, parsed_args['bucket'], parsed_args['collection']) elif which_command == 'start': pserve_argv = ['pserve', config_file] if parsed_args['reload']: pserve_argv.append('--reload') pserve_argv.append('http_port=%s' % parsed_args['port']) pserve.main(pserve_argv) elif which_command == 'version': print(__version__) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto commands") parser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=CONFIG_FILE) parser.add_argument('--backend', help='Specify backend', dest='backend', required=False, default=None) parser.add_argument('-v', '--version', action='version', version=__version__, help='Print the Kinto version and exit.') subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands', dest='subcommand', help='init/start/migrate') subparsers.required = True parser_init = subparsers.add_parser('init') parser_init.set_defaults(which='init') parser_migrate = subparsers.add_parser('migrate') parser_migrate.set_defaults(which='migrate') parser_start = subparsers.add_parser('start') parser_start.add_argument('--reload', action='store_true', help='Restart when code or config changes', required=False, default=False) parser_start.add_argument('--port', type=int, help='Listening port number', required=False, default=8888) parser_start.set_defaults(which='start') parsed_args = vars(parser.parse_args(args)) config_file = parsed_args['ini_file'] if parsed_args['which'] == 'init': if os.path.exists(config_file): print("%s already exists." % config_file, file=sys.stderr) return 1 backend = parsed_args['backend'] if not backend: while True: prompt = ("Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) ") answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass init(config_file, backend) # Install postgresql libraries if necessary if backend == "postgresql": try: import psycopg2 # NOQA except ImportError: import pip pip.main(['install', "kinto[postgresql]"]) elif parsed_args['which'] == 'migrate': env = bootstrap(config_file) scripts.migrate(env) elif parsed_args['which'] == 'start': pserve_argv = ['pserve', config_file] if parsed_args['reload']: pserve_argv.append('--reload') pserve_argv.append('http_port=%s' % parsed_args['port']) pserve.main(pserve_argv) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto Command-Line " "Interface") # XXX: deprecate this option, unnatural as first argument. parser.add_argument( "--ini", help="Application configuration file", dest="ini_file", required=False, default=DEFAULT_CONFIG_FILE ) parser.add_argument( "-q", "--quiet", action="store_const", const=logging.CRITICAL, dest="verbosity", help="Show only critical errors.", ) parser.add_argument( "--debug", action="store_const", const=logging.DEBUG, dest="verbosity", help="Show all messages, including debug messages.", ) commands = ("init", "start", "migrate", "delete-collection", "version") subparsers = parser.add_subparsers( title="subcommands", description="Main Kinto CLI commands", dest="subcommand", help="Choose and run with --help" ) subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) if command == "init": subparser.add_argument( "--backend", help="{memory,redis,postgresql}", dest="backend", required=False, default=None ) elif command == "migrate": subparser.add_argument( "--dry-run", action="store_true", help="Simulate the migration operations " "and show information", dest="dry_run", required=False, default=False, ) elif command == "delete-collection": subparser.add_argument("--bucket", help="The bucket where the collection " "belongs to.", required=True) subparser.add_argument("--collection", help="The collection to remove.", required=True) elif command == "start": subparser.add_argument( "--reload", action="store_true", help="Restart when code or config changes", required=False, default=False, ) subparser.add_argument( "--port", type=int, help="Listening port number", required=False, default=DEFAULT_PORT ) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args["ini_file"] which_command = parsed_args["which"] # Initialize logging from level = parsed_args.get("verbosity") or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == "init": if os.path.exists(config_file): print("%s already exists." % config_file, file=sys.stderr) return 1 backend = parsed_args["backend"] if not backend: while True: prompt = "Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) " answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass init(config_file, backend) # Install postgresql libraries if necessary if backend == "postgresql": try: import psycopg2 # NOQA except ImportError: import pip pip.main(["install", "kinto[postgresql]"]) elif backend == "redis": try: import kinto_redis # NOQA except ImportError: import pip pip.main(["install", "kinto[redis]"]) elif which_command == "migrate": dry_run = parsed_args["dry_run"] env = bootstrap(config_file) scripts.migrate(env, dry_run=dry_run) elif which_command == "delete-collection": env = bootstrap(config_file) return scripts.delete_collection(env, parsed_args["bucket"], parsed_args["collection"]) elif which_command == "start": pserve_argv = ["pserve", config_file] if parsed_args["reload"]: pserve_argv.append("--reload") pserve_argv.append("http_port=%s" % parsed_args["port"]) pserve.main(pserve_argv) elif which_command == "version": print(__version__) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description='Kinto Command-Line ' 'Interface') commands = ('init', 'start', 'migrate', 'delete-collection', 'version', 'rebuild-quotas', 'create-user') subparsers = parser.add_subparsers(title='subcommands', description='Main Kinto CLI commands', dest='subcommand', help='Choose and run with --help') subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) subparser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=DEFAULT_CONFIG_FILE) subparser.add_argument('-q', '--quiet', action='store_const', const=logging.CRITICAL, dest='verbosity', help='Show only critical errors.') subparser.add_argument( '-v', '--debug', action='store_const', const=logging.DEBUG, dest='verbosity', help='Show all messages, including debug messages.') if command == 'init': subparser.add_argument('--backend', help='{memory,redis,postgresql}', dest='backend', required=False, default=None) subparser.add_argument('--cache-backend', help='{memory,redis,postgresql,memcached}', dest='cache-backend', required=False, default=None) subparser.add_argument('--host', help='Host to listen() on.', dest='host', required=False, default='127.0.0.1') elif command == 'migrate': subparser.add_argument('--dry-run', action='store_true', help='Simulate the migration operations ' 'and show information', dest='dry_run', required=False, default=False) elif command == 'delete-collection': subparser.add_argument('--bucket', help='The bucket where the collection ' 'belongs to.', required=True) subparser.add_argument('--collection', help='The collection to remove.', required=True) elif command == 'rebuild-quotas': subparser.add_argument('--dry-run', action='store_true', help='Simulate the rebuild operation ' 'and show information', dest='dry_run', required=False, default=False) elif command == 'start': subparser.add_argument('--reload', action='store_true', help='Restart when code or config changes', required=False, default=False) subparser.add_argument('--port', type=int, help='Listening port number', required=False, default=DEFAULT_PORT) elif command == 'create-user': subparser.add_argument('-u', '--username', help='Superuser username', required=False, default=None) subparser.add_argument('-p', '--password', help='Superuser password', required=False, default=None) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args['ini_file'] which_command = parsed_args['which'] # Initialize logging from level = parsed_args.get('verbosity') or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == 'init': if os.path.exists(config_file): print('{} already exists.'.format(config_file), file=sys.stderr) return 1 backend = parsed_args['backend'] cache_backend = parsed_args['cache-backend'] if not backend: while True: prompt = ('Select the backend you would like to use: ' '(1 - postgresql, 2 - redis, default - memory) ') answer = input(prompt).strip() try: backends = {'1': 'postgresql', '2': 'redis', '': 'memory'} backend = backends[answer] break except KeyError: pass if not cache_backend: while True: prompt = ( 'Select the cache backend you would like to use: ' '(1 - postgresql, 2 - redis, 3 - memcached, default - memory) ' ) answer = input(prompt).strip() try: cache_backends = { '1': 'postgresql', '2': 'redis', '3': 'memcached', '': 'memory' } cache_backend = cache_backends[answer] break except KeyError: pass init(config_file, backend, cache_backend, parsed_args['host']) # Install postgresql libraries if necessary if backend == 'postgresql' or cache_backend == 'postgresql': try: import psycopg2 # NOQA except ImportError: subprocess.check_call([ sys.executable, '-m', 'pip', 'install', 'kinto[postgresql]' ]) elif backend == 'redis' or cache_backend == 'redis': try: import kinto_redis # NOQA except ImportError: subprocess.check_call( [sys.executable, '-m', 'pip', 'install', 'kinto[redis]']) elif cache_backend == 'memcached': try: import memcache # NOQA except ImportError: subprocess.check_call([ sys.executable, '-m', 'pip', 'install', 'kinto[memcached]' ]) elif which_command == 'migrate': dry_run = parsed_args['dry_run'] env = bootstrap(config_file, options={'command': 'migrate'}) scripts.migrate(env, dry_run=dry_run) elif which_command == 'delete-collection': env = bootstrap(config_file, options={'command': 'delete-collection'}) return scripts.delete_collection(env, parsed_args['bucket'], parsed_args['collection']) elif which_command == 'rebuild-quotas': dry_run = parsed_args['dry_run'] env = bootstrap(config_file, options={'command': 'rebuild-quotas'}) return scripts.rebuild_quotas(env, dry_run=dry_run) elif which_command == 'create-user': username = parsed_args['username'] password = parsed_args['password'] env = bootstrap(config_file, options={'command': 'create-user'}) return create_user(env, username=username, password=password) elif which_command == 'start': pserve_argv = ['pserve'] if parsed_args['reload']: pserve_argv.append('--reload') if level == logging.DEBUG: pserve_argv.append('-v') if level == logging.CRITICAL: pserve_argv.append('-q') pserve_argv.append(config_file) pserve_argv.append('http_port={}'.format(parsed_args['port'])) pserve.main(argv=pserve_argv) else: print(__version__) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser( description="Kinto Command-Line Interface") commands = ( "init", "start", "migrate", "flush-cache", "version", "rebuild-quotas", "create-user", ) subparsers = parser.add_subparsers( title="subcommands", description="Main Kinto CLI commands", dest="subcommand", help="Choose and run with --help", ) subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) subparser.add_argument( "--ini", help="Application configuration file", dest="ini_file", required=False, default=DEFAULT_CONFIG_FILE, ) subparser.add_argument( "-q", "--quiet", action="store_const", const=logging.CRITICAL, dest="verbosity", help="Show only critical errors.", ) subparser.add_argument( "-v", "--debug", action="store_const", const=logging.DEBUG, dest="verbosity", help="Show all messages, including debug messages.", ) if command == "init": subparser.add_argument( "--backend", help="{memory,redis,postgresql}", dest="backend", required=False, default=None, ) subparser.add_argument( "--cache-backend", help="{memory,redis,postgresql,memcached}", dest="cache-backend", required=False, default=None, ) subparser.add_argument( "--host", help="Host to listen() on.", dest="host", required=False, default="127.0.0.1", ) elif command == "migrate": subparser.add_argument( "--dry-run", action="store_true", help="Simulate the migration operations and show information", dest="dry_run", required=False, default=False, ) elif command == "rebuild-quotas": subparser.add_argument( "--dry-run", action="store_true", help="Simulate the rebuild operation and show information", dest="dry_run", required=False, default=False, ) elif command == "start": subparser.add_argument( "--reload", action="store_true", help="Restart when code or config changes", required=False, default=False, ) subparser.add_argument( "--port", type=int, help="Listening port number", required=False, default=DEFAULT_PORT, ) elif command == "create-user": subparser.add_argument("-u", "--username", help="Superuser username", required=False, default=None) subparser.add_argument("-p", "--password", help="Superuser password", required=False, default=None) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args["ini_file"] which_command = parsed_args["which"] # Initialize logging from level = parsed_args.get("verbosity") or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == "init": if os.path.exists(config_file): print(f"{config_file} already exists.", file=sys.stderr) return 1 backend = parsed_args["backend"] cache_backend = parsed_args["cache-backend"] if not backend: while True: prompt = ("Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) ") answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass if not cache_backend: while True: prompt = ( "Select the cache backend you would like to use: " "(1 - postgresql, 2 - redis, 3 - memcached, default - memory) " ) answer = input(prompt).strip() try: cache_backends = { "1": "postgresql", "2": "redis", "3": "memcached", "": "memory", } cache_backend = cache_backends[answer] break except KeyError: pass init(config_file, backend, cache_backend, parsed_args["host"]) # Install postgresql libraries if necessary if backend == "postgresql" or cache_backend == "postgresql": try: import psycopg2 # NOQA except ImportError: subprocess.check_call([ sys.executable, "-m", "pip", "install", "kinto[postgresql]" ]) elif backend == "redis" or cache_backend == "redis": try: import kinto_redis # NOQA except ImportError: subprocess.check_call( [sys.executable, "-m", "pip", "install", "kinto[redis]"]) elif cache_backend == "memcached": try: import memcache # NOQA except ImportError: subprocess.check_call([ sys.executable, "-m", "pip", "install", "kinto[memcached]" ]) elif which_command == "migrate": dry_run = parsed_args["dry_run"] env = bootstrap(config_file, options={"command": "migrate"}) core_scripts.migrate(env, dry_run=dry_run) elif which_command == "flush-cache": env = bootstrap(config_file, options={"command": "flush-cache"}) core_scripts.flush_cache(env) elif which_command == "rebuild-quotas": dry_run = parsed_args["dry_run"] env = bootstrap(config_file, options={"command": "rebuild-quotas"}) return kinto_scripts.rebuild_quotas(env, dry_run=dry_run) elif which_command == "create-user": username = parsed_args["username"] password = parsed_args["password"] env = bootstrap(config_file, options={"command": "create-user"}) return accounts_scripts.create_user(env, username=username, password=password) elif which_command == "start": pserve_argv = ["pserve"] if parsed_args["reload"]: pserve_argv.append("--reload") if level == logging.DEBUG: pserve_argv.append("-v") if level == logging.CRITICAL: pserve_argv.append("-q") pserve_argv.append(config_file) pserve_argv.append(f"http_port={parsed_args['port']}") pserve.main(argv=pserve_argv) else: print(__version__) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description="Kinto Command-Line " "Interface") # XXX: deprecate this option, unnatural as first argument. parser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=DEFAULT_CONFIG_FILE) parser.add_argument('-q', '--quiet', action='store_const', const=logging.CRITICAL, dest='verbosity', help='Show only critical errors.') parser.add_argument('--debug', action='store_const', const=logging.DEBUG, dest='verbosity', help='Show all messages, including debug messages.') commands = ('init', 'start', 'migrate', 'delete-collection', 'version') subparsers = parser.add_subparsers(title='subcommands', description='Main Kinto CLI commands', dest='subcommand', help="Choose and run with --help") subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) if command == 'init': subparser.add_argument('--backend', help='{memory,redis,postgresql}', dest='backend', required=False, default=None) subparser.add_argument('--host', help='Host to listen() on.', dest='host', required=False, default='127.0.0.1') elif command == 'migrate': subparser.add_argument('--dry-run', action='store_true', help='Simulate the migration operations ' 'and show information', dest='dry_run', required=False, default=False) elif command == 'delete-collection': subparser.add_argument('--bucket', help='The bucket where the collection ' 'belongs to.', required=True) subparser.add_argument('--collection', help='The collection to remove.', required=True) elif command == 'start': subparser.add_argument('--reload', action='store_true', help='Restart when code or config changes', required=False, default=False) subparser.add_argument('--port', type=int, help='Listening port number', required=False, default=DEFAULT_PORT) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args['ini_file'] which_command = parsed_args['which'] # Initialize logging from level = parsed_args.get('verbosity') or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == 'init': if os.path.exists(config_file): print("%s already exists." % config_file, file=sys.stderr) return 1 backend = parsed_args['backend'] if not backend: while True: prompt = ("Select the backend you would like to use: " "(1 - postgresql, 2 - redis, default - memory) ") answer = input(prompt).strip() try: backends = {"1": "postgresql", "2": "redis", "": "memory"} backend = backends[answer] break except KeyError: pass init(config_file, backend, parsed_args['host']) # Install postgresql libraries if necessary if backend == "postgresql": try: import psycopg2 # NOQA except ImportError: import pip pip.main(['install', "kinto[postgresql]"]) elif backend == "redis": try: import kinto_redis # NOQA except ImportError: import pip pip.main(['install', "kinto[redis]"]) elif which_command == 'migrate': dry_run = parsed_args['dry_run'] env = bootstrap(config_file) scripts.migrate(env, dry_run=dry_run) elif which_command == 'delete-collection': env = bootstrap(config_file) return scripts.delete_collection(env, parsed_args['bucket'], parsed_args['collection']) elif which_command == 'start': pserve_argv = ['pserve', config_file] if parsed_args['reload']: pserve_argv.append('--reload') pserve_argv.append('http_port=%s' % parsed_args['port']) pserve.main(pserve_argv) elif which_command == 'version': print(__version__) return 0
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser(description='Kinto Command-Line ' 'Interface') commands = ('init', 'start', 'migrate', 'delete-collection', 'version', 'rebuild-quotas', 'create-user') subparsers = parser.add_subparsers(title='subcommands', description='Main Kinto CLI commands', dest='subcommand', help='Choose and run with --help') subparsers.required = True for command in commands: subparser = subparsers.add_parser(command) subparser.set_defaults(which=command) subparser.add_argument('--ini', help='Application configuration file', dest='ini_file', required=False, default=DEFAULT_CONFIG_FILE) subparser.add_argument('-q', '--quiet', action='store_const', const=logging.CRITICAL, dest='verbosity', help='Show only critical errors.') subparser.add_argument('-v', '--debug', action='store_const', const=logging.DEBUG, dest='verbosity', help='Show all messages, including debug messages.') if command == 'init': subparser.add_argument('--backend', help='{memory,redis,postgresql}', dest='backend', required=False, default=None) subparser.add_argument('--host', help='Host to listen() on.', dest='host', required=False, default='127.0.0.1') elif command == 'migrate': subparser.add_argument('--dry-run', action='store_true', help='Simulate the migration operations ' 'and show information', dest='dry_run', required=False, default=False) elif command == 'delete-collection': subparser.add_argument('--bucket', help='The bucket where the collection ' 'belongs to.', required=True) subparser.add_argument('--collection', help='The collection to remove.', required=True) elif command == 'rebuild-quotas': subparser.add_argument('--dry-run', action='store_true', help='Simulate the rebuild operation ' 'and show information', dest='dry_run', required=False, default=False) elif command == 'start': subparser.add_argument('--reload', action='store_true', help='Restart when code or config changes', required=False, default=False) subparser.add_argument('--port', type=int, help='Listening port number', required=False, default=DEFAULT_PORT) elif command == 'create-user': subparser.add_argument('-u', '--username', help='Superuser username', required=False, default=None) subparser.add_argument('-p', '--password', help='Superuser password', required=False, default=None) # Parse command-line arguments parsed_args = vars(parser.parse_args(args)) config_file = parsed_args['ini_file'] which_command = parsed_args['which'] # Initialize logging from level = parsed_args.get('verbosity') or DEFAULT_LOG_LEVEL logging.basicConfig(level=level, format=DEFAULT_LOG_FORMAT) if which_command == 'init': if os.path.exists(config_file): print('{} already exists.'.format(config_file), file=sys.stderr) return 1 backend = parsed_args['backend'] if not backend: while True: prompt = ('Select the backend you would like to use: ' '(1 - postgresql, 2 - redis, default - memory) ') answer = input(prompt).strip() try: backends = {'1': 'postgresql', '2': 'redis', '': 'memory'} backend = backends[answer] break except KeyError: pass init(config_file, backend, parsed_args['host']) # Install postgresql libraries if necessary if backend == 'postgresql': try: import psycopg2 # NOQA except ImportError: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'kinto[postgresql]']) elif backend == 'redis': try: import kinto_redis # NOQA except ImportError: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'kinto[redis]']) elif which_command == 'migrate': dry_run = parsed_args['dry_run'] env = bootstrap(config_file) scripts.migrate(env, dry_run=dry_run) elif which_command == 'delete-collection': env = bootstrap(config_file) return scripts.delete_collection(env, parsed_args['bucket'], parsed_args['collection']) elif which_command == 'rebuild-quotas': dry_run = parsed_args['dry_run'] env = bootstrap(config_file) return scripts.rebuild_quotas(env, dry_run=dry_run) elif which_command == 'create-user': username = parsed_args['username'] password = parsed_args['password'] env = bootstrap(config_file) return create_user(env, username=username, password=password) elif which_command == 'start': pserve_argv = ['pserve'] if parsed_args['reload']: pserve_argv.append('--reload') if level == logging.DEBUG: pserve_argv.append('-v') if level == logging.CRITICAL: pserve_argv.append('-q') pserve_argv.append(config_file) pserve_argv.append('http_port={}'.format(parsed_args['port'])) pserve.main(argv=pserve_argv) else: print(__version__) return 0