- Timestamp:
- Jul 28, 2017, 11:57:45 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lliurex-cdlocker/trunk/fuentes/lliurex-cdlocker.install/usr/share/n4d/python-plugins/LliurexCdLocker.py
r5601 r5611 1 #!/usr/bin/env python 2 import re 3 from shutil import copy2 4 import os 5 import threading 6 import subprocess 7 8 class CDLocker: 9 def __init__(self): 10 pass 11 12 def startup(self,options): 13 t = threading.Thread(target=self._startup) 14 t.daemon = True 15 t.start() 16 17 def _startup(self): 18 try: 19 if objects.has_key("VariablesManager"): 20 objects["VariablesManager"].register_trigger("CDLOCK_ACTIVE","CDLocker",self.do_action) 21 self.do_action() 22 except Exception as e: 23 print str(e) 24 25 def getState(self,*args,**kwargs): 26 try: 27 if objects.has_key("VariablesManager"): 28 state=objects["VariablesManager"].get_variable("CDLOCK_ACTIVE") 29 if state == None or state == '0': 30 return [True,0] 31 else: 32 return [True,1] 33 else: 34 return [False] 35 except Exception as e: 36 return [False,str(e)] 37 38 def enable(self,*args,**kwargs): 39 dstfile="/etc/udev/rules.d/60-cdrom_id.rules" 40 origfile="/lib/udev/rules.d/60-cdrom_id.rules" 41 tmpfile="/tmp/cdlocker_tmp_rules" 42 if not os.path.exists(dstfile): 43 if os.path.exists(origfile): 44 try: 45 copy2(origfile,dstfile) 46 except Exception as e: 47 return [False,str(e)] 48 try: 49 with open(dstfile,"r") as f: 50 changed=False 51 with open(tmpfile,"w") as out: 52 for line in f: 53 m = re.match("^(ENV{DISK_EJECT_REQUEST}==.*)",line); 54 if not m: 55 out.write(line) 56 else: 57 changed=True 58 out.write('#'+m.group(1)+"\n") 59 if changed: 60 try: 61 copy2(tmpfile, dstfile) 62 subprocess.call(['/bin/chmod','0700','/usr/bin/eject']) 63 subprocess.call(['/usr/bin/eject','-i','on','/dev/sr0']) 64 except Exception as e: 65 return [False,str(e)] 66 return [True,"Enabled now!"] 67 except Exception as e: 68 return [False,str(e)] 69 70 def disable(self,*args,**kwargs): 71 dstfile="/etc/udev/rules.d/60-cdrom_id.rules" 72 origfile="/lib/udev/rules.d/60-cdrom_id.rules" 73 tmpfile="/tmp/cdlocker_tmp_rules" 74 if not os.path.exists(dstfile): 75 if os.path.exists(origfile): 76 try: 77 copy2(origfile,dstfile) 78 except Exception as e: 79 return [False,str(e)] 80 try: 81 with open(dstfile,"r") as f: 82 changed=False 83 with open(tmpfile,"w") as out: 84 for line in f: 85 m = re.match("^#(ENV{DISK_EJECT_REQUEST}==.*)",line); 86 if not m: 87 out.write(line) 88 else: 89 changed=True 90 out.write(m.group(1)+"\n") 91 if changed: 92 try: 93 copy2(tmpfile, dstfile) 94 subprocess.call(['/bin/chmod','0755','/usr/bin/eject']) 95 subprocess.call(['/usr/bin/eject','-i','off','/dev/sr0']) 96 except Exception as e: 97 return [False,str(e)] 98 return [True,"Disabled now!"] 99 except Exception as e: 100 return [False,str(e)] 101 102 def do_action(self,*args,**kwargs): 103 result,state=self.getState() 104 if result == True: 105 if state == 1: 106 r,txt=self.enable() 107 else: 108 r,txt=self.disable() 109 else: 110 r=False 111 if r == True: 112 return [True,txt] 113 else: 114 return [False,txt] 115 116 if __name__ == '__main__': 117 cdl=CDLocker() 118 print cdl.getState()
Note: See TracChangeset
for help on using the changeset viewer.