mesaport.Installer.installer
1import os 2import shlex 3import subprocess 4 5from rich import console, print, prompt 6 7from . import choice, downloader, extractor, mesaurls, prerequisites, syscheck 8 9class Installer: 10 """Class for installing MESA and MESA SDK. 11 """ 12 def __init__(self, version='', parentDir='', cleanAfter=False, logging=True): 13 """Constructor for Installer class. 14 15 Args: 16 version (str, optional): 17 Version of MESA to install. CLI is used to choose version if not specified. 18 19 parentDir (str, optional): 20 Path to a directory to install MESA and MESA SDK. CLI is used to choose directory if not specified. 21 22 cleanAfter (bool, optional): 23 If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. Defaults to False. 24 """ 25 ostype = syscheck.whichos() 26 directory = choice.choose_directory(parentDir) 27 print(f"[orange3]{ostype}[/orange3] system detected.\n") 28 version = choice.choose_ver(ostype, version) 29 self.logging = logging 30 self.install(version, ostype, directory, cleanAfter) 31 32 33 34 35 def install(self, version, ostype, directory, cleanAfter): 36 """Install MESA. 37 38 Args: 39 version (str): Version of MESA to install. 40 ostype (str): OS type. 41 directory (str): Path to a directory to install MESA and MESA SDK. 42 cleanAfter (bool): If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. 43 Defaults to False. 44 """ 45 downloaded = downloader.Download(directory, version, ostype) 46 sdk_download, mesa_zip = downloaded.sdk_download, downloaded.mesa_zip 47 mesa_dir = os.path.join(directory, mesa_zip.split('/')[-1][0:-4]) 48 49 if self.logging is False: 50 logfile = subprocess.DEVNULL 51 else: 52 logfile = open(f"install.log", "w+") 53 54 ## to get sudo password prompt out of the way 55 print("[blue b]Installing pre-requisites...\n") 56 sudo_privileges = prompt.Prompt.ask("[green b]Do you have sudo privileges?[/green b]", choices=["y", "n"]) 57 if sudo_privileges == "y": 58 subprocess.Popen(shlex.split("sudo echo"), stdin=subprocess.PIPE, stdout=logfile, stderr=logfile).wait() 59 with console.Console().status("[green b]Installing pre-requisites", spinner="moon"): 60 prerequisites.install_prerequisites(directory, ostype, cleanAfter, logfile) 61 print("[blue b]Pre-requisites installation complete.\n") 62 else: 63 print("[yellow b]WARNING: You do not have sudo privileges.[/yellow b]") 64 print("You can either install the pre-requisites manually, or try to skip the pre-requisites installation.\n\ 65 NOTE: Skipping pre-requisites installation may cause the Installer to fail.\n\n") 66 skip_prompt = prompt.Prompt.ask("[green b]Do you wish to continue this installation?[/green b]", choices=["y", "n"]) 67 if skip_prompt == "y": 68 print("[yellow b]WARNING: Skipping pre-requisites installation.[/yellow b]\n") 69 else: 70 print("[red b]Exiting. Please install the pre-requisites manually and try again.\n[/red b]") 71 exit() 72 73 extractor.extract_mesa(directory, ostype, cleanAfter, sdk_download, mesa_zip, logfile) 74 with console.Console().status("[green b]Installing MESA", spinner="moon"): 75 if ostype == "Linux": 76 sdk_dir = os.path.join(directory, 'mesasdk') 77 elif "macOS" in ostype: 78 sdk_dir = '/Applications/mesasdk' 79 80 with subprocess.Popen(f"/bin/bash -c \"export MESASDK_ROOT={sdk_dir} && \ 81 source {sdk_dir}/bin/mesasdk_init.sh && gfortran --version\"", 82 shell=True, stdout=logfile, stderr=logfile, env=os.environ.copy()) as proc: 83 proc.wait() 84 if proc.returncode != 0: 85 raise Exception("MESA SDK initialization failed. \ 86 Please check the install.log file for details.") 87 88 run_in_shell = f''' 89 /bin/bash -c \" 90 export MESASDK_ROOT={sdk_dir} \\ 91 && source {sdk_dir}/bin/mesasdk_init.sh \\ 92 && export MESA_DIR={mesa_dir} \\ 93 && export OMP_NUM_THREADS=2 \\ 94 && chmod -R +x {mesa_dir} \\ 95 && cd {mesa_dir} && ./clean && ./install \\ 96 && make -C {mesa_dir}/gyre/gyre \\ 97 && export GYRE_DIR={mesa_dir}/gyre/gyre \" 98 ''' 99 with subprocess.Popen(run_in_shell, shell=True, stdout=logfile, stderr=logfile, env=os.environ.copy()) as proc: 100 proc.wait() 101 if proc.returncode != 0: 102 raise Exception("MESA installation failed. \ 103 Please check the install_log.txt file for details.") 104 elif self.logging is True: 105 logfile.write("MESA installation complete.\n") 106 logfile.write("Build Successful.\n") 107 108 if self.logging is True: 109 logfile.close() 110 111 self.write_env_vars(mesa_dir, sdk_dir) 112 print("[b bright_cyan]Installation complete.\n") 113 114 115 116 117 def write_env_vars(self, mesa_dir, sdk_dir): 118 """Write the environment variables to the shell .rc file. 119 120 Args: 121 mesa_dir (path): Path to the MESA directory. 122 sdk_dir (path): Path to the MESA SDK directory. 123 """ 124 source_this=f''' 125 126 ############ MESA environment variables ############### 127 export MESASDK_ROOT={sdk_dir} 128 source $MESASDK_ROOT/bin/mesasdk_init.sh 129 export MESA_DIR={mesa_dir} 130 export OMP_NUM_THREADS=2 ## max should be 2 times the cores on your machine 131 export GYRE_DIR=$MESA_DIR/gyre/gyre 132 ####################################################### 133 134 ''' 135 136 env_shell = os.environ.get('SHELL') 137 if env_shell is None: 138 env_shell = "bash" 139 else: 140 env_shell = env_shell.split('/')[-1] 141 if env_shell == "bash": 142 env_file = os.path.join(os.environ.get('HOME'), ".bashrc") 143 elif env_shell == "zsh": 144 env_file = os.path.join(os.environ.get('HOME'), ".zshrc") 145 elif env_shell == "csh": 146 env_file = os.path.join(os.environ.get('HOME'), ".cshrc") 147 elif env_shell == "tcsh": 148 env_file = os.path.join(os.environ.get('HOME'), ".tcshrc") 149 else: 150 env_file = os.path.join(os.environ.get('HOME'), ".profile") 151 152 with open(env_file, "a+") as f: 153 f.write(source_this) 154 155 print(f"The following environment variables have been written to your {env_file} file:") 156 print(source_this) 157 print("To activate these variables in your current shell, run the following command:\n") 158 print(f"[yellow]source {env_file}\n") 159 160
class
Installer:
10class Installer: 11 """Class for installing MESA and MESA SDK. 12 """ 13 def __init__(self, version='', parentDir='', cleanAfter=False, logging=True): 14 """Constructor for Installer class. 15 16 Args: 17 version (str, optional): 18 Version of MESA to install. CLI is used to choose version if not specified. 19 20 parentDir (str, optional): 21 Path to a directory to install MESA and MESA SDK. CLI is used to choose directory if not specified. 22 23 cleanAfter (bool, optional): 24 If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. Defaults to False. 25 """ 26 ostype = syscheck.whichos() 27 directory = choice.choose_directory(parentDir) 28 print(f"[orange3]{ostype}[/orange3] system detected.\n") 29 version = choice.choose_ver(ostype, version) 30 self.logging = logging 31 self.install(version, ostype, directory, cleanAfter) 32 33 34 35 36 def install(self, version, ostype, directory, cleanAfter): 37 """Install MESA. 38 39 Args: 40 version (str): Version of MESA to install. 41 ostype (str): OS type. 42 directory (str): Path to a directory to install MESA and MESA SDK. 43 cleanAfter (bool): If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. 44 Defaults to False. 45 """ 46 downloaded = downloader.Download(directory, version, ostype) 47 sdk_download, mesa_zip = downloaded.sdk_download, downloaded.mesa_zip 48 mesa_dir = os.path.join(directory, mesa_zip.split('/')[-1][0:-4]) 49 50 if self.logging is False: 51 logfile = subprocess.DEVNULL 52 else: 53 logfile = open(f"install.log", "w+") 54 55 ## to get sudo password prompt out of the way 56 print("[blue b]Installing pre-requisites...\n") 57 sudo_privileges = prompt.Prompt.ask("[green b]Do you have sudo privileges?[/green b]", choices=["y", "n"]) 58 if sudo_privileges == "y": 59 subprocess.Popen(shlex.split("sudo echo"), stdin=subprocess.PIPE, stdout=logfile, stderr=logfile).wait() 60 with console.Console().status("[green b]Installing pre-requisites", spinner="moon"): 61 prerequisites.install_prerequisites(directory, ostype, cleanAfter, logfile) 62 print("[blue b]Pre-requisites installation complete.\n") 63 else: 64 print("[yellow b]WARNING: You do not have sudo privileges.[/yellow b]") 65 print("You can either install the pre-requisites manually, or try to skip the pre-requisites installation.\n\ 66 NOTE: Skipping pre-requisites installation may cause the Installer to fail.\n\n") 67 skip_prompt = prompt.Prompt.ask("[green b]Do you wish to continue this installation?[/green b]", choices=["y", "n"]) 68 if skip_prompt == "y": 69 print("[yellow b]WARNING: Skipping pre-requisites installation.[/yellow b]\n") 70 else: 71 print("[red b]Exiting. Please install the pre-requisites manually and try again.\n[/red b]") 72 exit() 73 74 extractor.extract_mesa(directory, ostype, cleanAfter, sdk_download, mesa_zip, logfile) 75 with console.Console().status("[green b]Installing MESA", spinner="moon"): 76 if ostype == "Linux": 77 sdk_dir = os.path.join(directory, 'mesasdk') 78 elif "macOS" in ostype: 79 sdk_dir = '/Applications/mesasdk' 80 81 with subprocess.Popen(f"/bin/bash -c \"export MESASDK_ROOT={sdk_dir} && \ 82 source {sdk_dir}/bin/mesasdk_init.sh && gfortran --version\"", 83 shell=True, stdout=logfile, stderr=logfile, env=os.environ.copy()) as proc: 84 proc.wait() 85 if proc.returncode != 0: 86 raise Exception("MESA SDK initialization failed. \ 87 Please check the install.log file for details.") 88 89 run_in_shell = f''' 90 /bin/bash -c \" 91 export MESASDK_ROOT={sdk_dir} \\ 92 && source {sdk_dir}/bin/mesasdk_init.sh \\ 93 && export MESA_DIR={mesa_dir} \\ 94 && export OMP_NUM_THREADS=2 \\ 95 && chmod -R +x {mesa_dir} \\ 96 && cd {mesa_dir} && ./clean && ./install \\ 97 && make -C {mesa_dir}/gyre/gyre \\ 98 && export GYRE_DIR={mesa_dir}/gyre/gyre \" 99 ''' 100 with subprocess.Popen(run_in_shell, shell=True, stdout=logfile, stderr=logfile, env=os.environ.copy()) as proc: 101 proc.wait() 102 if proc.returncode != 0: 103 raise Exception("MESA installation failed. \ 104 Please check the install_log.txt file for details.") 105 elif self.logging is True: 106 logfile.write("MESA installation complete.\n") 107 logfile.write("Build Successful.\n") 108 109 if self.logging is True: 110 logfile.close() 111 112 self.write_env_vars(mesa_dir, sdk_dir) 113 print("[b bright_cyan]Installation complete.\n") 114 115 116 117 118 def write_env_vars(self, mesa_dir, sdk_dir): 119 """Write the environment variables to the shell .rc file. 120 121 Args: 122 mesa_dir (path): Path to the MESA directory. 123 sdk_dir (path): Path to the MESA SDK directory. 124 """ 125 source_this=f''' 126 127 ############ MESA environment variables ############### 128 export MESASDK_ROOT={sdk_dir} 129 source $MESASDK_ROOT/bin/mesasdk_init.sh 130 export MESA_DIR={mesa_dir} 131 export OMP_NUM_THREADS=2 ## max should be 2 times the cores on your machine 132 export GYRE_DIR=$MESA_DIR/gyre/gyre 133 ####################################################### 134 135 ''' 136 137 env_shell = os.environ.get('SHELL') 138 if env_shell is None: 139 env_shell = "bash" 140 else: 141 env_shell = env_shell.split('/')[-1] 142 if env_shell == "bash": 143 env_file = os.path.join(os.environ.get('HOME'), ".bashrc") 144 elif env_shell == "zsh": 145 env_file = os.path.join(os.environ.get('HOME'), ".zshrc") 146 elif env_shell == "csh": 147 env_file = os.path.join(os.environ.get('HOME'), ".cshrc") 148 elif env_shell == "tcsh": 149 env_file = os.path.join(os.environ.get('HOME'), ".tcshrc") 150 else: 151 env_file = os.path.join(os.environ.get('HOME'), ".profile") 152 153 with open(env_file, "a+") as f: 154 f.write(source_this) 155 156 print(f"The following environment variables have been written to your {env_file} file:") 157 print(source_this) 158 print("To activate these variables in your current shell, run the following command:\n") 159 print(f"[yellow]source {env_file}\n")
Class for installing MESA and MESA SDK.
Installer(version='', parentDir='', cleanAfter=False, logging=True)
13 def __init__(self, version='', parentDir='', cleanAfter=False, logging=True): 14 """Constructor for Installer class. 15 16 Args: 17 version (str, optional): 18 Version of MESA to install. CLI is used to choose version if not specified. 19 20 parentDir (str, optional): 21 Path to a directory to install MESA and MESA SDK. CLI is used to choose directory if not specified. 22 23 cleanAfter (bool, optional): 24 If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. Defaults to False. 25 """ 26 ostype = syscheck.whichos() 27 directory = choice.choose_directory(parentDir) 28 print(f"[orange3]{ostype}[/orange3] system detected.\n") 29 version = choice.choose_ver(ostype, version) 30 self.logging = logging 31 self.install(version, ostype, directory, cleanAfter)
Constructor for Installer class.
Arguments:
- version (str, optional):
- Version of MESA to install. CLI is used to choose version if not specified.
- parentDir (str, optional):
- Path to a directory to install MESA and MESA SDK. CLI is used to choose directory if not specified.
- cleanAfter (bool, optional):
- If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. Defaults to False.
def
install(self, version, ostype, directory, cleanAfter):
36 def install(self, version, ostype, directory, cleanAfter): 37 """Install MESA. 38 39 Args: 40 version (str): Version of MESA to install. 41 ostype (str): OS type. 42 directory (str): Path to a directory to install MESA and MESA SDK. 43 cleanAfter (bool): If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. 44 Defaults to False. 45 """ 46 downloaded = downloader.Download(directory, version, ostype) 47 sdk_download, mesa_zip = downloaded.sdk_download, downloaded.mesa_zip 48 mesa_dir = os.path.join(directory, mesa_zip.split('/')[-1][0:-4]) 49 50 if self.logging is False: 51 logfile = subprocess.DEVNULL 52 else: 53 logfile = open(f"install.log", "w+") 54 55 ## to get sudo password prompt out of the way 56 print("[blue b]Installing pre-requisites...\n") 57 sudo_privileges = prompt.Prompt.ask("[green b]Do you have sudo privileges?[/green b]", choices=["y", "n"]) 58 if sudo_privileges == "y": 59 subprocess.Popen(shlex.split("sudo echo"), stdin=subprocess.PIPE, stdout=logfile, stderr=logfile).wait() 60 with console.Console().status("[green b]Installing pre-requisites", spinner="moon"): 61 prerequisites.install_prerequisites(directory, ostype, cleanAfter, logfile) 62 print("[blue b]Pre-requisites installation complete.\n") 63 else: 64 print("[yellow b]WARNING: You do not have sudo privileges.[/yellow b]") 65 print("You can either install the pre-requisites manually, or try to skip the pre-requisites installation.\n\ 66 NOTE: Skipping pre-requisites installation may cause the Installer to fail.\n\n") 67 skip_prompt = prompt.Prompt.ask("[green b]Do you wish to continue this installation?[/green b]", choices=["y", "n"]) 68 if skip_prompt == "y": 69 print("[yellow b]WARNING: Skipping pre-requisites installation.[/yellow b]\n") 70 else: 71 print("[red b]Exiting. Please install the pre-requisites manually and try again.\n[/red b]") 72 exit() 73 74 extractor.extract_mesa(directory, ostype, cleanAfter, sdk_download, mesa_zip, logfile) 75 with console.Console().status("[green b]Installing MESA", spinner="moon"): 76 if ostype == "Linux": 77 sdk_dir = os.path.join(directory, 'mesasdk') 78 elif "macOS" in ostype: 79 sdk_dir = '/Applications/mesasdk' 80 81 with subprocess.Popen(f"/bin/bash -c \"export MESASDK_ROOT={sdk_dir} && \ 82 source {sdk_dir}/bin/mesasdk_init.sh && gfortran --version\"", 83 shell=True, stdout=logfile, stderr=logfile, env=os.environ.copy()) as proc: 84 proc.wait() 85 if proc.returncode != 0: 86 raise Exception("MESA SDK initialization failed. \ 87 Please check the install.log file for details.") 88 89 run_in_shell = f''' 90 /bin/bash -c \" 91 export MESASDK_ROOT={sdk_dir} \\ 92 && source {sdk_dir}/bin/mesasdk_init.sh \\ 93 && export MESA_DIR={mesa_dir} \\ 94 && export OMP_NUM_THREADS=2 \\ 95 && chmod -R +x {mesa_dir} \\ 96 && cd {mesa_dir} && ./clean && ./install \\ 97 && make -C {mesa_dir}/gyre/gyre \\ 98 && export GYRE_DIR={mesa_dir}/gyre/gyre \" 99 ''' 100 with subprocess.Popen(run_in_shell, shell=True, stdout=logfile, stderr=logfile, env=os.environ.copy()) as proc: 101 proc.wait() 102 if proc.returncode != 0: 103 raise Exception("MESA installation failed. \ 104 Please check the install_log.txt file for details.") 105 elif self.logging is True: 106 logfile.write("MESA installation complete.\n") 107 logfile.write("Build Successful.\n") 108 109 if self.logging is True: 110 logfile.close() 111 112 self.write_env_vars(mesa_dir, sdk_dir) 113 print("[b bright_cyan]Installation complete.\n")
Install MESA.
Arguments:
- version (str): Version of MESA to install.
- ostype (str): OS type.
- directory (str): Path to a directory to install MESA and MESA SDK.
- cleanAfter (bool): If True, the downloaded MESA SDK and MESA zip files will be deleted after installation. Defaults to False.
def
write_env_vars(self, mesa_dir, sdk_dir):
118 def write_env_vars(self, mesa_dir, sdk_dir): 119 """Write the environment variables to the shell .rc file. 120 121 Args: 122 mesa_dir (path): Path to the MESA directory. 123 sdk_dir (path): Path to the MESA SDK directory. 124 """ 125 source_this=f''' 126 127 ############ MESA environment variables ############### 128 export MESASDK_ROOT={sdk_dir} 129 source $MESASDK_ROOT/bin/mesasdk_init.sh 130 export MESA_DIR={mesa_dir} 131 export OMP_NUM_THREADS=2 ## max should be 2 times the cores on your machine 132 export GYRE_DIR=$MESA_DIR/gyre/gyre 133 ####################################################### 134 135 ''' 136 137 env_shell = os.environ.get('SHELL') 138 if env_shell is None: 139 env_shell = "bash" 140 else: 141 env_shell = env_shell.split('/')[-1] 142 if env_shell == "bash": 143 env_file = os.path.join(os.environ.get('HOME'), ".bashrc") 144 elif env_shell == "zsh": 145 env_file = os.path.join(os.environ.get('HOME'), ".zshrc") 146 elif env_shell == "csh": 147 env_file = os.path.join(os.environ.get('HOME'), ".cshrc") 148 elif env_shell == "tcsh": 149 env_file = os.path.join(os.environ.get('HOME'), ".tcshrc") 150 else: 151 env_file = os.path.join(os.environ.get('HOME'), ".profile") 152 153 with open(env_file, "a+") as f: 154 f.write(source_this) 155 156 print(f"The following environment variables have been written to your {env_file} file:") 157 print(source_this) 158 print("To activate these variables in your current shell, run the following command:\n") 159 print(f"[yellow]source {env_file}\n")
Write the environment variables to the shell .rc file.
Arguments:
- mesa_dir (path): Path to the MESA directory.
- sdk_dir (path): Path to the MESA SDK directory.