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 | |
---|
10 | |
---|
11 | DEBUG=True |
---|
12 | GDRIVE_CONFIG_DIR=os.path.expanduser("~/.gdfuse/") |
---|
13 | LLIUREX_CONFI_FILE='/home/lliurex/config' |
---|
14 | |
---|
15 | class LliurexGoogleDriveManager: |
---|
16 | |
---|
17 | def __init__(self): |
---|
18 | |
---|
19 | self.config_dir=os.path.expanduser("~/.config/lliurex-google-drive-profiles/") |
---|
20 | self.config_file=self.config_dir+"config" |
---|
21 | |
---|
22 | self.mount_cmd="google-drive-ocamlfuse -label %s %s" |
---|
23 | |
---|
24 | self.read_conf() |
---|
25 | |
---|
26 | |
---|
27 | #def init |
---|
28 | |
---|
29 | def create_conf(self): |
---|
30 | |
---|
31 | self.dprint("Creating conf...") |
---|
32 | |
---|
33 | if not os.path.exists(self.config_dir): |
---|
34 | os.makedirs(self.config_dir) |
---|
35 | |
---|
36 | var={} |
---|
37 | # var["default"]={} |
---|
38 | # var["default"]["email"]="" |
---|
39 | # var["default"]["mountpoint"]="" |
---|
40 | # var["default"]["automount"]="" |
---|
41 | |
---|
42 | f=open(self.config_file,"w") |
---|
43 | data=unicode(json.dumps(var,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
44 | f.write(data) |
---|
45 | f.close() |
---|
46 | |
---|
47 | #def create_conf |
---|
48 | |
---|
49 | def read_conf(self): |
---|
50 | |
---|
51 | if not os.path.exists(self.config_file): |
---|
52 | self.create_conf() |
---|
53 | |
---|
54 | self.dprint("Reading conf...") |
---|
55 | |
---|
56 | f=open(self.config_file) |
---|
57 | self.profiles_config=json.load(f) |
---|
58 | f.close() |
---|
59 | |
---|
60 | #def read_conf |
---|
61 | |
---|
62 | def check_config(self,profile): |
---|
63 | |
---|
64 | path=GDRIVE_CONFIG_DIR+profile+"/state" |
---|
65 | |
---|
66 | if os.path.exists(path): |
---|
67 | f=open(path) |
---|
68 | line=f.readline() |
---|
69 | f.close() |
---|
70 | |
---|
71 | if "1970-" in line: |
---|
72 | msg_error="%s not configured"%profile |
---|
73 | self.dprint(msg_error) |
---|
74 | return {"result":False,"output":None,"error":msg_error} |
---|
75 | |
---|
76 | |
---|
77 | return {"result":True,"output":None,"error":None} |
---|
78 | |
---|
79 | else: |
---|
80 | msg_error="%s not yet create"%profile |
---|
81 | |
---|
82 | self.dprint(msg_error) |
---|
83 | return {"result":False,"output":None,"error": msg_error} |
---|
84 | |
---|
85 | |
---|
86 | #def check_configuration |
---|
87 | |
---|
88 | def check_google_connections(self): |
---|
89 | |
---|
90 | try: |
---|
91 | req=urllib2.Request("http://google.com") |
---|
92 | res=urllib2.urlopen(req) |
---|
93 | return True |
---|
94 | except: |
---|
95 | return False |
---|
96 | |
---|
97 | |
---|
98 | def mount_drive(self,profile,mountpoint): |
---|
99 | |
---|
100 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
101 | check= self.check_config(profile) |
---|
102 | if check["result"]: |
---|
103 | #if profile in self.profiles_config: |
---|
104 | #mount_point=os.path.expanduser(self.profiles_config[profile]["mountpoint"]) |
---|
105 | print mountpoint,type(mountpoint) |
---|
106 | #if type(mount_point)==type(u""): |
---|
107 | if str(mountpoint)!="": |
---|
108 | self.dprint("Mounting '%s'..."%profile) |
---|
109 | if not os.path.exists(mountpoint): |
---|
110 | try: |
---|
111 | os.makedirs(mountpoint) |
---|
112 | except: |
---|
113 | error_msg="Unable to create '%s' mount destination"%mountpoint |
---|
114 | self.dprint(error_msg) |
---|
115 | return {"result":False,"output":None,"error":error_msg} |
---|
116 | |
---|
117 | |
---|
118 | if os.access(mountpoint,os.W_OK): |
---|
119 | #os.system(self.mount_cmd%(profile,mountpoint)) |
---|
120 | cmd=self.mount_cmd%(profile,mountpoint) |
---|
121 | p=subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
122 | poutput,perror=p.communicate() |
---|
123 | if len(perror)==0: |
---|
124 | return {"result":True,"output":str(poutput),"error":str(perror)} |
---|
125 | else: |
---|
126 | error_msg="Error mount '%s':'%s'"%(mountpoint,str(perror)) |
---|
127 | self.dprint(error_msg) |
---|
128 | |
---|
129 | else: |
---|
130 | error_msg="'%s' mount destination is not owned by user"%mountpoint |
---|
131 | self.dprint(error_msg) |
---|
132 | |
---|
133 | else: |
---|
134 | error_msg="'%s' mountpoint not configured"%profile |
---|
135 | self.dprint(error_msg) |
---|
136 | else: |
---|
137 | error_msg="'%s' profile unknown"%profile |
---|
138 | self.dprint(error_msg) |
---|
139 | else: |
---|
140 | error_msg="'%s' GDrive profile path does not exist"%profile |
---|
141 | self.dprint(error_msg) |
---|
142 | |
---|
143 | |
---|
144 | return {"result":False,"output":None,"error":error_msg} |
---|
145 | |
---|
146 | #def mount_drive |
---|
147 | |
---|
148 | def mount_drives(self): |
---|
149 | |
---|
150 | for profile in self.profiles_config: |
---|
151 | self.dprint(profile) |
---|
152 | automount=self.profiles_config[profile]["automount"] |
---|
153 | |
---|
154 | if automount: |
---|
155 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
156 | self.mount_drive(profile,mountpoint) |
---|
157 | |
---|
158 | |
---|
159 | #def mount_drives |
---|
160 | |
---|
161 | def dprint(self,msg): |
---|
162 | |
---|
163 | if DEBUG: |
---|
164 | print("[LGDM] %s"%msg) |
---|
165 | |
---|
166 | #def dprint |
---|
167 | |
---|
168 | |
---|
169 | def save_profiles(self,info): |
---|
170 | |
---|
171 | self.profiles_config=info |
---|
172 | |
---|
173 | f=open(self.config_file,"w") |
---|
174 | data=unicode(json.dumps(info,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
175 | f.write(data) |
---|
176 | f.close() |
---|
177 | |
---|
178 | |
---|
179 | def check_mountpoint_status(self,mountpoint): |
---|
180 | |
---|
181 | mountpoint_size="" |
---|
182 | mountpoint_used="" |
---|
183 | mountpoint_available="" |
---|
184 | mountpoint_per="" |
---|
185 | |
---|
186 | command='df -h | grep "google-drive-ocamlfuse" | grep ' +str(mountpoint) |
---|
187 | |
---|
188 | p=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
189 | poutput,perror=p.communicate() |
---|
190 | |
---|
191 | if len(poutput)>0: |
---|
192 | status=True |
---|
193 | mountpoint_size=poutput.split(" ")[4] |
---|
194 | mountpoint_used=poutput.split(" ")[8] |
---|
195 | mountpoint_available=poutput.split(" ")[11] |
---|
196 | mountpoint_per=poutput.split(" ")[14] |
---|
197 | |
---|
198 | else: |
---|
199 | status=False |
---|
200 | |
---|
201 | return {"status":status,"size":mountpoint_size,"used":mountpoint_used,"available":mountpoint_available,"Used%":mountpoint_per} |
---|
202 | |
---|
203 | |
---|
204 | def check_profile_info(self,profile,mountpoint,edition): |
---|
205 | |
---|
206 | if not edition: |
---|
207 | if profile=="": |
---|
208 | return False |
---|
209 | else: |
---|
210 | for item in self.profiles_config: |
---|
211 | if profile==item: |
---|
212 | return False |
---|
213 | |
---|
214 | for item in self.profiles_config: |
---|
215 | if profile!=item: |
---|
216 | if mountpoint==self.profiles_config[item]["mountpoint"]: |
---|
217 | return False |
---|
218 | |
---|
219 | return True |
---|
220 | |
---|
221 | def create_profile(self,profile): |
---|
222 | |
---|
223 | result=False |
---|
224 | |
---|
225 | profile=str(profile) |
---|
226 | path=GDRIVE_CONFIG_DIR+profile+"/config" |
---|
227 | |
---|
228 | if not self.check_config(profile)["result"]: |
---|
229 | os.system("google-drive-ocamlfuse -label %s"%profile) |
---|
230 | self.dprint("'%s' profile has been create"%profile) |
---|
231 | |
---|
232 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
233 | shutil.copy(LLIUREX_CONFI_FILE,path ) |
---|
234 | return True |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | #def create_profile |
---|
239 | |
---|
240 | def create_mountpoint(self,info,profile): |
---|
241 | |
---|
242 | mountpoint=info[profile]["mountpoint"] |
---|
243 | result=self.mount_drive(profile,mountpoint) |
---|
244 | |
---|
245 | if result["result"]: |
---|
246 | self.save_profiles(info) |
---|
247 | else: |
---|
248 | if profile !="": |
---|
249 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
250 | self.dprint("'%s' profile has been delete"%profile) |
---|
251 | |
---|
252 | return result |
---|
253 | |
---|
254 | def dismount_mountpoint(self,mountpoint): |
---|
255 | |
---|
256 | cmd='fusermount -u ' + mountpoint |
---|
257 | p=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
258 | poutput,perror=p.communicate() |
---|
259 | |
---|
260 | return {"output":poutput,"error":perror} |
---|
261 | |
---|
262 | |
---|
263 | def delete_profile(self,info,profile): |
---|
264 | |
---|
265 | result=True |
---|
266 | |
---|
267 | profile=str(profile) |
---|
268 | mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
269 | |
---|
270 | |
---|
271 | if os.path.exists(GDRIVE_CONFIG_DIR+profile): |
---|
272 | |
---|
273 | is_mountpoint_mounted=self.check_mountpoint_status(mountpoint) |
---|
274 | |
---|
275 | if is_mountpoint_mounted["status"]: |
---|
276 | dismount=self.dismount_mountpoint(mountpoint) |
---|
277 | |
---|
278 | if len(dismount["error"])==0: |
---|
279 | self.dprint("'%s' mountpoint has been dismounted"%mountpoint) |
---|
280 | if profile!="": |
---|
281 | shutil.rmtree(os.path.join(GDRIVE_CONFIG_DIR+profile)) |
---|
282 | self.dprint("'%s' profile has been delete"%profile) |
---|
283 | else: |
---|
284 | self.dprint("Error dismounted '%s': '%s'"%(mountpoint,str(dismount["error"]))) |
---|
285 | result=False |
---|
286 | |
---|
287 | |
---|
288 | else: |
---|
289 | self.save_profiles(info) |
---|
290 | self.dprint("'%s' GDrive profile path does not exist"%profile) |
---|
291 | |
---|
292 | self.save_profiles(info) |
---|
293 | |
---|
294 | # delete_profile |
---|
295 | |
---|
296 | |
---|
297 | def edit_profile(self,info,profile): |
---|
298 | |
---|
299 | result={"result":True,"output":None,"error":None} |
---|
300 | |
---|
301 | old_mountpoint=self.profiles_config[profile]["mountpoint"] |
---|
302 | old_automount=self.profiles_config[profile]["automount"] |
---|
303 | new_mountpoint=info[profile]["mountpoint"] |
---|
304 | new_automount=info[profile]["automount"] |
---|
305 | |
---|
306 | |
---|
307 | if old_mountpoint!=new_mountpoint: |
---|
308 | status=self.check_mountpoint_status(old_mountpoint) |
---|
309 | |
---|
310 | if status["status"]: |
---|
311 | dismount=self.dismount_mountpoint(old_mountpoint) |
---|
312 | |
---|
313 | if len(dismount["error"])==0: |
---|
314 | self.dprint("'%s' mountpoint has been dismounted"%old_mountpoint) |
---|
315 | |
---|
316 | result=self.mount_drive(profile,new_mountpoint) |
---|
317 | |
---|
318 | if result["result"]: |
---|
319 | self.save_profiles(info) |
---|
320 | |
---|
321 | return result |
---|
322 | |
---|
323 | def sync_profile(self,profile,mountpoint): |
---|
324 | |
---|
325 | status=self.check_mountpoint_status(mountpoint) |
---|
326 | |
---|
327 | if status["status"]: |
---|
328 | cmd='fusermount -u ' + mountpoint |
---|
329 | |
---|
330 | os.system(cmd) |
---|
331 | |
---|
332 | self.dprint("'%s' mountpoint has been dismounted"%mountpoint) |
---|
333 | return False |
---|
334 | |
---|
335 | else: |
---|
336 | return self.mount_drive(profile,mountpoint) |
---|
337 | |
---|
338 | if __name__=="__main__": |
---|
339 | |
---|
340 | llxgd=LliurexGoogleDriveManager() |
---|
341 | llxgd.mount_drives() |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | #llxgd.save_profiles(llxgd.var) |
---|
346 | |
---|
347 | |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | |
---|