
On commodity hardware without olpc-powerd, there is unnecessary filesystem access. On XO laptop hardware there are unnecessary errors in log for every object delete: ERROR root: Inhibit Suspend: Could not delete file /var/run/powerd-inhibit-suspend/1773 Exception AttributeError: "'NoneType' object has no attribute 'endswith'" in <bound method PowerManager.__del__ of <sugar3.power.PowerManager instance at 0xa15962c>> ignored The Clock activity in speaking mode is a good reproducer. Following changes are made: - move the directory check to __init__, and set self._path to None if olpc-powerd is not present, - on inhibit_suspend, use self._path, which avoids a check of the directory, - on restore_suspend or __del__, avoid a call to os.unlink if olpc-powerd is not present.
86 lines
2.5 KiB
Python
86 lines
2.5 KiB
Python
# Copyright (C) 2014, Sugarlabs
|
|
#
|
|
# This library is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
# License as published by the Free Software Foundation; either
|
|
# version 2 of the License, or (at your option) any later version.
|
|
#
|
|
# This library 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
|
|
# Lesser General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
# License along with this library; if not, write to the
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
import os
|
|
import logging
|
|
|
|
_POWERD_INHIBIT_DIR = '/var/run/powerd-inhibit-suspend'
|
|
|
|
_power_manager = None
|
|
|
|
|
|
def get_power_manager():
|
|
global _power_manager
|
|
if _power_manager is None:
|
|
_power_manager = PowerManager()
|
|
return _power_manager
|
|
|
|
|
|
class PowerManager():
|
|
""" control of powerd idle suspend,
|
|
reference counted,
|
|
does nothing if powerd is not present
|
|
"""
|
|
|
|
def __init__(self):
|
|
self._suspend_inhibit_counter = 0
|
|
if os.path.exists(_POWERD_INHIBIT_DIR):
|
|
self._path = os.path.join(_POWERD_INHIBIT_DIR, str(os.getpid()))
|
|
else:
|
|
self._path = None
|
|
|
|
def __del__(self):
|
|
self._remove_flag_file()
|
|
|
|
def suspend_breaks_collaboration(self):
|
|
return True
|
|
|
|
def inhibit_suspend(self):
|
|
if self._path and self._suspend_inhibit_counter == 0:
|
|
try:
|
|
with open(self._path, 'w') as flag_file:
|
|
flag_file.write('')
|
|
except IOError:
|
|
logging.error("Inhibit Suspend: Could not create file %s",
|
|
self._path)
|
|
|
|
self._suspend_inhibit_counter += 1
|
|
|
|
def restore_suspend(self):
|
|
self._suspend_inhibit_counter -= 1
|
|
if self._suspend_inhibit_counter > 0:
|
|
return
|
|
self._remove_flag_file()
|
|
|
|
def is_suspend_inhibited(self):
|
|
return self._suspend_inhibit_counter > 0
|
|
|
|
def shutdown(self):
|
|
"""
|
|
This method clean the flag file if exists,
|
|
is already called when the activity is closed.
|
|
"""
|
|
self._remove_flag_file()
|
|
|
|
def _remove_flag_file(self):
|
|
if self._path:
|
|
try:
|
|
os.unlink(self._path)
|
|
except OSError:
|
|
pass
|
|
self._suspend_inhibit_counter = 0
|