def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) imported_objects = OrderedDict() imported_objects["request"] = request imported_objects["dbsession"] = request.dbsession imported_objects["transaction"] = transaction imported_objects["redis"] = get_redis(request) imported_objects["now"] = now imported_objects["datetime"] = datetime for name, cls in Base._decl_class_registry.items(): if name == "_sa_module_registry": continue imported_objects[name] = cls print("") print("Following classes and objects are available:") for var, val in imported_objects.items(): print("{:30}: {}".format(var, str(val).replace("\n", " ").replace("\r", " "))) print("") embed(user_ns=imported_objects)
def task_app_request(task_ini_file, ini_settings): request = init_websauna(task_ini_file) if ini_settings["sqlalchemy.url"].startswith("sqlite://"): pytest.skip("These tests are run only for PostgreSQL database") return request
def main(argv=sys.argv): def usage(argv): cmd = os.path.basename(argv[0]) print('usage: %s <config_uri> <network name>\n' '(example: "%s conf/production.ini testnet")' % (cmd, cmd)) sys.exit(1) if len(argv) < 2: usage(argv) config_uri = argv[1] network_name = argv[2] # console_app sets up colored log output from websauna.system.devop.cmdline import init_websauna request = init_websauna(config_uri, sanity_check=True) services = ServiceCore.parse_network_config(request) one_shot = OneShot(request, network_name, services[network_name], require_unlock=False) one_shot.setup() coinbase = one_shot.web3.eth.coinbase pw = getpass("Give password to unlock {} on {}:".format(coinbase, network_name)) one_shot.web3.personal.unlockAccount(coinbase, pw, 30*24*3600) print("Unlock complete") sys.exit(0)
def main(argv=sys.argv): def usage(argv): cmd = os.path.basename(argv[0]) print('usage: %s <config_uri> <network name>\n' '(example: "%s conf/production.ini testnet")' % (cmd, cmd)) sys.exit(1) if len(argv) < 2: usage(argv) config_uri = argv[1] network_name = argv[2] # console_app sets up colored log output from websauna.system.devop.cmdline import init_websauna request = init_websauna(config_uri, sanity_check=True) services = ServiceCore.parse_network_config(request) one_shot = OneShot(request, network_name, services[network_name], require_unlock=False) one_shot.setup() coinbase = one_shot.web3.eth.coinbase pw = getpass("Give password to unlock {} on {}:".format( coinbase, network_name)) one_shot.web3.personal.unlockAccount(coinbase, pw, 30 * 24 * 3600) print("Unlock complete") sys.exit(0)
def main(argv=sys.argv): if len(argv) < 3: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) User = get_user_class(request.registry) dbsession = request.dbsession if len(argv) == 4: password = argv[3] else: password = getpass.getpass("Password:"******"Password (again):") if password != password2: sys.exit("Password did not match") with transaction.manager: u = User(email=argv[2], username=argv[2]) u.password = password u.registration_source = "command_line" u.activated_at = now() dbsession.add(u) dbsession.flush() request.registry.notify(UserCreated(request, u)) print("Created user #{}: {}, admin: {}".format(u.id, u.email, u.is_admin()))
def main(argv: t.List[str]=sys.argv): """Create a new site user from command line. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 3: usage_message(argv) config_uri = get_config_uri(argv) request = init_websauna(config_uri) email = argv[2] is_admin = True if '--admin' in argv else False password = argv[3] if len(argv) >= 4 and argv[3] != '--admin' else '' if not password: password = getpass.getpass('Password:'******'Password (again):') if password != password2: feedback_and_exit('Passwords did not match', display_border=False) with request.tm: u = create(request, email=email, username=email, password=password, admin=is_admin) message = 'Created user #{id}: {email}, admin: {is_admin}'.format( id=u.id, email=u.email, is_admin=u.is_admin() ) feedback_and_exit(message, status_code=None, display_border=True)
def main(argv=sys.argv): if len(argv) < 3: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) if len(argv) >= 4: password = argv[3] else: password = getpass.getpass("Password:"******"Password (again):") if password != password2: sys.exit("Password did not match") admin = False if len(argv) == 5: # TODO: use optparse if argv[4] == "--admin": admin = True with request.tm: u = create(request, email=argv[2], username=argv[2], password=password, admin=admin) print("Created user #{}: {}, admin: {}".format(u.id, u.email, u.is_admin()))
def main(argv=sys.argv): if len(argv) < 3: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) if len(argv) >= 4: password = argv[3] else: password = getpass.getpass("Password:"******"Password (again):") if password != password2: sys.exit("Password did not match") admin = False if len(argv) == 5: # TODO: use optparse if argv[4] == "--admin": admin = True with transaction.manager: u = create(request, email=argv[2], username=argv[2], password=password, admin=admin) print("Created user #{}: {}, admin: {}".format(u.id, u.email, u.is_admin()))
def main(argv: t.List[str] = sys.argv): """Wrapper for pgsql-dump.bash script. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message( argv, additional_params='[ARG1, ARG2]', additional_line='All arguments are passed to pg_dump command') config_uri = get_config_uri(argv) request = init_websauna(config_uri) # Export all secrets and settings bash_env = create_settings_env(request.registry) # subprocess.check_output([DUMP_SCRIPT] + args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) args = argv[2:] cmd = [DUMP_SCRIPT] + args logger.info("Running %s", " ".join(cmd)) with subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, env=bash_env, universal_newlines=True) as p: for line in p.stdout: print(line, end='')
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri, sanity_check=False) request.registry.static_asset_policy.collect_static() sys.exit(0)
def main(argv: t.List[str] = sys.argv): """Execute a sanity check on the configuration. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv) config_uri = get_config_uri(argv) try: init_websauna(config_uri, sanity_check=True) except SanityCheckFailed as exc: feedback_and_exit(FAIL_MSG.format(exception=str(exc)), 10) except FileNotFoundError as exc: feedback_and_exit(NOT_FOUND_MSG.format(config_uri=config_uri), 10) feedback_and_exit(SUCCESS_MSG, 0)
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri, sanity_check=False) print('Active deployment settings in {}'.format(config_uri)) pprint(request.registry.settings) sys.exit(0)
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) with transaction.manager: engine = request.dbsession.get_bind() Base.metadata.create_all(engine)
def _set_request(self): """Set a request for WebsaunaLoader.""" # TODO Make sanity_check True by default, # but make it sure we can disable it in tests, # because otherwise tests won't run self.request = init_websauna(ini_file, sanity_check=False) #: Associate this process as Celery app object of the environment self.request.registry.celery = self.app #: Associate request for celery app, so #: task executor knows about Request object self.app.cmdline_request = self.request
def main(argv: t.List[str]=sys.argv): """Read through all configured static views and compile their assets to ``collected-static`` folder. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv) config_uri = get_config_uri(argv) request = init_websauna(config_uri, sanity_check=False) request.registry.static_asset_policy.collect_static() feedback_and_exit('ws-collect-static: Collected all static assets', 0, True)
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] try: request = init_websauna(config_uri, sanity_check=True) except SanityCheckFailed: print("It did not go that well.") sys.exit(10) print("All good.") sys.exit(0)
def on_worker_init(self): """This method is called when a child process starts.""" # TODO Make sanity_check True by default, # but make it sure we can disable it in tests, # because otherwise tests won't run self.request = init_websauna(ini_file, sanity_check=False) #: Associate this process as Celery app object of the environment self.request.registry.celery = self.app #: Associate request for celery app, so #: task executor knows about Request object self.app.cmdline_request = self.request self.register_tasks()
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) engine = request.dbsession.get_bind() for name, cls in Base._decl_class_registry.items(): if name == "_sa_module_registry": continue print(CreateTable(cls.__table__, bind=engine))
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) with transaction.manager: engine = request.dbsession.get_bind() # Always enable UUID extension for PSQL # TODO: Convenience for now, because we assume UUIDs, but make this somehow configurable engine.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"') Base.metadata.create_all(engine)
def main(argv: t.List[str]=sys.argv): """Display settings for a given configuration file. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv) config_uri = get_config_uri(argv) request = init_websauna(config_uri, sanity_check=False) message = 'Active deployment settings in {config_uri}'.format(config_uri=config_uri) feedback(message) pprint(request.registry.settings) feedback_and_exit('', status_code=0, display_border=True)
def main(argv=sys.argv): monkey_patch_paster_config_parser() if len(argv) < 2: usage(argv) config_uri = argv[1] # Print out the connection URL with the password masked out request = init_websauna(config_uri) url = request.registry.settings.get("sqlalchemy.url") engine = request.dbsession.get_bind() print("Connecting to {}".format(engine)) os.system("pgcli {}".format(url))
def main(argv=sys.argv): def usage(argv): cmd = os.path.basename(argv[0]) print('usage: %s <config_uri>\n' '(example: "%s conf/production.ini")' % (cmd, cmd)) sys.exit(1) if len(argv) < 2: usage(argv) config_uri = argv[1] # console_app sets up colored log output request = init_websauna(config_uri, sanity_check=True) run_services(request) sys.exit(0)
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) # Export all secrets and settings bash_env = create_settings_env(request.registry) # subprocess.check_output([DUMP_SCRIPT] + args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) args = argv[2:] cmd = [DUMP_SCRIPT] + args with subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, env=bash_env, universal_newlines=True) as p: for line in p.stdout: print(line, end='')
def main(argv=sys.argv): size = 200 tags_rel_min = 5 tags_rel_max = 30 config_uri = argv[1] request = init_websauna(config_uri) base_fakefactory.DB_SESSION_PROXY.session = request.dbsession with request.tm: user = get_or_create_user(request, email="*****@*****.**", password="******") tags = fakefactory.TagFactory.create_batch(size=size) posts = fakefactory.PostFactory.create_batch( size=size, author=user, tags=random.sample(tags, k=random.randint(tags_rel_min, tags_rel_max)), )
def main(argv: t.List[str] = sys.argv): """Create initial tables for the database specified on the configuration file. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv) config_uri = get_config_uri(argv) request = init_websauna(config_uri) with transaction.manager: engine = request.dbsession.get_bind() # Enable pgcrypto and implement a uuid_generate_v4 function engine.execute(UUID_SUPPORT_STMT) Base.metadata.create_all(engine)
def main(argv: t.List[str] = sys.argv): """Create initial tables for the database specified on the configuration file. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv) config_uri = get_config_uri(argv) request = init_websauna(config_uri) with transaction.manager: engine = request.dbsession.get_bind() # Always enable UUID extension for PSQL # TODO: Convenience for now, because we assume UUIDs, but make this somehow configurable engine.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"') Base.metadata.create_all(engine)
def main(argv: t.List[str] = sys.argv): """Print out sql statements needed to construct currently configured models. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv) config_uri = get_config_uri(argv) request = init_websauna(config_uri) engine = request.dbsession.get_bind() for name, cls in Base._decl_class_registry.items(): if name == "_sa_module_registry": continue print(CreateTable(cls.__table__, bind=engine))
def main(argv=sys.argv): monkey_patch_paster_config_parser() if len(argv) < 2: usage(argv) config_uri = argv[1] # Print out the connection URL with the password masked out request = init_websauna(config_uri) url = request.registry.settings.get("sqlalchemy.url") engine = request.dbsession.get_bind() print("Connecting to {}".format(engine)) if not which("pgcli"): sys.exit("pgcli not installed - please install websauna as pip install websauna[dev] to get this dependency") os.system("pgcli {}".format(url))
def one_shot_main(argv=sys.argv): """Run one debug cycle""" def usage(argv): cmd = os.path.basename(argv[0]) print('usage: %s <config_uri> <network name>\n' '(example: "%s conf/production.ini") ethereum' % (cmd, cmd)) sys.exit(1) if len(argv) < 3: usage(argv) config_uri = argv[1] network_name = argv[2] # console_app sets up colored log output request = init_websauna(config_uri, sanity_check=True) one_shot(request, network_name) sys.exit(0)
def main(argv=sys.argv): def usage(argv): cmd = os.path.basename(argv[0]) print('usage: %s <config_uri>\n' '(example: "%s conf/production.ini")' % (cmd, cmd)) sys.exit(1) if len(argv) < 2: usage(argv) config_uri = argv[1] # console_app sets up colored log output from websauna.system.devop.cmdline import init_websauna request = init_websauna(config_uri, sanity_check=True) bootstrap(request) print("Bootstrap complete") sys.exit(0)
def main(argv: t.List[str]=sys.argv): """Run pgcli shell on the database specified on the configuration file. :param argv: Command line arguments, second one needs to be the uri to a configuration file. """ if len(argv) < 2: usage_message(argv, additional_params='[var=value]') config_uri = get_config_uri(argv) request = init_websauna(config_uri) url = request.registry.settings.get('sqlalchemy.url') engine = request.dbsession.get_bind() if not which('pgcli'): message = 'pgcli is not installed\nPlease install Websauna as pip install websauna[utils] to get this dependency' feedback_and_exit(message, display_border=False) feedback('Connecting to {engine}'.format(engine=engine), display_border=False) os.system('pgcli {url}'.format(url=url))
def main(argv=sys.argv): if len(argv) < 3: usage(argv) config_uri = argv[1] request = init_websauna(config_uri) if len(argv) == 4: password = argv[3] else: password = getpass.getpass("Password:"******"Password (again):") if password != password2: sys.exit("Password did not match") with transaction.manager: u = create(request, email=argv[2], username=argv[2], password=password) print("Created user #{}: {}, admin: {}".format(u.id, u.email, u.is_admin()))
def main(argv=sys.argv): monkey_patch_paster_config_parser() if len(argv) < 2: usage(argv) config_uri = argv[1] # Print out the connection URL with the password masked out request = init_websauna(config_uri) url = request.registry.settings.get("sqlalchemy.url") engine = request.dbsession.get_bind() print("Connecting to {}".format(engine)) if not which("pgcli"): sys.exit( "pgcli not installed - please install websauna as pip install websauna[utils] to get this dependency" ) os.system("pgcli {}".format(url))
def main(argv: t.List[str] = sys.argv): """Execute the IPython shell prompt with Websauna configuration already initialised. :param argv: Command line arguments, second one needs to be the uri to a configuration file. :raises sys.SystemExit: """ if len(argv) < 2: usage_message(argv, additional_params='[var=value]') config_uri = get_config_uri(argv) request = init_websauna(config_uri) imported_objects = OrderedDict() imported_objects["request"] = request imported_objects["dbsession"] = request.dbsession imported_objects["transaction"] = transaction imported_objects["redis"] = get_redis(request) imported_objects["now"] = now imported_objects["datetime"] = datetime for name, cls in Base._decl_class_registry.items(): if name == "_sa_module_registry": continue imported_objects[name] = cls feedback('', False) feedback('Following classes and objects are available:', False) for var, val in imported_objects.items(): line = "{key:30}: {value}".format(key=var, value=str(val).replace( '\n', ' ').replace('\r', ' ')) feedback(line) feedback('', False) embed(user_ns=imported_objects)
def main(argv=sys.argv): def usage(argv): cmd = os.path.basename(argv[0]) print('usage: %s <config_uri> <network name>\n' '(example: "%s conf/production.ini")' % (cmd, cmd)) sys.exit(1) if len(argv) < 2: usage(argv) config_uri = argv[1] # console_app sets up colored log output from websauna.system.devop.cmdline import init_websauna request = init_websauna(config_uri, sanity_check=True) # Get list of configured networks services = ServiceCore.parse_network_config(request) redis = get_redis(request) for network_name in services.keys(): # Update each network separately and have a lock to ensure we don't # accidentally do two overlapping update runs # https://pypi.python.org/pypi/python-redis-lock lock = redis_lock.Lock(redis, "network-update-lock-{}".format(network_name)) if not lock.acquire(blocking=False): # This network is still procesing pending operations from the previous task run print("Lock {} is blocked, reseting".format(network_name)) lock.reset() else: lock.release() print("Unlock complete") sys.exit(0)
def run_alembic(package:str): """Alembic env.py script entry point for Websauna application. Initialize the application, load models and pass control to Alembic migration handler. :param package: String of the Python package name whose model the migration concerns. E.g. the name of the current Websauna package. """ global logger global version_table current_package = package # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config # This was -c passed to ws-alembic command config_file = config.config_file_name setup_logging(config_file) # Load the WSGI application, etc. request = init_websauna(config_file) engine = request.dbsession.get_bind() # Delay logger creation until we have initialized the logging system logger = logging.getLogger(__name__) # Extract database connection URL from the settings url = request.registry.settings["sqlalchemy.url"] allowed_packages = parse_allowed_packages(current_package) #: Pull out MetaData instance for the system target_metadata = get_sqlalchemy_metadata(allowed_packages) # Each package needs to maintain its own alembic_history table version_table = get_migration_table_name(allowed_packages, current_package) def include_object(object, name, type_, reflected, compare_to): """ """ # Try to figure out smartly table from different object types if type_ in ("index", "column", "foreign_key_constraint"): table_name = object.table.name elif type_ == "table": table_name = object.name else: raise RuntimeError("Don't know how to check type for migration inclusion list: {}".format(type_)) model = get_class_by_table_name(table_name) if not model: # Don't know what's this... let's skip print("No model available", object, type_, table_name) return False module = model.__module__ # XXX: This is not very beautiful check but works for now return consider_module_for_migration(module, allowed_packages) if context.is_offline_mode(): run_migrations_offline(url, target_metadata, version_table, include_object) else: logger.info("Starting online migration engine on database connection {} version history table {}".format(engine, version_table)) run_migrations_online(engine, target_metadata, version_table, include_object) # TODO: If a migration file is written, post-edit it and add websauna import logger.info("All done")
qual = qual[len("builtins"):] # No API docs for Jinja builtins if qual.startswith("jinja2."): qual = None filters.append((name, qual, doc, heading)) filters = sorted(filters, key=lambda x: x[0]) return filters def find_vars(): vars = websauna.system.core.vars result = [] for name, func in vars._template_variables.items(): qual = fullname(func) doc = strip_indent(func.__doc__) heading = "-" * len(name) result.append((name, qual, doc, heading)) result = sorted(result, key=lambda x: x[0]) return result request = init_websauna("../development.ini") modules = {} modules["filters"] = find_filters(request) modules["vars"] = find_vars() print(template.render(dict(modules=modules)))
def task_app_request(task_ini_file): request = init_websauna(task_ini_file) return request
# No API docs for Jinja builtins if qual.startswith("jinja2."): qual = None filters.append((name, qual, doc, heading)) filters = sorted(filters, key=lambda x: x[0]) return filters def find_vars(): vars = websauna.system.core.vars result = [] for name, func in vars._template_variables.items(): qual = fullname(func) doc = strip_indent(func.__doc__) heading = "-" * len(name) result.append((name, qual, doc, heading)) result = sorted(result, key=lambda x: x[0]) return result request = init_websauna("../websauna/conf/development.ini") modules = {} modules["filters"] = find_filters(request) modules["vars"] = find_vars() print(template.render(dict(modules=modules)))
def tutorial_req(request): """Construct a WSGI app with tutorial models and admins loaded.""" ini_file = os.path.join(HERE, "tutorial-test.ini") request = init_websauna(ini_file) return request
def cache_app(request): """Construct a WSGI app with static asset caching enabled.""" request = init_websauna(STATIC_CONF_FILE) return request.app