Skip to content

Commit

Permalink
Modules installer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsiri7 committed Sep 15, 2014
1 parent 4167492 commit 8070250
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_modules(cls):
"""

module_locations = [os.path.join(os.path.dirname(__file__), 'modules')]
module_names = [name for loader, name, ispkg in pkgutil.iter_modules(module_locations)]
module_names = [name for loader, name, ispkg in pkgutil.walk_packages(module_locations)]
modules = []
for name in module_names:
mod = importlib.import_module("modules.%s" % name)
Expand Down
70 changes: 70 additions & 0 deletions client/modinstaller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python2
import argparse
import os
import urllib2
import json
import subprocess

class ModuleInstaller():
MODULES_URL = 'http://jaspermoduleshub.herokuapp.com'
def __init__(self, module):
self.module = module
self.PROCESS_FILE = {
'file': self.download_single_file,
'git': self.download_git
}


def install(self):
if self.get_module_metadata():
self.create_module_folder()
self.download_module_files()
self.install_requirements()
print "Installed module: %s" % self.module

def create_module_folder(self):
self.module_path = os.path.abspath('modules/'+self.module)
if not os.path.exists(self.module_path):
os.makedirs(self.module_path)

def get_module_metadata(self):
try:
response = urllib2.urlopen(self.MODULES_URL+'/plugins/%s.json' % self.module)
self.module_json = json.loads(response.read())
except urllib2.HTTPError:
print "Sorry, that module could not be found"
return False
else:
return True

def download_module_files(self):
self.PROCESS_FILE[self.module_json['last_version']['file_type']]()

def download_single_file(self):
self.download_file('.py')

def download_git(self):
subprocess.call(['git', 'clone', self.module_json['last_version']['file'], self.module_path])

def download_file(self, extension):
module_code = urllib2.urlopen(self.module_json['last_version']['file']).read()
module_file = os.path.join(self.module_path, self.module + extension)
with open(module_file, 'w') as file:
file.write(module_code)


def install_requirements(self):
reqs_file = os.path.join(self.module_path, 'requirements.txt')
if os.path.isfile(reqs_file):
subprocess.call(['pip', 'install', '-r', reqs_file])

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Jasper modules installer')
parser.add_argument('--install', nargs=1,
help='Modules to install')

args = parser.parse_args()
for module in args.install:
ModuleInstaller(module).install()


0 comments on commit 8070250

Please sign in to comment.