From cc549fff6170e60c226be569a0146a044427fad9 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sat, 4 Oct 2014 12:19:00 +0530 Subject: [PATCH] Load modules using explict paths instead of config file name --- data/etc/plinth/modules-enabled/apps | 1 + data/etc/plinth/modules-enabled/config | 1 + data/etc/plinth/modules-enabled/diagnostics | 1 + data/etc/plinth/modules-enabled/expert_mode | 1 + data/etc/plinth/modules-enabled/firewall | 1 + data/etc/plinth/modules-enabled/first_boot | 1 + data/etc/plinth/modules-enabled/help | 1 + data/etc/plinth/modules-enabled/lib | 1 + data/etc/plinth/modules-enabled/owncloud | 1 + data/etc/plinth/modules-enabled/packages | 1 + data/etc/plinth/modules-enabled/pagekite | 1 + data/etc/plinth/modules-enabled/system | 1 + data/etc/plinth/modules-enabled/tor | 1 + data/etc/plinth/modules-enabled/users | 1 + data/etc/plinth/modules-enabled/xmpp | 1 + plinth/__main__.py | 8 +++-- plinth/module_loader.py | 37 ++++++++++++--------- plinth/modules/config/__init__.py | 2 +- plinth/modules/diagnostics/__init__.py | 2 +- plinth/modules/expert_mode/__init__.py | 2 +- plinth/modules/firewall/__init__.py | 2 +- plinth/modules/owncloud/__init__.py | 2 +- plinth/modules/packages/__init__.py | 2 +- plinth/modules/pagekite/__init__.py | 2 +- plinth/modules/tor/__init__.py | 2 +- plinth/modules/users/__init__.py | 2 +- plinth/modules/xmpp/__init__.py | 2 +- 27 files changed, 51 insertions(+), 29 deletions(-) diff --git a/data/etc/plinth/modules-enabled/apps b/data/etc/plinth/modules-enabled/apps index e69de29bb..a58ec54d5 100644 --- a/data/etc/plinth/modules-enabled/apps +++ b/data/etc/plinth/modules-enabled/apps @@ -0,0 +1 @@ +plinth.modules.apps diff --git a/data/etc/plinth/modules-enabled/config b/data/etc/plinth/modules-enabled/config index e69de29bb..e49242be0 100644 --- a/data/etc/plinth/modules-enabled/config +++ b/data/etc/plinth/modules-enabled/config @@ -0,0 +1 @@ +plinth.modules.config diff --git a/data/etc/plinth/modules-enabled/diagnostics b/data/etc/plinth/modules-enabled/diagnostics index e69de29bb..3bf02fcd3 100644 --- a/data/etc/plinth/modules-enabled/diagnostics +++ b/data/etc/plinth/modules-enabled/diagnostics @@ -0,0 +1 @@ +plinth.modules.diagnostics diff --git a/data/etc/plinth/modules-enabled/expert_mode b/data/etc/plinth/modules-enabled/expert_mode index e69de29bb..116c63d31 100644 --- a/data/etc/plinth/modules-enabled/expert_mode +++ b/data/etc/plinth/modules-enabled/expert_mode @@ -0,0 +1 @@ +plinth.modules.expert_mode diff --git a/data/etc/plinth/modules-enabled/firewall b/data/etc/plinth/modules-enabled/firewall index e69de29bb..a5becc1cc 100644 --- a/data/etc/plinth/modules-enabled/firewall +++ b/data/etc/plinth/modules-enabled/firewall @@ -0,0 +1 @@ +plinth.modules.firewall diff --git a/data/etc/plinth/modules-enabled/first_boot b/data/etc/plinth/modules-enabled/first_boot index e69de29bb..68df2a1db 100644 --- a/data/etc/plinth/modules-enabled/first_boot +++ b/data/etc/plinth/modules-enabled/first_boot @@ -0,0 +1 @@ +plinth.modules.first_boot diff --git a/data/etc/plinth/modules-enabled/help b/data/etc/plinth/modules-enabled/help index e69de29bb..52293215b 100644 --- a/data/etc/plinth/modules-enabled/help +++ b/data/etc/plinth/modules-enabled/help @@ -0,0 +1 @@ +plinth.modules.help diff --git a/data/etc/plinth/modules-enabled/lib b/data/etc/plinth/modules-enabled/lib index e69de29bb..d23f5ae5e 100644 --- a/data/etc/plinth/modules-enabled/lib +++ b/data/etc/plinth/modules-enabled/lib @@ -0,0 +1 @@ +plinth.modules.lib diff --git a/data/etc/plinth/modules-enabled/owncloud b/data/etc/plinth/modules-enabled/owncloud index e69de29bb..c3596a2c4 100644 --- a/data/etc/plinth/modules-enabled/owncloud +++ b/data/etc/plinth/modules-enabled/owncloud @@ -0,0 +1 @@ +plinth.modules.owncloud diff --git a/data/etc/plinth/modules-enabled/packages b/data/etc/plinth/modules-enabled/packages index e69de29bb..4e007642b 100644 --- a/data/etc/plinth/modules-enabled/packages +++ b/data/etc/plinth/modules-enabled/packages @@ -0,0 +1 @@ +plinth.modules.packages diff --git a/data/etc/plinth/modules-enabled/pagekite b/data/etc/plinth/modules-enabled/pagekite index e69de29bb..3c2d7a2ef 100644 --- a/data/etc/plinth/modules-enabled/pagekite +++ b/data/etc/plinth/modules-enabled/pagekite @@ -0,0 +1 @@ +plinth.modules.pagekite diff --git a/data/etc/plinth/modules-enabled/system b/data/etc/plinth/modules-enabled/system index e69de29bb..de4c8dd50 100644 --- a/data/etc/plinth/modules-enabled/system +++ b/data/etc/plinth/modules-enabled/system @@ -0,0 +1 @@ +plinth.modules.system diff --git a/data/etc/plinth/modules-enabled/tor b/data/etc/plinth/modules-enabled/tor index e69de29bb..a1c73f019 100644 --- a/data/etc/plinth/modules-enabled/tor +++ b/data/etc/plinth/modules-enabled/tor @@ -0,0 +1 @@ +plinth.modules.tor diff --git a/data/etc/plinth/modules-enabled/users b/data/etc/plinth/modules-enabled/users index e69de29bb..50a4e62dc 100644 --- a/data/etc/plinth/modules-enabled/users +++ b/data/etc/plinth/modules-enabled/users @@ -0,0 +1 @@ +plinth.modules.users diff --git a/data/etc/plinth/modules-enabled/xmpp b/data/etc/plinth/modules-enabled/xmpp index e69de29bb..89238446c 100644 --- a/data/etc/plinth/modules-enabled/xmpp +++ b/data/etc/plinth/modules-enabled/xmpp @@ -0,0 +1 @@ +plinth.modules.xmpp diff --git a/plinth/__main__.py b/plinth/__main__.py index 8c31288a1..c362e6858 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -4,6 +4,7 @@ import argparse import django.conf import django.core.management import django.core.wsgi +import importlib import logging import os import stat @@ -98,9 +99,10 @@ def setup_server(): # TODO: our modules are mimicking django apps. It'd be better to convert # our modules to Django apps instead of reinventing the wheel. # (we'll still have to serve the static files with cherrypy though) - for module in module_loader.LOADED_MODULES: - static_dir = os.path.join(cfg.file_root, 'plinth', 'modules', module, - 'static') + for module_name in module_loader.loaded_modules: + module = importlib.import_module(module_name) + module_path = os.path.dirname(module.__file__) + static_dir = os.path.join(module_path, 'static') if not os.path.isdir(static_dir): continue diff --git a/plinth/module_loader.py b/plinth/module_loader.py index 70ea7f491..f32370f4d 100644 --- a/plinth/module_loader.py +++ b/plinth/module_loader.py @@ -23,6 +23,7 @@ import django import importlib import logging import os +import re from plinth import cfg from plinth import urls @@ -30,7 +31,7 @@ from plinth.signals import pre_module_loading, post_module_loading LOGGER = logging.getLogger(__name__) -LOADED_MODULES = [] +loaded_modules = [] def load_modules(): @@ -39,27 +40,30 @@ def load_modules(): import them from modules directory. """ pre_module_loading.send_robust(sender="module_loader") - module_names = [] modules = {} module_directory = os.path.join(cfg.config_dir, 'modules-enabled') - for name in os.listdir(module_directory): - full_name = 'plinth.modules.{module}'.format(module=name) + for file_name in os.listdir(module_directory): + full_file_name = os.path.join(module_directory, file_name) + with open(full_file_name, 'r') as file: + for line in file: + line = re.sub('#.*', '', line) + line = line.strip() + if line: + modules[line] = None - LOGGER.info('Importing %s', full_name) + for module_name in modules: + LOGGER.info('Importing %s', module_name) try: - module = importlib.import_module(full_name) - modules[name] = module - module_names.append(name) + modules[module_name] = importlib.import_module(module_name) except Exception as exception: - LOGGER.exception('Could not import modules/%s: %s', - name, exception) + LOGGER.exception('Could not import %s: %s', module_name, exception) if cfg.debug: raise - _include_module_urls(full_name, name) + _include_module_urls(module_name) ordered_modules = [] - remaining_modules = dict(modules) + remaining_modules = dict(modules) # Make a copy for module_name in modules: if module_name not in remaining_modules: continue @@ -76,9 +80,9 @@ def load_modules(): for module_name in ordered_modules: _initialize_module(modules[module_name]) - LOADED_MODULES.append(module_name) - post_module_loading.send_robust(sender="module_loader") + loaded_modules.append(module_name) + post_module_loading.send_robust(sender="module_loader") def _insert_modules(module_name, module, remaining_modules, ordered_modules): @@ -88,7 +92,7 @@ def _insert_modules(module_name, module, remaining_modules, ordered_modules): dependencies = [] try: - dependencies = module.DEPENDS + dependencies = module.depends except AttributeError: pass @@ -108,8 +112,9 @@ def _insert_modules(module_name, module, remaining_modules, ordered_modules): ordered_modules.append(module_name) -def _include_module_urls(module_name, namespace): +def _include_module_urls(module_name): """Include the module's URLs in global project URLs list""" + namespace = module_name.split('.')[-1] url_module = module_name + '.urls' try: urls.urlpatterns += django.conf.urls.patterns( diff --git a/plinth/modules/config/__init__.py b/plinth/modules/config/__init__.py index 9e8ba0667..5b72f8424 100644 --- a/plinth/modules/config/__init__.py +++ b/plinth/modules/config/__init__.py @@ -24,4 +24,4 @@ from .config import init __all__ = ['config', 'init'] -DEPENDS = ['system'] +depends = ['plinth.modules.system'] diff --git a/plinth/modules/diagnostics/__init__.py b/plinth/modules/diagnostics/__init__.py index 5346b6ae7..866a9e57c 100644 --- a/plinth/modules/diagnostics/__init__.py +++ b/plinth/modules/diagnostics/__init__.py @@ -24,4 +24,4 @@ from .diagnostics import init __all__ = ['diagnostics', 'init'] -DEPENDS = ['system'] +depends = ['plinth.modules.system'] diff --git a/plinth/modules/expert_mode/__init__.py b/plinth/modules/expert_mode/__init__.py index 071bd92a6..e0c375074 100644 --- a/plinth/modules/expert_mode/__init__.py +++ b/plinth/modules/expert_mode/__init__.py @@ -24,4 +24,4 @@ from .expert_mode import init __all__ = ['expert_mode', 'init'] -DEPENDS = ['system'] +depends = ['plinth.modules.system'] diff --git a/plinth/modules/firewall/__init__.py b/plinth/modules/firewall/__init__.py index 3e3718c6c..27daf85a0 100644 --- a/plinth/modules/firewall/__init__.py +++ b/plinth/modules/firewall/__init__.py @@ -24,4 +24,4 @@ from .firewall import init __all__ = ['firewall', 'init'] -DEPENDS = ['system'] +depends = ['plinth.modules.system'] diff --git a/plinth/modules/owncloud/__init__.py b/plinth/modules/owncloud/__init__.py index e77100b70..4a29d7bfb 100644 --- a/plinth/modules/owncloud/__init__.py +++ b/plinth/modules/owncloud/__init__.py @@ -24,4 +24,4 @@ from .owncloud import init __all__ = ['owncloud', 'init'] -DEPENDS = ['apps'] +depends = ['plinth.modules.apps'] diff --git a/plinth/modules/packages/__init__.py b/plinth/modules/packages/__init__.py index f448daaab..64b060a1a 100644 --- a/plinth/modules/packages/__init__.py +++ b/plinth/modules/packages/__init__.py @@ -24,4 +24,4 @@ from .packages import init __all__ = ['packages', 'init'] -DEPENDS = ['system'] +depends = ['plinth.modules.system'] diff --git a/plinth/modules/pagekite/__init__.py b/plinth/modules/pagekite/__init__.py index 3a017a1cd..0d836a04f 100644 --- a/plinth/modules/pagekite/__init__.py +++ b/plinth/modules/pagekite/__init__.py @@ -24,4 +24,4 @@ from .pagekite import init __all__ = ['pagekite', 'init'] -DEPENDS = ['apps'] +depends = ['plinth.modules.apps'] diff --git a/plinth/modules/tor/__init__.py b/plinth/modules/tor/__init__.py index 0a806432a..1d70edb1e 100644 --- a/plinth/modules/tor/__init__.py +++ b/plinth/modules/tor/__init__.py @@ -24,4 +24,4 @@ from .tor import init __all__ = ['tor', 'init'] -DEPENDS = ['apps'] +depends = ['plinth.modules.apps'] diff --git a/plinth/modules/users/__init__.py b/plinth/modules/users/__init__.py index 3dc71aeb5..593dea9fc 100644 --- a/plinth/modules/users/__init__.py +++ b/plinth/modules/users/__init__.py @@ -24,4 +24,4 @@ from .users import init __all__ = ['users', 'init'] -DEPENDS = ['system'] +depends = ['plinth.modules.system'] diff --git a/plinth/modules/xmpp/__init__.py b/plinth/modules/xmpp/__init__.py index 578564afb..fdf2c24df 100644 --- a/plinth/modules/xmpp/__init__.py +++ b/plinth/modules/xmpp/__init__.py @@ -24,4 +24,4 @@ from .xmpp import init __all__ = ['xmpp', 'init'] -DEPENDS = ['apps'] +depends = ['plinth.modules.apps']