#!/usr/bin/env python3 import re from shutil import copy2 import os import time import threading import subprocess import n4d.server.core as n4dcore import n4d.responses class CDLockerClient: def __init__(self): self.core=n4dcore.Core.get_core() 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.core.register_variable_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): #Old n4d:self.var=objects["VariablesManager"].get_variable("CDLOCKER") self.var=self.core.get_variable("CDLOCKER").get('return',None) 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") #Old n4d: return True return n4d.responses.build_successful_call_response(True) else: #Old n4d: return False return n4d.responses.build_successful_call_response(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") #Old n4d: return True return n4d.responses.build_successful_call_response(False) #def unlock