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 | |
---|
11 | |
---|
12 | DEBUG=True |
---|
13 | GDRIVE_CONFIG_DIR=os.path.expanduser("~/.gdfuse/") |
---|
14 | LLIUREX_CONFIG_FILE='/usr/share/lliurex-gdrive/llx-data/config' |
---|
15 | |
---|
16 | |
---|
17 | class LliurexGoogleDriveManager(object): |
---|
18 | |
---|
19 | def __init__(self): |
---|
20 | |
---|
21 | super(LliurexGoogleDriveManager, self).__init__() |
---|
22 | |
---|
23 | self.config_dir=os.path.expanduser("~/.config/lliurex-google-drive-profiles/") |
---|
24 | self.config_file=self.config_dir+"configProfiles" |
---|
25 | |
---|
26 | self.mount_cmd="google-drive-ocamlfuse -label %s %s" |
---|
27 | |
---|
28 | self.read_conf() |
---|
29 | |
---|
30 | |
---|
31 | #def init |
---|
32 | |
---|
33 | def create_conf(self): |
---|
34 | |
---|
35 | if not os.path.exists(self.config_dir): |
---|
36 | os.makedirs(self.config_dir) |
---|
37 | |
---|
38 | var={} |
---|
39 | # var["default"]={} |
---|
40 | # var["default"]["email"]="" |
---|
41 | # var["default"]["mountpoint"]="" |
---|
42 | # var["default"]["automount"]="" |
---|
43 | |
---|
44 | f=open(self.config_file,"w") |
---|
45 | data=unicode(json.dumps(var,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
46 | |
---|
47 | f.write(data) |
---|
48 | f.close() |
---|
49 | |
---|
50 | log_msg="------------------------------------------\n"+"LLIUREX-GDRIVE STARTING AT: " + datetime.datetime.today().strftime("%d/%m/%y %H:%M:%S") +"\n------------------------------------------" |
---|
51 | self.log(log_msg) |
---|
52 | msg_log="Creating conf..." |
---|
53 | self.dprint(msg_log) |
---|
54 | self.log(msg_log) |
---|
55 | |
---|
56 | #def create_conf |
---|
57 | |
---|
58 | def read_conf(self): |
---|
59 | |
---|
60 | if not os.path.exists(self.config_file): |
---|
61 | self.create_conf() |
---|
62 | |
---|
63 | msg_log="Init: Reading conf..." |
---|
64 | self.dprint(msg_log) |
---|
65 | self.log(msg_log) |
---|
66 | |
---|
67 | f=open(self.config_file) |
---|
68 | try: |
---|
69 | self.profiles_config=json.load(f) |
---|
70 | except: |
---|
71 | self.profiles_config={} |
---|
72 | f.close() |
---|
73 | |
---|
74 | #def read_conf |
---|
75 | |
---|
76 | def check_config(self,profile): |
---|
77 | |
---|
78 | path=GDRIVE_CONFIG_DIR+profile+"/state" |
---|
79 | |
---|
80 | if os.path.exists(path): |
---|
81 | f=open(path) |
---|
82 | line=f.readline() |
---|
83 | f.close() |
---|
84 | |
---|
85 | if "1970-" in line: |
---|
86 | msg_log="Check config: '%s' not configured"%profile |
---|
87 | self.dprint(msg_log) |
---|
88 | #self.log(msg_log) |
---|
89 | |
---|
90 | return False |
---|
91 | msg_log="Check config: '%s' yet configured"%profile |
---|
92 | self.dprint(msg_log) |
---|
93 | return True |
---|
94 | |
---|
95 | else: |
---|
96 | msg_log="Check config: '%s' not yet create"%profile |
---|
97 | self.dprint(msg_log) |
---|
98 | #self.log(msg_log) |
---|
99 | |
---|
100 | return False |
---|
101 | |
---|
102 | |
---|
103 | #def check_config |
---|
104 | |
---|
105 | def check_google_connection(self): |
---|
106 | |
---|
107 | try: |
---|
108 | req=urllib2.Request("http://google.com") |
---|
109 | res=urllib2.urlopen(req) |
---|
110 | return True |
---|
111 | except: |
---|
112 | msg_log="Check connection: Cannot connect to google.com" |
---|
113 | self.dprint(msg_log) |
---|
114 | self.log(msg_log) |
---|
115 | return False |
---|
116 | |
---|
117 | #def check_google_connections |
---|
118 | |
---|
119 | def mount_drive(self,profile,mountpoint): |
---|
120 | |
---|
121 | #profile=profile.encode("utf-8") |
---|
122 | #mountpoint=mountpoint.encode("utf-8") |
---|
123 | |
---|
124 | try: |
---|
125 | |
---|
126 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
127 | check= self.check_config(profile) |
---|
128 | if check: |
---|
129 | #if profile in self.profiles_config: |
---|
130 | #mount_point=os.path.expanduser(self.profiles_config[profile]["mountpoint"]) |
---|
131 | #if type(mount_point)==type(u""): |
---|
132 | if mountpoint!="": |
---|
133 | self.dprint("Mounting '%s'..."%profile) |
---|
134 | if not os.path.exists(mountpoint): |
---|
135 | try: |
---|
136 | os.makedirs(mountpoint) |
---|
137 | except: |
---|
138 | msg_log="Mount drive: Unable to create '%s' mount destination"%mountpoint |
---|
139 | self.dprint(msg_log) |
---|
140 | #self.log(msg_log) |
---|
141 | return False |
---|
142 | |
---|
143 | |
---|
144 | if os.access(mountpoint,os.W_OK): |
---|
145 | #os.system(self.mount_cmd%(profile,mountpoint)) |
---|
146 | cmd=self.mount_cmd%(profile,mountpoint) |
---|
147 | p=subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
148 | poutput,perror=p.communicate() |
---|
149 | if len(perror)==0: |
---|
150 | msg_log="Mount drive: drive '%s' mounted sucessfully:"%mountpoint |
---|
151 | self.dprint(msg_log) |
---|
152 | #self.log(msg_log) |
---|
153 | return True |
---|
154 | |
---|
155 | else: |
---|
156 | msg_log="Mount drive: Error mount '%s': %s"%(mountpoint,str(perror)) |
---|
157 | self.dprint(msg_log) |
---|
158 | #self.log(msg_log) |
---|
159 | |
---|
160 | else: |
---|
161 | msg_log="Mount drive: Mount drive: '%s' mount destination is not owned by user"%mountpoint |
---|
162 | self.dprint(msg_log) |
---|
163 | #self.log(msg_log) |
---|
164 | |
---|
165 | else: |
---|
166 | msg_log="Mount drive: '%s' mountpoint not configured"%profile |
---|
167 | self.dprint(msg_log) |
---|
168 | #self.log(msg_log) |
---|
169 | else: |
---|
170 | msg_log="Mount drive: '%s' profile unknown"%profile |
---|
171 | self.dprint(msg_log) |
---|
172 | #self.log(msg_log) |
---|
173 | else: |
---|
174 | msg_log="Mount drive: '%s' GDrive profile path does not exist"%profile |
---|
175 | self.dprint(msg_log) |
---|
176 | #self.log(msg_log) |
---|
177 | |
---|
178 | except Exception as e: |
---|
179 | |
---|
180 | raise e |
---|
181 | |
---|
182 | |
---|
183 | return False |
---|
184 | |
---|
185 | #def mount_drive |
---|
186 | |
---|
187 | def mount_drives(self): |
---|
188 | |
---|
189 | for profile in self.profiles_config: |
---|
190 | automount=self.profiles_config[profile]["automount"] |
---|
191 | if automount: |
---|
192 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
193 | profile=profile.encode("utf-8") |
---|
194 | mountpoint=mountpoint.encode("utf-8") |
---|
195 | self.mount_drive(profile,mountpoint) |
---|
196 | |
---|
197 | |
---|
198 | #def mount_drives |
---|
199 | |
---|
200 | def dprint(self,msg): |
---|
201 | |
---|
202 | if DEBUG: |
---|
203 | print("[LGDM] %s"%msg) |
---|
204 | |
---|
205 | #def dprint |
---|
206 | |
---|
207 | |
---|
208 | def save_profiles(self,info): |
---|
209 | |
---|
210 | self.profiles_config=info |
---|
211 | |
---|
212 | f=open(self.config_file,"w") |
---|
213 | #data=json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False) |
---|
214 | data=unicode(json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
215 | f.write(data) |
---|
216 | f.close() |
---|
217 | |
---|
218 | #def save_profiles |
---|
219 | |
---|
220 | def check_mountpoint_status(self,mountpoint): |
---|
221 | |
---|
222 | mountpoint_size="" |
---|
223 | mountpoint_used="" |
---|
224 | mountpoint_available="" |
---|
225 | mountpoint_per="" |
---|
226 | |
---|
227 | |
---|
228 | command='df -h | grep "google-drive-ocamlfuse" | grep ' +mountpoint+'$' |
---|
229 | |
---|
230 | p=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
231 | poutput,perror=p.communicate() |
---|
232 | |
---|
233 | if len(poutput)>0: |
---|
234 | status=True |
---|
235 | mountpoint_size=poutput.split(" ")[4] |
---|
236 | mountpoint_used=poutput.split(" ")[8] |
---|
237 | mountpoint_available=poutput.split(" ")[11] |
---|
238 | mountpoint_per=poutput.split(" ")[14] |
---|
239 | |
---|
240 | else: |
---|
241 | status=False |
---|
242 | |
---|
243 | return {"status":status,"size":mountpoint_size,"used":mountpoint_used,"available":mountpoint_available,"used%":mountpoint_per} |
---|
244 | |
---|
245 | #def check_mountpoint_status |
---|
246 | |
---|
247 | def check_profile_info(self,profile,mountpoint,edition): |
---|
248 | |
---|
249 | ''' |
---|
250 | code=0: Form OK |
---|
251 | code=1: Profile empty |
---|
252 | code=2: Profile with blanck spaces |
---|
253 | code=3: Profile duplicate |
---|
254 | code=4: Mountpoint duplicate |
---|
255 | |
---|
256 | ''' |
---|
257 | #profile=profile.decode("utf-8") |
---|
258 | if not edition: |
---|
259 | |
---|
260 | |
---|
261 | # type(unicode) ==> encode("utf-8") |
---|
262 | # type(str) ==> decode("utf-8") |
---|
263 | |
---|
264 | |
---|
265 | if profile=="": |
---|
266 | return {"result":False,"code":1} |
---|
267 | |
---|
268 | if ' ' in profile: |
---|
269 | return {"result":False,"code":2} |
---|
270 | |
---|
271 | else: |
---|
272 | for item in self.profiles_config: |
---|
273 | if profile==item: |
---|
274 | return {"result":False,"code":3} |
---|
275 | |
---|
276 | for item in self.profiles_config: |
---|
277 | if profile!=item: |
---|
278 | if mountpoint==self.profiles_config[item]["mountpoint"]: |
---|
279 | return {"result":False,"code":4} |
---|
280 | |
---|
281 | return {"result":True,"code":0} |
---|
282 | |
---|
283 | #def check_profile_info |
---|
284 | |
---|
285 | def create_profile(self,profile): |
---|
286 | |
---|
287 | print profile,type(profile) |
---|
288 | |
---|
289 | profile=unicode(profile).encode("utf-8") |
---|
290 | path=GDRIVE_CONFIG_DIR+profile+"/config" |
---|
291 | |
---|
292 | if not self.check_config(profile): |
---|
293 | os.system("google-drive-ocamlfuse -label %s"%profile) |
---|
294 | msg_log=("'%s' profile has been create")%profile |
---|
295 | #self.log(msg_log) |
---|
296 | self.dprint(msg_log) |
---|
297 | |
---|
298 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
299 | shutil.copy(LLIUREX_CONFIG_FILE,path ) |
---|
300 | |
---|
301 | return True |
---|
302 | |
---|
303 | |
---|
304 | #def create_profile |
---|
305 | |
---|
306 | def create_mountpoint(self,info,profile): |
---|
307 | |
---|
308 | mountpoint=info[profile]["mountpoint"] |
---|
309 | |
---|
310 | result=self.mount_drive(profile,mountpoint) |
---|
311 | |
---|
312 | if result: |
---|
313 | self.save_profiles(info) |
---|
314 | else: |
---|
315 | if profile !="": |
---|
316 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
317 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
318 | self.dprint("'%s' profile has been delete"%profile) |
---|
319 | |
---|
320 | return result |
---|
321 | |
---|
322 | #def create_mountpoint |
---|
323 | |
---|
324 | def dismount_mountpoint(self,mountpoint): |
---|
325 | |
---|
326 | cmd='fusermount -u ' + mountpoint |
---|
327 | p=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
328 | poutput,perror=p.communicate() |
---|
329 | |
---|
330 | if len(perror)>0: |
---|
331 | msg_log="Dismount mountpoint: Error dismounted '%s': '%s'"%(mountpoint,str(perror)) |
---|
332 | #self.log(msg_log) |
---|
333 | self.dprint(msg_log) |
---|
334 | result=False |
---|
335 | else: |
---|
336 | msg_log="Dismount mountpoint:'%s' mountpoint has been dismounted"%mountpoint |
---|
337 | #self.log(msg_log) |
---|
338 | self.dprint(msg_log) |
---|
339 | result=True |
---|
340 | |
---|
341 | return result |
---|
342 | |
---|
343 | #def dismount_mountpoint |
---|
344 | |
---|
345 | def delete_profile(self,info,profile): |
---|
346 | |
---|
347 | result=True |
---|
348 | dismount=True |
---|
349 | |
---|
350 | #profile=str(profile) |
---|
351 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
352 | print mountpoint |
---|
353 | |
---|
354 | |
---|
355 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
356 | is_mountpoint_mounted=self.check_mountpoint_status(mountpoint) |
---|
357 | |
---|
358 | if is_mountpoint_mounted["status"]: |
---|
359 | dismount=self.dismount_mountpoint(mountpoint) |
---|
360 | |
---|
361 | if dismount: |
---|
362 | if profile!="": |
---|
363 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
364 | msg_log="Delete profile: '%s' profile has been delete"%profile |
---|
365 | #self.log(msg_log) |
---|
366 | self.dprint(msg_log) |
---|
367 | self.save_profiles(info) |
---|
368 | return dismount |
---|
369 | |
---|
370 | else: |
---|
371 | self.save_profiles(info) |
---|
372 | msg_log="Delete profile: '%s' GDrive profile path does not exist"%profile |
---|
373 | #self.log(msg_log) |
---|
374 | self.dprint(msg_log) |
---|
375 | |
---|
376 | return True |
---|
377 | |
---|
378 | |
---|
379 | # delete_profile |
---|
380 | |
---|
381 | |
---|
382 | def edit_profile(self,info,profile): |
---|
383 | |
---|
384 | result=True |
---|
385 | old_mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
386 | old_automount=self.profiles_config[profile]["automount"] |
---|
387 | new_mountpoint=info[profile]["mountpoint"] |
---|
388 | new_automount=info[profile]["automount"] |
---|
389 | |
---|
390 | print old_mountpoint |
---|
391 | print new_mountpoint |
---|
392 | |
---|
393 | if old_mountpoint!=new_mountpoint: |
---|
394 | status=self.check_mountpoint_status(old_mountpoint) |
---|
395 | |
---|
396 | if status["status"]: |
---|
397 | dismount=self.dismount_mountpoint(old_mountpoint) |
---|
398 | |
---|
399 | if dismount: |
---|
400 | result=self.mount_drive(profile,new_mountpoint) |
---|
401 | |
---|
402 | if result: |
---|
403 | self.save_profiles(info) |
---|
404 | msg_log="Edit profile: '%s' profile has been edited"%profile.encode("utf-8") |
---|
405 | self.log(msg_log) |
---|
406 | self.dprint(msg_log) |
---|
407 | |
---|
408 | |
---|
409 | return result |
---|
410 | |
---|
411 | #def edit_profile |
---|
412 | |
---|
413 | def sync_profile(self,profile,mountpoint): |
---|
414 | |
---|
415 | status=self.check_mountpoint_status(mountpoint) |
---|
416 | |
---|
417 | if status["status"]: |
---|
418 | action="Dismount" |
---|
419 | result=self.dismount_mountpoint(mountpoint) |
---|
420 | |
---|
421 | else: |
---|
422 | action="Mount" |
---|
423 | result=self.mount_drive(profile,mountpoint) |
---|
424 | |
---|
425 | return {"action":action,"result":result} |
---|
426 | |
---|
427 | #def_sync_profile |
---|
428 | |
---|
429 | def log(self,msg): |
---|
430 | |
---|
431 | log_file=self.config_dir+"lliurex-gdrive.log" |
---|
432 | |
---|
433 | f=open(log_file,"a+") |
---|
434 | f.write(msg + '\n') |
---|
435 | f.close() |
---|
436 | |
---|
437 | #def log |
---|
438 | |
---|
439 | if __name__=="__main__": |
---|
440 | |
---|
441 | llxgd=LliurexGoogleDriveManager() |
---|
442 | llxgd.mount_drives() |
---|
443 | |
---|
444 | |
---|
445 | |
---|
446 | #llxgd.save_profiles(llxgd.var) |
---|
447 | |
---|
448 | |
---|
449 | |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | |
---|
456 | |
---|