| 143 | * @param opts Options |
| 144 | */ |
| 145 | export async function initPython({pythonPath, dryRun = false, upgrade = false}: InitPythonOptions = {}): Promise<void> { |
| 146 | const foundPythonPath = await requirePython(pythonPath); |
| 147 | |
| 148 | const args = ['-m', 'pip', 'install', '-r', REQUIREMENTS_TXT_PATH]; |
| 149 | if (upgrade) { |
| 150 | args.push('--upgrade'); |
| 151 | } |
| 152 | if (dryRun) { |
| 153 | dryRunLog.info( |
| 154 | 'Would execute command: %s %s (environment variables: %s)', |
| 155 | foundPythonPath, |
| 156 | args.join(' '), |
| 157 | PIP_ENV_VARS, |
| 158 | ); |
| 159 | } else { |
| 160 | log.debug('Executing command: %s %s (environment variables: %s)', foundPythonPath, args.join(' '), PIP_ENV_VARS); |
| 161 | log.info('Installing Python dependencies...'); |
| 162 | try { |
| 163 | const result = await exec(foundPythonPath, args, {env: PIP_ENV_VARS, shell: true}); |
| 164 | const {code, stdout} = result; |
| 165 | if (code !== 0) { |
| 166 | throw new DocutilsError(`Could not install Python dependencies. Reason: ${stdout}`); |
| 167 | } |
| 168 | } catch (err) { |
| 169 | throw new DocutilsError(`Could not install Python dependencies. Reason: ${(err as Error).message}`); |
| 170 | } |
| 171 | } |
| 172 | log.success('Successfully installed Python dependencies'); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Main handler for `init` command. |