-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|