Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize alignments #703

Merged
merged 12 commits into from
Jun 27, 2024
18 changes: 14 additions & 4 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time
from pathlib import Path
from subprocess import CompletedProcess
from typing import List, Optional, Union
from typing import List, Optional, Tuple, Union

import zstandard as zstd

Expand Down Expand Up @@ -198,13 +198,22 @@ def run_task(
command_parts[index] = part

# If using a venv, prepend the binary directory to the path so it is used.
python_bin_dir = get_python_bin_dir(requirements)
python_bin_dir, venv_dir = get_python_dirs(requirements)
if python_bin_dir:
final_env = {**final_env, "PATH": f'{python_bin_dir}:{os.environ.get("PATH", "")}'}
if command_parts[0].endswith(".py"):
# This script is relying on a shebang, add the python3 from the executable instead.
command_parts.insert(0, os.path.join(python_bin_dir, "python3"))

# We have to set the path to the C++ lib before the process is started
# https://github.com/Helsinki-NLP/opus-fast-mosestokenizer/issues/6
with open(requirements) as f:
reqs_txt = f.read()
if "opus-fast-mosestokenizer" in reqs_txt:
lib_path = os.path.join(venv_dir, "lib/python3.10/site-packages/mosestokenizer/lib")
print(f"Setting LD_LIBRARY_PATH to {lib_path}")
final_env["LD_LIBRARY_PATH"] = lib_path

print("┌──────────────────────────────────────────────────────────")
print("│ run_task:", " ".join(command_parts))
print("└──────────────────────────────────────────────────────────")
Expand Down Expand Up @@ -455,7 +464,7 @@ def get_path(name: str):
) # fmt: skip


def get_python_bin_dir(requirements: Optional[str]) -> Optional[str]:
def get_python_dirs(requirements: Optional[str]) -> Optional[Tuple[str, str]]:
"""
Creates a virtual environment for each requirements file that a task needs. The virtual
environment is hashed based on the requirements file contents, and the system details. This
Expand Down Expand Up @@ -511,8 +520,9 @@ def get_python_bin_dir(requirements: Optional[str]) -> Optional[str]:
print("Removing the venv due to an error in its creation.")
shutil.rmtree(venv_dir)
raise exception
print(f"Using virtual environment {venv_dir}")

return python_bin_dir
return python_bin_dir, venv_dir


def hash_file(hash: any, path: str):
Expand Down