#!/usr/bin/python3
from gi.repository import GLib
from pydbus import SystemBus
import raw_write
import logging
import threading
DEAMON_ORG = 'org.mageia.Magiback'
DEAMON_INTERFACE = DEAMON_ORG
ISODUMPER_INTERFACE = DEAMON_ORG + ".Isodumper"
logger = logging.getLogger('magiback.system')
class Isodumper(raw_write.Dumper):
"""
"""
def __init__(self):
super().__init__()
self.finished = threading.Event()
self._progress = 0
self.loop = GLib.MainLoop()
self.return_state = False
self.return_message = ""
self.authorized_sender_read = set()
self.authorized_sender_write = set()
def do_write(self,source, target, dbus_context):
if check_permission('org.mageia.Magiback.Isodumper.write',dbus_context):
print("Writing")
self.finished.clear()
self.thread = threading.Thread(target=self._do_write, args=(source, target, ))
self.thread.start()
else:
self.return_message = "Access denied"
self.finished.set()
@property
def done(self):
return self.finished.wait(1)
@property
def message(self):
return self.return_message
@property
def state(self):
return self.return_state
def end(self):
if hasattr(self, 'thread'):
self.thread.join()
self._progress = 0
self.finished.clear()
return self.return_state, self.return_message
@property
def progress(self):
return self._progress
def do_unmount(self, device):
print("Unmounting")
return self._do_unmount([device])
def check_write(self,target, source):
self.thread = threading.Thread(target=self._check_write, args=(target, source,))
self.thread.start()
def run(self):
self.loop.run()
def check_permission(action, dbus_context):
""" Check permission
"""
return dbus_context.is_authorized(action, {'polkit.icon_name': 'isodumper.png',}, interactive=True)
class ConfFile(object):
"""
"""
def __init__(self):
super().__init__()
def setName(self,file_name):
self.file_name = file_name
def getFile(self):
with open(self.file_name,'r') as tcf :
# read the file
content=""
while 1:
line = tcf.readline()
if not line:
break
content += line
return content
def saveFile(self, tc, dbus_context):
if check_permission('org.mageia.Magiback.write',dbus_context):
try:
with open(self.file_name,'w') as tcf :
tcf.write(tc)
except:
return False
return True
if __name__ == '__main__':
print("Running Magiback service.")
bus = SystemBus()
conf_file = ConfFile()
bus.publish(DEAMON_ORG, conf_file,
("Isodumper", Isodumper()))
loop = GLib.MainLoop()
loop.run()