Skip to content
Permalink
Browse files

Use errno constants instead of magic numbers.

Removed a few no-op comments on the way.

Change-Id: I0b01e85d398a2f29378e0179cefae3a3ec5db135
  • Loading branch information...
svenpanne committed May 15, 2019
1 parent b89db3d commit 83666bb629b78d7c1b7482ae6d514b12781a3294
@@ -98,6 +98,8 @@
You should find an example configuration file at
'../cfg_examples/filestats.cfg' relative to this file.
"""

import errno
import re
import os
import sys
@@ -176,7 +178,7 @@ def _stat(self):
try:
stat = os.stat(path)
except OSError, exc:
self.stat_status = "file vanished" if exc.errno == 2 else str(exc)
self.stat_status = "file vanished" if exc.errno == errno.ENOENT else str(exc)
return

try:
@@ -191,10 +191,10 @@ def set_permissions(path, uid, gid, mode):
try:
os.chown(path, uid, gid)
except OSError, e:
if e.errno == 13:
if e.errno == errno.EACCES:
pass # On CIFS mounts where "uid=0,forceuid,gid=1000,forcegid" mount options
# are set, this is not possible. So skip over.
elif e.errno == 1:
elif e.errno == errno.EPERM:
pass # On NFS mounts where "" mount options are set, we get an
# "Operation not permitted" error when trying to change e.g.
# the group permission.
@@ -204,10 +204,10 @@ def set_permissions(path, uid, gid, mode):
try:
os.chmod(path, mode)
except OSError, e:
if e.errno == 13:
if e.errno == errno.EACCES:
pass # On CIFS mounts where "uid=0,forceuid,gid=1000,forcegid" mount options
# are set, this is not possible. So skip over.
elif e.errno == 1:
elif e.errno == errno.EPERM:
pass # On NFS mounts where "" mount options are set, we get an
# "Operation not permitted" error when trying to change e.g.
# the group permission.
@@ -51,6 +51,7 @@ __version__ = "1.6.0i1"
# | |_| |
# '----------------------------------------------------------------------'

import errno
import os
import re
import ast
@@ -325,7 +326,7 @@ class Test(object):
try:
os.makedirs(test_dir)
except OSError, e:
if e.errno == 17: # file exists
if e.errno == errno.EEXIST:
pass
else:
raise
@@ -2567,7 +2568,7 @@ class TestAgentBasedStandardHosts(TestCase):
try:
os.makedirs(config_dir)
except OSError, e:
if e.errno == 17: # file exists
if e.errno == errno.EEXIST:
pass
else:
raise
@@ -2625,7 +2626,7 @@ class TestAgentBasedStandardHosts(TestCase):
try:
os.makedirs(config_dir)
except OSError, e:
if e.errno == 17: # file exists
if e.errno == errno.EEXIST:
pass
else:
raise
@@ -2755,7 +2756,7 @@ class TestAgentBasedStandardHosts(TestCase):
try:
shutil.copytree(src_dir, target_dir)
except OSError, e:
if e.errno == 17: # file exists
if e.errno == errno.EEXIST:
pass
else:
raise
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.

import errno
import os
import cmk.utils.paths
import cmk.utils.debug
@@ -477,7 +478,7 @@ def check_logwatch_generic(item, params, loglines, found, groups=False):
try:
os.makedirs(logdir)
except OSError, e:
if e.errno == 17:
if e.errno == errno.EEXIST:
pass # Exists
else:
raise
@@ -1025,7 +1026,7 @@ def logwatch_spool_messages(message_chunks, result):
try:
os.makedirs(path)
except OSError, e:
if e.errno == 17:
if e.errno == errno.EEXIST:
pass # Exists
else:
raise
@@ -1040,7 +1041,7 @@ def logwatch_spool_messages(message_chunks, result):
os.unlink(spool_file_path)
continue
except OSError, e:
if e.errno == 2:
if e.errno == errno.ENOENT:
continue
else:
raise
@@ -1067,7 +1068,7 @@ def logwatch_load_spooled_messages(method, result):
try:
spool_files = sorted(os.listdir(logwatch_spool_path()))
except OSError, e:
if e.errno == 2: # No such file or directory
if e.errno == errno.ENOENT:
return []
else:
raise
@@ -1116,7 +1117,7 @@ def logwatch_load_spooled_messages(method, result):
try:
messages = ast.literal_eval(open(path).read())
except IOError, e:
if e.errno == 2: # No such file or directory
if e.errno == errno.ENOENT:
continue
else:
raise
@@ -3080,7 +3080,7 @@ def handle_client(self, client_socket, allow_commands, client_ip):
try:
self._answer_query(client_socket, query, response)
except socket.error as e:
if e.errno == 32: # Broken pipe -> ignore this
if e.errno == errno.EPIPE:
pass
else:
raise
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.

import errno
import logging
import multiprocessing
import os
@@ -359,7 +360,7 @@ def _delete_work_dir(self):
try:
shutil.rmtree(self._work_dir)
except OSError as e:
if e.errno == 2: # No such file or directory
if e.errno == errno.ENOENT:
pass
else:
raise
@@ -30,6 +30,7 @@
# BE AWARE: This code is directly used by the appliance. So if you are
# about to refactor things, you will have to care about the appliance!

import errno
import glob
import os
import shutil
@@ -224,7 +225,7 @@ def cleanup(self):
try:
os.unlink(self.state_file_path())
except OSError as e:
if e.errno == 2:
if e.errno == errno.ENOENT:
pass
else:
raise
@@ -233,7 +234,7 @@ def state(self):
try:
state = json.load(file(self.state_file_path()))
except IOError as e:
if e.errno == 2: # not existant
if e.errno == errno.ENOENT: # not existant
state = {
"state": None,
"started": None,
@@ -289,7 +290,7 @@ def stop(self):
try:
os.killpg(pgid, signal.SIGTERM)
except OSError as e:
if e.errno == 3:
if e.errno == errno.ESRCH:
pass
else:
raise
@@ -304,7 +305,7 @@ def stop(self):
try:
os.killpg(pgid, signal.SIGKILL)
except OSError as e:
if e.errno == 3:
if e.errno == errno.ESRCH:
pass
else:
raise
@@ -45,6 +45,7 @@
# import ...
#

import errno
import os
import sys
from types import ModuleType
@@ -153,7 +154,7 @@ def _find_local_web_plugins():
try:
plugin_dirs = os.listdir(basedir)
except OSError as e:
if e.errno == 2:
if e.errno == errno.ENOENT:
return
else:
raise
@@ -27,6 +27,7 @@
# This module contains some helper functions dealing with the creation
# of multi-tier tar files (tar files containing tar files)

import errno
import hashlib
import os
import tarfile
@@ -686,7 +687,7 @@ def wipe_directory(path):
try:
os.remove(p)
except OSError as e:
if e.errno == 2: # no such file or directory
if e.errno == errno.ENOENT:
continue
else:
raise
@@ -49,6 +49,7 @@

import abc
import copy
import errno
from multiprocessing.pool import ThreadPool
from multiprocessing import TimeoutError
import os
@@ -405,7 +406,7 @@ def clear_all_ldap_caches(cls):
try:
shutil.rmtree(cls._ldap_caches_filepath())
except OSError as e:
if e.errno != 2:
if e.errno != errno.ENOENT:
raise

# Bind with the default credentials
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.

import errno
import os
import re
import time
@@ -2560,7 +2561,7 @@ def _load_snmp_mibs(self, path):
try:
file_names = os.listdir(path)
except OSError as e:
if e.errno == 2: # not existing directories are ok
if e.errno == errno.ENOENT:
return found
else:
raise
@@ -331,7 +331,7 @@ def _perform_tests_for_site(self, site_id, result_queue):
# try:
# os.close(x)
# except OSError, e:
# if e.errno == 9: # Bad file descriptor
# if e.errno == errno.EBADF:
# pass
# else:
# raise
@@ -863,7 +863,7 @@ def _fetch_for_site(self, site_id, site, result_queue):
# try:
# os.close(x)
# except OSError, e:
# if e.errno == 9: # Bad file descriptor
# if e.errno == errno.EBADF:
# pass
# else:
# raise
@@ -23,6 +23,8 @@
# License along with GNU Make; see the file COPYING. If not, write
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.

import errno
import ast
import os
import shutil
@@ -156,7 +158,7 @@ def _cleanup_legacy_replication_status():
try:
os.unlink(var_dir + "replication_status.mk")
except OSError as e:
if e.errno == 2:
if e.errno == errno.ENOENT:
pass # Not existant -> OK
else:
raise
@@ -166,7 +168,7 @@ def clear_site_replication_status(site_id):
try:
os.unlink(_site_replication_status_path(site_id))
except OSError as e:
if e.errno == 2:
if e.errno == errno.ENOENT:
pass # Not existant -> OK
else:
raise
@@ -522,7 +524,7 @@ def _save_activation(self):
try:
os.makedirs(os.path.dirname(self._info_path()))
except OSError as e:
if e.errno == 17: # File exists
if e.errno == errno.EEXIST:
pass
else:
raise
@@ -682,7 +684,7 @@ def create_site_globals_file(self, site_id, tmp_dir, sites=None):
try:
os.makedirs(tmp_dir)
except OSError as e:
if e.errno == 17: # File exists
if e.errno == errno.EEXIST:
pass
else:
raise
@@ -856,7 +858,7 @@ def run(self):
try:
os.close(x)
except OSError as e:
if e.errno == 9: # Bad file descriptor
if e.errno == errno.EBADF:
pass
else:
raise
@@ -1028,7 +1030,7 @@ def _cleanup_snapshot(self):
try:
os.unlink(self._snapshot_file)
except OSError as e:
if e.errno == 2:
if e.errno == errno.ENOENT:
pass # Not existant -> OK
else:
raise
@@ -26,6 +26,7 @@
"""Functions for logging changes and keeping the "Activate Changes" state and finally activating changes."""

import ast
import errno
import os
import time

@@ -191,7 +192,7 @@ def load(self, lock=False):
if entry:
changes.append(ast.literal_eval(entry))
except IOError as e:
if e.errno == 2: # No such file or directory
if e.errno == errno.ENOENT:
pass
else:
raise
@@ -231,7 +232,7 @@ def clear(self):
try:
os.unlink(self._site_changes_path())
except OSError as e:
if e.errno == 2:
if e.errno == errno.ENOENT:
pass # Not existant -> OK
else:
raise
Oops, something went wrong.

0 comments on commit 83666bb

Please sign in to comment.
You can’t perform that action at this time.