Skip to content

Commit

Permalink
fix(pip_setup): Run pip setup extract in the correct directory (#8320)
Browse files Browse the repository at this point in the history
  • Loading branch information
hisener committed Jan 18, 2021
1 parent 75b1e0c commit 6da2f1d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
6 changes: 2 additions & 4 deletions data/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
import distutils.core
from os.path import dirname, realpath
from os.path import basename

if sys.version_info[:2] >= (3, 3):
from importlib.machinery import SourceFileLoader
Expand Down Expand Up @@ -33,10 +33,8 @@ def setup():
@mock.patch.object(setuptools, 'setup')
@mock.patch.object(distutils.core, 'setup')
def invoke(mock1, mock2):
# Inserting the parent directory of the target setup.py in Python import path:
sys.path.append(dirname(realpath(sys.argv[-1])))
# This is setup.py which calls setuptools.setup
load_source('_target_setup_', sys.argv[-1])
load_source('_target_setup_', basename(sys.argv[-1]))
# called arguments are in `mock_setup.call_args`
call_args = mock1.call_args or mock2.call_args
args, kwargs = call_args
Expand Down
36 changes: 18 additions & 18 deletions lib/manager/pip_setup/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Array [
Object {
"cmd": "python --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -23,7 +23,7 @@ Array [
Object {
"cmd": "python3 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -41,7 +41,7 @@ Array [
Object {
"cmd": "python3.8 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -57,9 +57,9 @@ Array [
},
},
Object {
"cmd": "<extract.py> \\"/tmp/folders/foobar.py\\"",
"cmd": "<extract.py> \\"folders/foobar.py\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"cwd": "/tmp/github/some/repo/folders",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand Down Expand Up @@ -338,7 +338,7 @@ Array [
Object {
"cmd": "python --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -356,7 +356,7 @@ Array [
Object {
"cmd": "python3 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -374,7 +374,7 @@ Array [
Object {
"cmd": "python3.8 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -392,7 +392,7 @@ Array [
Object {
"cmd": "<extract.py> \\"lib/manager/pip_setup/__fixtures__/setup.py\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"cwd": "/tmp/github/some/repo/lib/manager/pip_setup/__fixtures__",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -415,7 +415,7 @@ Array [
Object {
"cmd": "python --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -433,7 +433,7 @@ Array [
Object {
"cmd": "python3 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -451,7 +451,7 @@ Array [
Object {
"cmd": "python3.8 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -469,7 +469,7 @@ Array [
Object {
"cmd": "<extract.py> \\"lib/manager/pip_setup/__fixtures__/setup.py\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"cwd": "/tmp/github/some/repo/lib/manager/pip_setup/__fixtures__",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -492,7 +492,7 @@ Array [
Object {
"cmd": "python --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -510,7 +510,7 @@ Array [
Object {
"cmd": "python3 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -528,7 +528,7 @@ Array [
Object {
"cmd": "python3.8 --version",
"options": Object {
"cwd": null,
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand All @@ -544,9 +544,9 @@ Array [
},
},
Object {
"cmd": "<extract.py> \\"/tmp/folders/foobar.py\\"",
"cmd": "<extract.py> \\"folders/foobar.py\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"cwd": "/tmp/github/some/repo/folders",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
Expand Down
9 changes: 4 additions & 5 deletions lib/manager/pip_setup/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BinarySource } from '../../util/exec/common';
import { isSkipComment } from '../../util/ignore';
import { ExtractConfig, PackageDependency, PackageFile } from '../common';
import { dependencyPattern } from '../pip_requirements/extract';
import { PythonSetup, copyExtractFile, parseReport } from './util';
import { PythonSetup, getExtractFile, parseReport } from './util';

export const pythonVersions = ['python', 'python3', 'python3.8'];
let pythonAlias: string | null = null;
Expand Down Expand Up @@ -44,17 +44,16 @@ export async function extractSetupFile(
packageFile: string,
config: ExtractConfig
): Promise<PythonSetup> {
const cwd = config.localDir;
let cmd = 'python';
const extractPy = await copyExtractFile();
const extractPy = await getExtractFile();
const args = [`"${extractPy}"`, `"${packageFile}"`];
if (config.binarySource !== BinarySource.Docker) {
logger.debug('Running python via global command');
cmd = await getPythonAlias();
}
logger.debug({ cmd, args }, 'python command');
const res = await exec(`${cmd} ${args.join(' ')}`, {
cwd,
cwdFile: packageFile,
timeout: 30000,
docker: {
image: 'renovate/pip',
Expand All @@ -69,7 +68,7 @@ export async function extractSetupFile(
logger.warn({ stdout: res.stdout, stderr }, 'Error in read setup file');
}
}
return parseReport();
return parseReport(packageFile);
}

export async function extractPackageFile(
Expand Down
9 changes: 6 additions & 3 deletions lib/manager/pip_setup/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
mockExecSequence,
} from '../../../test/exec-util';
import { env, getName } from '../../../test/util';
import { setUtilConfig } from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as fs from '../../util/fs';
import * as extract from './extract';
Expand All @@ -20,6 +21,7 @@ const jsonContent = readFileSync(packageFileJson, 'utf8');

const config = {
localDir: '/tmp/github/some/repo',
cacheDir: '/tmp/renovate/cache',
};

jest.mock('child_process');
Expand All @@ -40,11 +42,12 @@ const fixSnapshots = (snapshots: ExecSnapshots): ExecSnapshots =>

describe(getName(__filename), () => {
describe('extractPackageFile()', () => {
beforeEach(() => {
beforeEach(async () => {
jest.resetAllMocks();
jest.resetModules();
extract.resetModule();

await setUtilConfig(config);
env.getChildProcessEnv.mockReturnValue(envMock.basic);

// do not copy extract.py
Expand Down Expand Up @@ -105,7 +108,7 @@ describe(getName(__filename), () => {
expect(
await extractPackageFile(
'raise Exception()',
'/tmp/folders/foobar.py',
'folders/foobar.py',
config
)
).toBeNull();
Expand All @@ -117,7 +120,7 @@ describe(getName(__filename), () => {
expect(
await extractPackageFile(
'raise Exception()',
'/tmp/folders/foobar.py',
'folders/foobar.py',
config
)
).toBeNull();
Expand Down
20 changes: 12 additions & 8 deletions lib/manager/pip_setup/util.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { dirname } from 'path';
import { join } from 'upath';
import dataFiles from '../../data-files.generated';
import { readLocalFile, writeLocalFile } from '../../util/fs';
import { ensureCacheDir, outputFile, readLocalFile } from '../../util/fs';

// need to match filename in `data/extract.py`
const REPORT = 'renovate-pip_setup-report.json';
const EXTRACT = 'renovate-pip_setup-extract.py';

let extractPy: string | undefined;

export async function copyExtractFile(): Promise<string> {
if (extractPy === undefined) {
extractPy = dataFiles.get('extract.py');
export async function getExtractFile(): Promise<string> {
if (extractPy) {
return extractPy;
}

await writeLocalFile(EXTRACT, extractPy);
const cacheDir = await ensureCacheDir('./others/pip_setup');
extractPy = join(cacheDir, EXTRACT);
await outputFile(extractPy, dataFiles.get('extract.py'));

return EXTRACT;
return extractPy;
}

export interface PythonSetup {
extras_require: Record<string, string[]>;
install_requires: string[];
}

export async function parseReport(): Promise<PythonSetup> {
const data = await readLocalFile(REPORT, 'utf8');
export async function parseReport(packageFile: string): Promise<PythonSetup> {
const data = await readLocalFile(join(dirname(packageFile), REPORT), 'utf8');
return JSON.parse(data);
}

0 comments on commit 6da2f1d

Please sign in to comment.