import re import fnmatch import os import commands import subprocess import shutil import xmlrpclib import locale import gettext locale.textdomain("xdg-user-dirs") gettext.textdomain("xdg-user-dirs") class MovingProfiles: def __init__(self,login): #self.cfg=ConfigParser.SafeConfigParser() #self.cfg.read([MovingProfiles.config_path]) self.login=login server_port="9779" proxy=xmlrpclib.ServerProxy("https://localhost:9779") server_name=proxy.get_variable("","VariablesManager","SRV_IP") print server_name if server_name==None: print "using localhost" server_name="localhost" self.connection_string = "https://"+server_name+":"+server_port proxy = xmlrpclib.ServerProxy(self.connection_string) self.cfg = proxy.load_conf(login,"MovingProfiles") def isInclude(self,filename): reti=[] for name in self.cfg["include"]: value=self.cfg["include"][name] if fnmatch.fnmatchcase(filename,value): reti.append(name) if len(reti)==0: return None else: return reti def isExclude(self,filename): rete=[] for name in self.cfg["exclude"]: value=self.cfg["exclude"][name] if fnmatch.fnmatchcase(filename,value): rete.append(name) if len(rete)==0: return None else: return rete def Match(self,fname): include=False for name in self.cfg["include"]: value=self.cfg["include"][name] if fnmatch.fnmatchcase(fname,value): include=True break if include: for name in self.cfg["exclude"]: value=self.cfg["exclude"][name] if fnmatch.fnmatchcase(fname,value): return False return True else: return False def Clear(self): self.cfg={"include":{},"exclude":{}} def Save(self): proxy = xmlrpclib.ServerProxy(self.connection_string) proxy.save_conf(self.login,"MovingProfiles",self.cfg) def GetProfilePath(self): try: user=os.getenv("USER") documents=gettext.gettext("Documents") moving_profiles = "/home/%s/%s/.moving_profiles/"%(user,documents) except: print ("[GetProfilePath] Failed constructing home path") raise return moving_profiles def LoadSession(self): moving_path=self.GetProfilePath() if not os.path.exists(moving_path): raise Exception("Profile dir not found, aborting load") print("[LoadSession] Synchronization") print("[LoadSession] Stage 1") home=os.path.expanduser("~") profile_files=os.listdir(moving_path) for fname in os.listdir(home): if self.Match(fname): if fname not in profile_files: print("[rm] ",fname) if os.path.isdir(home+os.path.sep+fname): shutil.rmtree(home+os.path.sep+fname) if os.path.isfile(home+os.path.sep+fname): os.remove(home+os.path.sep+fname) print("[LoadSession] Stage 2") profile_files==os.listdir(moving_path) for fname in profile_files: if self.Match(fname): print("[rsync] ",home+os.path.sep+fname) subprocess.call(["rsync","-az","--delete",moving_path+os.path.sep+fname,home+os.path.sep,'--exclude=".config/user-dirs.*"']) #mr package mantainer. FIX THIS. FOR THE LOVE OF GOD #subprocess.call(["rsync", "-rlptD",moving_path,home,'--exclude=".config/user-dirs.*"']) #cmd="rsync -rtuz --delete " + moving_path + " " + home + ' --exclude=".config/user-dirs.*"' #os.system(cmd) def SaveSession(self): moving_path=self.GetProfilePath() print("[SaveSession] moving path: ",moving_path) if not os.path.exists(moving_path): print("[SaveSession] Creating profile path") try: os.makedirs(moving_path) except: raise home=os.path.expanduser("~") print("[SaveSession] Synchronization") print("[SaveSession] Stage 1") for fname in os.listdir(home): if self.Match(fname): if os.path.isdir(home+os.path.sep+fname): print("[rsync] dir ",fname) subprocess.call(["rsync","-az","--delete",home+os.path.sep+fname+os.path.sep,moving_path+os.path.sep+fname,'--exclude=".config/user-dirs.*"']) if os.path.isfile(home+os.path.sep+fname): print("[rsync] file ",fname) subprocess.call(["rsync","-az","--delete",home+os.path.sep+fname,moving_path+os.path.sep+fname,'--exclude=".config/user-dirs.*"']) print("[SaveSession] Stage 2") home_files=os.listdir(home) for fname in os.listdir(moving_path): if not fname in home_files: if os.path.isdir(moving_path+os.path.sep+fname): print("[rm] "+fname) shutil.rmtree(moving_path+os.path.sep+fname) if os.path.isfile(moving_path+os.path.sep+fname): print("[rm] ",fname) os.remove(moving_path+os.path.sep+fname)