Skip to content
Snippets Groups Projects
script.py 2.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • from django.core.management.base import BaseCommand, CommandError
    
    from funkwhale_api.common import scripts
    
    
    class Command(BaseCommand):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        help = "Run a specific script from funkwhale_api/common/scripts/"
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            parser.add_argument("script_name", nargs="?", type=str)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                "--noinput",
                "--no-input",
                action="store_false",
                dest="interactive",
    
                help="Do NOT prompt the user for input of any kind.",
            )
    
        def handle(self, *args, **options):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            name = options["script_name"]
    
            if not name:
                self.show_help()
    
            available_scripts = self.get_scripts()
            try:
                script = available_scripts[name]
            except KeyError:
                raise CommandError(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                    "{} is not a valid script. Run python manage.py script for a "
                    "list of available scripts".format(name)
                )
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            self.stdout.write("")
            if options["interactive"]:
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                    "Are you sure you want to execute the script {}?\n\n"
    
                    "Type 'yes' to continue, or 'no' to cancel: "
                ).format(name)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                if input("".join(message)) != "yes":
    
                    raise CommandError("Script cancelled.")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            script["entrypoint"](self, **options)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            self.stdout.write("")
            self.stdout.write("Available scripts:")
            self.stdout.write("Launch with: python manage.py <script_name>")
    
            available_scripts = self.get_scripts()
            for name, script in sorted(available_scripts.items()):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                self.stdout.write("")
    
                self.stdout.write(self.style.SUCCESS(name))
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                self.stdout.write("")
                for line in script["help"].splitlines():
                    self.stdout.write("     {}".format(line))
            self.stdout.write("")
    
    
        def get_scripts(self):
            available_scripts = [
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                k for k in sorted(scripts.__dict__.keys()) if not k.startswith("__")
    
            ]
            data = {}
            for name in available_scripts:
                module = getattr(scripts, name)
                data[name] = {
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                    "name": name,
                    "help": module.__doc__.strip(),
                    "entrypoint": module.main,