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

fix: get pipenv py version from Pipfile #7254

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
Expand All @@ -13,8 +14,7 @@ export async function getPipenvDir(srcRoot: string): Promise<string> {
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any benefit to falling back to majMinPyVersion() if getPipfilePyVersion() fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no version specified in the Pipfile, creating the virtual env wont even work so I don't think we need a fallback here

if (process.platform.startsWith('win')) {
pipEnvPath = path.join(pipEnvDir, 'Lib', 'site-packages');
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
};