From 169b82261aaeb152a8428811f1bee2e0d879e00a Mon Sep 17 00:00:00 2001 From: emzubair Date: Wed, 19 Aug 2020 19:05:28 +0500 Subject: [PATCH] [BD-18] Optimize paver settings call --- .../util/management/commands/print_setting.py | 15 ++++++++------- pavelib/assets.py | 16 ++++++++++------ pavelib/paver_tests/test_servers.py | 5 ++--- pavelib/utils/envs.py | 15 +++++++++------ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/openedx/core/djangoapps/util/management/commands/print_setting.py b/openedx/core/djangoapps/util/management/commands/print_setting.py index 0938a037e9..523a303874 100644 --- a/openedx/core/djangoapps/util/management/commands/print_setting.py +++ b/openedx/core/djangoapps/util/management/commands/print_setting.py @@ -22,14 +22,15 @@ class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( - 'setting', - help='Specifies the setting to be printed.' + 'settings_to_print', + nargs='+', + help='Specifies the list of settings to be printed.' ) def handle(self, *args, **options): - setting = options.get('setting') + settings_to_print = options.get('settings_to_print') - if not hasattr(settings, setting): - raise CommandError(u'%s not found in settings.' % setting) - - print(getattr(settings, setting)) + for setting in settings_to_print: + if not hasattr(settings, setting): + raise CommandError('%s not found in settings.' % setting) + print(getattr(settings, setting)) diff --git a/pavelib/assets.py b/pavelib/assets.py index 3fa812da19..8d4d4b61b3 100644 --- a/pavelib/assets.py +++ b/pavelib/assets.py @@ -763,9 +763,9 @@ def webpack(options): Run a Webpack build. """ settings = getattr(options, 'settings', Env.DEVSTACK_SETTINGS) - static_root_lms = Env.get_django_setting("STATIC_ROOT", "lms", settings=settings) - static_root_cms = Env.get_django_setting("STATIC_ROOT", "cms", settings=settings) - config_path = Env.get_django_setting("WEBPACK_CONFIG_PATH", "lms", settings=settings) + result = Env.get_django_settings(['STATIC_ROOT', 'WEBPACK_CONFIG_PATH'], "lms", settings=settings) + static_root_lms, config_path = result + static_root_cms, = Env.get_django_settings(["STATIC_ROOT"], "cms", settings=settings) environment = 'NODE_ENV={node_env} STATIC_ROOT_LMS={static_root_lms} STATIC_ROOT_CMS={static_root_cms}'.format( node_env="development" if config_path == 'webpack.dev.config.js' else "production", static_root_lms=static_root_lms, @@ -788,13 +788,17 @@ def execute_webpack_watch(settings=None): # We only want Webpack to re-run on changes to its own entry points, # not all JS files, so we use its own watcher instead of subclassing # from Watchdog like the other watchers do. + + result = Env.get_django_settings(["STATIC_ROOT", "WEBPACK_CONFIG_PATH"], "lms", settings=settings) + static_root_lms, config_path = result + static_root_cms, = Env.get_django_settings(["STATIC_ROOT"], "cms", settings=settings) run_background_process( 'STATIC_ROOT_LMS={static_root_lms} STATIC_ROOT_CMS={static_root_cms} $(npm bin)/webpack {options}'.format( options='--watch --config={config_path}'.format( - config_path=Env.get_django_setting("WEBPACK_CONFIG_PATH", "lms", settings=settings) + config_path=config_path ), - static_root_lms=Env.get_django_setting("STATIC_ROOT", "lms", settings=settings), - static_root_cms=Env.get_django_setting("STATIC_ROOT", "cms", settings=settings), + static_root_lms=static_root_lms, + static_root_cms=static_root_cms, ) ) diff --git a/pavelib/paver_tests/test_servers.py b/pavelib/paver_tests/test_servers.py index fc3e3f8a68..3bbff733e3 100644 --- a/pavelib/paver_tests/test_servers.py +++ b/pavelib/paver_tests/test_servers.py @@ -44,9 +44,8 @@ EXPECTED_INDEX_COURSE_COMMAND = ( "python manage.py {system} --settings={settings} reindex_course --setup" ) EXPECTED_PRINT_SETTINGS_COMMAND = [ - "python manage.py lms --settings={settings} print_setting STATIC_ROOT 2>{log_file}", - "python manage.py cms --settings={settings} print_setting STATIC_ROOT 2>{log_file}", - "python manage.py lms --settings={settings} print_setting WEBPACK_CONFIG_PATH 2>{log_file}" + "python manage.py lms --settings={settings} print_setting STATIC_ROOT WEBPACK_CONFIG_PATH 2>{log_file}", + "python manage.py cms --settings={settings} print_setting STATIC_ROOT 2>{log_file}" ] EXPECTED_WEBPACK_COMMAND = ( "NODE_ENV={node_env} STATIC_ROOT_LMS={static_root_lms} STATIC_ROOT_CMS={static_root_cms} " diff --git a/pavelib/utils/envs.py b/pavelib/utils/envs.py index 56e78aa844..5baae419d6 100644 --- a/pavelib/utils/envs.py +++ b/pavelib/utils/envs.py @@ -236,10 +236,10 @@ class Env: SERVICE_VARIANT = 'lms' @classmethod - def get_django_setting(cls, django_setting, system, settings=None): + def get_django_settings(cls, django_settings, system, settings=None): """ Interrogate Django environment for specific settings values - :param django_setting: the django setting to get + :param django_settings: list of django settings values to get :param system: the django app to use when asking for the setting (lms | cms) :param settings: the settings file to use when asking for the value :return: unicode value of the django setting @@ -249,21 +249,24 @@ class Env: log_dir = os.path.dirname(cls.PRINT_SETTINGS_LOG_FILE) if not os.path.exists(log_dir): os.makedirs(log_dir) + settings_length = len(django_settings) + django_settings = ' '.join(django_settings) # parse_known_args makes a list again try: value = sh( django_cmd( system, settings, - "print_setting {django_setting} 2>{log_file}".format( - django_setting=django_setting, + "print_setting {django_settings} 2>{log_file}".format( + django_settings=django_settings, log_file=cls.PRINT_SETTINGS_LOG_FILE ) ), capture=True ) - return str(value).strip() + # else for cases where values are not found & sh returns one None value + return tuple(str(value).splitlines()) if value else tuple(None for _ in range(settings_length)) except BuildFailure: - print("Unable to print the value of the {} setting:".format(django_setting)) + print("Unable to print the value of the {} setting:".format(django_settings)) with open(cls.PRINT_SETTINGS_LOG_FILE, 'r') as f: print(f.read()) sys.exit(1)