summaryrefslogtreecommitdiff
path: root/systemd-inhibit-delay
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2018-11-08 17:59:32 -0500
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2018-11-08 17:59:32 -0500
commit451f0766a1a52b5436d5b9946dc9d11d113f4b7c (patch)
treeb9dd9f86e39fd8d6e03006dc6a160e57cd004820 /systemd-inhibit-delay
downloadsystemd-inhibit-delay-451f0766a1a52b5436d5b9946dc9d11d113f4b7c.tar.xz
systemd-inhibit-delay-451f0766a1a52b5436d5b9946dc9d11d113f4b7c.zip
Initial commitHEADmaster
Diffstat (limited to 'systemd-inhibit-delay')
-rwxr-xr-xsystemd-inhibit-delay79
1 files changed, 79 insertions, 0 deletions
diff --git a/systemd-inhibit-delay b/systemd-inhibit-delay
new file mode 100755
index 0000000..3608721
--- /dev/null
+++ b/systemd-inhibit-delay
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+
+# systemd-inhibit-delay
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+
+import argparse
+import dbus
+import os
+import subprocess
+import sys
+from dbus.mainloop.glib import DBusGMainLoop
+from gi.repository import GLib
+
+class Inhibitor:
+ def __init__(self, bus, what, cmd):
+ if what == 'sleep':
+ self.signal_name = 'PrepareForSleep'
+ elif what == 'shutdown':
+ self.signal_name = 'PrepareForShutdown'
+ else:
+ raise Exception('for what, only sleep and shutdown supported')
+
+ if cmd == '':
+ raise Exception('command cannot be empty')
+
+ self.bus = bus
+ self.what = what
+ self.cmd = cmd
+
+ def start(self):
+ self.bus.add_signal_receiver(
+ self.sleep_handler,
+ dbus_interface='org.freedesktop.login1.Manager',
+ signal_name=self.signal_name
+ )
+ self.take_lock()
+
+ def sleep_handler(self, active):
+ if active:
+ subprocess.call(self.cmd)
+ if self.fd != -1:
+ os.close(self.fd)
+ self.fd = -1
+ else:
+ self.take_lock()
+
+ def take_lock(self):
+ login1 = self.bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1')
+ self.fd = login1.Inhibit(self.what, 'systemd-inhibit-delay', '', 'delay',
+ dbus_interface='org.freedesktop.login1.Manager').take()
+
+DBusGMainLoop(set_as_default=True)
+
+parser = argparse.ArgumentParser(description='systemd inhibit delay')
+parser.add_argument('cmd', help='the command to run (MUST EXIT WHEN READY)')
+parser.add_argument('args', nargs=argparse.REMAINDER,
+ help='the command arguments')
+parser.add_argument('--what', default='sleep',
+ help='what to delay, sleep or shutdown (default: sleep)')
+args = parser.parse_args()
+
+system_bus = dbus.SystemBus()
+inhibitor = Inhibitor(system_bus, args.what, [args.cmd] + args.args)
+inhibitor.start()
+loop = GLib.MainLoop()
+loop.run()