Skip to content

Commit

Permalink
TVB-2113: add bin path to modify distribution python path
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianciu committed Dec 4, 2024
1 parent ae03542 commit c8d0dd9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
31 changes: 28 additions & 3 deletions tvb_build/build_from_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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():
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:")
Expand Down
2 changes: 1 addition & 1 deletion tvb_build/docker/Dockerfile-build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tvb_build/setup_mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit c8d0dd9

Please sign in to comment.