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=False |
---|
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 | connection=self.check_google_connection() |
---|
190 | if connection: |
---|
191 | for profile in self.profiles_config: |
---|
192 | automount=self.profiles_config[profile]["automount"] |
---|
193 | if automount: |
---|
194 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
195 | profile=profile.encode("utf-8") |
---|
196 | mountpoint=mountpoint.encode("utf-8") |
---|
197 | self.mount_drive(profile,mountpoint) |
---|
198 | |
---|
199 | |
---|
200 | #def mount_drives |
---|
201 | |
---|
202 | def dprint(self,msg): |
---|
203 | |
---|
204 | if DEBUG: |
---|
205 | print("[LGDM] %s"%msg) |
---|
206 | |
---|
207 | #def dprint |
---|
208 | |
---|
209 | |
---|
210 | def save_profiles(self,info): |
---|
211 | |
---|
212 | self.profiles_config=info |
---|
213 | |
---|
214 | f=open(self.config_file,"w") |
---|
215 | #data=json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False) |
---|
216 | data=unicode(json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
217 | f.write(data) |
---|
218 | f.close() |
---|
219 | |
---|
220 | #def save_profiles |
---|
221 | |
---|
222 | def check_mountpoint_status(self,mountpoint): |
---|
223 | |
---|
224 | mountpoint_size="" |
---|
225 | mountpoint_used="" |
---|
226 | mountpoint_available="" |
---|
227 | mountpoint_per="" |
---|
228 | tmp=[] |
---|
229 | |
---|
230 | |
---|
231 | command='df -h | grep "google-drive-ocamlfuse" | grep ' +mountpoint+'$' |
---|
232 | |
---|
233 | p=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
234 | poutput,perror=p.communicate() |
---|
235 | |
---|
236 | if len(poutput)>0: |
---|
237 | status=True |
---|
238 | for item in poutput.split(" "): |
---|
239 | if len(item)>1: |
---|
240 | tmp.append(item) |
---|
241 | |
---|
242 | mountpoint_size=tmp[1] |
---|
243 | mountpoint_used=tmp[2] |
---|
244 | mountpoint_available=tmp[3] |
---|
245 | mountpoint_per=tmp[4] |
---|
246 | |
---|
247 | else: |
---|
248 | status=False |
---|
249 | |
---|
250 | return {"status":status,"size":mountpoint_size,"used":mountpoint_used,"available":mountpoint_available,"used%":mountpoint_per} |
---|
251 | |
---|
252 | #def check_mountpoint_status |
---|
253 | |
---|
254 | def check_profile_info(self,profile,mountpoint,edition): |
---|
255 | |
---|
256 | ''' |
---|
257 | code=0: Form OK |
---|
258 | code=1: Profile empty |
---|
259 | code=2: Profile with blanck spaces |
---|
260 | code=3: Profile duplicate |
---|
261 | code=4: Mountpoint duplicate |
---|
262 | |
---|
263 | ''' |
---|
264 | #profile=profile.decode("utf-8") |
---|
265 | if not edition: |
---|
266 | |
---|
267 | |
---|
268 | # type(unicode) ==> encode("utf-8") |
---|
269 | # type(str) ==> decode("utf-8") |
---|
270 | |
---|
271 | |
---|
272 | if profile=="": |
---|
273 | return {"result":False,"code":1} |
---|
274 | |
---|
275 | if ' ' in profile: |
---|
276 | return {"result":False,"code":2} |
---|
277 | |
---|
278 | else: |
---|
279 | for item in self.profiles_config: |
---|
280 | if profile==item: |
---|
281 | return {"result":False,"code":3} |
---|
282 | |
---|
283 | for item in self.profiles_config: |
---|
284 | if profile!=item: |
---|
285 | if mountpoint==self.profiles_config[item]["mountpoint"]: |
---|
286 | return {"result":False,"code":4} |
---|
287 | |
---|
288 | return {"result":True,"code":0} |
---|
289 | |
---|
290 | #def check_profile_info |
---|
291 | |
---|
292 | def create_profile(self,profile): |
---|
293 | |
---|
294 | profile=unicode(profile).encode("utf-8") |
---|
295 | path=GDRIVE_CONFIG_DIR+profile+"/config" |
---|
296 | |
---|
297 | if not self.check_config(profile): |
---|
298 | os.system("google-drive-ocamlfuse -label %s"%profile) |
---|
299 | msg_log=("'%s' profile has been create")%profile |
---|
300 | #self.log(msg_log) |
---|
301 | self.dprint(msg_log) |
---|
302 | |
---|
303 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
304 | shutil.copy(LLIUREX_CONFIG_FILE,path ) |
---|
305 | |
---|
306 | return True |
---|
307 | |
---|
308 | |
---|
309 | #def create_profile |
---|
310 | |
---|
311 | def create_mountpoint(self,info,profile): |
---|
312 | |
---|
313 | mountpoint=info[profile]["mountpoint"] |
---|
314 | |
---|
315 | result=self.mount_drive(profile,mountpoint) |
---|
316 | |
---|
317 | if result: |
---|
318 | self.save_profiles(info) |
---|
319 | else: |
---|
320 | if profile !="": |
---|
321 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
322 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
323 | self.dprint("'%s' profile has been delete"%profile) |
---|
324 | |
---|
325 | return result |
---|
326 | |
---|
327 | #def create_mountpoint |
---|
328 | |
---|
329 | def dismount_mountpoint(self,mountpoint): |
---|
330 | |
---|
331 | cmd='fusermount -u ' + mountpoint |
---|
332 | p=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
333 | poutput,perror=p.communicate() |
---|
334 | |
---|
335 | if len(perror)>0: |
---|
336 | msg_log="Dismount mountpoint: Error dismounted '%s': '%s'"%(mountpoint,str(perror)) |
---|
337 | #self.log(msg_log) |
---|
338 | self.dprint(msg_log) |
---|
339 | result=False |
---|
340 | else: |
---|
341 | msg_log="Dismount mountpoint:'%s' mountpoint has been dismounted"%mountpoint |
---|
342 | #self.log(msg_log) |
---|
343 | self.dprint(msg_log) |
---|
344 | result=True |
---|
345 | |
---|
346 | return result |
---|
347 | |
---|
348 | #def dismount_mountpoint |
---|
349 | |
---|
350 | def delete_profile(self,info,profile): |
---|
351 | |
---|
352 | result=True |
---|
353 | dismount=True |
---|
354 | |
---|
355 | #profile=str(profile) |
---|
356 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
357 | print mountpoint |
---|
358 | |
---|
359 | |
---|
360 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
361 | is_mountpoint_mounted=self.check_mountpoint_status(mountpoint) |
---|
362 | |
---|
363 | if is_mountpoint_mounted["status"]: |
---|
364 | dismount=self.dismount_mountpoint(mountpoint) |
---|
365 | |
---|
366 | if dismount: |
---|
367 | if profile!="": |
---|
368 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
369 | msg_log="Delete profile: '%s' profile has been delete"%profile |
---|
370 | #self.log(msg_log) |
---|
371 | self.dprint(msg_log) |
---|
372 | self.save_profiles(info) |
---|
373 | return dismount |
---|
374 | |
---|
375 | else: |
---|
376 | self.save_profiles(info) |
---|
377 | msg_log="Delete profile: '%s' GDrive profile path does not exist"%profile |
---|
378 | #self.log(msg_log) |
---|
379 | self.dprint(msg_log) |
---|
380 | |
---|
381 | return True |
---|
382 | |
---|
383 | |
---|
384 | # delete_profile |
---|
385 | |
---|
386 | |
---|
387 | def edit_profile(self,info,profile): |
---|
388 | |
---|
389 | result=True |
---|
390 | old_mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
391 | old_automount=self.profiles_config[profile]["automount"] |
---|
392 | new_mountpoint=info[profile]["mountpoint"] |
---|
393 | new_automount=info[profile]["automount"] |
---|
394 | |
---|
395 | if old_mountpoint!=new_mountpoint: |
---|
396 | status=self.check_mountpoint_status(old_mountpoint) |
---|
397 | |
---|
398 | if status["status"]: |
---|
399 | dismount=self.dismount_mountpoint(old_mountpoint) |
---|
400 | |
---|
401 | if dismount: |
---|
402 | result=self.mount_drive(profile,new_mountpoint) |
---|
403 | |
---|
404 | if result: |
---|
405 | self.save_profiles(info) |
---|
406 | msg_log="Edit profile: '%s' profile has been edited"%profile.encode("utf-8") |
---|
407 | self.log(msg_log) |
---|
408 | self.dprint(msg_log) |
---|
409 | |
---|
410 | |
---|
411 | return result |
---|
412 | |
---|
413 | #def edit_profile |
---|
414 | |
---|
415 | def sync_profile(self,profile,mountpoint): |
---|
416 | |
---|
417 | status=self.check_mountpoint_status(mountpoint) |
---|
418 | |
---|
419 | if status["status"]: |
---|
420 | action="Dismount" |
---|
421 | result=self.dismount_mountpoint(mountpoint) |
---|
422 | |
---|
423 | else: |
---|
424 | action="Mount" |
---|
425 | result=self.mount_drive(profile,mountpoint) |
---|
426 | |
---|
427 | return {"action":action,"result":result} |
---|
428 | |
---|
429 | #def_sync_profile |
---|
430 | |
---|
431 | def log(self,msg): |
---|
432 | |
---|
433 | log_file=self.config_dir+"lliurex-gdrive.log" |
---|
434 | |
---|
435 | f=open(log_file,"a+") |
---|
436 | f.write(msg + '\n') |
---|
437 | f.close() |
---|
438 | |
---|
439 | #def log |
---|
440 | |
---|
441 | if __name__=="__main__": |
---|
442 | |
---|
443 | llxgd=LliurexGoogleDriveManager() |
---|
444 | llxgd.mount_drives() |
---|
445 | |
---|
446 | |
---|
447 | |
---|
448 | #llxgd.save_profiles(llxgd.var) |
---|
449 | |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | |
---|
456 | |
---|
457 | |
---|
458 | |
---|