forked from pritambaral/magiclock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagiclock-admin
98 lines (87 loc) · 3.13 KB
/
magiclock-admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python2
import dbus,optparse
from os import environ,kill
from subprocess import Popen
from sys import exit, exc_info
bus=dbus.SystemBus()
bus_obj=bus.get_object('org.freedesktop.UDisks','/org/freedesktop/UDisks')
bus_iface=dbus.Interface(bus_obj,'org.freedesktop.UDisks')
SerialOfLock=''
LockMode=0
def kill_app():
try:
config=open(environ['HOME']+'/.config/magiclock')
import ast
pid=ast.literal_eval(config.read())['pid']
kill(pid,15)
except:
pass
def start_app():
kill_app()
child=Popen(['magiclock'],stdout=None)
pid=child.pid
configString="{'serial':'%s','pid':%d,'lockmode':%d}" % (SerialOfLock,pid,LockMode)
try:
f=open(environ['HOME']+'/.config/magiclock','w')
f.write(configString)
f.close()
except:
print "Could not save. My apologies"
kill(pid,9)
exit()
RadioVar=''
CheckVar=0
frame=0
def save():
device=RadioVar.get()
global LockMode
LockMode=CheckVar.get()
global SerialOfLock
if SerialOfLock == '':
dev_obj=bus.get_object('org.freedesktop.UDisks',device)
dev_iface=dbus.Interface(dev_obj,dbus.PROPERTIES_IFACE)
from hashlib import sha256
s=sha256()
s.update(dev_iface.Get('org.freedesktop.UDisks.Device','DriveSerial'))
SerialOfLock=str(s.digest())
start_app()
def gui():
rootwindow=Tkinter.Tk()
global frame
rootwindow.title('Select Locking Device')
frame=Tkinter.Frame(rootwindow)
refresh_frame()
rootwindow.mainloop()
def refresh_frame():
if SerialOfLock=='':
global RadioVar
RadioVar=Tkinter.StringVar()
for w in frame.children.values():
w.destroy()
for w in bus_iface.get_dbus_method('EnumerateDevices')():
get_device_info(w,frame,RadioVar)
if LockMode == -30 :
global CheckVar
CheckVar=Tkinter.IntVar()
CheckBox=Tkinter.Checkbutton(frame,text='Unlock Only', variable=CheckVar)
CheckBox.pack()
Tkinter.Button(frame,text='Save',command=save).pack()
Tkinter.Button(frame,text='Refresh',command=refresh_frame).pack()
frame.pack()
def get_device_info(device,frame,RadioVar):
dev_obj=bus.get_object('org.freedesktop.UDisks',device)
dev_iface=dbus.Interface(dev_obj,dbus.PROPERTIES_IFACE)
if dev_iface.Get('org.freedesktop.UDisks.Device','DeviceIsDrive'):
button_text=str(dev_iface.Get('org.freedesktop.UDisks.Device','DeviceFile'))+' '+str(dev_iface.Get('org.freedesktop.UDisks.Device','DriveModel'))
Tkinter.Radiobutton(frame,text=button_text,variable=RadioVar,value=device).pack(anchor=Tkinter.E)
parser=optparse.OptionParser("usage: %prog -s <serial> -l <locking mode>\nlocking mode is 0 for both unlocking and locking; 1 for unlocking only")
parser.add_option('-s','--serial',dest='SerialOfLock',type='string',default='',help='Serial of USB Key')
parser.add_option('-l','--lock',dest='LockMode',type='int',default=-30,help='1 for unlock only, 0 for both')
(options,args)=parser.parse_args()
SerialOfLock=options.SerialOfLock
LockMode=options.LockMode
if SerialOfLock == '' or LockMode == -30 :
import Tkinter
gui()
else:
start_app()