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

Use Python 3.11 for integrated Python #2144

Merged
merged 6 commits into from
Aug 28, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 60 additions & 45 deletions src/main/python/integratedPython.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@ import decompress from 'decompress';
import fs from 'fs/promises';
import Downloader from 'nodejs-file-downloader';
import path from 'path';
import { eq } from 'semver';
import { PythonInfo } from '../../common/common-types';
import { isArmMac } from '../../common/env';
import { log } from '../../common/log';
import { assertNever, checkFileExists } from '../../common/util';
import { checkFileExists } from '../../common/util';
import { SupportedPlatform, getPlatform } from '../platform';
import { checkPythonPaths } from './checkPythonPaths';

const downloads: Record<SupportedPlatform, string> = {
linux: 'https://github.com/indygreg/python-build-standalone/releases/download/20211017/cpython-3.9.7-x86_64-unknown-linux-gnu-install_only-20211017T1616.tar.gz',
darwin: isArmMac
? 'https://github.com/indygreg/python-build-standalone/releases/download/20220318/cpython-3.9.11+20220318-aarch64-apple-darwin-install_only.tar.gz'
: 'https://github.com/indygreg/python-build-standalone/releases/download/20211017/cpython-3.9.7-x86_64-apple-darwin-install_only-20211017T1616.tar.gz',
win32: 'https://github.com/indygreg/python-build-standalone/releases/download/20211017/cpython-3.9.7-x86_64-pc-windows-msvc-shared-install_only-20211017T1616.tar.gz',
};
interface PythonDownload {
url: string;
version: string;
path: string;
}

const getExecutableRelativePath = (platform: SupportedPlatform): string => {
switch (platform) {
case 'win32':
return '/python/python.exe';
case 'linux':
return '/python/bin/python3.9';
case 'darwin':
return '/python/bin/python3.9';
default:
return assertNever(platform);
}
const downloads: Record<SupportedPlatform, PythonDownload> = {
linux: {
url: 'https://github.com/indygreg/python-build-standalone/releases/download/20230726/cpython-3.11.4+20230726-x86_64-unknown-linux-gnu-install_only.tar.gz',
version: '3.11.4',
path: 'python/bin/python3.11',
},
darwin: {
url: isArmMac
? 'https://github.com/indygreg/python-build-standalone/releases/download/20230726/cpython-3.11.4+20230726-aarch64-apple-darwin-install_only.tar.gz'
: 'https://github.com/indygreg/python-build-standalone/releases/download/20230726/cpython-3.11.4+20230726-x86_64-apple-darwin-install_only.tar.gz',
version: '3.11.4',
path: 'python/bin/python3.11',
},
win32: {
url: 'https://github.com/indygreg/python-build-standalone/releases/download/20230726/cpython-3.11.4+20230726-x86_64-pc-windows-msvc-shared-install_only.tar.gz',
version: '3.11.4',
path: 'python/python.exe',
},
};

const extractPython = async (
Expand Down Expand Up @@ -61,40 +67,49 @@ export const getIntegratedPython = async (
onProgress: (percentage: number, stage: 'download' | 'extract') => void
): Promise<PythonInfo> => {
const platform = getPlatform();
const pythonPath = path.resolve(path.join(directory, getExecutableRelativePath(platform)));
const { url, version, path: relativePath } = downloads[platform];

const pythonPath = path.resolve(path.join(directory, relativePath));
const pythonBinExists = await checkFileExists(pythonPath);

if (!pythonBinExists) {
log.info(`Integrated Python not found at ${pythonPath}`);
if (pythonBinExists) {
const pythonInfo = await checkPythonPaths([pythonPath]);
if (eq(pythonInfo.version, version)) {
joeyballentine marked this conversation as resolved.
Show resolved Hide resolved
return pythonInfo;
}
// Invalid version, remove legacy integrated python
const legacyPythonFolder = path.resolve(path.join(directory, '/python'));
await fs.rm(legacyPythonFolder, { recursive: true, force: true });
}
joeyballentine marked this conversation as resolved.
Show resolved Hide resolved

log.info(`Integrated Python not found at ${pythonPath}`);

const tarName = 'python.tar.gz';
const tarPath = path.join(directory, tarName);
const tarName = 'python.tar.gz';
const tarPath = path.join(directory, tarName);

log.info('Downloading integrated Python...');
onProgress(0, 'download');
await new Downloader({
url: downloads[platform],
directory,
fileName: tarName,
cloneFiles: false,
onProgress: (percentage) => onProgress(Number(percentage), 'download'),
}).download();
log.info('Downloading integrated Python...');
onProgress(0, 'download');
await new Downloader({
url,
directory,
fileName: tarName,
cloneFiles: false,
onProgress: (percentage) => onProgress(Number(percentage), 'download'),
}).download();

log.info('Extracting integrated Python...');
onProgress(0, 'extract');
await extractPython(directory, tarPath, (percentage) => onProgress(percentage, 'extract'));
log.info('Extracting integrated Python...');
onProgress(0, 'extract');
await extractPython(directory, tarPath, (percentage) => onProgress(percentage, 'extract'));

log.info('Removing downloaded files...');
await fs.rm(tarPath);
log.info('Removing downloaded files...');
await fs.rm(tarPath);

if (platform === 'linux' || platform === 'darwin') {
log.info('Granting permissions for integrated python...');
try {
await fs.chmod(pythonPath, 0o7777);
} catch (error) {
log.warn(error);
}
if (platform === 'linux' || platform === 'darwin') {
log.info('Granting permissions for integrated python...');
try {
await fs.chmod(pythonPath, 0o7777);
} catch (error) {
log.warn(error);
}
}

Expand Down