Commit c96db48e authored by Xavier Thompson's avatar Xavier Thompson

slapgrid: Refactor iterating over dir entries

parent 1d0a9a76
......@@ -44,7 +44,7 @@ import hashlib
from datetime import datetime
from multiprocessing import Process, Queue as MQueue
from six.moves import queue, reload_module
from slapos.util import str2bytes, mkdir_p, chownDirectory
from slapos.util import str2bytes, mkdir_p, chownDirectory, listifdir
from slapos.grid.utils import dropPrivileges, killProcessTree
from slapos.grid.promise import interface
from slapos.grid.promise.generic import (GenericPromise, PromiseQueueResult,
......@@ -731,10 +731,8 @@ class PromiseLauncher(object):
error = 0
success = 0
promise_name_list = []
if os.path.exists(self.promise_folder) and os.path.isdir(self.promise_folder):
for promise_name in os.listdir(self.promise_folder):
for suffix in ['.pyc', '.pyo']:
if promise_name.endswith(suffix):
for promise_name in listifdir(self.promise_folder):
if promise_name.endswith(('.pyc', '.pyo')):
promise_path = os.path.join(self.promise_folder, promise_name)
if not os.path.exists(promise_path[:-1]):
try:
......@@ -816,10 +814,9 @@ class PromiseLauncher(object):
if key not in promise_name_list:
new_state_dict.pop(key, None)
if not self.run_only_promise_list and os.path.exists(self.legacy_promise_folder) \
and os.path.isdir(self.legacy_promise_folder):
if not self.run_only_promise_list:
# run legacy promise styles
for promise_name in os.listdir(self.legacy_promise_folder):
for promise_name in listifdir(self.legacy_promise_folder):
promise_path = os.path.join(self.legacy_promise_folder, promise_name)
if not os.path.isfile(promise_path) or \
not os.access(promise_path, os.X_OK):
......
......@@ -55,7 +55,7 @@ from slapos import manager as slapmanager
from slapos.slap.slap import NotFoundError
from slapos.slap.slap import ServerError
from slapos.slap.slap import COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
from slapos.util import mkdir_p, chownDirectory, string_to_boolean
from slapos.util import mkdir_p, chownDirectory, string_to_boolean, listifdir
from slapos.grid.exception import BuildoutFailedError
from slapos.grid.SlapObject import Software, Partition
from slapos.grid.svcbackend import (launchSupervisord,
......@@ -1596,10 +1596,7 @@ stderr_logfile_backups=1
instance_path = os.path.join(self.instance_root,
computer_partition.getId())
report_path = os.path.join(instance_path, 'etc', 'report')
if os.path.isdir(report_path):
script_list_to_run = os.listdir(report_path)
else:
script_list_to_run = []
script_list_to_run = listifdir(report_path)
# We now generate the pseudorandom name for the xml file
# and we add it in the invocation_list
......
......@@ -95,6 +95,18 @@ def mkdir_p(path, mode=0o700):
raise
def listifdir(path):
"""
Like listdir, but returns an empty tuple if the path is not a directory.
"""
try:
return os.listdir(path)
except OSError as e:
if e.errno != errno.ENOENT:
raise
return ()
def chownDirectory(path, uid, gid):
if os.getuid() != 0:
# we are probably inside of a webrunner
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment