-
Notifications
You must be signed in to change notification settings - Fork 0
/
swUpdate.py
145 lines (127 loc) · 4.51 KB
/
swUpdate.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
# -*- coding: utf-8 -*-
from twisted.internet import reactor, threads
import time
import download_from_url
import json
import os
from os.path import expanduser
class swUpdate():
def __init__(self,cb_download=None, cb_update=None):
self.sw_update_cb = cb_update;
self.sw_download_cb = cb_download;
self.downloaded_files=["","",""];
self.downloaded=False;
self.downloading=False;
self.can_update=False;
def append_current_ver(self, post_data):
home = expanduser("~")
main_fn=home+"/main.ver"
main_f = open(main_fn)
try:
main_ver=main_f.readline()
main_ver=main_ver.strip('\n');
finally:
main_f.close()
model_fn=home+"/model.ver"
model_f = open(model_fn)
try:
model_ver=model_f.readline()
model_ver=model_ver.strip("\n");
finally:
model_f.close()
web_fn=home+"/web.ver"
web_f = open(web_fn)
try:
web_ver=web_f.readline()
web_ver=web_ver.strip("\n")
finally:
web_f.close()
current_ver={'ver':{'main':main_ver,'model':model_ver,'web':web_ver}}
return dict(post_data.items()+current_ver.items())
def can_send_readyupdate(self):
if self.downloaded==True:
return True
return False
def has_download_urls(self, urls):
get_json = json.loads(urls)
xx = get_json['ver'] if 'ver' in get_json else ""
if xx == "":
return False
x = get_json['ver']['main'] if 'main' in get_json['ver'] else ""
if x != "" :
return True
x = get_json['ver']['model'] if 'model' in get_json['ver'] else ""
if x != "" :
return True
x = get_json['ver']['web'] if 'web' in get_json['ver'] else ""
if x != "" :
return True;
return False
def download_cb(result):
print "dowload result =", result
def sw_download(self, urls):
if self.has_download_urls(urls) == True and self.downloading==False:
self.downloading=True
get_json = json.loads(urls)
xx = get_json['ver'] if 'ver' in get_json else ""
if xx == "":
return
call_cb = False
x = get_json['ver']['main'] if 'main' in get_json['ver'] else ""
if x != "" :
self.downloaded_files[0]=download_from_url.downloadChunks(x)
call_cb=True
else:
self.downloaded_files[0]=""
x = get_json['ver']['model'] if 'model' in get_json['ver'] else ""
if x != "" :
self.downloaded_files[1]=download_from_url.downloadChunks(x)
call_cb=True
else:
self.downloaded_files[1]=""
x = get_json['ver']['web'] if 'web' in get_json['ver'] else ""
if x != "" :
self.downloaded_files[2]=download_from_url.downloadChunks(x)
call_cb=True
else:
self.downloaded_files[2]=""
print self.downloaded_files
if self.sw_download_cb!= None and call_cb==True:
self.sw_download_cb()
self.downloaded=True
def sw_update(self, resp):
if self.can_update == True:
return
get_json = json.loads(resp)
print get_json
xx = get_json['ret'] if 'ret' in get_json else ""
if xx == "":
self.can_update=False
return
x = get_json['ret']['action'] if 'action' in get_json['ret'] else ""
if x == "" :
self.can_update=False
return
if x == "upgrade":
self.can_update=True
while self.downloaded==False:
sleep(1)
file_num = len(self.downloaded_files)
for f in self.downloaded_files :
if f != "" :
print "update file "+f;
cmd = "cd /tmp && tar xvfz " +f
print cmd
os.system(cmd)
if self.sw_update_cb!= None and file_num>0:
self.sw_update_cb()
if __name__ == "__main__":
a_swUpdate = swUpdate()
urls='{"ver":{"web":"http://135.251.101.152:80/html.gz"}}'
#reactor.callInThread(a_swUpdate.sw_download,urls)
#print "after def download"
#a_swUpdate.sw_update()
post_data={}
print a_swUpdate.append_current_ver(post_data)
a_swUpdate.sw_download(urls)
reactor.run()