From ae90b6586fcbd8d74272c3f01342e8308a3e8875 Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 13:36:34 +0100 Subject: [PATCH 1/7] mconf: Do not turn opt.choices into a string It is inconsistent with other option groups. It also breaks print_aligned as it handles lists. --- mesonbuild/mconf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index 45b3d8256..5129c7bca 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -233,8 +233,7 @@ class Conf: # Zero length list or string choices = '' else: - # A non zero length list or string, convert to string - choices = str(opt.choices) + choices = opt.choices optarr.append({'name': key, 'descr': opt.description, 'value': opt.value, From 41b598382333b50a0d58cd9909e1d64daaedb348 Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 13:42:28 +0100 Subject: [PATCH 2/7] Re-implement function print_aligned It is now more readable, more pythonic and fixes #3149 --- mesonbuild/mconf.py | 85 ++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index 5129c7bca..65a47089a 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -46,65 +46,48 @@ class Conf: # Ninja is run. def print_aligned(self, arr): + def make_lower_case(val): + if isinstance(val, bool): + return str(val).lower() + elif isinstance(val, list): + return [make_lower_case(i) for i in val] + else: + return str(val) + if not arr: return - titles = {'name': 'Option', 'descr': 'Description', 'value': 'Current Value', 'choices': 'Possible Values'} - len_name = longest_name = len(titles['name']) - len_descr = longest_descr = len(titles['descr']) - len_value = longest_value = len(titles['value']) - longest_choices = 0 # not printed if we don't get any optional values - # calculate the max length of each - for x in arr: - name = x['name'] - descr = x['descr'] - value = x['value'] if isinstance(x['value'], str) else str(x['value']).lower() - choices = '' - if isinstance(x['choices'], list): - if x['choices']: - x['choices'] = [s if isinstance(s, str) else str(s).lower() for s in x['choices']] - choices = '[%s]' % ', '.join(map(str, x['choices'])) - elif x['choices']: - choices = x['choices'] if isinstance(x['choices'], str) else str(x['choices']).lower() - - longest_name = max(longest_name, len(name)) - longest_descr = max(longest_descr, len(descr)) - longest_value = max(longest_value, len(value)) - longest_choices = max(longest_choices, len(choices)) + titles = {'name': 'Option', 'descr': 'Description', 'value': 'Current Value', 'choices': 'Possible Values'} - # update possible non strings - x['value'] = value - x['choices'] = choices + name_col = [titles['name'], '-' * len(titles['name'])] + value_col = [titles['value'], '-' * len(titles['value'])] + choices_col = [titles['choices'], '-' * len(titles['choices'])] + descr_col = [titles['descr'], '-' * len(titles['descr'])] - # prints header - namepad = ' ' * (longest_name - len_name) - valuepad = ' ' * (longest_value - len_value) - if longest_choices: - len_choices = len(titles['choices']) - longest_choices = max(longest_choices, len_choices) - choicepad = ' ' * (longest_choices - len_choices) - print(' %s%s %s%s %s%s %s' % (titles['name'], namepad, titles['value'], valuepad, titles['choices'], choicepad, titles['descr'])) - print(' %s%s %s%s %s%s %s' % ('-' * len_name, namepad, '-' * len_value, valuepad, '-' * len_choices, choicepad, '-' * len_descr)) - else: - print(' %s%s %s%s %s' % (titles['name'], namepad, titles['value'], valuepad, titles['descr'])) - print(' %s%s %s%s %s' % ('-' * len_name, namepad, '-' * len_value, valuepad, '-' * len_descr)) + choices_found = False + for opt in arr: + name_col.append(opt['name']) + descr_col.append(opt['descr']) + if isinstance(opt['value'], list): + value_col.append('[{0}]'.format(', '.join(make_lower_case(opt['value'])))) + else: + value_col.append(make_lower_case(opt['value'])) + if opt['choices']: + choices_found = True + choices_col.append('[{0}]'.format(', '.join(make_lower_case(opt['choices'])))) + else: + choices_col.append('') - # print values - for i in arr: - name = i['name'] - descr = i['descr'] - value = i['value'] - choices = i['choices'] + col_widths = (max([len(i) for i in name_col], default=0), + max([len(i) for i in value_col], default=0), + max([len(i) for i in choices_col], default=0), + max([len(i) for i in descr_col], default=0)) - namepad = ' ' * (longest_name - len(name)) - valuepad = ' ' * (longest_value - len(value)) - if longest_choices: - choicespad = ' ' * (longest_choices - len(choices)) - f = ' %s%s %s%s %s%s %s' % (name, namepad, value, valuepad, choices, choicespad, descr) + for line in zip(name_col, value_col, choices_col, descr_col): + if choices_found: + print(' {0:{width[0]}} {1:{width[1]}} {2:{width[2]}} {3:{width[3]}}'.format(*line, width=col_widths)) else: - f = ' %s%s %s%s %s' % (name, namepad, value, valuepad, descr) - - print(f) + print(' {0:{width[0]}} {1:{width[1]}} {3:{width[3]}}'.format(*line, width=col_widths)) def set_options(self, options): for o in options: From f805f8e109f5a344b2edf2b62f66bd94aac31fc2 Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 19:22:50 +0100 Subject: [PATCH 3/7] mconf: Replace usage of keys() on dicts with direct call by sorted Calling sorted or list on a dictionary will return the keys. --- mesonbuild/mconf.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index 65a47089a..8a78f3d84 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -140,7 +140,7 @@ class Conf: 'choices': coredata.get_builtin_option_choices(key)}) self.print_aligned(carr) print('') - bekeys = sorted(self.coredata.backend_options.keys()) + bekeys = sorted(self.coredata.backend_options) if not bekeys: print(' No backend options\n') else: @@ -151,7 +151,7 @@ class Conf: self.print_aligned(bearr) print('') print('Base options:') - okeys = sorted(self.coredata.base_options.keys()) + okeys = sorted(self.coredata.base_options) if not okeys: print(' No base options\n') else: @@ -170,7 +170,7 @@ class Conf: print(' ' + lang + '_link_args', str(args)) print('') print('Compiler options:') - okeys = sorted(self.coredata.compiler_options.keys()) + okeys = sorted(self.coredata.compiler_options) if not okeys: print(' No compiler options\n') else: @@ -207,8 +207,7 @@ class Conf: print(' This project does not have any options') else: options = self.coredata.user_options - keys = list(options.keys()) - keys.sort() + keys = sorted(options) optarr = [] for key in keys: opt = options[key] From 84d382f9638e1dd5db5c9d1d62224dcc123e8983 Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 19:26:45 +0100 Subject: [PATCH 4/7] mconf: User newline instead of printing empty string --- mesonbuild/mconf.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index 8a78f3d84..b33261ef3 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -130,8 +130,7 @@ class Conf: print('Core properties:') print(' Source dir', self.build.environment.source_dir) print(' Build dir ', self.build.environment.build_dir) - print('') - print('Core options:') + print('\nCore options:\n') carr = [] for key in ['buildtype', 'warning_level', 'werror', 'strip', 'unity', 'default_library']: carr.append({'name': key, @@ -139,7 +138,6 @@ class Conf: 'value': self.coredata.get_builtin_option(key), 'choices': coredata.get_builtin_option_choices(key)}) self.print_aligned(carr) - print('') bekeys = sorted(self.coredata.backend_options) if not bekeys: print(' No backend options\n') @@ -149,8 +147,7 @@ class Conf: o = self.coredata.backend_options[k] bearr.append({'name': k, 'descr': o.description, 'value': o.value, 'choices': ''}) self.print_aligned(bearr) - print('') - print('Base options:') + print('\nBase options:') okeys = sorted(self.coredata.base_options) if not okeys: print(' No base options\n') @@ -160,16 +157,13 @@ class Conf: o = self.coredata.base_options[k] coarr.append({'name': k, 'descr': o.description, 'value': o.value, 'choices': o.choices}) self.print_aligned(coarr) - print('') - print('Compiler arguments:') + print('\nCompiler arguments:') for (lang, args) in self.coredata.external_args.items(): print(' ' + lang + '_args', str(args)) - print('') - print('Linker args:') + print('\nLinker args:') for (lang, args) in self.coredata.external_link_args.items(): print(' ' + lang + '_link_args', str(args)) - print('') - print('Compiler options:') + print('\nCompiler options:') okeys = sorted(self.coredata.compiler_options) if not okeys: print(' No compiler options\n') @@ -179,8 +173,7 @@ class Conf: o = self.coredata.compiler_options[k] coarr.append({'name': k, 'descr': o.description, 'value': o.value, 'choices': ''}) self.print_aligned(coarr) - print('') - print('Directories:') + print('\nDirectories:') parr = [] for key in ['prefix', 'libdir', @@ -201,8 +194,7 @@ class Conf: 'value': self.coredata.get_builtin_option(key), 'choices': coredata.get_builtin_option_choices(key)}) self.print_aligned(parr) - print('') - print('Project options:') + print('\nProject options:') if not self.coredata.user_options: print(' This project does not have any options') else: @@ -221,8 +213,7 @@ class Conf: 'value': opt.value, 'choices': choices}) self.print_aligned(optarr) - print('') - print('Testing options:') + print('\nTesting options:') tarr = [] for key in ['stdsplit', 'errorlogs']: tarr.append({'name': key, From 9d31c751b622da707fb19c06939d8964ab971cd0 Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 19:35:59 +0100 Subject: [PATCH 5/7] mconf: Do not store dict keys for no reason If a dict is empty it will evaluate to False --- mesonbuild/mconf.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index b33261ef3..823affe14 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -138,22 +138,20 @@ class Conf: 'value': self.coredata.get_builtin_option(key), 'choices': coredata.get_builtin_option_choices(key)}) self.print_aligned(carr) - bekeys = sorted(self.coredata.backend_options) - if not bekeys: + if not self.coredata.backend_options: print(' No backend options\n') else: bearr = [] - for k in bekeys: + for k in sorted(self.coredata.backend_options): o = self.coredata.backend_options[k] bearr.append({'name': k, 'descr': o.description, 'value': o.value, 'choices': ''}) self.print_aligned(bearr) print('\nBase options:') - okeys = sorted(self.coredata.base_options) - if not okeys: + if not self.coredata.base_options: print(' No base options\n') else: coarr = [] - for k in okeys: + for k in sorted(self.coredata.base_options): o = self.coredata.base_options[k] coarr.append({'name': k, 'descr': o.description, 'value': o.value, 'choices': o.choices}) self.print_aligned(coarr) @@ -164,12 +162,11 @@ class Conf: for (lang, args) in self.coredata.external_link_args.items(): print(' ' + lang + '_link_args', str(args)) print('\nCompiler options:') - okeys = sorted(self.coredata.compiler_options) - if not okeys: + if not self.coredata.compiler_options: print(' No compiler options\n') else: coarr = [] - for k in okeys: + for k in self.coredata.compiler_options: o = self.coredata.compiler_options[k] coarr.append({'name': k, 'descr': o.description, 'value': o.value, 'choices': ''}) self.print_aligned(coarr) @@ -198,11 +195,9 @@ class Conf: if not self.coredata.user_options: print(' This project does not have any options') else: - options = self.coredata.user_options - keys = sorted(options) optarr = [] - for key in keys: - opt = options[key] + for key in sorted(self.coredata.user_options): + opt = self.coredata.user_options[key] if (opt.choices is None) or (not opt.choices): # Zero length list or string choices = '' From 9bafde953a18f08e722ffc94c051c51ef397251b Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 19:50:29 +0100 Subject: [PATCH 6/7] mconf: make print_aligned static --- mesonbuild/mconf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index 823affe14..494d1bd44 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -45,7 +45,8 @@ class Conf: # are erased when Meson is executed the next time, i.e. when # Ninja is run. - def print_aligned(self, arr): + @staticmethod + def print_aligned(arr): def make_lower_case(val): if isinstance(val, bool): return str(val).lower() From 3a0ab6a2bc6c70a88673b5f7016daf171759c1da Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Sun, 4 Mar 2018 19:56:53 +0100 Subject: [PATCH 7/7] mconf: Make it pep8 compliant --- mesonbuild/mconf.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index 494d1bd44..db109b77d 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys, os +import os +import sys import argparse -from . import coredata, mesonlib, build +from . import (coredata, mesonlib, build) parser = argparse.ArgumentParser(prog='meson configure') @@ -24,9 +25,11 @@ parser.add_argument('directory', nargs='*') parser.add_argument('--clearcache', action='store_true', default=False, help='Clear cached state (e.g. found dependencies)') + class ConfException(mesonlib.MesonException): pass + class Conf: def __init__(self, build_dir): self.build_dir = build_dir @@ -218,6 +221,7 @@ class Conf: 'choices': coredata.get_builtin_option_choices(key)}) self.print_aligned(tarr) + def run(args): args = mesonlib.expand_arguments(args) if not args: @@ -250,5 +254,6 @@ def run(args): return 1 return 0 + if __name__ == '__main__': sys.exit(run(sys.argv[1:]))