printrun-src/printrun/power/__init__.py

Fri, 03 Jun 2016 09:16:07 +0200

author
mbayer
date
Fri, 03 Jun 2016 09:16:07 +0200
changeset 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added printrun sourcecode from
https://github.com/kliment/Printrun
03.06.2016 09:10

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 import platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 if platform.system() == "Darwin":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 from .osx import inhibit_sleep_osx, deinhibit_sleep_osx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 inhibit_sleep = inhibit_sleep_osx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 deinhibit_sleep = deinhibit_sleep_osx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 elif platform.system() == "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import ctypes
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 ES_CONTINUOUS = 0x80000000
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 ES_SYSTEM_REQUIRED = 0x00000001
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 def inhibit_sleep(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 mode = ES_CONTINUOUS | ES_SYSTEM_REQUIRED
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 ctypes.windll.kernel32.SetThreadExecutionState(ctypes.c_int(mode))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 def deinhibit_sleep():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 ctypes.windll.kernel32.SetThreadExecutionState(ctypes.c_int(ES_CONTINUOUS))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 import dbus
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 inhibit_sleep_handler = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 inhibit_sleep_token = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 bus = dbus.SessionBus()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 # GNOME uses the right object path, try it first
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 service_name = "org.freedesktop.ScreenSaver"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 proxy = bus.get_object(service_name,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 "/org/freedesktop/ScreenSaver")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 inhibit_sleep_handler = dbus.Interface(proxy, service_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 # Do a test run
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 token = inhibit_sleep_handler.Inhibit("printrun", "test")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 inhibit_sleep_handler.UnInhibit(token)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 except dbus.DBusException:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 # KDE uses /ScreenSaver object path, let's try it as well
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 proxy = bus.get_object(service_name,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 "/ScreenSaver")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 inhibit_sleep_handler = dbus.Interface(proxy, service_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 token = inhibit_sleep_handler.Inhibit("printrun", "test")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 inhibit_sleep_handler.UnInhibit(token)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 def inhibit_sleep(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 global inhibit_sleep_handler, inhibit_sleep_token
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 inhibit_sleep_token = inhibit_sleep_handler.Inhibit("printrun", reason)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 def deinhibit_sleep():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 global inhibit_sleep_handler, inhibit_sleep_token
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 if inhibit_sleep_handler is None or inhibit_sleep_token is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 inhibit_sleep_handler.UnInhibit(inhibit_sleep_token)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 inhibit_sleep_token = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 except Exception, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 logging.warning("Could not setup DBus for sleep inhibition: %s" % e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 def inhibit_sleep(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 def deinhibit_sleep():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 import psutil
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def get_nice(nice, p = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 if not p: p = psutil.Process(os.getpid())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 if callable(p.nice):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 return p.nice()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 return p.nice
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 def set_nice(nice, p = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 if not p: p = psutil.Process(os.getpid())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 if callable(p.nice):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 p.nice(nice)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 p.nice = nice
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 if platform.system() != "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 import resource
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 if hasattr(psutil, "RLIMIT_NICE"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 nice_limit, _ = resource.getrlimit(psutil.RLIMIT_NICE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 high_priority_nice = 20 - nice_limit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 high_priority_nice = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 # RLIMIT_NICE is not available (probably OSX), let's probe
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 # Try setting niceness to -20 .. -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 p = psutil.Process(os.getpid())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 orig_nice = get_nice(p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 for i in range(-20, 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 set_nice(i, p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 high_priority_nice = i
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 except psutil.AccessDenied, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 set_nice(orig_nice, p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 def set_priority():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 if platform.system() == "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 set_nice(psutil.HIGH_PRIORITY_CLASS)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 if high_priority_nice < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 set_nice(high_priority_nice)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 def reset_priority():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 if platform.system() == "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 set_nice(psutil.NORMAL_PRIORITY_CLASS)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 if high_priority_nice < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 set_nice(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 def powerset_print_start(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 set_priority()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 inhibit_sleep(reason)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 def powerset_print_stop():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 reset_priority()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 deinhibit_sleep()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 except ImportError, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 logging.warning("psutil unavailable, could not import power utils:" + str(e))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 def powerset_print_start(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 def powerset_print_stop():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 pass

mercurial