[jhbuild/wip/conditions: 4/6] Add --conditions= commandline argument



commit 3142b6766ba547dc514d645af9fe952a9fc78387
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Dec 20 17:07:09 2013 -0500

    Add --conditions= commandline argument
    
    This can be used like --conditions=+doc,-wayland in order to modify the
    condition set from the commandline.
    
    The user's jhbuildrc is capable of inspecting conditions set from the
    commandline by way of checking
    
        if 'flag' in conditions:
            ...
    
    but the arguments on the commandline always override what is written in
    jhbuildrc.  This is done by applying the modifiers twice -- once before
    parsing the config and then again, after.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=720839

 jhbuild/config.py |   31 +++++++++++++++++++++++++++++--
 jhbuild/main.py   |    5 ++++-
 tests/tests.py    |    2 +-
 3 files changed, 34 insertions(+), 4 deletions(-)
---
diff --git a/jhbuild/config.py b/jhbuild/config.py
index 5c9a708..6078c26 100644
--- a/jhbuild/config.py
+++ b/jhbuild/config.py
@@ -169,10 +169,20 @@ def get_default_conditions():
     # we will only hit this if someone removed the '' entry above
     raise FatalError('failed to find matching condition set...')
 
+def modify_conditions(conditions, conditions_modifiers):
+    for flag in conditions_modifiers:
+        for mod in flag.split(','):
+            if mod.startswith('+'):
+                conditions.add(mod[1:])
+            elif mod.startswith('-'):
+                conditions.discard(mod[1:])
+            else:
+                raise FatalError(_("Invalid condition set modifier: '%s'.  Must start with '+' or '-'.") % 
mod)
+
 class Config:
     _orig_environ = None
 
-    def __init__(self, filename):
+    def __init__(self, filename, conditions_modifiers):
         self._config = {
             '__file__': _defaults_file,
             'addpath':  addpath,
@@ -247,13 +257,30 @@ class Config:
             self._config['__file__'] = new_config
             self.filename = new_config
 
+        # we might need to redo this process on config reloads, so save these
+        self.saved_conditions_modifiers = conditions_modifiers
+
+        # We handle the conditions flags like so:
+        #   - get the default set of conditions (determined by the OS)
+        #   - modify it with the commandline arguments
+        #   - load the config file so that it can make further modifications
+        #   - modify it with the commandline arguments again
+        #
+        # We apply the commandline argument condition modifiers both before
+        # and after parsing the configuration so that the jhbuildrc has a
+        # chance to inspect the modified set of flags (and conditionally act
+        # on it to set new autogenargs, for example) but also so that the
+        # condition flags given on the commandline will ultimately override
+        # those in jhbuildrc.
         self._config['conditions'] = get_default_conditions()
+        modify_conditions(self._config['conditions'], conditions_modifiers)
         self.load(filename)
+        modify_conditions(self.conditions, conditions_modifiers)
         self.setup_env()
 
     def reload(self):
         os.environ = self._orig_environ.copy()
-        self.__init__(filename=self._config.get('__file__'))
+        self.__init__(filename=self._config.get('__file__'), 
conditions_modifiers=self.saved_conditions_modifiers)
         self.set_from_cmdline_options(options=None)
 
     def include(self, filename):
diff --git a/jhbuild/main.py b/jhbuild/main.py
index e0927cd..76712f7 100644
--- a/jhbuild/main.py
+++ b/jhbuild/main.py
@@ -124,11 +124,14 @@ def main(args):
     parser.add_option('--no-interact', action='store_true',
                       dest='nointeract', default=False,
                       help=_('do not prompt for input'))
+    parser.add_option('--conditions', action='append',
+                      dest='conditions', default=[],
+                      help=_('modify the condition set'))
 
     options, args = parser.parse_args(args)
 
     try:
-        config = jhbuild.config.Config(options.configfile)
+        config = jhbuild.config.Config(options.configfile, options.conditions)
     except FatalError, exc:
         sys.stderr.write('jhbuild: %s\n' % exc.args[0].encode(_encoding, 'replace'))
         sys.exit(1)
diff --git a/tests/tests.py b/tests/tests.py
index ff21742..6718c70 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -110,7 +110,7 @@ class JhbuildConfigTestCase(unittest.TestCase):
 
     def make_config(self):
         temp_dir = self.make_temp_dir()
-        config = TestConfig(None)
+        config = TestConfig(None, [])
         config.checkoutroot = os.path.abspath(os.path.join(temp_dir, 'checkout'))
         config.prefix = os.path.abspath(os.path.join(temp_dir, 'prefix'))
         config.top_builddir = os.path.join(config.prefix, '_jhbuild')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]