Changeset 6432 for lliurex-gdrive/trunk/WIP_files/lliurexGdriveIndicator
- Timestamp:
- Dec 5, 2017, 6:00:35 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lliurex-gdrive/trunk/WIP_files/lliurexGdriveIndicator
r6071 r6432 8 8 9 9 import os 10 from os import listdir 11 from os.path import isfile, isfile,isdir,join 10 12 import threading 11 13 import subprocess … … 16 18 import notify2 17 19 import time 20 import datetime 21 import copy 18 22 19 23 … … 45 49 SP8=RSRC+"rsrc/sp8.png" 46 50 51 formato = '%d-%m-%y %H:%M:%S' 47 52 TIME_CHECK_STATUS=900000 48 53 49 54 class LliurexGdriveIndicator: 50 55 51 WATCH_DIR=os.path.expanduser("~/.config/lliurex-google-drive-profiles/ configProfiles")56 WATCH_DIR=os.path.expanduser("~/.config/lliurex-google-drive-profiles/") 52 57 53 58 def __init__(self,icon_name): … … 66 71 self.profiles_info=self.LliurexGoogleDriveManager.profiles_config.copy() 67 72 self.current_status={} 73 self.blacklist=['.Trash','.Trash-1000'] 74 self.init_files=[] 75 self.update_files=[] 76 self.local_changes={} 68 77 69 78 … … 146 155 self.main.update_info() 147 156 self.main.initial_connection=True 148 for profile in self.main.update_profiles: 149 if not profile in self.main.profiles_info: 150 mountpoint=self.main.update_profiles[profile]["mountpoint"] 151 self.main.add_menu_item(profile,mountpoint) 152 153 for item in self.main.menu.get_children(): 154 if item.profile !="": 155 if not item.profile in self.main.update_profiles: 156 self.main.menu.remove(item) 157 158 self.main.profiles_info=self.main.update_profiles.copy() 159 self.main.menu.show_all() 157 158 if os.path.expanduser(event.pathname)==os.path.join(LliurexGdriveIndicator.WATCH_DIR,"configProfiles"): 159 160 for profile in self.main.update_profiles: 161 mountpoint_new=self.main.update_profiles[profile]["mountpoint"] 162 gdriveFolder_new=self.main.update_profiles[profile]["gdrive_folder"] 163 if not profile in self.main.profiles_info: 164 self.main.add_menu_item(profile,mountpoint_new) 165 wdd=wm.add_watch(mountpoint_new,mask,rec=True) 166 else: 167 mountpoint_old=self.main.profiles_info[profile]["mountpoint"] 168 gdriveFolder_old=self.main.profiles_info[profile]["gdrive_folder"] 169 if (mountpoint_new != mountpoint_old) or (gdriveFolder_new != gdriveFolder_old): 170 self.main.remove_folder_info(profile) 171 ide=wm.get_wd(mountpoint_old) 172 wdd=wm.rm_watch(ide,rec=True) 173 self.main.init_folders_info(profile,mountpoint_new) 174 wdd=wm.add_watch(mountpoint_new,mask,rec=True) 175 176 profile_rm="" 177 for item in self.main.menu.get_children(): 178 if item.profile !="": 179 if not item.profile in self.main.update_profiles: 180 self.main.menu.remove(item) 181 profile_rm=item.profile 182 183 if profile_rm !="": 184 mountpoint=self.main.profiles_info[profile_rm]["mountpoint"] 185 self.main.remove_folder_info(profile_rm) 186 ide=wm.get_wd(mountpoint) 187 wdd=wm.rm_watch(ide,rec=True) 160 188 161 189 190 self.main.profiles_info=self.main.update_profiles.copy() 191 self.main.menu.show_all() 192 193 else: 194 if 'mountToken' in os.path.basename(event.pathname): 195 tmp_profile=os.path.basename(event.pathname).split("__")[0] 196 print tmp_profile 197 for item in self.main.profiles_info: 198 if item==tmp_profile: 199 tmp_mountpoint=self.main.profiles_info[item]["mountpoint"] 200 if os.path.exists(event.pathname): 201 self.main.remove_folder_info(item) 202 ide=wm.get_wd(tmp_mountpoint) 203 wdd=wm.rm_watch(ide,rec=True) 204 self.main.init_folders_info(item,tmp_mountpoint) 205 wdd=wm.add_watch(tmp_mountpoint,mask,rec=True) 206 else: 207 self.main.remove_folder_info(item) 208 ide=wm.get_wd(tmp_mountpoint) 209 wdd=wm.rm_watch(ide,rec=True) 210 211 else: 212 time.sleep(2) 213 t=join(event.path,event.name) 214 print t 215 if isfile(t): 216 try: 217 218 estado=os.stat(t) 219 except Exception as e: 220 print str(e) 221 t=t.split('trashinfo') 222 t=t[0]+'trashinfo' 223 estado=os.stat(t) 224 225 226 acceso=datetime.datetime.fromtimestamp(estado.st_mtime) 227 self.main.local_changes[t]=acceso 228 229 162 230 notifier=Notifier(wm,Process_handler(self)) 163 231 wdd=wm.add_watch(LliurexGdriveIndicator.WATCH_DIR,mask,rec=True) 164 232 233 for profile in self.profiles_info: 234 path=os.path.expanduser(self.profiles_info[profile]["mountpoint"]) 235 wdd=wm.add_watch(path,mask,rec=True) 236 237 165 238 while True: 166 239 try: 167 240 168 241 notifier.process_events() 169 242 if notifier.check_events(): 170 notifier.read_events()171 243 notifier.read_events() 244 172 245 except Exception as e: 246 print str(e) 173 247 notifier.stop() 174 248 175 249 return False 176 250 … … 249 323 item.status=False 250 324 self.menu.insert(item,4) 325 self.init_folders_info(profile,mountpoint) 251 326 252 327 #def add_menu_item … … 298 373 #def populate_menu 299 374 300 375 def init_folders_info(self,profile,mountpoint): 376 377 self.folders_info={} 378 self.folders_info['profile']=profile 379 self.files=set() 380 self.controlDirectory(mountpoint) 381 self.folders_info['files']=self.files 382 self.init_files.append(self.folders_info) 383 384 #def init_folders_info 385 386 387 def controlDirectory(self,path): 388 389 390 for item in listdir(path): 391 if item not in self.blacklist: 392 393 t=join(path,item) 394 395 if isfile(t): 396 397 compartido=False 398 estado=os.stat(t) 399 acceso=datetime.datetime.fromtimestamp(estado.st_mtime) 400 last_update=acceso.strftime(formato) 401 tmp=t+":__"+str(acceso) 402 403 self.files.add(tmp) 404 else: 405 if isdir(t): 406 self.controlDirectory(t) 407 408 #def controlDirectory 409 410 def remove_folder_info(self,profile): 411 412 i=0 413 for item in self.init_files: 414 print item 415 if profile ==item["profile"]: 416 print "borrado OK" 417 self.init_files.pop(i) 418 i=i+1 419 420 #def remove_folder_info 421 422 301 423 def item_clicked(self,widget,profile): 302 424 … … 487 609 for profile in self.profiles_info: 488 610 if self.connection: 611 cmd="google-drive-ocamlfuse -cc -label %s"%profile 612 os.system(cmd) 489 613 mountpoint=self.profiles_info[profile]["mountpoint"] 490 614 status_info=self.LliurexGoogleDriveManager.check_mountpoint_status(mountpoint) 615 self.update_folders_info(profile,mountpoint) 616 491 617 else: 492 618 status_info={} … … 507 633 508 634 #self.result_connect_threads.pop(id) 635 self.notify_changes() 509 636 return False 510 637 … … 516 643 517 644 #def check_connect 645 646 647 def update_folders_info(self,profile,mountpoint): 648 649 self.folders_info={} 650 self.folders_info['profile']=profile 651 self.files=set() 652 self.controlDirectory(mountpoint) 653 self.folders_info['files']=self.files 654 self.update_files.append(self.folders_info) 655 656 #def update_folders_info 657 658 def notify_changes(self): 659 660 661 if self.init_files!=self.update_files: 662 toInfo=self.remove_local_changes() 663 664 for item in toInfo: 665 numFiles_up=0 666 numFiles_rm=0 667 numFiles_add=0 668 p=item['profile'] 669 if 'files_changes' in item: 670 numFiles_up=len(item['files_changes']) 671 if 'files_delete' in item: 672 numFiles_rm=len(item['files_delete']) 673 if 'files_add' in item: 674 numFiles_add=len(item['files_add']) 675 if numFiles_up>0 or numFiles_rm>0 or numFiles_add>0: 676 message1=_(': changes in last 15 minutes:\n') 677 message_up="" 678 message_rm="" 679 message_add="" 680 if numFiles_up>0: 681 message_up=_("-Files updated: ") 682 if numFiles_up==1: 683 for element in item['files_changes']: 684 file=os.path.basename(element.split(":__")[0]) 685 file_extension=os.path.splitex(file) 686 if file_extension[1]==".desktop": 687 file=file_extension[0] 688 689 message_up=message_up + file +'\n' 690 691 else: 692 message_up=message_up+str(numFiles_up) + '\n' 693 694 if numFiles_add>0: 695 message_add=_("-Files added: ") 696 if numFiles_add==1: 697 for element in item['files_add']: 698 file=os.path.basename(element.split(":__")[0]) 699 file_extension=os.path.splitex(file) 700 if file_extension[1]==".desktop": 701 file=file_extension[0] 702 message_add=message_add + file +'\n' 703 else: 704 message_add=message_add+str(numFiles_add) + '\n' 705 706 if numFiles_rm>0: 707 message_rm=_("-Files deleted: ") 708 if numFiles_rm==1: 709 for element in item['files_delete']: 710 file=os.path.basename(element.split(":__")[0]) 711 file_extension=os.path.splitex(file) 712 if file_extension[1]==".desktop": 713 file=file_extension[0] 714 message_rm= message_rm + file +'\n' 715 else: 716 message_rm= message_rm+str(numFiles_rm) 717 print message_rm 718 719 self.show_message(p + message1 + message_add + message_up + message_rm) 720 721 self.init_files=copy.copy(self.update_files) 722 self.local_changes={} 723 724 def remove_local_changes(self): 725 726 changes=self.detect_changes() 727 changes_toNotify=[] 728 729 if len(changes)>0: 730 for item in changes: 731 tmp_changes=[] 732 tmp_deletes=[] 733 tmp_add=[] 734 to_info={} 735 to_info['profile']=item['profile'] 736 737 if 'files_changes' in item: 738 tmp_changes=item['files_changes'] 739 toInfo_up=tmp_changes.copy() 740 if 'files_deletes' in item: 741 tmp_deletes=item['files_deletes'] 742 toInfo_rm=tmp_deletes.copy() 743 if 'files_add' in item: 744 tmp_add=item['files_add'] 745 toInfo_add=tmp_add.copy() 746 747 if len(self.local_changes)>0: 748 if len(tmp_changes)>0: 749 for item in tmp_changes: 750 #file=item.keys()[0] 751 file=item.split(":__")[0] 752 data=item.split(":__")[1] 753 for i in self.local_changes: 754 if file==i: 755 data=datetime.datetime.strptime(data,'%Y-%m-%d %H:%M:%S') 756 if data<=self.local_changes[i]: 757 toInfo_up.remove(item) 758 to_info['files_changes']=toInfo_up 759 if len(tmp_deletes)>0: 760 for item in tmp_deletes: 761 file=os.path.basename(item.split(":__")[0]) 762 file=os.path.splitext(file)[0] 763 data=item.split(":__")[1] 764 for i in self.local_changes: 765 print i 766 if file in i: 767 data=datetime.datetime.strptime(data,'%Y-%m-%d %H:%M:%S') 768 if data<=self.local_changes[i]: 769 toInfo_rm.remove(item) 770 to_info['files_delete']=toInfo_rm 771 772 if len(tmp_add)>0: 773 for item in tmp_add: 774 file=item.split(":__")[0] 775 data=item.split(":__")[1] 776 for i in self.local_changes: 777 if file==i: 778 data=datetime.datetime.strptime(data,'%Y-%m-%d %H:%M:%S') 779 if data<=self.local_changes[i]: 780 toInfo_add.remove(item) 781 to_info['files_add']=toInfo_add 782 else: 783 to_info['files_changes']=tmp_changes 784 to_info['files_delete']=tmp_deletes 785 to_info['files_add']=tmp_add 786 787 changes_toNotify.append(to_info) 788 789 return changes_toNotify 790 791 #remove_local_changes 792 793 def detect_changes(self): 794 795 changes=[] 796 797 798 for item in self.update_files: 799 info_changes={} 800 info_changes['profile']=item['profile'] 801 final_files=item['files'] 802 803 for item in self.init_files: 804 profile_init=item['profile'] 805 original_files=item['files'] 806 if len(original_files)>0: 807 if len(final_files)>0: 808 if info_changes['profile']==profile_init: 809 diff_changes=final_files.difference(original_files) 810 diff_deletes=original_files.difference(final_files) 811 if len(diff_changes)>0: 812 changes.append(info_changes) 813 if len(diff_deletes)>0: 814 tmp_del=diff_deletes.copy() 815 tmp_add=diff_changes.copy() 816 for item in diff_deletes: 817 file_d=item.split(":__")[0] 818 for element in diff_changes: 819 file_c=element.split(":__")[0] 820 if file_d == file_c: 821 tmp_del.remove(item) 822 tmp_add.remove(element) 823 if len(tmp_del)>0: 824 info_changes['files_deletes']=tmp_del 825 826 if len(tmp_add)>0: 827 info_changes['files_add']=tmp_add 828 tmp_up=diff_changes.difference(tmp_add) 829 info_changes['files_changes']=tmp_up 830 else: 831 info_changes['files_changes']=diff_changes 832 833 else: 834 info_changes['files_add']=diff_changes 835 else: 836 if len(original_files)>len(final_files) and len(diff_deletes)>0: 837 info_changes['files_deletes']=diff_deletes 838 changes.append(info_changes) 839 840 return changes 841 842 #def detect_changes 518 843 519 844
Note: See TracChangeset
for help on using the changeset viewer.