Skip to content

Commit 5164ed3

Browse files
committed
working around Jython 2.2.1 bug when env var values have a newline
1 parent dd1b0df commit 5164ed3

File tree

12 files changed

+103
-59
lines changed

12 files changed

+103
-59
lines changed

core/src/main/python/compare_model.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
# Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2-
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3-
#
4-
# ------------
5-
# Description:
6-
# ------------
7-
#
8-
# This code compares python dictionaries. It is used to compare the new vs the old version.
9-
# If the flag -output_dir <directory> is provided, the differences is written as yaml and json
10-
# diffed_model.json diffed_model.yaml in the directory; the tool output is written as diffed_output_rc.
11-
#
12-
# If the flag is not provided then all output is written to the standard out.
13-
#
1+
"""
2+
Copyright (c) 2020, 2023, Oracle and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
5+
------------
6+
Description:
7+
------------
8+
9+
This code compares python dictionaries. It is used to compare the new vs the old version.
10+
If the flag -output_dir <directory> is provided, the differences is written as yaml and json
11+
diffed_model.json diffed_model.yaml in the directory; the tool output is written as diffed_output_rc.
12+
13+
If the flag is not provided then all output is written to the standard out.
14+
15+
"""
1416
import os
1517
import sets
1618
import sys
@@ -39,6 +41,7 @@
3941
from wlsdeploy.tool.compare.model_comparer import ModelComparer
4042
from wlsdeploy.tool.validate.validator import Validator
4143
from wlsdeploy.util import cla_helper
44+
from wlsdeploy.util import env_helper
4245
from wlsdeploy.util import tool_main
4346
from wlsdeploy.util import validate_configuration
4447
from wlsdeploy.util import variables
@@ -235,7 +238,7 @@ def debug(format_string, *arguments):
235238
:param format_string: python formatted string
236239
:param arguments: arguments for the formatted string
237240
"""
238-
if os.environ.has_key('DEBUG_COMPARE_MODEL_TOOL'):
241+
if env_helper.has_env('DEBUG_COMPARE_MODEL_TOOL'):
239242
print(format_string % arguments)
240243
else:
241244
__logger.finest(format_string, arguments)

core/src/main/python/create.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Copyright (c) 2017, 2023, Oracle Corporation and/or its affiliates.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
The main module for the WLSDeploy tool to create empty domains.
66
"""
@@ -41,6 +41,7 @@
4141
from wlsdeploy.tool.util import wlst_helper
4242
from wlsdeploy.tool.validate.content_validator import ContentValidator
4343
from wlsdeploy.util import cla_helper
44+
from wlsdeploy.util import env_helper
4445
from wlsdeploy.util import getcreds
4546
from wlsdeploy.util import tool_main
4647
from wlsdeploy.util.cla_utils import CommandLineArgUtil
@@ -128,7 +129,7 @@ def __process_java_home_arg(optional_arg_map):
128129
_method_name = '__process_java_home_arg'
129130

130131
if CommandLineArgUtil.JAVA_HOME_SWITCH not in optional_arg_map:
131-
java_home_name = os.environ.get('JAVA_HOME')
132+
java_home_name = env_helper.getenv('JAVA_HOME')
132133
try:
133134
java_home = FileUtils.validateExistingDirectory(java_home_name)
134135
except IllegalArgumentException, iae:

core/src/main/python/discover.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Copyright (c) 2017, 2023, Oracle Corporation and/or its affiliates.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
The entry point for the discoverDomain tool.
66
"""
@@ -49,6 +49,7 @@
4949
from wlsdeploy.tool.validate.validator import Validator
5050
from wlsdeploy.util import cla_helper
5151
from wlsdeploy.util import cla_utils
52+
from wlsdeploy.util import env_helper
5253
from wlsdeploy.util import model_translator
5354
from wlsdeploy.util import path_utils
5455
from wlsdeploy.util import tool_main
@@ -200,7 +201,7 @@ def __process_java_home(optional_arg_map):
200201
if CommandLineArgUtil.JAVA_HOME_SWITCH in optional_arg_map:
201202
java_home_name = optional_arg_map[CommandLineArgUtil.JAVA_HOME_SWITCH]
202203
else:
203-
java_home_name = os.environ.get('JAVA_HOME')
204+
java_home_name = env_helper.getenv('JAVA_HOME')
204205

205206
try:
206207
FileUtils.validateExistingDirectory(java_home_name)
@@ -545,14 +546,14 @@ def __check_and_customize_model(model, model_context, aliases, credential_inject
545546
def __generate_remote_report_json(model_context):
546547
_method_name = '__remote_report'
547548

548-
if not model_context.is_remote() or not os.environ.has_key(_store_result_environment_variable):
549+
if not model_context.is_remote() or not env_helper.has_env(_store_result_environment_variable):
549550
return
550551

551552
# write JSON output if the __WLSDEPLOY_STORE_RESULT__ environment variable is set.
552553
# write to the file before the stdout so any logging messages come first.
553554
remote_map = discoverer.remote_dict
554555

555-
store_path = os.environ.get(_store_result_environment_variable)
556+
store_path = env_helper.getenv(_store_result_environment_variable)
556557
__logger.info('WLSDPLY-06034', store_path, class_name=_class_name, method_name=_method_name)
557558
missing_archive_entries = []
558559
for key in remote_map:

core/src/main/python/model_help.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
Copyright (c) 2020, 2022, Oracle Corporation and/or its affiliates.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
2+
Copyright (c) 2020, 2023, Oracle Corporation and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
The entry point for the modelHelp tool.
66
"""
@@ -16,6 +16,7 @@
1616
from wlsdeploy.tool.modelhelp.model_help_printer import ModelHelpPrinter
1717
from wlsdeploy.tool.modelhelp.model_help_utils import ControlOptions
1818
from wlsdeploy.tool.util import model_context_helper
19+
from wlsdeploy.util import env_helper
1920
from wlsdeploy.util import model
2021
from wlsdeploy.util import tool_main
2122
from wlsdeploy.util.cla_utils import CommandLineArgUtil
@@ -348,7 +349,7 @@ def interactive_help_main_loop(aliases, model_path, printer):
348349
history = ['top']
349350

350351
# optionally get input from file instead of stdin (undocumented feature)
351-
input_file_name = os.environ.get('WDT_INTERACTIVE_MODE_INPUT_FILE')
352+
input_file_name = env_helper.getenv('WDT_INTERACTIVE_MODE_INPUT_FILE')
352353
input_file = None
353354
if input_file_name:
354355
input_file = open(input_file_name, "r")

core/src/main/python/wlsdeploy/util/cla_helper.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Copyright (c) 2019, 2023, Oracle Corporation and/or its affiliates.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
Utility CLS methods shared by multiple tools.
66
"""
@@ -10,6 +10,7 @@
1010
from java.io import IOException
1111
from java.lang import IllegalArgumentException
1212
from java.lang import String
13+
1314
from oracle.weblogic.deploy.util import FileUtils
1415
from oracle.weblogic.deploy.util import TranslateException
1516
from oracle.weblogic.deploy.util import VariableException
@@ -26,6 +27,7 @@
2627
from wlsdeploy.util import model_helper
2728
from wlsdeploy.util import model_translator
2829
from wlsdeploy.util import path_utils
30+
from wlsdeploy.util import env_helper
2931

3032
from wlsdeploy.util import variables
3133
from wlsdeploy.util.cla_utils import CommandLineArgUtil
@@ -403,7 +405,7 @@ def persist_model(model_context, model_dictionary):
403405
_method_name = 'persist_model'
404406

405407
if check_persist_model():
406-
store_value = os.environ.get(_store_environment_variable)
408+
store_value = env_helper.getenv(_store_environment_variable)
407409

408410
if os.path.isabs(store_value):
409411
file_path = store_value
@@ -428,4 +430,4 @@ def check_persist_model():
428430
Determine if the model should be persisted, based on the environment variable __WLSDEPLOY_STORE_MODEL__
429431
:return: True if the model should be persisted
430432
"""
431-
return os.environ.has_key(_store_environment_variable)
433+
return env_helper.has_env(_store_environment_variable)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Copyright (c) 2023, Oracle Corporation and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
5+
Helper class to work around bugs in Jython 2.2.1 where environment variables with
6+
newline characters mess up the os.environ data structure.
7+
"""
8+
import os
9+
10+
from java.lang import System
11+
from oracle.weblogic.deploy.util import StringUtils
12+
13+
def getenv(env_var_name, default_value=None):
14+
if StringUtils.isEmpty(env_var_name):
15+
return default_value
16+
elif env_var_name in os.environ:
17+
return os.environ[env_var_name]
18+
else:
19+
retval = System.getenv(env_var_name)
20+
if retval is None:
21+
return default_value
22+
return retval
23+
24+
def has_env(env_var_name):
25+
if StringUtils.isEmpty(env_var_name):
26+
return False
27+
elif env_var_name in os.environ:
28+
return True
29+
else:
30+
return System.getenv(env_var_name) is not None

core/src/main/python/wlsdeploy/util/path_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""
2-
Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates. All rights reserved.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
2+
Copyright (c) 2017, 2023, Oracle Corporation and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import os
66

77
import java.io.File as JFile
88

99
import oracle.weblogic.deploy.util.StringUtils as JStringUtils
1010
from wlsdeploy.logging.platform_logger import PlatformLogger
11+
from wlsdeploy.util import env_helper
1112

1213
WLSDEPLOY_HOME_VARIABLE = 'WLSDEPLOY_HOME'
1314
CUSTOM_CONFIG_VARIABLE = 'WDT_CUSTOM_CONFIG'
@@ -139,7 +140,7 @@ def is_jar_file(file_path):
139140

140141

141142
def get_wls_deploy_path():
142-
return os.environ.get(WLSDEPLOY_HOME_VARIABLE, None)
143+
return env_helper.getenv(WLSDEPLOY_HOME_VARIABLE, None)
143144

144145

145146
def find_config_path(file_path):
@@ -152,12 +153,12 @@ def find_config_path(file_path):
152153
"""
153154
_method_name = 'find_config_path'
154155

155-
custom_config_dir = os.environ.get(CUSTOM_CONFIG_VARIABLE, None)
156+
custom_config_dir = env_helper.getenv(CUSTOM_CONFIG_VARIABLE)
156157
if custom_config_dir is not None:
157158
custom_file_path = os.path.join(custom_config_dir, file_path)
158159
if os.path.isfile(custom_file_path):
159160
__logger.info('WLSDPLY-01725', custom_file_path, class_name=_class_name, method_name=_method_name)
160161
return custom_file_path
161162

162-
wls_deploy_path = os.environ.get(WLSDEPLOY_HOME_VARIABLE, '')
163+
wls_deploy_path = env_helper.getenv(WLSDEPLOY_HOME_VARIABLE, '')
163164
return os.path.join(wls_deploy_path, 'lib', file_path)

core/src/main/python/wlsdeploy/util/variables.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
2+
Copyright (c) 2017, 2023, Oracle Corporation and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import os
66
import re
@@ -22,6 +22,7 @@
2222
from wlsdeploy.exception import exception_helper
2323
from wlsdeploy.logging import platform_logger
2424
from wlsdeploy.util import dictionary_utils
25+
from wlsdeploy.util import env_helper
2526
from wlsdeploy.util.cla_utils import CommandLineArgUtil
2627

2728
_class_name = "variables"
@@ -284,16 +285,16 @@ def _substitute(text, variables, model_context, error_info, attribute_name=None)
284285
#
285286
env_var_name = str_helper.to_string(key)
286287
is_windows = System.getProperty('os.name').startswith('Windows')
287-
if is_windows and env_var_name not in os.environ and env_var_name.upper() in os.environ:
288+
if is_windows and not env_helper.has_env(env_var_name) and env_var_name.has_env(env_var_name.upper()):
288289
env_var_name = env_var_name.upper()
289290

290-
if env_var_name not in os.environ:
291+
if not env_helper.has_env(env_var_name):
291292
allow_unresolved = validation_config.allow_unresolved_environment_tokens()
292293
_report_token_issue('WLSDPLY-01737', method_name, allow_unresolved, key)
293294
_increment_error_count(error_info, allow_unresolved)
294295
problem_found = True
295296
continue
296-
value = os.environ.get(env_var_name)
297+
value = env_helper.getenv(env_var_name)
297298
text = text.replace(token, value)
298299

299300
# check secret variables before @@FILE:/dir/@@SECRET:name:key@@.txt@@
@@ -420,7 +421,7 @@ def _init_secret_token_map(model_context):
420421

421422
# add name/key pairs for files in sub-directories of directories in WDT_MODEL_SECRETS_DIRS.
422423

423-
locations = os.environ.get(str_helper.to_string(_secret_dirs_variable), None)
424+
locations = env_helper.getenv(str_helper.to_string(_secret_dirs_variable))
424425
if locations is not None:
425426
for secret_dir in locations.split(","):
426427
if not os.path.isdir(secret_dir):
@@ -437,7 +438,7 @@ def _init_secret_token_map(model_context):
437438
# add name/key pairs for files in directories assigned in WDT_MODEL_SECRETS_NAME_DIR_PAIRS.
438439
# these pairs will override if they were previously added as sub-directory pairs.
439440

440-
dir_pairs_text = os.environ.get(str_helper.to_string(_secret_dir_pairs_variable), None)
441+
dir_pairs_text = env_helper.getenv(str_helper.to_string(_secret_dir_pairs_variable))
441442
if dir_pairs_text is not None:
442443
dir_pairs = dir_pairs_text.split(',')
443444
for dir_pair in dir_pairs:
@@ -533,10 +534,10 @@ def substitute_key(text, variables):
533534
matches = _environment_pattern.findall(text)
534535
for token, key in matches:
535536
# log, or throw an exception if key is not found.
536-
if str_helper.to_string(key) not in os.environ:
537+
if not env_helper.has_env(str_helper.to_string(key)):
537538
continue
538539

539-
value = os.environ.get(str_helper.to_string(key))
540+
value = env_helper.getenv(str_helper.to_string(key))
540541
text = text.replace(token, value)
541542

542543
return text

core/src/test/python/base_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Copyright (c) 2021, 2023, Oracle and/or its affiliates.
3-
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import os
66
import shutil
@@ -9,6 +9,7 @@
99
from java.util.logging import Level
1010

1111
from wlsdeploy.logging.platform_logger import PlatformLogger
12+
from wlsdeploy.util import env_helper
1213

1314

1415
class BaseTestCase(unittest.TestCase):
@@ -24,7 +25,7 @@ def __init__(self, *args):
2425
self.TEST_CONFIG_DIR = os.path.join(self.TEST_OUTPUT_DIR, 'wdt-config')
2526
self.log_levels = {}
2627

27-
self.original_config_dir = os.environ.get('WDT_CUSTOM_CONFIG', self.TEST_CONFIG_DIR)
28+
self.original_config_dir = env_helper.getenv('WDT_CUSTOM_CONFIG', self.TEST_CONFIG_DIR)
2829
# config items may need to be copied from here
2930
self.INSTALLER_LIB_DIR = os.path.abspath(os.path.join(self.TEST_CLASSES_DIR, '../../../installer/src/main/lib'))
3031

0 commit comments

Comments
 (0)