def start(self): import strut from channels.routing import get_default_application from daphne.access import AccessLogGenerator from daphne.server import Server strut.setup() application = get_default_application() endpoint = build_endpoint_description_string(self.bind) print(f"> listening on {endpoint}...") Server( application=application, signal_handlers=True, endpoints=[endpoint], action_logger=AccessLogGenerator(sys.stdout), ping_interval=20, ping_timeout=30, websocket_timeout=7200, websocket_handshake_timeout=5, verbosity=1, proxy_forwarded_address_header="X-Forwarded-For", proxy_forwarded_port_header="X-Forwarded-Port", proxy_forwarded_proto_header="X-Forwarded-Proto", ).run()
def shell(): "Start interactive python shell." # Disable Sentry before we shell os.environ.pop("SENTRY_DSN", None) import strut strut.setup() import sys import requests from django.conf import settings from django.db import connection from django.db.models import Model from strut.db.utils import last_query, explain def py_version(): return sys.version.split("\n", 1)[0] banner = f"""\ _____________ __ ________ / __/_ __/ _ \/ / / /_ __/ _\ \ / / / , _/ /_/ / / / /___/ /_/ /_/|_|\____/ /_/ Python {py_version()} """ # Preload things we will always use context = { "os": os, "sys": sys, "requests": requests, "settings": settings, "connection": connection, "last_query": last_query, "explain": lambda *a, **kw: sys.stdout.write(explain(*a, **kw)) and None, } models = __import__("strut.models", fromlist=["*"]) for key, attr in vars(models).items(): if isinstance(attr, type) and issubclass(attr, Model): context[key] = attr try: from IPython.terminal.embed import InteractiveShellEmbed sh = InteractiveShellEmbed.instance(banner1=banner) except ImportError: pass else: sh(local_ns=context) return from code import interact interact(banner, local=context)
def django(ctx, management_args): "Execute Django subcommands." import strut strut.setup() from django.core.management import execute_from_command_line execute_from_command_line(argv=[ctx.command_path] + list(management_args))
def start(self): import strut strut.setup() from rq import Connection, Queue, Worker queues = ["default"] with Connection(): Worker( map(Queue, queues), exception_handlers=[sentry_handler()], default_result_ttl=0, name=get_name(), ).work()
def load_fixture(ctx): import strut strut.setup() from cryptography.hazmat.primitives import serialization from cryptography.hazmat.backends import default_backend from strut import models user = models.User.objects.create(email="*****@*****.**", is_superuser=True) org = models.Organization.objects.create(name="Matt", slug="matt") models.OrganizationMember.objects.create( organization=org, user=user, role=models.OrganizationMember.Role.Owner) models.OrganizationDomain.objects.create(organization=org, domain="robenolt.com") with open("scratch/client.pub", "rb") as fp: device = models.Device.objects.create( name="Matt Device", serial="0000000000000000", pubkey=serialization.load_pem_public_key( fp.read(), backend=default_backend()).public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo, ), ) org.associate_device(device) models.LockitronLock.objects.create(organization=org, lock_id="lock-abc123", name="Matt Lock") models.LockitronUser.objects.create(user_id="user-abc123", email=user.email) playlist = models.Playlist.objects.create(owner=user) models.PlaylistSubscription.objects.create(user=user, playlist=playlist) meta = models.SongMeta.objects.create( source=models.SongMeta.Source.YouTube, identifier="BB0DU4DoPP4") meta.resolver.sync() song = models.Song.objects.create(meta=meta, start=0, length=4) song.process(user=user, sync=True) models.PlaylistSong.objects.create(playlist=playlist, song=song)
def import_(ctx, path): assert os.path.exists(path) import sys import json from glob import iglob users = [] for path in iglob(os.path.join(path, "users", "*", "*.json")): with open(path) as fp: users.append(json.load(fp)) import strut strut.setup() from strut import models org, _ = models.Organization.objects.get_or_create( slug="sentry", defaults={"name": "Sentry"}) for domain in "sentry.io", "getsentry.com": models.OrganizationDomain.objects.get_or_create(organization=org, domain=domain) for u in users: print(f'+ {u["email"]}') user, _ = models.User.objects.get_or_create( email=models.User.objects.normalize_email(u["email"])) models.OrganizationMember.objects.create(organization=org, user=user) playlist = models.Playlist.objects.create(owner=user) models.PlaylistSubscription.objects.create(user=user, playlist=playlist) for s in u.get("songs", []): s = s["options"] if s["duration"] < 1: continue sys.stderr.write(f' * {s["video_id"]} ... ') sys.stdout.flush() try: meta = models.SongMeta.objects.get( source=models.SongMeta.Source.YouTube, identifier=s["video_id"]) except models.SongMeta.DoesNotExist: meta = models.SongMeta(source=models.SongMeta.Source.YouTube, identifier=s["video_id"]) try: meta.resolver.sync() except meta.DataUnsynced as e: sys.stderr.write(f"! ({e})\n") sys.stderr.flush() continue song, _ = models.Song.objects.get_or_create(meta=meta, start=s["start"], length=s["duration"]) song.process(force=True) models.PlaylistSong.objects.create(playlist=playlist, song=song) sys.stderr.write("ok\n") sys.stderr.flush()
from django.core.wsgi import get_wsgi_application from raven.contrib.django.raven_compat.middleware.wsgi import Sentry import strut strut.setup() application = Sentry(get_wsgi_application())