1 | #!/usr/bin/env python |
---|
2 | import glob |
---|
3 | import ConfigParser |
---|
4 | import os |
---|
5 | import json |
---|
6 | import subprocess |
---|
7 | import shutil |
---|
8 | import urllib2 |
---|
9 | import datetime |
---|
10 | import platform |
---|
11 | import time |
---|
12 | import tempfile |
---|
13 | |
---|
14 | |
---|
15 | DEBUG=False |
---|
16 | GDRIVE_CONFIG_DIR=os.path.expanduser("~/.gdfuse/") |
---|
17 | LLIUREX_CONFIG_FILE_64='/usr/share/lliurex-gdrive/llx-data/config_64' |
---|
18 | LLIUREX_CONFIG_FILE_32='/usr/share/lliurex-gdrive/llx-data/config_32' |
---|
19 | FIREFOX_BROWSER_BIN='/usr/share/lliurex-gdrive/llx-data/firefox-browser' |
---|
20 | CHROME_BROWSER_BIN='/usr/share/lliurex-gdrive/llx-data/chrome-browser' |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | class LliurexGoogleDriveManager(object): |
---|
25 | |
---|
26 | def __init__(self): |
---|
27 | |
---|
28 | super(LliurexGoogleDriveManager, self).__init__() |
---|
29 | |
---|
30 | self.config_dir=os.path.expanduser("~/.config/lliurex-google-drive-profiles/") |
---|
31 | self.config_file=self.config_dir+"configProfiles" |
---|
32 | |
---|
33 | self.bin_dir=os.path.expanduser("~/.local/bin") |
---|
34 | self.chromium_path=self.bin_dir+"/chromium-browser" |
---|
35 | self.mount_cmd="google-drive-ocamlfuse -label %s %s" |
---|
36 | self.clean_cache="google-drive-ocamlfuse -cc -label %s" |
---|
37 | |
---|
38 | self.gdrive_path=[] |
---|
39 | self.read_gdrive_folder=False |
---|
40 | self.browser_changed=False |
---|
41 | |
---|
42 | |
---|
43 | self.read_conf() |
---|
44 | |
---|
45 | |
---|
46 | #def init |
---|
47 | |
---|
48 | def create_conf(self): |
---|
49 | |
---|
50 | if not os.path.exists(self.config_dir): |
---|
51 | os.makedirs(self.config_dir) |
---|
52 | |
---|
53 | var={} |
---|
54 | # var["default"]={} |
---|
55 | # var["default"]["email"]="" |
---|
56 | # var["default"]["mountpoint"]="" |
---|
57 | # var["default"]["automount"]="" |
---|
58 | |
---|
59 | f=open(self.config_file,"w") |
---|
60 | data=unicode(json.dumps(var,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
61 | |
---|
62 | f.write(data) |
---|
63 | f.close() |
---|
64 | |
---|
65 | msg_log="Creating conf..." |
---|
66 | self.dprint(msg_log) |
---|
67 | |
---|
68 | |
---|
69 | #def create_conf |
---|
70 | |
---|
71 | def read_conf(self): |
---|
72 | |
---|
73 | if not os.path.exists(self.config_file): |
---|
74 | self.create_conf() |
---|
75 | |
---|
76 | log_msg="------------------------------------------\n"+"LLIUREX-GDRIVE STARTING AT: " + datetime.datetime.today().strftime("%d/%m/%y %H:%M:%S") +"\n------------------------------------------" |
---|
77 | self.log(log_msg) |
---|
78 | msg_log="Init: Reading conf..." |
---|
79 | self.dprint(msg_log) |
---|
80 | self.log(msg_log) |
---|
81 | |
---|
82 | f=open(self.config_file) |
---|
83 | try: |
---|
84 | self.profiles_config=json.load(f) |
---|
85 | except: |
---|
86 | self.profiles_config={} |
---|
87 | f.close() |
---|
88 | |
---|
89 | #def read_conf |
---|
90 | |
---|
91 | def check_config(self,profile): |
---|
92 | |
---|
93 | path=GDRIVE_CONFIG_DIR+profile+"/state" |
---|
94 | profile=profile.encode("utf-8") |
---|
95 | |
---|
96 | if os.path.exists(path): |
---|
97 | f=open(path) |
---|
98 | line=f.readline() |
---|
99 | f.close() |
---|
100 | |
---|
101 | if "1970-" in line: |
---|
102 | msg_log="Check config: '%s' not configured"%profile |
---|
103 | self.dprint(msg_log) |
---|
104 | self.log(msg_log) |
---|
105 | return False |
---|
106 | |
---|
107 | msg_log="Check config: '%s' yet configured"%profile |
---|
108 | self.dprint(msg_log) |
---|
109 | self.log(msg_log) |
---|
110 | return True |
---|
111 | |
---|
112 | else: |
---|
113 | msg_log="Check config: '%s' not yet create"%profile |
---|
114 | self.dprint(msg_log) |
---|
115 | self.log(msg_log) |
---|
116 | return False |
---|
117 | |
---|
118 | |
---|
119 | #def check_config |
---|
120 | |
---|
121 | def check_google_connection(self): |
---|
122 | |
---|
123 | try: |
---|
124 | req=urllib2.Request("http://google.com") |
---|
125 | res=urllib2.urlopen(req) |
---|
126 | return True |
---|
127 | except: |
---|
128 | msg_log="Check connection: Cannot connect to google.com" |
---|
129 | self.dprint(msg_log) |
---|
130 | self.log(msg_log) |
---|
131 | return False |
---|
132 | |
---|
133 | #def check_google_connection |
---|
134 | |
---|
135 | def mount_drive(self,profile,mountpoint): |
---|
136 | |
---|
137 | #profile=profile.encode("utf-8") |
---|
138 | #mountpoint=mountpoint.encode("utf-8") |
---|
139 | |
---|
140 | try: |
---|
141 | |
---|
142 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
143 | check= self.check_config(profile) |
---|
144 | if check: |
---|
145 | #if profile in self.profiles_config: |
---|
146 | #mount_point=os.path.expanduser(self.profiles_config[profile]["mountpoint"]) |
---|
147 | #if type(mount_point)==type(u""): |
---|
148 | if mountpoint!="": |
---|
149 | self.dprint("Mounting '%s'..."%profile) |
---|
150 | if not os.path.exists(mountpoint): |
---|
151 | try: |
---|
152 | os.makedirs(mountpoint) |
---|
153 | except: |
---|
154 | msg_log="Mount drive: Unable to create '%s' mount destination"%mountpoint.encode("utf-8") |
---|
155 | self.dprint(msg_log) |
---|
156 | self.log(msg_log) |
---|
157 | return {"result":False,"code":1} |
---|
158 | |
---|
159 | |
---|
160 | if os.access(mountpoint,os.W_OK): |
---|
161 | #os.system(self.mount_cmd%(profile,mountpoint)) |
---|
162 | cmd=self.mount_cmd%(profile,mountpoint) |
---|
163 | p=subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
164 | poutput,perror=p.communicate() |
---|
165 | if len(perror)==0: |
---|
166 | msg_log="Mount drive: drive '%s' mounted sucessfully:"%mountpoint.encode("utf-8") |
---|
167 | self.dprint(msg_log) |
---|
168 | self.log(msg_log) |
---|
169 | return {"result":True,"code":0} |
---|
170 | |
---|
171 | else: |
---|
172 | msg_log="Mount drive: Error mount '%s': %s"%(mountpoint.encode("utf-8"),str(perror)) |
---|
173 | self.code=2 |
---|
174 | self.dprint(msg_log) |
---|
175 | self.log(msg_log) |
---|
176 | |
---|
177 | else: |
---|
178 | msg_log="Mount drive: Mount drive: '%s' mount destination is not owned by user"%mountpoint.encode("utf-8") |
---|
179 | self.code=3 |
---|
180 | self.dprint(msg_log) |
---|
181 | self.log(msg_log) |
---|
182 | |
---|
183 | else: |
---|
184 | msg_log="Mount drive: No mount point indicated" |
---|
185 | self.code=4 |
---|
186 | self.dprint(msg_log) |
---|
187 | self.log(msg_log) |
---|
188 | else: |
---|
189 | msg_log="Mount drive: '%s' mount point not configured"%profile.encode("utf-8") |
---|
190 | self.code=5 |
---|
191 | self.dprint(msg_log) |
---|
192 | self.log(msg_log) |
---|
193 | else: |
---|
194 | msg_log="Mount drive: '%s' GDrive profile path does not exist"%profile.encode("utf-8") |
---|
195 | self.code=6 |
---|
196 | self.dprint(msg_log) |
---|
197 | self.log(msg_log) |
---|
198 | |
---|
199 | except Exception as e: |
---|
200 | |
---|
201 | raise e |
---|
202 | |
---|
203 | |
---|
204 | return {"result":False,"code":self.code} |
---|
205 | |
---|
206 | #def mount_drive |
---|
207 | |
---|
208 | def mount_drives(self): |
---|
209 | |
---|
210 | if self.check_google_connection(): |
---|
211 | for profile in self.profiles_config: |
---|
212 | automount=self.profiles_config[profile]["automount"] |
---|
213 | if automount: |
---|
214 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
215 | #profile=profile.encode("utf-8") |
---|
216 | #mountpoint=mountpoint.encode("utf-8") |
---|
217 | self.mount_drive(profile,mountpoint) |
---|
218 | |
---|
219 | |
---|
220 | #def mount_drives |
---|
221 | |
---|
222 | def dprint(self,msg): |
---|
223 | |
---|
224 | if DEBUG: |
---|
225 | print("[LGDM] %s"%msg) |
---|
226 | |
---|
227 | #def dprint |
---|
228 | |
---|
229 | |
---|
230 | def save_profiles(self,info): |
---|
231 | |
---|
232 | self.profiles_config=info |
---|
233 | |
---|
234 | f=open(self.config_file,"w") |
---|
235 | #data=json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False) |
---|
236 | data=unicode(json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
237 | f.write(data) |
---|
238 | f.close() |
---|
239 | |
---|
240 | #def save_profiles |
---|
241 | |
---|
242 | def check_mountpoint_status(self,mountpoint,arg=None): |
---|
243 | |
---|
244 | mountpoint_size="" |
---|
245 | mountpoint_used="" |
---|
246 | mountpoint_available="" |
---|
247 | mountpoint_per="" |
---|
248 | error=False |
---|
249 | tmp=[] |
---|
250 | connect=True |
---|
251 | |
---|
252 | if arg!=None: |
---|
253 | connect=self.check_google_connection() |
---|
254 | |
---|
255 | if connect: |
---|
256 | command='df -h | grep "google-drive-ocamlfuse" | grep ' +mountpoint+'$' |
---|
257 | |
---|
258 | p=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
259 | poutput,perror=p.communicate() |
---|
260 | |
---|
261 | if len(poutput)>0: |
---|
262 | status=True |
---|
263 | |
---|
264 | for item in poutput.split(" "): |
---|
265 | if len(item)>1: |
---|
266 | tmp.append(item) |
---|
267 | |
---|
268 | mountpoint_size=tmp[1] |
---|
269 | mountpoint_used=tmp[2] |
---|
270 | mountpoint_available=tmp[3] |
---|
271 | mountpoint_per=tmp[4] |
---|
272 | |
---|
273 | else: |
---|
274 | if len(perror)>0: |
---|
275 | error=True |
---|
276 | status=False |
---|
277 | self.dismount_mountpoint(mountpoint) |
---|
278 | else: |
---|
279 | status=False |
---|
280 | else: |
---|
281 | status=None |
---|
282 | |
---|
283 | return {"status":status,"size":mountpoint_size,"used":mountpoint_used,"available":mountpoint_available,"used%":mountpoint_per,"error":error} |
---|
284 | |
---|
285 | #def check_mountpoint_status |
---|
286 | |
---|
287 | def check_profile_info(self,profile,mountpoint,edition,root_folder,gdrive_folder): |
---|
288 | |
---|
289 | ''' |
---|
290 | code=0: Form OK |
---|
291 | code=10: Profile empty |
---|
292 | code=11: Profile with blanck spaces |
---|
293 | code=12: Profile duplicate |
---|
294 | |
---|
295 | ''' |
---|
296 | #profile=profile.decode("utf-8") |
---|
297 | ###############NUEVO PUNTO ##########"" |
---|
298 | if not edition: |
---|
299 | |
---|
300 | |
---|
301 | # type(unicode) ==> encode("utf-8") |
---|
302 | # type(str) ==> decode("utf-8") |
---|
303 | |
---|
304 | |
---|
305 | if profile=="": |
---|
306 | return {"result":False,"code":10} |
---|
307 | |
---|
308 | if ' ' in profile: |
---|
309 | return {"result":False,"code":11} |
---|
310 | |
---|
311 | else: |
---|
312 | for item in self.profiles_config: |
---|
313 | if profile==item: |
---|
314 | return {"result":False,"code":12} |
---|
315 | |
---|
316 | else: |
---|
317 | |
---|
318 | if root_folder: |
---|
319 | if len(self.gdrive_path)>1: |
---|
320 | if gdrive_folder=="": |
---|
321 | return {"result":False,"code":17} |
---|
322 | else: |
---|
323 | cont=0 |
---|
324 | for item in self.gdrive_path: |
---|
325 | if type(item) is str: |
---|
326 | item=item.decode("utf-8") |
---|
327 | if item == gdrive_folder: |
---|
328 | cont=cont+1 |
---|
329 | if cont==0: |
---|
330 | return {"result":False,"code":19} |
---|
331 | |
---|
332 | else: |
---|
333 | if self.read_gdrive_folder==True: |
---|
334 | return {"result":False,"code":20} |
---|
335 | else: |
---|
336 | tmp_mountpoint=tempfile.mkdtemp('_Gdrive') |
---|
337 | cmd=self.clean_cache%profile.encode("utf-8") |
---|
338 | os.system(cmd) |
---|
339 | self.mount_drive(profile,tmp_mountpoint) |
---|
340 | status=self.check_mountpoint_status(tmp_mountpoint) |
---|
341 | |
---|
342 | if not status['error']: |
---|
343 | self.dismount_mountpoint(tmp_mountpoint,profile) |
---|
344 | error=False |
---|
345 | else: |
---|
346 | error=True |
---|
347 | |
---|
348 | try: |
---|
349 | shutil.rmtree(tmp_mountpoint) |
---|
350 | |
---|
351 | except Exception as e: |
---|
352 | pass |
---|
353 | |
---|
354 | if error: |
---|
355 | return {"result":False,"code":19} |
---|
356 | |
---|
357 | ''' |
---|
358 | for item in self.profiles_config: |
---|
359 | if profile!=item: |
---|
360 | if mountpoint==self.profiles_config[item]["mountpoint"]: |
---|
361 | return {"result":False,"code":4} |
---|
362 | |
---|
363 | |
---|
364 | if not os.access(mountpoint,os.W_OK): |
---|
365 | return {"result":False,"code":6} |
---|
366 | |
---|
367 | |
---|
368 | if os.listdir(mountpoint): |
---|
369 | if not edition: |
---|
370 | return {"result":False,"code":5} |
---|
371 | else: |
---|
372 | if mountpoint != self.profiles_config[profile]["mountpoint"]: |
---|
373 | return {"result":False,"code":5} |
---|
374 | |
---|
375 | |
---|
376 | return {"result":True,"code":0} |
---|
377 | |
---|
378 | ''' |
---|
379 | return self.check_mountpoint_folder(profile,mountpoint,edition) |
---|
380 | |
---|
381 | |
---|
382 | #def check_profile_info |
---|
383 | |
---|
384 | |
---|
385 | def check_mountpoint_folder(self,profile,mountpoint,edition): |
---|
386 | |
---|
387 | ''' |
---|
388 | code=13: Mountpoint duplicate |
---|
389 | code=14: Mountpoint not empty |
---|
390 | code=15: Mountpoint not owned by user |
---|
391 | ''' |
---|
392 | |
---|
393 | for item in self.profiles_config: |
---|
394 | if profile!=item: |
---|
395 | if mountpoint==self.profiles_config[item]["mountpoint"]: |
---|
396 | return {"result":False,"code":13} |
---|
397 | |
---|
398 | if not os.access(mountpoint,os.W_OK): |
---|
399 | return {"result":False,"code":15} |
---|
400 | |
---|
401 | if ' ' in mountpoint: |
---|
402 | return {"result":False,"code":16} |
---|
403 | |
---|
404 | if os.listdir(mountpoint): |
---|
405 | if not edition: |
---|
406 | return {"result":False,"code":14} |
---|
407 | else: |
---|
408 | if mountpoint != self.profiles_config[profile]["mountpoint"]: |
---|
409 | return {"result":False,"code":14} |
---|
410 | |
---|
411 | |
---|
412 | return {"result":True,"code":0} |
---|
413 | |
---|
414 | #def check_mountpoint_folder |
---|
415 | |
---|
416 | |
---|
417 | def create_profile(self,profile): |
---|
418 | |
---|
419 | #profile=unicode(profile).encode("utf-8") |
---|
420 | path=GDRIVE_CONFIG_DIR+profile+"/config" |
---|
421 | |
---|
422 | if not self.check_config(profile): |
---|
423 | os.system("google-drive-ocamlfuse -label %s"%unicode(profile).encode("utf-8")) |
---|
424 | msg_log=("'%s' profile has been create")%profile.encode("utf-8") |
---|
425 | self.log(msg_log) |
---|
426 | self.dprint(msg_log) |
---|
427 | |
---|
428 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
429 | if platform.architecture()[0]=='64bit': |
---|
430 | shutil.copy(LLIUREX_CONFIG_FILE_64,path ) |
---|
431 | else: |
---|
432 | shutil.copy(LLIUREX_CONFIG_FILE_32,path ) |
---|
433 | |
---|
434 | self.remove_chromium_bin() |
---|
435 | |
---|
436 | return True |
---|
437 | |
---|
438 | |
---|
439 | #def create_profile |
---|
440 | |
---|
441 | def create_mountpoint(self,info,profile): |
---|
442 | |
---|
443 | mountpoint=info[profile]["mountpoint"] |
---|
444 | |
---|
445 | result=self.mount_drive(profile,mountpoint) |
---|
446 | |
---|
447 | if result["result"]: |
---|
448 | self.save_profiles(info) |
---|
449 | else: |
---|
450 | if profile !="": |
---|
451 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
452 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
453 | self.dprint("'%s' profile has been delete"%profile) |
---|
454 | |
---|
455 | return result |
---|
456 | |
---|
457 | #def create_mountpoint |
---|
458 | |
---|
459 | def dismount_mountpoint(self,mountpoint,profile=None): |
---|
460 | |
---|
461 | if profile != None: |
---|
462 | cmd='fusermount -u -z ' + mountpoint + ";"+self.clean_cache%profile |
---|
463 | else: |
---|
464 | cmd='fusermount -u -z ' + mountpoint |
---|
465 | |
---|
466 | p=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
467 | poutput,perror=p.communicate() |
---|
468 | |
---|
469 | mountpoint=mountpoint.encode("utf-8") |
---|
470 | if len(perror)>0: |
---|
471 | msg_log="Dismount mountpoint: Error dismounted '%s': '%s'"%(mountpoint,str(perror)) |
---|
472 | self.dprint(msg_log) |
---|
473 | self.log(msg_log) |
---|
474 | result=False |
---|
475 | code=7 |
---|
476 | else: |
---|
477 | msg_log="Dismount mountpoint:'%s' mountpoint has been dismounted"%mountpoint |
---|
478 | self.dprint(msg_log) |
---|
479 | self.log(msg_log) |
---|
480 | result=True |
---|
481 | code=0 |
---|
482 | |
---|
483 | return {"result":result,"code":code} |
---|
484 | |
---|
485 | #def dismount_mountpoint |
---|
486 | |
---|
487 | def delete_profile(self,info,profile): |
---|
488 | |
---|
489 | result={} |
---|
490 | dismount={} |
---|
491 | dismount["result"]=True |
---|
492 | dismount["code"]=0 |
---|
493 | |
---|
494 | connect=self.check_google_connection() |
---|
495 | #profile=str(profile) |
---|
496 | #if connect: |
---|
497 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
498 | |
---|
499 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
500 | is_mountpoint_mounted=self.check_mountpoint_status(mountpoint,True) |
---|
501 | |
---|
502 | if is_mountpoint_mounted["status"]==None: |
---|
503 | result['result']=False |
---|
504 | result['code']=8 |
---|
505 | else: |
---|
506 | if is_mountpoint_mounted["status"]: |
---|
507 | dismount=self.dismount_mountpoint(mountpoint,profile) |
---|
508 | |
---|
509 | if dismount["result"]: |
---|
510 | if profile!="": |
---|
511 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
512 | msg_log="Delete profile: '%s' profile has been delete"%profile.encode("utf-8") |
---|
513 | self.log(msg_log) |
---|
514 | self.dprint(msg_log) |
---|
515 | self.save_profiles(info) |
---|
516 | |
---|
517 | return dismount |
---|
518 | |
---|
519 | |
---|
520 | else: |
---|
521 | self.save_profiles(info) |
---|
522 | msg_log="Delete profile: '%s' GDrive profile path does not exist"%profile.encode("utf-8") |
---|
523 | self.log(msg_log) |
---|
524 | self.dprint(msg_log) |
---|
525 | result['result']=True |
---|
526 | result['code']=0 |
---|
527 | |
---|
528 | return result |
---|
529 | |
---|
530 | |
---|
531 | #def delete_profile |
---|
532 | |
---|
533 | |
---|
534 | def edit_profile(self,info,profile): |
---|
535 | |
---|
536 | result={} |
---|
537 | result["result"]=True |
---|
538 | result["code"]=0 |
---|
539 | |
---|
540 | change_config=False |
---|
541 | |
---|
542 | old_mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
543 | old_automount=self.profiles_config[profile]["automount"] |
---|
544 | new_mountpoint=info[profile]["mountpoint"] |
---|
545 | new_automount=info[profile]["automount"] |
---|
546 | |
---|
547 | try: |
---|
548 | old_root_folder=self.profiles_config[profile]["root_folder"] |
---|
549 | old_gdrive_folder=self.profiles_config[profile]["gdrive_folder"] |
---|
550 | except Exception as e: |
---|
551 | old_root_folder="" |
---|
552 | old_gdrive_folder="" |
---|
553 | |
---|
554 | new_root_folder=info[profile]["root_folder"] |
---|
555 | new_gdrive_folder=info[profile]["gdrive_folder"] |
---|
556 | |
---|
557 | |
---|
558 | if new_root_folder != old_root_folder: |
---|
559 | change_config=True |
---|
560 | |
---|
561 | else: |
---|
562 | if old_gdrive_folder !=new_gdrive_folder: |
---|
563 | change_config=True |
---|
564 | |
---|
565 | status=self.check_mountpoint_status(old_mountpoint) |
---|
566 | |
---|
567 | if old_mountpoint!=new_mountpoint: |
---|
568 | |
---|
569 | if status["status"]: |
---|
570 | result=self.dismount_mountpoint(old_mountpoint,profile) |
---|
571 | |
---|
572 | if result["result"]: |
---|
573 | if change_config: |
---|
574 | self.change_config_file(profile,new_gdrive_folder) |
---|
575 | result=self.mount_drive(profile,new_mountpoint) |
---|
576 | else: |
---|
577 | if change_config: |
---|
578 | self.change_config_file(profile,new_gdrive_folder) |
---|
579 | |
---|
580 | else: |
---|
581 | if change_config: |
---|
582 | if status["status"]: |
---|
583 | result=self.dismount_mountpoint(old_mountpoint,profile) |
---|
584 | if result["result"]: |
---|
585 | self.change_config_file(profile,new_gdrive_folder) |
---|
586 | result=self.mount_drive(profile,new_mountpoint) |
---|
587 | else: |
---|
588 | self.change_config_file(profile,new_gdrive_folder) |
---|
589 | |
---|
590 | |
---|
591 | |
---|
592 | if result["result"]: |
---|
593 | self.save_profiles(info) |
---|
594 | msg_log="Edit profile: '%s' profile has been edited"%profile.encode("utf-8") |
---|
595 | self.log(msg_log) |
---|
596 | self.dprint(msg_log) |
---|
597 | |
---|
598 | return {"result":result["result"],"code":result["code"]} |
---|
599 | |
---|
600 | #def edit_profile |
---|
601 | |
---|
602 | def sync_profile(self,profile,mountpoint,current_status): |
---|
603 | |
---|
604 | result={} |
---|
605 | status=self.check_mountpoint_status(mountpoint,True) |
---|
606 | token_mount=profile + "__MountToken" |
---|
607 | token_mount_path=os.path.join(self.config_dir,token_mount) |
---|
608 | token_dismount=profile + "__DismountToken" |
---|
609 | token_dismount_path=os.path.join(self.config_dir,token_dismount) |
---|
610 | |
---|
611 | |
---|
612 | if status["status"]==None: |
---|
613 | result['result']=False |
---|
614 | result['code']=8 |
---|
615 | |
---|
616 | else: |
---|
617 | if current_status!=None and status['status']==current_status: |
---|
618 | if status['status']: |
---|
619 | result=self.dismount_mountpoint(mountpoint,profile) |
---|
620 | f=open(token_dismount_path,'w') |
---|
621 | f.close() |
---|
622 | os.remove(token_dismount_path) |
---|
623 | else: |
---|
624 | if not status['error']: |
---|
625 | result=self.mount_drive(profile,mountpoint) |
---|
626 | f=open(token_mount_path,'w') |
---|
627 | f.close() |
---|
628 | os.remove(token_mount_path) |
---|
629 | else: |
---|
630 | result['result']=False |
---|
631 | result['code']=18 |
---|
632 | return result,status |
---|
633 | |
---|
634 | else: |
---|
635 | result['result']=True |
---|
636 | result['code']=9 |
---|
637 | |
---|
638 | status=self.check_mountpoint_status(mountpoint) |
---|
639 | if status['error']: |
---|
640 | result['result']=False |
---|
641 | result['code']=18 |
---|
642 | |
---|
643 | return result,status |
---|
644 | |
---|
645 | #result["result"]:result["result"] |
---|
646 | #result["code"]:result["code"] |
---|
647 | #return {"result":result["result"],"code":result["code"]} |
---|
648 | |
---|
649 | #def_sync_profile |
---|
650 | |
---|
651 | def read_mountpoint_directory(self,profile): |
---|
652 | |
---|
653 | result={} |
---|
654 | result['result']=True |
---|
655 | path_orig=GDRIVE_CONFIG_DIR+profile+"/config" |
---|
656 | path_tmp=GDRIVE_CONFIG_DIR+profile+"/config_tmp" |
---|
657 | create_tmp=False |
---|
658 | copy_config=False |
---|
659 | directory=[] |
---|
660 | |
---|
661 | |
---|
662 | mountpoint=self.profiles_config[profile.decode("utf-8")]["mountpoint"] |
---|
663 | |
---|
664 | |
---|
665 | |
---|
666 | try: |
---|
667 | root_folder=self.profiles_config[profile.decode("utf-8")]["root_folder"] |
---|
668 | except Exception as e: |
---|
669 | root_folder=False |
---|
670 | |
---|
671 | |
---|
672 | status=self.check_mountpoint_status(mountpoint) |
---|
673 | |
---|
674 | if status["status"]: |
---|
675 | if root_folder: |
---|
676 | create_tmp=True |
---|
677 | copy_config=True |
---|
678 | |
---|
679 | else: |
---|
680 | create_tmp=True |
---|
681 | if root_folder: |
---|
682 | copy_config=True |
---|
683 | |
---|
684 | |
---|
685 | if copy_config: |
---|
686 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
687 | if os.path.exists(path_orig): |
---|
688 | os.rename(path_orig,path_tmp) |
---|
689 | if platform.architecture()[0]=='64bit': |
---|
690 | shutil.copy(LLIUREX_CONFIG_FILE_64,path_orig ) |
---|
691 | else: |
---|
692 | shutil.copy(LLIUREX_CONFIG_FILE_32,path_orig ) |
---|
693 | |
---|
694 | if create_tmp: |
---|
695 | |
---|
696 | if status['status']: |
---|
697 | cmd=self.clean_cache%profile |
---|
698 | os.system(cmd) |
---|
699 | |
---|
700 | tmp_mountpoint=tempfile.mkdtemp('_Gdrive') |
---|
701 | result=self.mount_drive(profile.decode("utf-8"),tmp_mountpoint) |
---|
702 | |
---|
703 | mountpoint=tmp_mountpoint |
---|
704 | |
---|
705 | |
---|
706 | if result['result']: |
---|
707 | |
---|
708 | self.gdrive_path=[] |
---|
709 | self.read_gdrive_folder=True |
---|
710 | |
---|
711 | for base,dirs,file in os.walk(mountpoint): |
---|
712 | if 'Trash' not in base: |
---|
713 | if 'shared' not in base: |
---|
714 | if base !=mountpoint: |
---|
715 | directory.append(base) |
---|
716 | |
---|
717 | directory=sorted(directory) |
---|
718 | |
---|
719 | self.gdrive_path.append('') |
---|
720 | |
---|
721 | for item in directory: |
---|
722 | path=os.path.relpath(item,mountpoint) |
---|
723 | self.gdrive_path.append(path) |
---|
724 | |
---|
725 | |
---|
726 | if create_tmp: |
---|
727 | |
---|
728 | if copy_config: |
---|
729 | os.rename(path_tmp,path_orig) |
---|
730 | |
---|
731 | result=self.dismount_mountpoint(mountpoint,profile) |
---|
732 | |
---|
733 | if result['result']: |
---|
734 | |
---|
735 | shutil.rmtree(mountpoint) |
---|
736 | |
---|
737 | else: |
---|
738 | if copy_config: |
---|
739 | os.rename(path_tmp,path_orig) |
---|
740 | |
---|
741 | cmd=self.clean_cache%profile |
---|
742 | os.system(cmd) |
---|
743 | |
---|
744 | return self.gdrive_path |
---|
745 | |
---|
746 | #def read_mountpoint_directory |
---|
747 | |
---|
748 | |
---|
749 | def change_config_file(self,profile,gdrive_folder): |
---|
750 | |
---|
751 | |
---|
752 | file=GDRIVE_CONFIG_DIR+profile+"/config" |
---|
753 | |
---|
754 | |
---|
755 | old_config=open(file,'r') |
---|
756 | params=[] |
---|
757 | cont=0 |
---|
758 | for line in old_config: |
---|
759 | if 'root_folder' in line: |
---|
760 | line='root_folder='+'/'+gdrive_folder.encode("utf-8")+'\n' |
---|
761 | cont=cont+1 |
---|
762 | params.append(line) |
---|
763 | |
---|
764 | |
---|
765 | if cont==0: |
---|
766 | line='root_folder='+'\n' |
---|
767 | params.append(line) |
---|
768 | |
---|
769 | old_config.close() |
---|
770 | |
---|
771 | new_config=open(file,'w') |
---|
772 | |
---|
773 | for item in params: |
---|
774 | new_config.write(item) |
---|
775 | |
---|
776 | new_config.close() |
---|
777 | |
---|
778 | #def change_config_file |
---|
779 | |
---|
780 | def is_chromium_favourite_browser(self): |
---|
781 | |
---|
782 | result=False |
---|
783 | |
---|
784 | if os.system("xdg-mime query default x-scheme-handler/https | grep chromium 1>/dev/null")==0: |
---|
785 | result=True |
---|
786 | msg_log="Detected chromium as favourite browser. Unable to add profile" |
---|
787 | self.log(msg_log) |
---|
788 | |
---|
789 | return result |
---|
790 | |
---|
791 | #def is_chromium_favourite_browser |
---|
792 | |
---|
793 | |
---|
794 | def can_change_browser(self): |
---|
795 | |
---|
796 | result=False |
---|
797 | |
---|
798 | if not os.path.exists(self.chromium_path): |
---|
799 | self.installed_browser_detect() |
---|
800 | if len(self.installed_browsers)>0: |
---|
801 | result=True |
---|
802 | |
---|
803 | return result |
---|
804 | |
---|
805 | #def can_change_browser |
---|
806 | |
---|
807 | def installed_browser_detect(self): |
---|
808 | |
---|
809 | self.installed_browsers=[] |
---|
810 | |
---|
811 | if os.system('dpkg -l firefox | grep firefox | grep "^i[i]" 1>/dev/null')==0: |
---|
812 | self.installed_browsers.append("firefox") |
---|
813 | |
---|
814 | if os.system('dpkg -l google-chrome-stable | grep google-chrome-stable | grep "^i[i]" 1>/dev/null')==0: |
---|
815 | self.installed_browsers.append("google-chrome-stable") |
---|
816 | |
---|
817 | |
---|
818 | #def installed_browser_detec |
---|
819 | |
---|
820 | |
---|
821 | def change_default_browser(self): |
---|
822 | |
---|
823 | |
---|
824 | if not os.path.exists(self.bin_dir): |
---|
825 | os.makedirs(self.bin_dir) |
---|
826 | |
---|
827 | |
---|
828 | if "firefox" in self.installed_browsers: |
---|
829 | shutil.copy(FIREFOX_BROWSER_BIN,self.chromium_path) |
---|
830 | else: |
---|
831 | if "google-chrome-stable" in self.installed_browsers: |
---|
832 | shutil.copy(CHROME_BROWSER_BIN,self.chromium_path) |
---|
833 | |
---|
834 | self.browser_changed=True |
---|
835 | |
---|
836 | try: |
---|
837 | cmd='chmod +x '+self.chromium_path |
---|
838 | os.system(cmd) |
---|
839 | except: |
---|
840 | pass |
---|
841 | |
---|
842 | #def change_default_browser |
---|
843 | |
---|
844 | |
---|
845 | def remove_chromium_bin(self): |
---|
846 | |
---|
847 | |
---|
848 | if self.browser_changed: |
---|
849 | if os.path.exists(self.chromium_path): |
---|
850 | os.remove(self.chromium_path) |
---|
851 | |
---|
852 | return |
---|
853 | |
---|
854 | #def remove_chromium_bin |
---|
855 | |
---|
856 | def log(self,msg): |
---|
857 | |
---|
858 | log_file=self.config_dir+"lliurex-gdrive.log" |
---|
859 | |
---|
860 | f=open(log_file,"a+") |
---|
861 | f.write(msg + '\n') |
---|
862 | f.close() |
---|
863 | |
---|
864 | #def log |
---|
865 | |
---|
866 | if __name__=="__main__": |
---|
867 | |
---|
868 | llxgd=LliurexGoogleDriveManager() |
---|
869 | llxgd.mount_drives() |
---|
870 | |
---|
871 | |
---|
872 | |
---|
873 | #llxgd.save_profiles(llxgd.var) |
---|
874 | |
---|
875 | |
---|
876 | |
---|
877 | |
---|
878 | |
---|
879 | |
---|
880 | |
---|
881 | |
---|
882 | |
---|
883 | |
---|