From fb0e1df2003cfb4cac75bc17606129dbde149c49 Mon Sep 17 00:00:00 2001 From: Edward Foyle Date: Tue, 4 May 2021 13:02:39 -0700 Subject: [PATCH] fix: get pipenv py version from Pipfile --- .../package.json | 1 + .../src/util/pyUtils.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/amplify-python-function-runtime-provider/package.json b/packages/amplify-python-function-runtime-provider/package.json index 89f9fac1dd6..f0b01a7d96c 100644 --- a/packages/amplify-python-function-runtime-provider/package.json +++ b/packages/amplify-python-function-runtime-provider/package.json @@ -26,6 +26,7 @@ "archiver": "^3.1.1", "execa": "^4.1.0", "glob": "^7.1.6", + "ini": "^1.3.5", "semver": "^7.1.3", "which": "^2.0.2" }, diff --git a/packages/amplify-python-function-runtime-provider/src/util/pyUtils.ts b/packages/amplify-python-function-runtime-provider/src/util/pyUtils.ts index 1b9ce1f79b5..961b40fb12a 100644 --- a/packages/amplify-python-function-runtime-provider/src/util/pyUtils.ts +++ b/packages/amplify-python-function-runtime-provider/src/util/pyUtils.ts @@ -3,6 +3,7 @@ import fs from 'fs-extra'; import { ExecOptions } from 'child_process'; import execa from 'execa'; import * as which from 'which'; +import { parse } from 'ini'; // Gets the pipenv dir where this function's dependencies are located export async function getPipenvDir(srcRoot: string): Promise { @@ -13,8 +14,7 @@ export async function getPipenvDir(srcRoot: string): Promise { throw new Error(`Could not find 'python3' or 'python' executable in the PATH.`); } - const pyVersion = await execAsStringPromise(`${pyBinary} --version`); - let pipEnvPath = path.join(pipEnvDir, 'lib', 'python' + majMinPyVersion(pyVersion), 'site-packages'); + let pipEnvPath = path.join(pipEnvDir, 'lib', `python${getPipfilePyVersion(path.join(srcRoot, 'Pipfile'))}`, 'site-packages'); if (process.platform.startsWith('win')) { pipEnvPath = path.join(pipEnvDir, 'Lib', 'site-packages'); } @@ -29,10 +29,7 @@ export function majMinPyVersion(pyVersion: string): string { throw new Error(`Cannot interpret Python version "${pyVersion}"`); } const versionNum = pyVersion.split(' ')[1]; - return versionNum - .split('.') - .slice(0, 2) - .join('.'); + return versionNum.split('.').slice(0, 2).join('.'); } // wrapper for executing a shell command and returning the result as a string promise @@ -65,3 +62,12 @@ export const getPythonBinaryName = (): string | undefined => { } } }; + +const getPipfilePyVersion = (pipfilePath: string) => { + const pipfile = parse(fs.readFileSync(pipfilePath, 'utf-8')); + const version = pipfile?.requires?.python_version; + if (!version) { + throw new Error(`Did not find Python version specified in ${pipfilePath}`); + } + return version as string; +};