URL: https://github.com/freeipa/freeipa/pull/115
Author: tomaskrizek
 Title: #115: Don't show traceback when ipa config file is not an absolute path
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/115/head:pr115
git checkout pr115
From d625b8071a828e283cad863958acc832b9a33da9 Mon Sep 17 00:00:00 2001
From: Tomas Krizek <tkri...@redhat.com>
Date: Tue, 27 Sep 2016 17:23:17 +0200
Subject: [PATCH 1/2] ipa: allow relative paths for config file

Remove unnecessary check for absolute file paths for config file.

https://fedorahosted.org/freeipa/ticket/6114
---
 ipalib/config.py | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)

diff --git a/ipalib/config.py b/ipalib/config.py
index eb6c3ae..a273e3d 100644
--- a/ipalib/config.py
+++ b/ipalib/config.py
@@ -352,23 +352,10 @@ def _merge_from_file(self, config_file):
         containing first the number of variables that were actually set, and
         second the total number of variables found in ``config_file``.
 
-        This method will raise a ``ValueError`` if ``config_file`` is not an
-        absolute path.  For example:
-
-        >>> env = Env()
-        >>> env._merge_from_file('my/config.conf')
-        Traceback (most recent call last):
-          ...
-        ValueError: config_file must be an absolute path; got 'my/config.conf'
-
         Also see `Env._merge()`.
 
-        :param config_file: Absolute path of the configuration file to load.
+        :param config_file: Path of the configuration file to load.
         """
-        if path.abspath(config_file) != config_file:
-            raise ValueError(
-                'config_file must be an absolute path; got %r' % config_file
-            )
         if not path.isfile(config_file):
             return
         parser = RawConfigParser()

From 3e45d56bb71318aa074024ddbd1525135cd22b1f Mon Sep 17 00:00:00 2001
From: Tomas Krizek <tkri...@redhat.com>
Date: Tue, 27 Sep 2016 17:23:38 +0200
Subject: [PATCH 2/2] ipa: check if provided config file exists

Add a parser check to verify config file supplied to the ipa
command exists. Previously, invalid file paths would not results
in any error and would just silently proceed with default config.

https://fedorahosted.org/freeipa/ticket/6114
---
 ipalib/plugable.py | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index af35f5b..76fb9fd 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -44,6 +44,7 @@
 from ipalib.util import classproperty
 from ipalib.base import ReadOnly, lock, islocked
 from ipalib.constants import DEFAULT_CONFIG
+from ipapython import ipautil
 from ipapython.ipa_log_manager import (
     log_mgr,
     LOGGING_FORMAT_FILE,
@@ -494,6 +495,13 @@ def build_global_parser(self, parser=None, context=None):
         """
         Add global options to an optparse.OptionParser instance.
         """
+        def config_file_callback(option, opt, value, parser):
+            if not ipautil.file_exists(value):
+                parser.error(
+                    _("%(filename)s: file not found") % dict(filename=value))
+
+            parser.values.conf = value
+
         if parser is None:
             parser = optparse.OptionParser(
                 add_help_option=False,
@@ -517,8 +525,9 @@ def build_global_parser(self, parser=None, context=None):
         parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
             help='Set environment variable KEY to VAL',
         )
-        parser.add_option('-c', dest='conf', metavar='FILE',
-            help='Load configuration from FILE',
+        parser.add_option('-c', dest='conf', metavar='FILE', action='callback',
+            callback=config_file_callback, type='string',
+            help='Load configuration from FILE.',
         )
         parser.add_option('-d', '--debug', action='store_true',
             help='Produce full debuging output',
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to