#!/usr/bin/env python import re from shutil import copy2 import os import threading import subprocess class CDLockerClient: def __init__(self): self.etc_udev_file="/etc/udev/rules.d/60-cdrom_id.rules" self.udev_file="/lib/udev/rules.d/60-cdrom_id.rules" #def init def startup(self,options): t = threading.Thread(target=self._startup) t.daemon = True t.start() #def startup def _startup(self): try: if objects.has_key("VariablesManager"): objects["VariablesManager"].register_trigger("CDLOCKER","CDLockerClient",self.lock_trigger) self.get_value() self.lock_trigger() except Exception as e: print str(e) #def _startup def get_value(self): # Making sure we're able to read CDLOCK_ACTIVE var from server tries=10 for x in range(0,tries): self.var=objects["VariablesManager"].get_variable("CDLOCKER") if self.var != None: self.lock_trigger(self.var) break else: time.sleep(1) if self.var== None: self.var={} self.var["enabled"]=False #def get_value def is_enabled(self): try: return self.var["enabled"] except: return False #def is_enabled def lock_trigger(self,lock_state=None): if lock_state!=None: self.var=lock_state if self.is_enabled(): self.lock() else: self.unlock() #def lock_trigger def refresh_udev(self): os.system("udevadm control --reload-rules") os.system("udevadm trigger") #def refresh_udev def create_etc_file(self): try: f=open(self.udev_file) lines=f.readlines() f.close() f=open(self.etc_udev_file,"w") f.write("# ### FILE AUTOMATICALLY GENERATED BY N4D-CDLOCKER-CLIENT ### #\n\n") for line in lines: if line.find("ENV{DISK_EJECT_REQUEST}")==0: line="# DISABLED BY LLIUREX\n# " + line f.write(line) f.close() return True except: return False #def create_etc_file def lock(self): if self.create_etc_file(): self.refresh_udev() os.system("eject -i on /dev/sr0 1>/dev/null") os.system("chmod 0700 /usr/bin/eject") return True else: return False #def lock def unlock(self): if os.path.exists(self.etc_udev_file): os.remove(self.etc_udev_file) self.refresh_udev() os.system("eject -i off /dev/sr0 1>/dev/null") os.system("chmod 0755 /usr/bin/eject") return True #def unlock