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