better 'file not found' handling

This commit is contained in:
meejah 2018-08-24 15:50:55 -06:00
parent c0772cdd5f
commit 536ccf8b6d

View File

@ -6,6 +6,7 @@ import datetime
import os.path
import re
import types
import errno
import ConfigParser
import tempfile
from io import BytesIO
@ -14,6 +15,7 @@ from base64 import b32decode, b32encode
from twisted.internet import reactor
from twisted.python import log as twlog
from twisted.application import service
from twisted.python.failure import Failure
from foolscap.api import Tub, app_versions
import foolscap.logging.log
from allmydata import get_package_versions, get_package_versions_string
@ -176,8 +178,8 @@ def read_config(basedir, portnumfile, generated_files=[], _valid_config_sections
parser = ConfigParser.SafeConfigParser()
try:
parser = configutil.get_config(config_fname)
except EnvironmentError:
if os.path.exists(config_fname):
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise
configutil.validate_config(config_fname, parser, _valid_config_sections())
@ -269,9 +271,11 @@ class _Config(object):
fn = os.path.join(self._basedir, name)
try:
fileutil.write(fn, value, mode)
except EnvironmentError as e:
log.msg("Unable to write config file '{}'".format(fn))
log.err(e)
except EnvironmentError:
log.err(
Failure(),
"Unable to write config file '{}'".format(fn),
)
def get_config(self, section, option, default=_None, boolean=False):
try:
@ -302,7 +306,9 @@ class _Config(object):
fn = os.path.join(self._basedir, name)
try:
return fileutil.read(fn).strip()
except EnvironmentError:
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
if not required:
return None
raise
@ -322,9 +328,9 @@ class _Config(object):
privname = os.path.join(self._basedir, "private", name)
try:
value = fileutil.read(privname)
except EnvironmentError:
if os.path.exists(privname):
raise
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
if default is _None:
raise MissingConfigEntry("The required configuration file %s is missing."
% (quote_output(privname),))
@ -353,9 +359,9 @@ class _Config(object):
privname = os.path.join(self._basedir, "private", name)
try:
return fileutil.read(privname).strip()
except EnvironmentError:
if os.path.exists(privname):
raise
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
if default is _None:
raise MissingConfigEntry("The required configuration file %s is missing."
% (quote_output(privname),))