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