-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkano-sync
executable file
·278 lines (215 loc) · 7.62 KB
/
kano-sync
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# kano-sync
#
# Copyright (C) 2014, 2015 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
#
"""
Sync your local Kano profile to Kano World
Usage:
kano-sync [-s ][-d] [--sync] [--backup] [--restore] [--upload-tracking-data] [--skip-kdesk]
kano-sync -h | --help
Options:
-h, --help Show this screen
-s, --silent Silent mode, makes failures quiet
-d, --dialog Display dialog
--sync Sync Profile Data
--backup Backup app content
--restore Restore app content
--skip-kdesk Skip kdesk refresh (happens at the end of sync and restore)
"""
import os
import sys
import time
import tempfile
from sys import platform as _platform
from docopt import docopt
if __name__ == '__main__' and __package__ is None:
dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if dir_path != '/usr':
sys.path.insert(1, dir_path)
from kano_world.functions import sync, login_using_token, backup_content, \
restore_content, upload_tracking_data, mark_notification_read
from kano.utils import get_home, run_cmd, read_json, delete_file, run_bg, \
is_gui, delete_dir
from kano_profile.paths import app_profiles_file
from kano.logging import logger
from kano_profile.profile import load_profile, save_profile, save_profile_variable
from kano.notifications import display_generic_notification
home_dir = get_home()
MAX_WORLD_NOTIFICATIONS = 5
def display_msg(msg, level, dialog=True):
if dialog and is_gui():
from kano.gtk3.kano_dialog import KanoDialog
kdialog = KanoDialog(msg, "")
kdialog.run()
if level == 'info':
logger.info(msg)
elif level == 'error':
logger.error(msg)
def do_login(silent=False, dialog=True):
logger.debug('do_login')
# login first with token, if not working then try with dialog
# do not show dialog if in silent mode
login_success, value = login_using_token()
if not login_success:
logger.debug('token login not successful msg: {}'.format(value))
if silent:
logger.debug(
"Can't login, exiting gracefully because of the --silent flag"
)
return False
logger.debug('trying to log in with kano-login')
run_cmd(os.path.join(dir_path, 'bin', 'kano-login'))
login_success, value = login_using_token()
if not login_success:
display_msg(
'Login not possible, error: {}'.format(value),
'error',
dialog=dialog
)
return False
return True
def _any_notifications_available(profile):
return ('notifications' in profile and
type(profile['notifications']) is list and
len(profile['notifications']) > 0)
def _display_notifications():
profile = load_profile()
if _any_notifications_available(profile):
for n in profile['notifications'][0:MAX_WORLD_NOTIFICATIONS]:
image = n["image"] if "image" in n else None
command = n["command"] if "command" in n else None
sound = n["sound"] if "sound" in n else None
display_generic_notification(
n["title"], n["byline"], image, command, sound, "world"
)
mark_notification_read(n['id'])
profile['last_notifications_prompt'] = int(time.time())
save_profile(profile, skip_kdesk_refresh=True)
else:
logger.debug('No notifications to show')
def do_sync(skip_kdesk_refresh=False, dialog=True):
logger.debug('do_sync')
success, value = sync()
if not success:
display_msg(
'Sync not possible, error: {}'.format(value),
'error',
dialog=dialog
)
return False
# Push notifications
try:
_display_notifications()
except Exception as e:
logger.warn(
"Unable to display notifications due to an exception ({})"
.format(e)
)
if not skip_kdesk_refresh:
refresh_kdesk()
save_profile_variable(
'first_sync_done',
True,
skip_kdesk_refresh=True
)
display_msg('Sync OK', 'info', dialog=dialog)
return True
def do_backup(dialog=True):
logger.debug('do_backup')
os.chdir(home_dir)
folders = []
app_profiles_data = read_json(app_profiles_file)
for key, values in app_profiles_data.iteritems():
if 'dir' in values:
dir_path = values['dir']
if os.path.exists(dir_path):
folders.append(values['dir'])
if not folders:
return
tmp_dir = tempfile.mkdtemp()
tmp_file = os.path.join(tmp_dir, 'backup.tar.gz')
delete_file(tmp_file)
tar_cmd = 'tar -czf {tmp_file} {folders} --exclude=webload'.format(
tmp_file=tmp_file, folders=' '.join(folders))
if _platform == 'darwin':
tar_cmd = 'g' + tar_cmd
_, e, _ = run_cmd(tar_cmd)
if e:
msg = 'Error with compressing backup data: {}'.format(e)
display_msg(msg, 'error', dialog=dialog)
delete_dir(tmp_dir)
return
success, error = backup_content(tmp_file)
if not success:
msg = 'Error with uploading backup data: {}'.format(e)
display_msg(msg, 'error', dialog=dialog)
else:
display_msg('Backup OK', 'info', dialog=dialog)
delete_file(tmp_file)
delete_dir(tmp_dir)
def do_restore(skip_kdesk_refresh=False, dialog=True):
logger.debug('do_restore')
os.chdir(home_dir)
tmp_dir = tempfile.mkdtemp()
tmp_file = os.path.join(tmp_dir, 'restore.tar.gz')
delete_file(tmp_file)
success, error = restore_content(tmp_file)
if not success:
msg = 'Error with downloading restore data: ' + error
display_msg(msg, 'error', dialog=dialog)
delete_dir(tmp_dir)
return
if _platform == 'darwin':
cmd_temp = 'gtar -xzf {} --skip-old-files --overwrite --overwrite-dir'
else:
cmd_temp = 'tar -xzf {} --overwrite'
tar_cmd = cmd_temp.format(tmp_file)
_, e, _ = run_cmd(tar_cmd)
if e:
msg = 'Error with uncompressing restore data: {}'.format(e)
display_msg(msg, 'error', dialog=dialog)
if not skip_kdesk_refresh:
refresh_kdesk()
delete_file(tmp_file)
delete_dir(tmp_dir)
display_msg('Restore OK', 'info', dialog=dialog)
def do_upload_tracking_data(dialog=True):
logger.debug('do_upload_tracking_data')
success, value = upload_tracking_data()
if not success:
display_msg(
'Upload not possible, error: {}'.format(value),
'error',
dialog=dialog
)
return
def refresh_kdesk():
logger.info('refreshing kdesk from kano-sync')
if os.path.exists('/usr/bin/kdesk'):
run_bg('kdesk -a profile ; kdesk -a world')
def main(args):
skip_kdesk_refresh = args['--skip-kdesk']
dialog = args['--dialog']
silent = args['--silent']
login_successful = do_login(silent=silent, dialog=dialog)
if login_successful:
if args['--sync']:
do_sync(skip_kdesk_refresh=skip_kdesk_refresh, dialog=dialog)
if args['--backup']:
do_backup(dialog=dialog)
if args['--restore']:
do_restore(skip_kdesk_refresh=skip_kdesk_refresh, dialog=dialog)
if args['--upload-tracking-data']:
do_upload_tracking_data(dialog=dialog)
return 0
else:
# We want to refresh kdesk even if the user is not, or can't log in
if not skip_kdesk_refresh:
refresh_kdesk()
return 1
if __name__ == '__main__':
args = docopt(__doc__)
rc = main(args)
sys.exit(rc)