Skip to content

Commit

Permalink
Merge pull request #1185 from rdmorganiser/rdmo_admin
Browse files Browse the repository at this point in the history
Add rdmo-admin script
  • Loading branch information
jochenklar authored Nov 19, 2024
2 parents 7c51c4c + cba45de commit 282426b
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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*"]
Expand Down
27 changes: 27 additions & 0 deletions rdmo/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions rdmo/core/management/commands/build.py
Original file line number Diff line number Diff line change
@@ -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'])
53 changes: 53 additions & 0 deletions rdmo/core/management/commands/clean.py
Original file line number Diff line number Diff line change
@@ -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!')
17 changes: 17 additions & 0 deletions rdmo/core/management/commands/messages.py
Original file line number Diff line number Diff line change
@@ -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')
22 changes: 22 additions & 0 deletions rdmo/core/management/commands/npm.py
Original file line number Diff line number Diff line change
@@ -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}'])
11 changes: 11 additions & 0 deletions rdmo/core/management/settings.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 282426b

Please sign in to comment.