From c8d0dd9b60b4cf0862f74b52a4c639478f39b810 Mon Sep 17 00:00:00 2001 From: adrianciu Date: Fri, 29 Nov 2024 10:55:36 +0100 Subject: [PATCH] TVB-2113: add bin path to modify distribution python path --- tvb_build/build_from_conda.py | 31 ++++++++++++++++++++++++++++--- tvb_build/docker/Dockerfile-build | 2 +- tvb_build/setup_mac.py | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/tvb_build/build_from_conda.py b/tvb_build/build_from_conda.py index 573862f5b6..7e4a84e1cb 100644 --- a/tvb_build/build_from_conda.py +++ b/tvb_build/build_from_conda.py @@ -35,7 +35,7 @@ class Config: - def __init__(self, platform_name, anaconda_env_path, site_packages_suffix, commands_map, command_factory): + def __init__(self, platform_name, anaconda_env_path, site_packages_suffix, commands_map, command_factory, bin_path=None): # System paths: self.anaconda_env_path = anaconda_env_path @@ -52,6 +52,7 @@ def __init__(self, platform_name, anaconda_env_path, site_packages_suffix, comma self.target_3rd_licences_folder = join(self.target_root, 'THIRD_PARTY_LICENSES') self.target_site_packages = join(self.target_library_root, site_packages_suffix) self.easy_install_pth = join(self.target_site_packages, "easy-install.pth") + self.bin_folder = join(self.target_site_packages, "easy-install.pth") self.to_read_licenses_from = [os.path.dirname(self.target_library_root)] # TVB sources and specify where to copy them in distribution @@ -69,6 +70,10 @@ def __init__(self, platform_name, anaconda_env_path, site_packages_suffix, comma _artifact_glob = "TVB_" + platform_name + "_*.zip" self.artifact_glob = join(self.build_folder, _artifact_glob) # this is used to match old artifacts self.artifact_pth = join(self.build_folder, self.artifact_name) + if bin_path: + self.bin_path = join(self.target_library_root, bin_path) + else: + self.bin_path = None @staticmethod def win64(): @@ -116,11 +121,11 @@ def linux64(): 'bin/tvb_stop.sh': 'bash ./distribution.sh stop', 'bin/jupyter_notebook.sh': set_path + 'cd ../bin\n../tvb_data/bin/python -m jupyterlab ../demo_scripts', 'demo_scripts/jupyter_notebook.sh': set_path + 'cd ../demo_scripts\n../tvb_data/bin/python -m jupyterlab', - 'bin/activate_tvb_env.sh': '# Conda must be installed before running this script \n conda activate ../tvb_data \n $SHELL' + 'bin/activate_tvb_env.sh': '# Conda must be installed before running this script; \n # Run this script with source. \n conda activate ../tvb_data \n' } return Config("Linux", "/opt/conda/envs/tvb-run", join("lib", Environment.PYTHON_FOLDER, "site-packages"), - commands_map, _create_unix_command) + commands_map, _create_unix_command, "bin") def _log(indent, msg): @@ -203,7 +208,24 @@ def _create_windows_script(target_file, command): os.chmod(target_file, 0o755) +def _replace_first_line_if_pattern(pathname: str, pattern: str, replacement: str): + """ + Replaces the first line of a file with a given string if the pathname contains a specific pattern. + """ + for filename in os.listdir(pathname): + file_path = os.path.join(pathname, filename) + if pattern in file_path: + with open(file_path, 'r') as file: + lines = file.readlines() + if lines: + lines[0] = replacement + '\n' + with open(file_path, 'w') as file: + file.writelines(lines) + _log(1, f"First line of {file_path} replaced with: {replacement}") + + def _modify_pth(pth_name): + # Log if one of the files pip, pip3 or pip3.11 are missing, but do not stop the execution if it is missing """ Replace tvb links with paths """ @@ -233,6 +255,7 @@ def _modify_pth(pth_name): fw.write(new_content) + def _fix_jupyter_kernel(tvb_data_folder, is_windows): kernel_json = os.path.join(tvb_data_folder, "share", "jupyter", "kernels", "python3", "kernel.json") if os.path.exists(kernel_json): @@ -280,6 +303,8 @@ def prepare_anaconda_dist(config): _log(1, "Modifying PTH " + config.easy_install_pth) _modify_pth(config.easy_install_pth) + if config.bin_path: + _replace_first_line_if_pattern(config.bin_path, 'bin/pip', '#!../tvb_data/bin/python') _fix_jupyter_kernel(config.target_library_root, config.platform_name == "Windows") _log(1, "Creating command files:") diff --git a/tvb_build/docker/Dockerfile-build b/tvb_build/docker/Dockerfile-build index ed98fe37e7..b4acca0a5d 100644 --- a/tvb_build/docker/Dockerfile-build +++ b/tvb_build/docker/Dockerfile-build @@ -18,7 +18,7 @@ RUN service postgresql start && createdb -O postgres tvb-test && psql --command USER root RUN conda update -n base -c defaults conda; conda init bash -RUN conda create -y --name tvb-run python=3.11 nomkl numba scipy numpy cython psycopg2 +RUN conda create -y --name tvb-run python=3.11 pip nomkl numba scipy numpy cython psycopg2 RUN conda install -y --name tvb-run -c conda-forge jupyterlab tvb-gdist RUN /opt/conda/envs/tvb-run/bin/pip install --upgrade pip RUN /opt/conda/envs/tvb-run/bin/pip install lockfile scikit-build diff --git a/tvb_build/setup_mac.py b/tvb_build/setup_mac.py index 025df24351..c78fc9427c 100644 --- a/tvb_build/setup_mac.py +++ b/tvb_build/setup_mac.py @@ -257,7 +257,7 @@ def prepare_mac_dist(): _create_command_file(os.path.join(DIST_FOLDER, "bin", 'tvb_stop'), 'source ./distribution.command stop', 'Stopping TVB related processes.', True) _create_command_file(os.path.join(DIST_FOLDER, "bin", 'activate_tvb_env'), - 'conda activate ../tvb_data \n $SHELL', 'Conda must be installed before running this script', True) + 'conda activate ../tvb_data \n', 'Conda must be installed before running this script', True) jupyter_command = '/Applications/{}/Contents/Resources/bin/jupyter lab '.format(APP) _create_command_file(os.path.join(DIST_FOLDER, "bin", 'jupyter_notebook'), jupyter_command + '../demo_scripts', 'Launching IPython Notebook from TVB Distribution')