Skip to content

Commit

Permalink
feat(poetry): support binarySource=install (#13794)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jan 25, 2022
1 parent 5df664c commit 47d158d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/manager/poetry/__snapshots__/artifacts.spec.ts.snap
Expand Up @@ -100,7 +100,7 @@ Array [
},
},
Object {
"cmd": "docker run --rm --name=renovate_python --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/python:2.7.5 bash -l -c \\"pip install 'poetry>=1.0' && poetry update --lock --no-interaction dep1\\"",
"cmd": "docker run --rm --name=renovate_python --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/python:2.7.5 bash -l -c \\"install-tool poetry 1.2.0 && poetry update --lock --no-interaction dep1\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
Expand Down Expand Up @@ -135,7 +135,7 @@ Array [
},
},
Object {
"cmd": "docker run --rm --name=renovate_python --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/python:3.4.2 bash -l -c \\"pip install 'poetry>=1.1.2' setuptools 'poetry-dynamic-versioning>1' && poetry update --lock --no-interaction dep1\\"",
"cmd": "docker run --rm --name=renovate_python --label=renovate_child -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -w \\"/tmp/github/some/repo\\" renovate/python:3.4.2 bash -l -c \\"install-tool poetry 1.2.0 && poetry update --lock --no-interaction dep1\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
Expand Down
4 changes: 1 addition & 3 deletions lib/manager/poetry/__snapshots__/extract.spec.ts.snap
Expand Up @@ -533,9 +533,7 @@ Array [

exports[`manager/poetry/extract extractPackageFile() handles multiple constraint dependencies 1`] = `
Object {
"constraints": Object {
"poetry": "poetry>=1.1.2 setuptools poetry-dynamic-versioning",
},
"constraints": Object {},
"deps": Array [
Object {
"currentValue": "",
Expand Down
28 changes: 24 additions & 4 deletions lib/manager/poetry/artifacts.spec.ts
Expand Up @@ -12,6 +12,7 @@ import * as _hostRules from '../../util/host-rules';
import type { UpdateArtifactsConfig } from '../types';
import { updateArtifacts } from './artifacts';

const pyproject1toml = loadFixture('pyproject.1.toml');
const pyproject10toml = loadFixture('pyproject.10.toml');

jest.mock('fs-extra');
Expand Down Expand Up @@ -131,9 +132,19 @@ describe('manager/poetry/artifacts', () => {
});
it('returns updated poetry.lock using docker', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
// poetry.lock
fs.readFile.mockResolvedValueOnce('[metadata]\n' as any);
const execSnapshots = mockExecAll(exec);
fs.readFile.mockReturnValueOnce('New poetry.lock' as any);
// poetry
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
{ version: '1.1.0' },
{ version: '1.2.0' },
],
});
// python
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '2.7.5' }, { version: '3.4.2' }],
});
Expand All @@ -142,12 +153,11 @@ describe('manager/poetry/artifacts', () => {
await updateArtifacts({
packageFileName: 'pyproject.toml',
updatedDeps,
newPackageFileContent: '{}',
newPackageFileContent: pyproject1toml,
config: {
...config,
constraints: {
python: '~2.7 || ^3.4',
poetry: 'poetry>=1.1.2 setuptools poetry-dynamic-versioning>1',
},
},
})
Expand All @@ -156,11 +166,21 @@ describe('manager/poetry/artifacts', () => {
});
it('returns updated poetry.lock using docker (constraints)', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
// poetry.lock
fs.readFile.mockResolvedValueOnce(
'[metadata]\npython-versions = "~2.7 || ^3.4"' as any
);
const execSnapshots = mockExecAll(exec);
fs.readFile.mockReturnValueOnce('New poetry.lock' as any);
// poetry
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
{ version: '1.1.0' },
{ version: '1.2.0' },
],
});
// python
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '2.7.5' }, { version: '3.3.2' }],
});
Expand All @@ -169,10 +189,10 @@ describe('manager/poetry/artifacts', () => {
await updateArtifacts({
packageFileName: 'pyproject.toml',
updatedDeps,
newPackageFileContent: '{}',
newPackageFileContent: pyproject1toml,
config: {
...config,
constraints: { poetry: 'poetry>=1.0' },
constraints: {},
},
})
).not.toBeNull();
Expand Down
48 changes: 43 additions & 5 deletions lib/manager/poetry/artifacts.ts
Expand Up @@ -4,14 +4,16 @@ import { quote } from 'shlex';
import { TEMPORARY_ERROR } from '../../constants/error-messages';
import { logger } from '../../logger';
import { exec } from '../../util/exec';
import type { ExecOptions } from '../../util/exec/types';
import type { ExecOptions, ToolConstraint } from '../../util/exec/types';
import {
deleteLocalFile,
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../util/fs';
import { find } from '../../util/host-rules';
import { regEx } from '../../util/regex';
import { dependencyPattern } from '../pip_requirements/extract';
import type {
UpdateArtifact,
UpdateArtifactsConfig,
Expand Down Expand Up @@ -41,6 +43,39 @@ function getPythonConstraint(
return undefined;
}

const pkgValRegex = regEx(`^${dependencyPattern}$`);

function getPoetryRequirement(pyProjectContent: string): string | null {
try {
const pyproject: PoetryFile = parse(pyProjectContent);
// https://python-poetry.org/docs/pyproject/#poetry-and-pep-517
const buildBackend = pyproject['build-system']?.['build-backend'];
if (
(buildBackend === 'poetry.masonry.api' ||
buildBackend === 'poetry.core.masonry.api') &&
is.nonEmptyArray(pyproject['build-system']?.requires)
) {
for (const requirement of pyproject['build-system'].requires) {
if (is.nonEmptyString(requirement)) {
const pkgValMatch = pkgValRegex.exec(requirement);
if (pkgValMatch) {
const [, depName, , currVal] = pkgValMatch;
if (
(depName === 'poetry' || depName === 'poetry_core') &&
currVal
) {
return currVal.trim();
}
}
}
}
}
} catch (err) {
logger.debug({ err }, 'Error parsing pyproject.toml file');
}
return null;
}

function getPoetrySources(content: string, fileName: string): PoetrySource[] {
let pyprojectFile: PoetryFile;
try {
Expand Down Expand Up @@ -125,13 +160,16 @@ export async function updateArtifacts({
);
}
const tagConstraint = getPythonConstraint(existingLockFileContent, config);
const poetryRequirement = config.constraints?.poetry || 'poetry';
const poetryInstall =
'pip install ' + poetryRequirement.split(' ').map(quote).join(' ');
const constraint =
config.constraints?.poetry || getPoetryRequirement(newPackageFileContent);
const extraEnv = getSourceCredentialVars(
newPackageFileContent,
packageFileName
);
const toolConstraint: ToolConstraint = {
toolName: 'poetry',
constraint,
};

const execOptions: ExecOptions = {
cwdFile: packageFileName,
Expand All @@ -141,7 +179,7 @@ export async function updateArtifacts({
tagConstraint,
tagScheme: 'poetry',
},
preCommands: [poetryInstall],
toolConstraints: [toolConstraint],
};
await exec(cmd, execOptions);
const newPoetryLockContent = await readLocalFile(lockFileName, 'utf8');
Expand Down
1 change: 0 additions & 1 deletion lib/manager/poetry/extract.spec.ts
Expand Up @@ -40,7 +40,6 @@ describe('manager/poetry/extract', () => {
expect(res.deps).toMatchSnapshot();
expect(res.deps).toHaveLength(9);
expect(res.constraints).toEqual({
poetry: 'poetry>=1.0 wheel',
python: '~2.7 || ^3.4',
});
});
Expand Down
7 changes: 0 additions & 7 deletions lib/manager/poetry/extract.ts
Expand Up @@ -149,13 +149,6 @@ export async function extractPackageFile(

const constraints: Record<string, any> = {};

// https://python-poetry.org/docs/pyproject/#poetry-and-pep-517
if (
pyprojectfile['build-system']?.['build-backend'] === 'poetry.masonry.api'
) {
constraints.poetry = pyprojectfile['build-system']?.requires.join(' ');
}

if (is.nonEmptyString(pyprojectfile.tool?.poetry?.dependencies?.python)) {
constraints.python = pyprojectfile.tool?.poetry?.dependencies?.python;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/util/exec/buildpack.ts
Expand Up @@ -5,6 +5,7 @@ import { logger } from '../../logger';
import * as allVersioning from '../../versioning';
import { id as composerVersioningId } from '../../versioning/composer';
import { id as npmVersioningId } from '../../versioning/npm';
import { id as pep440VersioningId } from '../../versioning/pep440';
import { id as semverVersioningId } from '../../versioning/semver';
import type { ToolConfig, ToolConstraint } from './types';

Expand Down Expand Up @@ -35,6 +36,11 @@ const allToolConfig: Record<string, ToolConfig> = {
depName: 'pnpm',
versioning: npmVersioningId,
},
poetry: {
datasource: 'pypi',
depName: 'poetry',
versioning: pep440VersioningId,
},
};

export function supportsDynamicInstall(toolName: string): boolean {
Expand Down

0 comments on commit 47d158d

Please sign in to comment.