Merge pull request #12448 from ansible/close-module-path

Close module file once we're done reading from it
pull/12452/head
Brian Coca 9 years ago
commit d851df3b31

@ -156,49 +156,49 @@ def modify_module(module_path, module_args, task_vars=dict(), strip_comments=Fal
# minifier output) # minifier output)
# * Only split into lines and recombine into strings once # * Only split into lines and recombine into strings once
# * Cache the modified module? If only the args are different and we do # * Cache the modified module? If only the args are different and we do
# that as the last step we could cache sll the work up to that point. # that as the last step we could cache all the work up to that point.
with open(module_path) as f: with open(module_path) as f:
# read in the module source # read in the module source
module_data = f.read() module_data = f.read()
(module_data, module_style) = _find_snippet_imports(module_data, module_path, strip_comments) (module_data, module_style) = _find_snippet_imports(module_data, module_path, strip_comments)
module_args_json = json.dumps(module_args).encode('utf-8') module_args_json = json.dumps(module_args).encode('utf-8')
python_repred_args = repr(module_args_json) python_repred_args = repr(module_args_json)
# these strings should be part of the 'basic' snippet which is required to be included # these strings should be part of the 'basic' snippet which is required to be included
module_data = module_data.replace(REPLACER_VERSION, repr(__version__)) module_data = module_data.replace(REPLACER_VERSION, repr(__version__))
module_data = module_data.replace(REPLACER_COMPLEX, python_repred_args) module_data = module_data.replace(REPLACER_COMPLEX, python_repred_args)
module_data = module_data.replace(REPLACER_WINARGS, module_args_json) module_data = module_data.replace(REPLACER_WINARGS, module_args_json)
module_data = module_data.replace(REPLACER_JSONARGS, module_args_json) module_data = module_data.replace(REPLACER_JSONARGS, module_args_json)
if module_style == 'new': if module_style == 'new':
facility = C.DEFAULT_SYSLOG_FACILITY facility = C.DEFAULT_SYSLOG_FACILITY
if 'ansible_syslog_facility' in task_vars: if 'ansible_syslog_facility' in task_vars:
facility = task_vars['ansible_syslog_facility'] facility = task_vars['ansible_syslog_facility']
module_data = module_data.replace('syslog.LOG_USER', "syslog.%s" % facility) module_data = module_data.replace('syslog.LOG_USER', "syslog.%s" % facility)
lines = module_data.split(b"\n", 1) lines = module_data.split(b"\n", 1)
shebang = None shebang = None
if lines[0].startswith(b"#!"): if lines[0].startswith(b"#!"):
shebang = lines[0].strip() shebang = lines[0].strip()
args = shlex.split(str(shebang[2:])) args = shlex.split(str(shebang[2:]))
interpreter = args[0] interpreter = args[0]
interpreter_config = 'ansible_%s_interpreter' % os.path.basename(interpreter) interpreter_config = 'ansible_%s_interpreter' % os.path.basename(interpreter)
if interpreter_config in task_vars: if interpreter_config in task_vars:
interpreter = to_bytes(task_vars[interpreter_config], errors='strict') interpreter = to_bytes(task_vars[interpreter_config], errors='strict')
lines[0] = shebang = b"#!{0} {1}".format(interpreter, b" ".join(args[1:])) lines[0] = shebang = b"#!{0} {1}".format(interpreter, b" ".join(args[1:]))
if os.path.basename(interpreter).startswith('python'): if os.path.basename(interpreter).startswith('python'):
lines.insert(1, ENCODING_STRING) lines.insert(1, ENCODING_STRING)
else: else:
# No shebang, assume a binary module? # No shebang, assume a binary module?
pass pass
module_data = b"\n".join(lines) module_data = b"\n".join(lines)
return (module_data, module_style, shebang) return (module_data, module_style, shebang)

Loading…
Cancel
Save