Skip to content

Commit 4c3b1ef

Browse files
Refactor code to add comments and always run if build below 9
1 parent c0ec7e1 commit 4c3b1ef

File tree

6 files changed

+45
-20
lines changed

6 files changed

+45
-20
lines changed

__init__.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,48 @@ def initialize(self):
3939
platform_patch = IntentBuilder("PlatformPatchIntent"). \
4040
require("PlatformPatch").build()
4141
self.register_intent(platform_patch, self.patch_platform)
42-
if self.platform_build is not 2:
43-
self.patch_platform("")
44-
45-
def patch_platform(self, message):
46-
if self.platform_type == "mycroft_mark_1" or self.platform_type == "picroft":
47-
if self.platform_build < 4 or self.platform_build is None and self.platform_build is not 2:
48-
try:
49-
script_fn = NamedTemporaryFile().name
50-
ret_code = call('curl -sL https://mycroft.ai/to/platform_patch_1 | base64 --decode > ' + script_fn, shell=True)
51-
if ret_code == 0:
52-
if call('bash ' + script_fn, shell=True) == 0:
53-
self.speak_dialog("platform.patch.success")
54-
return
55-
except:
56-
pass
57-
self.speak_dialog("platform.patch.failure")
58-
else:
42+
if self.is_eligible() and self.must_apply():
43+
self.patch_platform()
44+
45+
@staticmethod
46+
def cmd(name):
47+
if call(name, shell=True) != 0:
48+
raise RuntimeError('Failed to run command: ' + name)
49+
50+
def is_eligible(self):
51+
"""Duplicate logic exists in remote platform patch script"""
52+
return self.platform_type == "mycroft_mark_1"
53+
54+
def must_apply(self):
55+
"""Duplicate logic exists in remote platform patch script"""
56+
return self.platform_build is None or self.platform_build < 9
57+
58+
@classmethod
59+
def download_patch(cls):
60+
filename = NamedTemporaryFile().name
61+
cls.cmd('curl -sL https://mycroft.ai/to/platform_patch_1 | base64 --decode > ' + filename)
62+
return filename
63+
64+
@classmethod
65+
def run_patch(cls, filename):
66+
"""Replaces crontab, updates GPG key, and sets platform_build to 9"""
67+
cls.cmd('bash ' + filename)
68+
69+
def patch_platform(self):
70+
if not self.is_eligible():
5971
self.speak_dialog("platform.patch.not.possible")
72+
elif not self.must_apply():
73+
self.speak_dialog("platform.patch.not.needed")
74+
else:
75+
try:
76+
name = self.download_patch()
77+
self.run_patch(name)
78+
self.speak_dialog("platform.patch.success")
79+
except Exception as e:
80+
self.speak_dialog("platform.patch.failure", data={
81+
'error': str(e),
82+
'type': e.__class__.__name__
83+
})
6084

6185
def stop(self):
6286
pass

dialog/en-us/platform.patch.failed.dialog

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Platform patch failed because of a {{type}}. {{error}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Platform patch is not needed.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
platform patching was not needed or failed
1+
Platform patch is not possible on this platform.

test/intent/sample1.intent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"utterance": "platform patch",
33
"intent_type": "PlatformPatchIntent",
44
"intent": {
5-
"ThankYouKeyword": "thank you"
5+
"PlatformPatch": "platform patch"
66
}
77
}

0 commit comments

Comments
 (0)