def func(u=None): if u is None: u = "" bytes_in = u.encode(defaults.LAMBDA_ENCODING, errors=defaults.ENCODING_ERRORS) stdout, stderr, return_code = call_script(path, bytes_in) return stdout.decode(defaults.LAMBDA_ENCODING, errors=defaults.ENCODING_ERRORS)
def test(self): with self.sandboxDir() as dir_path: path = os.path.join(dir_path, "test.sh") _create_file(path, SHEBANG_LINE) args = [path] # Will not work until we set the executable bit. # TODO: switch from Exception to OSError. We cannot do this # until we've changed our implementation of reraise() to # preserve the exception type. self.assertRaises(Exception, call_script, args) set_executable_bit(path) stdout_data, stderr_data, return_code = call_script(args)
def _call_script(self, script_name, u): """ Return stdout as a unicode string. """ script_path = self._get_script_path(script_name) base_name = os.path.basename(script_path) new_path = os.path.join(self.temp_dir, base_name) copyfile(script_path, new_path) set_executable_bit(new_path) bytes_in = u.encode(ENCODING_DEFAULT) stdout, stderr, return_code = call_script([new_path], bytes_in) stdout = stdout.decode(ENCODING_DEFAULT) return stdout
def _call_script(args): stdout, stderr, return_code = call_script(args) stdout, stderr = (s.decode(ENCODING_DEFAULT) for s in (stdout, stderr)) return stdout, stderr, return_code