1 | import re |
---|
2 | import fnmatch |
---|
3 | import os |
---|
4 | import commands |
---|
5 | import subprocess |
---|
6 | import shutil |
---|
7 | |
---|
8 | import xmlrpclib |
---|
9 | |
---|
10 | import locale |
---|
11 | import gettext |
---|
12 | |
---|
13 | locale.textdomain("xdg-user-dirs") |
---|
14 | gettext.textdomain("xdg-user-dirs") |
---|
15 | |
---|
16 | |
---|
17 | class MovingProfiles: |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | def __init__(self,login,port=9779): |
---|
22 | |
---|
23 | self.ignore={".config":["/user-dirs.*","/google-chrome/Singleton*"],".mozilla":["/firefox/*.default/storage","/firefox/*.default/lock"],".local":["/share/teamviewer*"]} |
---|
24 | |
---|
25 | self.login=login |
---|
26 | server_port=str(port) |
---|
27 | |
---|
28 | server_name="server" |
---|
29 | |
---|
30 | self.connection_string = "https://"+server_name+":"+server_port |
---|
31 | proxy = xmlrpclib.ServerProxy(self.connection_string) |
---|
32 | |
---|
33 | #perform a cached read |
---|
34 | self.cfg = proxy.get_list(login,"MovingProfiles") |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | def isInclude(self,filename): |
---|
39 | |
---|
40 | reti=[] |
---|
41 | for name in self.cfg["include"]: |
---|
42 | value=self.cfg["include"][name] |
---|
43 | if fnmatch.fnmatchcase(filename,value): |
---|
44 | reti.append(name) |
---|
45 | |
---|
46 | |
---|
47 | if len(reti)==0: |
---|
48 | return None |
---|
49 | else: |
---|
50 | return reti |
---|
51 | |
---|
52 | |
---|
53 | def isExclude(self,filename): |
---|
54 | |
---|
55 | rete=[] |
---|
56 | for name in self.cfg["exclude"]: |
---|
57 | value=self.cfg["exclude"][name] |
---|
58 | if fnmatch.fnmatchcase(filename,value): |
---|
59 | rete.append(name) |
---|
60 | |
---|
61 | if len(rete)==0: |
---|
62 | return None |
---|
63 | else: |
---|
64 | return rete |
---|
65 | |
---|
66 | |
---|
67 | def Match(self,fname): |
---|
68 | |
---|
69 | include=False |
---|
70 | |
---|
71 | |
---|
72 | for name in self.cfg["include"]: |
---|
73 | value=self.cfg["include"][name] |
---|
74 | if fnmatch.fnmatchcase(fname,value): |
---|
75 | include=True |
---|
76 | |
---|
77 | break |
---|
78 | |
---|
79 | if include: |
---|
80 | for name in self.cfg["exclude"]: |
---|
81 | value=self.cfg["exclude"][name] |
---|
82 | if fnmatch.fnmatchcase(fname,value): |
---|
83 | |
---|
84 | return False |
---|
85 | |
---|
86 | return True |
---|
87 | |
---|
88 | else: |
---|
89 | return False |
---|
90 | |
---|
91 | |
---|
92 | def MatchFolder(self,path): |
---|
93 | |
---|
94 | mlist=[] |
---|
95 | |
---|
96 | for fname in os.listdir(path): |
---|
97 | if(self.Match(fname)): |
---|
98 | mlist.append(fname) |
---|
99 | |
---|
100 | return mlist |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | def Clear(self): |
---|
106 | self.cfg={"include":{},"exclude":{}} |
---|
107 | |
---|
108 | |
---|
109 | def Save(self): |
---|
110 | proxy = xmlrpclib.ServerProxy(self.connection_string) |
---|
111 | proxy.save_conf(self.login,"MovingProfiles",self.cfg) |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | def GetProfilePath(self): |
---|
116 | |
---|
117 | try: |
---|
118 | |
---|
119 | home=os.path.expanduser("~") |
---|
120 | execfile(home+"/.config/user-dirs.dirs") |
---|
121 | documents_dir=locals()["XDG_DOCUMENTS_DIR"].split("/")[1] |
---|
122 | moving_profiles="%s/%s/.moving_profiles/"%(home,documents_dir) |
---|
123 | |
---|
124 | except Exception as e: |
---|
125 | |
---|
126 | try: |
---|
127 | user=os.getenv("USER") |
---|
128 | documents=gettext.gettext("Documents") |
---|
129 | moving_profiles = "/home/%s/%s/.moving_profiles/"%(user,documents) |
---|
130 | |
---|
131 | except: |
---|
132 | print ("[GetProfilePath] Failed constructing home path") |
---|
133 | raise |
---|
134 | |
---|
135 | |
---|
136 | return moving_profiles |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | def LoadSession(self): |
---|
141 | moving_path=self.GetProfilePath() |
---|
142 | home=os.path.expanduser("~") |
---|
143 | |
---|
144 | if not os.path.exists(moving_path): |
---|
145 | raise Exception("Profile dir not found, aborting load") |
---|
146 | |
---|
147 | |
---|
148 | print("[LoadSession] Synchronization") |
---|
149 | print("[LoadSession] Stage 1") |
---|
150 | |
---|
151 | |
---|
152 | cmd=["rsync","-aAX","--delete","--ignore-errors"] |
---|
153 | |
---|
154 | profile_files=self.MatchFolder(moving_path) |
---|
155 | |
---|
156 | for fname in profile_files: |
---|
157 | print("[rsync] "+fname) |
---|
158 | |
---|
159 | ecmd=[] |
---|
160 | if(fname in self.ignore): |
---|
161 | elist=self.ignore[fname] |
---|
162 | for erule in elist: |
---|
163 | ecmd.append("--exclude=%s"%erule) |
---|
164 | print("* excluding: "+erule) |
---|
165 | |
---|
166 | |
---|
167 | source = moving_path+"/"+fname |
---|
168 | destination = home+"/"+fname |
---|
169 | |
---|
170 | if(os.path.isdir(source)): |
---|
171 | source=source+"/" |
---|
172 | destination=destination+"/" |
---|
173 | |
---|
174 | rcmd = cmd + ecmd + [source,destination] |
---|
175 | plain_cmd=" ".join(rcmd) |
---|
176 | plain_cmd+=" || true" |
---|
177 | |
---|
178 | subprocess.call(plain_cmd,shell=True) |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | print("[LoadSession] Stage 2") |
---|
183 | |
---|
184 | home_files = self.MatchFolder(home) |
---|
185 | |
---|
186 | for hname in home_files: |
---|
187 | if not hname in profile_files: |
---|
188 | hpath=home+"/"+hname |
---|
189 | |
---|
190 | print("[rm] "+hname) |
---|
191 | |
---|
192 | rm=os.remove |
---|
193 | |
---|
194 | if(os.path.isdir(hpath)): |
---|
195 | rm=shutil.rmtree |
---|
196 | |
---|
197 | rm(hpath) |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | def SaveSession(self): |
---|
204 | moving_path=self.GetProfilePath() |
---|
205 | print("[SaveSession] moving path: "+moving_path) |
---|
206 | |
---|
207 | if not os.path.exists(moving_path): |
---|
208 | print("[SaveSession] Creating profile path") |
---|
209 | try: |
---|
210 | os.makedirs(moving_path) |
---|
211 | except: |
---|
212 | raise |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | cmd=["rsync","-aAX","--delete","--ignore-errors"] |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | print("rsync cmd:") |
---|
221 | for c in cmd: |
---|
222 | print(c) |
---|
223 | |
---|
224 | home=os.path.expanduser("~") |
---|
225 | print("[SaveSession] Synchronization") |
---|
226 | print("[SaveSession] Stage 1") |
---|
227 | |
---|
228 | home_files = self.MatchFolder(home) |
---|
229 | for fname in home_files: |
---|
230 | fpath=home+"/"+fname |
---|
231 | |
---|
232 | if(os.path.isdir(fpath)): |
---|
233 | fpath=fpath+"/" |
---|
234 | |
---|
235 | print("[rsync] "+fname) |
---|
236 | |
---|
237 | ecmd = [] |
---|
238 | |
---|
239 | if(fname in self.ignore): |
---|
240 | elist = self.ignore[fname] |
---|
241 | for erule in elist: |
---|
242 | ecmd.append("--exclude=%s"%erule) |
---|
243 | print("* excluding: "+erule) |
---|
244 | |
---|
245 | |
---|
246 | source = home +"/" + fname |
---|
247 | destination = moving_path+"/"+fname |
---|
248 | |
---|
249 | if(os.path.isdir(source)): |
---|
250 | source=source+"/" |
---|
251 | destination=destination+"/" |
---|
252 | |
---|
253 | |
---|
254 | rcmd = cmd + ecmd + [source,destination] |
---|
255 | subprocess.call(rcmd) |
---|
256 | |
---|
257 | |
---|
258 | |
---|
259 | print("[SaveSession] Stage 2") |
---|
260 | |
---|
261 | for fname in os.listdir(moving_path): |
---|
262 | if not fname in home_files: |
---|
263 | fpath=moving_path+"/"+fname |
---|
264 | |
---|
265 | print("[rm] "+fname) |
---|
266 | |
---|
267 | rm=os.remove |
---|
268 | |
---|
269 | if(os.path.isdir(fpath)): |
---|
270 | rm=shutil.rmtree |
---|
271 | |
---|
272 | rm(fpath) |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | def Backup(self,name): |
---|
277 | backup_path=self.GetProfilePath()+"../.moving_profiles_backup/"+name |
---|
278 | |
---|
279 | print("[Backup] backup path: "+backup_path) |
---|
280 | |
---|
281 | if(os.path.exists(backup_path)): |
---|
282 | print("[Backup] "+name+" already exists") |
---|
283 | return |
---|
284 | else: |
---|
285 | try: |
---|
286 | os.makedirs(backup_path) |
---|
287 | except: |
---|
288 | raise |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | cmd=["rsync","-az","--delete"] |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | home=os.path.expanduser("~") |
---|
297 | |
---|
298 | print("[Backup] Backing up as "+name) |
---|
299 | |
---|
300 | |
---|
301 | home_files = self.MatchFolder(home) |
---|
302 | for fname in home_files: |
---|
303 | fpath=home+"/"+fname |
---|
304 | |
---|
305 | if(os.path.isdir(fpath)): |
---|
306 | fpath=fpath+"/" |
---|
307 | |
---|
308 | print("[rsync] "+fname) |
---|
309 | |
---|
310 | rcmd = cmd + [fpath, backup_path+"/"+fname ] |
---|
311 | subprocess.call(rcmd) |
---|
312 | |
---|