Skip to content

Commit

Permalink
Allow specifying plugins in the config file
Browse files Browse the repository at this point in the history
JIRA: PDC-1564
  • Loading branch information
simozhan committed Jun 28, 2016
1 parent 77696ad commit a3ccb3e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Other possible keys are:
This is only useful for working with servers which don't require
authentication.

* `plugins`

Plugins are configurable which depends on the user's needs.
If no plugins are configured, the default plugins will be used.

### Example system configuration

This config defines connection to development server running on
Expand All @@ -55,6 +60,7 @@ localhost and a production server. :
},
"prod": {
"host": "https://pdc.example.com/rest_api/v1/",
"plugins": ["permission.py", "release.py"]
}
}

Expand Down
35 changes: 30 additions & 5 deletions pdc_client/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,27 @@ def autocomplete(*args):

# A list of paths to directories where plugins should be loaded from.
# The purpose of the plugins is to extend the default behaviour.
PLUGIN_DIRS = [
os.path.join(os.path.dirname(__file__), 'plugins')
LOCAL_DIR = os.path.join(os.path.dirname(__file__), 'plugins')
INSTALLED_DIR = os.path.join('/usr/share/pdc-client', 'plugins')
PLUGIN_DIRS = [LOCAL_DIR] if not os.path.exists(INSTALLED_DIR) else [INSTALLED_DIR]

DEFAULT_PLUGINS = [
'build_image_rtt_tests.py',
'build_images.py',
'component.py',
'compose_image_rtt_tests.py',
'compose.py',
'compose_tree_locations.py',
'contact.py',
'image.py',
'permission.py',
'release.py',
'repo.py',
'rpm.py'
]

CONFIG_PLUGINS_KEY_NAME = 'plugins'


class Runner(object):
def __init__(self):
Expand All @@ -49,10 +66,19 @@ def __init__(self):
self.logger = logging.getLogger('pdc')

def load_plugins(self):
ns, _ = self.parser.parse_known_args()
server = ns.__getattribute__('server')
if not server:
raise TypeError('Server must be specified')
config = pdc_client.read_config_file(server)
plugins = DEFAULT_PLUGINS
if config and config[CONFIG_PLUGINS_KEY_NAME]:
plugins = config[CONFIG_PLUGINS_KEY_NAME]

for dir in PLUGIN_DIRS:
self.logger.debug('Loading plugins from {0}'.format(dir))
for name in os.listdir(dir):
if not name.endswith('.py'):
if not name.endswith('.py') or name not in plugins:
continue
file, pathname, description = imp.find_module(name[:-3], [dir])
plugin = imp.load_module(name[:-3], file, pathname, description)
Expand All @@ -75,8 +101,6 @@ def run_hook(self, hook, *args, **kwargs):
getattr(plugin, hook)(*args, **kwargs)

def setup(self):
self.load_plugins()

self.parser = argparse.ArgumentParser(description='PDC Client')
self.parser.add_argument('-s', '--server', default='stage',
help='API URL or shortcut from config file')
Expand All @@ -88,6 +112,7 @@ def setup(self):
self.parser.add_argument('--version', action='version',
version='%(prog)s ' + pdc_client.__version__)

self.load_plugins()
subparsers = self.parser.add_subparsers(metavar='COMMAND')

for plugin in self.plugins:
Expand Down

0 comments on commit a3ccb3e

Please sign in to comment.