diff --git a/pyproject.toml b/pyproject.toml index 6103cd7faa..6f8cac418f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -113,6 +113,9 @@ issues = "https://github.com/rdmorganiser/rdmo/issues" repository = "https://github.com/rdmorganiser/rdmo.git" slack = "https://rdmo.slack.com" +[project.scripts] +rdmo-admin = "rdmo.__main__:main" + [tool.setuptools.packages.find] include = ["rdmo*"] exclude = ["*assets*", "*tests*"] diff --git a/rdmo/__main__.py b/rdmo/__main__.py new file mode 100644 index 0000000000..b19f323ed1 --- /dev/null +++ b/rdmo/__main__.py @@ -0,0 +1,27 @@ +''' +Runs rdmo-admin when the rdmo module is run as a script, much like django-admin +(see: https://github.com/django/django/blob/main/django/__main__.py): + + python -m rdmo check + +The main method is added as script in pyproject.toml so that + + rdmo-admin check + +works as well. Unlike django-admin, a set of generic settings is used for the +management scripts. +''' + +import os + +from django.core import management + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rdmo.core.management.settings") + + +def main(): + management.execute_from_command_line() + + +if __name__ == "__main__": + main() diff --git a/rdmo/core/management/commands/build.py b/rdmo/core/management/commands/build.py new file mode 100644 index 0000000000..9e67ff1905 --- /dev/null +++ b/rdmo/core/management/commands/build.py @@ -0,0 +1,13 @@ +import subprocess +import sys + +from django.core.management import call_command +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + + def handle(self, *args, **options): + call_command('npm', 'ci') + call_command('npm', 'run', 'build:prod') + subprocess.call(['/bin/bash', '-c', f'{sys.executable} -m build']) diff --git a/rdmo/core/management/commands/clean.py b/rdmo/core/management/commands/clean.py new file mode 100644 index 0000000000..bd7a3de439 --- /dev/null +++ b/rdmo/core/management/commands/clean.py @@ -0,0 +1,53 @@ +import os +import shutil + +from django.conf import settings +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument('command', choices=[ + 'all', + 'dist', + 'media', + 'npm', + 'python', + 'static', + ]) + + def handle(self, *args, **options): + if options['command'] in ['all', 'dist']: + self.clean_dir('dist') + self.clean_dir('rdmo.egg-info') + if options['command'] in ['all', 'media']: + self.clean_dir(settings.MEDIA_ROOT) + if options['command'] in ['all', 'npm']: + self.clean_dir('node_modules') + if options['command'] in ['all', 'static']: + self.clean_static() + if options['command'] in ['all', 'python']: + self.clean_python() + + def clean_python(self): + for root, dirs, files in os.walk('.'): + for dir_name in dirs: + if dir_name == '__pycache__': + self.clean_dir(os.path.join(root, dir_name), quiet=True) + + def clean_static(self): + self.clean_dir(settings.STATIC_ROOT) + + for path in [ + # 'rdmo/core/static', # TODO: enable after cleanup + 'rdmo/management/static', + # 'rdmo/projects/static' # TODO: enable after cleanup + ]: + self.clean_dir(path) + + def clean_dir(self, path, quiet=False): + if path and os.path.exists(path): + shutil.rmtree(path) + if not quiet: + print(f'Directory "{path}" has been removed!') diff --git a/rdmo/core/management/commands/messages.py b/rdmo/core/management/commands/messages.py new file mode 100644 index 0000000000..aa1703ca72 --- /dev/null +++ b/rdmo/core/management/commands/messages.py @@ -0,0 +1,17 @@ +import subprocess + +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument('command', choices=['make', 'compile']) + + def handle(self, *args, **options): + if options['command'] == 'make': + subprocess.check_call(['django-admin', 'makemessages', '--all'], cwd='rdmo') + subprocess.check_call(['django-admin', 'makemessages', '--all', '-d', 'djangojs'], cwd='rdmo') + + elif options['command'] == 'compile': + subprocess.check_call(['django-admin', 'compilemessages'], cwd='rdmo') diff --git a/rdmo/core/management/commands/npm.py b/rdmo/core/management/commands/npm.py new file mode 100644 index 0000000000..5e5929d931 --- /dev/null +++ b/rdmo/core/management/commands/npm.py @@ -0,0 +1,22 @@ +import os +import subprocess + +from django.core.management.base import BaseCommand, CommandError + + +class Command(BaseCommand): + + def add_arguments(self, parser): + parser.add_argument('command', nargs="*") + + def handle(self, *args, **options): + nvm_dir = os.getenv('NVM_DIR') + + if nvm_dir is None: + raise CommandError('NVM_DIR is not set, is nvm.sh installed?') + + if not os.path.exists(nvm_dir): + raise CommandError('NVM_DIR does not exist, is nvm.sh installed?') + + command = ' '.join(options['command']) + subprocess.call(['/bin/bash', '-c', f'source {nvm_dir}/nvm.sh; npm {command}']) diff --git a/rdmo/core/management/settings.py b/rdmo/core/management/settings.py new file mode 100644 index 0000000000..aebbda76e3 --- /dev/null +++ b/rdmo/core/management/settings.py @@ -0,0 +1,11 @@ +''' +Generic settings to be used with rdmo-admin outside of an rdmo-app. +''' + +from rdmo.core.settings import * # noqa: F403 + +ROOT_URLCONF = '' + +DATABASES = {} + +STATIC_ROOT = 'static_root'