From f1fc9886ccd267f2ccf06b151c4360368e9c890d Mon Sep 17 00:00:00 2001 From: James Cameron Date: Wed, 13 May 2015 09:14:30 +1000 Subject: [PATCH] power: avoid filesystem access if powerd absent 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 > 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. --- src/sugar3/power.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/sugar3/power.py b/src/sugar3/power.py index e17841b2..2f5d7384 100644 --- a/src/sugar3/power.py +++ b/src/sugar3/power.py @@ -38,7 +38,10 @@ class PowerManager(): def __init__(self): self._suspend_inhibit_counter = 0 - self._path = os.path.join(_POWERD_INHIBIT_DIR, str(os.getpid())) + 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() @@ -47,10 +50,7 @@ class PowerManager(): return True def inhibit_suspend(self): - if not os.path.exists(_POWERD_INHIBIT_DIR): - return - - if self._suspend_inhibit_counter == 0: + if self._path and self._suspend_inhibit_counter == 0: try: with open(self._path, 'w') as flag_file: flag_file.write('') @@ -77,8 +77,9 @@ class PowerManager(): self._remove_flag_file() def _remove_flag_file(self): - try: - os.unlink(self._path) - except OSError: - pass + if self._path: + try: + os.unlink(self._path) + except OSError: + pass self._suspend_inhibit_counter = 0