Skip to content

Commit

Permalink
Loading plugins from Github (#2992)
Browse files Browse the repository at this point in the history
* Checking github plugin file existence

* Loading plugins from github
  • Loading branch information
TheSavior committed Aug 8, 2016
1 parent 41ed10c commit 563f898
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pokemongo_bot/plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ def _get_correct_path(self, path):
return correct_path

def load_plugin(self, plugin):
correct_path = self._get_correct_path(plugin)
github_plugin = GithubPlugin(plugin)
if github_plugin.is_valid_plugin():
if not github_plugin.is_already_downloaded():
github_plugin.download()

correct_path = github_plugin.get_local_destination()
else:
correct_path = self._get_correct_path(plugin)

if correct_path not in self.folder_cache:
self.folder_cache.append(correct_path)
Expand All @@ -27,6 +34,7 @@ def load_plugin(self, plugin):
def remove_path(self, path):
correct_path = self._get_correct_path(path)
sys.path.remove(correct_path)
self.folder_cache.remove(correct_path)

def get_class(self, namespace_class):
[namespace, class_name] = namespace_class.split('.')
Expand Down Expand Up @@ -63,6 +71,10 @@ def get_local_destination(self):
full_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'plugins', file_name)
return full_path

def is_already_downloaded(self):
file_path = self.get_local_destination()
return os.path.isfile(file_path)

def get_github_download_url(self):
parts = self.plugin_parts
if parts is None:
Expand Down
37 changes: 37 additions & 0 deletions pokemongo_bot/test/plugin_loader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import importlib
import unittest
import os
import shutil
import mock
from datetime import timedelta, datetime
from mock import patch, MagicMock
from pokemongo_bot.plugin_loader import PluginLoader, GithubPlugin
Expand All @@ -27,6 +29,30 @@ def test_load_zip(self):
self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
self.plugin_loader.remove_path(package_path)

def copy_zip(self):
zip_fixture = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture_test.zip')
dest_path = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip'))
shutil.copyfile(zip_fixture, dest_path)
return dest_path

def test_load_github_already_downloaded(self):
dest_path = self.copy_zip()
self.plugin_loader.load_plugin('org/repo#sha')
loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask')
self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
self.plugin_loader.remove_path(dest_path)
os.remove(dest_path)

@mock.patch.object(GithubPlugin, 'download', copy_zip)
def test_load_github_not_downloaded(self):
self.plugin_loader.load_plugin('org/repo#sha')
loaded_class = self.plugin_loader.get_class('plugin_fixture_test.FakeTask')
self.assertEqual(loaded_class({}, {}).work(), 'FakeTask')
dest_path = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'plugins', 'org_repo_sha.zip'))
self.plugin_loader.remove_path(dest_path)
os.remove(dest_path)

class GithubPluginTest(unittest.TestCase):
def test_get_github_parts_for_valid_github(self):
github_plugin = GithubPlugin('org/repo#sha')
self.assertTrue(github_plugin.is_valid_plugin())
Expand All @@ -50,3 +76,14 @@ def test_get_github_download_url(self):
url = github_plugin.get_github_download_url()
expected = 'https://github.com/org/repo/archive/sha.zip'
self.assertEqual(url, expected)

def test_is_already_downloaded_not_downloaded(self):
github_plugin = GithubPlugin('org/repo#sha')
self.assertFalse(github_plugin.is_already_downloaded())

def test_is_already_downloaded_downloaded(self):
github_plugin = GithubPlugin('org/repo#sha')
dest = github_plugin.get_local_destination()
open(dest, 'a').close()
self.assertTrue(github_plugin.is_already_downloaded())
os.remove(dest)

0 comments on commit 563f898

Please sign in to comment.