Skip to content

Commit

Permalink
Refactor code to add comments and always run if build below 9
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewScholefield committed Oct 4, 2017
1 parent c0ec7e1 commit 4c3b1ef
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
58 changes: 41 additions & 17 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,48 @@ def initialize(self):
platform_patch = IntentBuilder("PlatformPatchIntent"). \
require("PlatformPatch").build()
self.register_intent(platform_patch, self.patch_platform)
if self.platform_build is not 2:
self.patch_platform("")

def patch_platform(self, message):
if self.platform_type == "mycroft_mark_1" or self.platform_type == "picroft":
if self.platform_build < 4 or self.platform_build is None and self.platform_build is not 2:
try:
script_fn = NamedTemporaryFile().name
ret_code = call('curl -sL https://mycroft.ai/to/platform_patch_1 | base64 --decode > ' + script_fn, shell=True)
if ret_code == 0:
if call('bash ' + script_fn, shell=True) == 0:
self.speak_dialog("platform.patch.success")
return
except:
pass
self.speak_dialog("platform.patch.failure")
else:
if self.is_eligible() and self.must_apply():
self.patch_platform()

@staticmethod
def cmd(name):
if call(name, shell=True) != 0:
raise RuntimeError('Failed to run command: ' + name)

def is_eligible(self):
"""Duplicate logic exists in remote platform patch script"""
return self.platform_type == "mycroft_mark_1"

def must_apply(self):
"""Duplicate logic exists in remote platform patch script"""
return self.platform_build is None or self.platform_build < 9

@classmethod
def download_patch(cls):
filename = NamedTemporaryFile().name
cls.cmd('curl -sL https://mycroft.ai/to/platform_patch_1 | base64 --decode > ' + filename)
return filename

@classmethod
def run_patch(cls, filename):
"""Replaces crontab, updates GPG key, and sets platform_build to 9"""
cls.cmd('bash ' + filename)

def patch_platform(self):
if not self.is_eligible():
self.speak_dialog("platform.patch.not.possible")
elif not self.must_apply():
self.speak_dialog("platform.patch.not.needed")
else:
try:
name = self.download_patch()
self.run_patch(name)
self.speak_dialog("platform.patch.success")
except Exception as e:
self.speak_dialog("platform.patch.failure", data={
'error': str(e),
'type': e.__class__.__name__
})

def stop(self):
pass
Expand Down
1 change: 0 additions & 1 deletion dialog/en-us/platform.patch.failed.dialog

This file was deleted.

1 change: 1 addition & 0 deletions dialog/en-us/platform.patch.failure.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Platform patch failed because of a {{type}}. {{error}}
1 change: 1 addition & 0 deletions dialog/en-us/platform.patch.not.needed.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Platform patch is not needed.
2 changes: 1 addition & 1 deletion dialog/en-us/platform.patch.not.possible.dialog
Original file line number Diff line number Diff line change
@@ -1 +1 @@
platform patching was not needed or failed
Platform patch is not possible on this platform.
2 changes: 1 addition & 1 deletion test/intent/sample1.intent.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"utterance": "platform patch",
"intent_type": "PlatformPatchIntent",
"intent": {
"ThankYouKeyword": "thank you"
"PlatformPatch": "platform patch"
}
}

0 comments on commit 4c3b1ef

Please sign in to comment.