-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmanager.py
155 lines (116 loc) · 6.01 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import Domoticz
import os
import subprocess
from shutil import rmtree
class Plugin():
def __init__(self, plugins_folder, plugin_data):
self.name = plugin_data['name']
self.author = plugin_data['author']
self.description = plugin_data['description']
self.repository = plugin_data['repository']
self.branch = plugin_data['branch']
self.folder_name = plugin_data['folder']
self.plugin_folder = str(plugins_folder + self.folder_name)
def is_installed(self):
return os.path.isdir(self.plugin_folder) == True
def is_update_available(self):
Domoticz.Debug('Checking plugin "' + self.name + '" for updates')
if (self.is_installed() == False):
return False
ppGitFetch="LANG=en_US /usr/bin/git fetch"
try:
prFetch=subprocess.Popen(ppGitFetch, cwd = self.plugin_folder, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
(outFetch, errorFetch)=prFetch.communicate()
if outFetch:
Domoticz.Debug("Git Response:" + str(outFetch))
if errorFetch:
Domoticz.Debug("Git Error:" + str(errorFetch.strip()))
except OSError as eFetch:
Domoticz.Error("Git ErrorNo:" + str(eFetch.errno))
Domoticz.Error("Git StrError:" + str(eFetch.strerror))
ppUrl="LANG=en_US /usr/bin/git status -uno"
Domoticz.Debug("Calling:" + ppUrl + " on folder " + self.plugin_folder)
try:
pr=subprocess.Popen(ppUrl, cwd = self.plugin_folder, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
(out, error)=pr.communicate()
if out:
Domoticz.Debug("Git Response:" + str(out))
if (str(out).find("up-to-date") != -1) or (str(out).find("up to date") != -1):
return False
elif (str(out).find("Your branch is behind") != -1) and (str(str(out).find("error")) == "-1"):
return True
elif (str(out).find("Your branch is ahead") != -1) and (str(str(out).find("error")) == "-1"):
return False
else:
Domoticz.Error('Something went wrong during plugin "' + self.name + '" update')
return None
if error:
Domoticz.Debug("Git Error:" + str(error.strip()))
if str(error).find("Not a git repository") != -1:
Domoticz.Log('Plugin "' + self.name + '" is not installed from gitHub. Ignoring!.')
except OSError as e:
Domoticz.Error("Git ErrorNo:" + str(e.errno))
Domoticz.Error("Git StrError:" + str(e.strerror))
return None
def install(self):
Domoticz.Log("Installing Plugin:" + self.description)
if (self.is_installed()):
return True
plugins_folder = os.path.dirname(str(os.getcwd()) + "/plugins/")
repository = self.repository + ".git"
clone_cmd="LANG=en_US /usr/bin/git clone -b " + self.branch + " " + repository + " " + self.folder_name
Domoticz.Debug("Calling: " + clone_cmd)
try:
pr=subprocess.Popen(clone_cmd, cwd = plugins_folder, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
(out, error)=pr.communicate()
if out:
Domoticz.Log("Succesfully installed:" + str(out).strip)
Domoticz.Log("---Restarting Domoticz MAY BE REQUIRED to activate new plugins---")
return True
if error:
Domoticz.Debug("Git Error:" + str(error))
if str(error).find("Cloning into") != -1:
Domoticz.Log("Plugin " + self.description + " installed Succesfully")
return True
except OSError as e:
Domoticz.Error("Git ErrorNo:" + str(e.errno))
Domoticz.Error("Git StrError:" + str(e.strerror))
return False
def update(self):
Domoticz.Log("Updating Plugin:" + self.description)
if (not self.is_update_available()):
return True
cmd = "LANG=en_US /usr/bin/git pull --force"
Domoticz.Debug('Calling: "' + cmd + '" on folder ' + self.plugin_folder)
try:
pr = subprocess.Popen(cmd, cwd = self.plugin_folder, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
(out, error) = pr.communicate()
if out:
Domoticz.Debug("Git Response:" + str(out))
if (str(out).find("Already up-to-date") != -1) or (str(out).find("Already up to date") != -1):
Domoticz.Debug('Plugin "' + self.description + '" already Up-To-Date')
return True
elif (str(out).find("Updating") != -1) and (str(str(out).find("error")) == "-1"):
Domoticz.Log("Succesfully pulled gitHub update:" + str(out)[str(out).find("Updating")+8:26] + " for plugin " + self.description)
Domoticz.Log("---Restarting Domoticz MAY BE REQUIRED to activate new plugins---")
return True
else:
Domoticz.Error("Something went wrong with update of " + self.description)
if error:
Domoticz.Debug("Git Error:" + str(error.strip()))
if str(error).find("Not a git repository") != -1:
Domoticz.Error("Plugin: " + self.description + " is not installed from gitHub. Cannot be updated with PP-Manager!!.")
except OSError as e:
Domoticz.Error("Git ErrorNo:" + str(e.errno))
Domoticz.Error("Git StrError:" + str(e.strerror))
return False
def uninstall(self):
Domoticz.Log("Uninstalling Plugin:" + self.description)
if not self.is_installed():
return True
try:
rmtree(self.plugin_folder)
return True
except Exception as e:
Domoticz.Error(repr(e))
return False