




"""simple docstring"""




# This code is adapted from OpenAI's release
# https://github.com/openai/human-eval/blob/master/human_eval/execution.py

import contextlib
import faulthandler
import io
import multiprocessing
import os
import platform
import signal
import tempfile







def      _a				(						SCREAMING_SNAKE_CASE_							:    Dict				,				SCREAMING_SNAKE_CASE_							:    int				,				SCREAMING_SNAKE_CASE_							:    Optional[int]				,				SCREAMING_SNAKE_CASE_							:    Tuple					):
					__lowerCAmelCase           =	multiprocessing.Manager()
					__lowerCAmelCase           =	manager.list()

					__lowerCAmelCase           =	multiprocessing.Process(target=snake_case__				,				args=(check_program, result, timeout)					)
					p.start()
					p.join(timeout=timeout + 1					)
					if p.is_alive():
										p.kill()

					if not result:
										result.append("timed out"					)

					return {
					    "task_id": task_id,
					    "passed": result[0] == "passed",
					    "result": result[0],
					    "completion_id": completion_id,
					}







def      _a				(						SCREAMING_SNAKE_CASE_							:    Any				,				SCREAMING_SNAKE_CASE_							:    Optional[int]				,				SCREAMING_SNAKE_CASE_							:    Union[str, Any]					):
					with create_tempdir():
										# These system calls are needed when cleaning up tempdir.
										import os
										import shutil

										__lowerCAmelCase           =	shutil.rmtree
										__lowerCAmelCase           =	os.rmdir
										__lowerCAmelCase           =	os.chdir

										# Disable functionalities that can make destructive changes to the test.
										reliability_guard()

										# Run program.
										try:
															__lowerCAmelCase           =	{}
															with swallow_io():
																				with time_limit(snake_case__					):
																									exec(snake_case__				,				snake_case__					)
															result.append("passed"					)
										except TimeoutException:
															result.append("timed out"					)
										except BaseException as e:
															result.append(F"""failed: {e}"""					)

										# Needed for cleaning up.
										__lowerCAmelCase           =	rmtree
										__lowerCAmelCase           =	rmdir
										__lowerCAmelCase           =	chdir







@contextlib.contextmanager
def      _a				(						SCREAMING_SNAKE_CASE_							:    Optional[Any]					):
					def signal_handler(SCREAMING_SNAKE_CASE_							:    Tuple				,				SCREAMING_SNAKE_CASE_							:    Optional[int]					):
										raise TimeoutException("Timed out!"					)

					signal.setitimer(signal.ITIMER_REAL				,				snake_case__					)
					signal.signal(signal.SIGALRM				,				snake_case__					)
					try:
										yield
					finally:
										signal.setitimer(signal.ITIMER_REAL				,				0					)







@contextlib.contextmanager
def      _a				(						):
					__lowerCAmelCase           =	WriteOnlyStringIO()
					with contextlib.redirect_stdout(snake_case__					):
										with contextlib.redirect_stderr(snake_case__					):
															with redirect_stdin(snake_case__					):
																				yield







@contextlib.contextmanager
def      _a				(						):
					with tempfile.TemporaryDirectory() as dirname:
										with chdir(snake_case__					):
															yield dirname
class       a__   ( __lowerCAmelCase				):
						pass






class       a__   ( io.StringIO				):






						def     __SCREAMING_SNAKE_CASE(       self		,      *_A		,      **_A      ):




											"""simple docstring"""



											raise OSError






						def     __SCREAMING_SNAKE_CASE(       self		,      *_A		,      **_A      ):




											"""simple docstring"""



											raise OSError






						def     __SCREAMING_SNAKE_CASE(       self		,      *_A		,      **_A      ):




											"""simple docstring"""



											raise OSError






						def     __SCREAMING_SNAKE_CASE(       self		,      *_A		,      **_A      ):




											"""simple docstring"""



											return False






class       a__   ( contextlib._RedirectStream				):  # type: ignore
						_a							:   Tuple =   '''stdin'''







@contextlib.contextmanager
def      _a				(						SCREAMING_SNAKE_CASE_							:    str					):
					if root == ".":
										yield
										return
					__lowerCAmelCase           =	os.getcwd()
					os.chdir(snake_case__					)
					try:
										yield
					except BaseException as exc:
										raise exc
					finally:
										os.chdir(snake_case__					)







def      _a				(						SCREAMING_SNAKE_CASE_							:    List[str]=None					):

					if maximum_memory_bytes is not None:
										import resource

										resource.setrlimit(resource.RLIMIT_AS				,				(maximum_memory_bytes, maximum_memory_bytes)					)
										resource.setrlimit(resource.RLIMIT_DATA				,				(maximum_memory_bytes, maximum_memory_bytes)					)
										if not platform.uname().system == "Darwin":
															resource.setrlimit(resource.RLIMIT_STACK				,				(maximum_memory_bytes, maximum_memory_bytes)					)

					faulthandler.disable()

					import builtins

					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None

					import os

					__lowerCAmelCase           =	"1"

					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None

					import shutil

					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None

					import subprocess

					__lowerCAmelCase           =	None  # type: ignore

					__lowerCAmelCase           =	None

					import sys

					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None
					__lowerCAmelCase           =	None










from pathlib import Path

import fire
from tqdm import tqdm







def      _a				(						SCREAMING_SNAKE_CASE_							:    Dict="ro"				,				SCREAMING_SNAKE_CASE_							:    Union[str, Any]="en"				,				SCREAMING_SNAKE_CASE_							:    Optional[Any]="wmt16"				,				SCREAMING_SNAKE_CASE_							:    List[str]=None					):
					try:
										import datasets
					except (ModuleNotFoundError, ImportError):
										raise ImportError("run pip install datasets"					)
					__lowerCAmelCase           =	F"""{src_lang}-{tgt_lang}"""
					print(F"""Converting {dataset}-{pair}"""					)
					__lowerCAmelCase           =	datasets.load_dataset(SCREAMING_SNAKE_CASE_				,				SCREAMING_SNAKE_CASE_					)
					if save_dir is None:
										__lowerCAmelCase           =	F"""{dataset}-{pair}"""
					__lowerCAmelCase           =	Path(SCREAMING_SNAKE_CASE_					)
					save_dir.mkdir(exist_ok=SCREAMING_SNAKE_CASE_					)

					for split in ds.keys():
										print(F"""Splitting {split} with {ds[split].num_rows} records"""					)

										# to save to val.source, val.target like summary datasets
										__lowerCAmelCase           =	"val" if split == "validation" else split
										__lowerCAmelCase           =	save_dir.joinpath(F"""{fn}.source"""					)
										__lowerCAmelCase           =	save_dir.joinpath(F"""{fn}.target"""					)
										__lowerCAmelCase           =	src_path.open("w+"					)
										__lowerCAmelCase           =	tgt_path.open("w+"					)

										# reader is the bottleneck so writing one record at a time doesn't slow things down
										for x in tqdm(ds[split]					):
															__lowerCAmelCase           =	x["translation"]
															src_fp.write(ex[src_lang] + "\n"					)
															tgt_fp.write(ex[tgt_lang] + "\n"					)

					print(F"""Saved {dataset} dataset to {save_dir}"""					)


if __name__ == "__main__":
					fire.Fire(download_wmt_dataset)




