Skip to content
Snippets Groups Projects
Unverified Commit 6da2f1d2 authored by Halil İbrahim Şener's avatar Halil İbrahim Şener Committed by GitHub
Browse files

fix(pip_setup): Run pip setup extract in the correct directory (#8320)

parent 75b1e0c7
No related branches found
Tags 39.46.2
No related merge requests found
......@@ -2,7 +2,7 @@ import sys
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
......@@ -33,10 +33,8 @@ except ImportError:
@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
......
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......@@ -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",
......
......@@ -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;
......@@ -44,9 +44,8 @@ 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');
......@@ -54,7 +53,7 @@ export async function extractSetupFile(
}
logger.debug({ cmd, args }, 'python command');
const res = await exec(`${cmd} ${args.join(' ')}`, {
cwd,
cwdFile: packageFile,
timeout: 30000,
docker: {
image: 'renovate/pip',
......@@ -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(
......
......@@ -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';
......@@ -20,6 +21,7 @@ const jsonContent = readFileSync(packageFileJson, 'utf8');
const config = {
localDir: '/tmp/github/some/repo',
cacheDir: '/tmp/renovate/cache',
};
jest.mock('child_process');
......@@ -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
......@@ -105,7 +108,7 @@ describe(getName(__filename), () => {
expect(
await extractPackageFile(
'raise Exception()',
'/tmp/folders/foobar.py',
'folders/foobar.py',
config
)
).toBeNull();
......@@ -117,7 +120,7 @@ describe(getName(__filename), () => {
expect(
await extractPackageFile(
'raise Exception()',
'/tmp/folders/foobar.py',
'folders/foobar.py',
config
)
).toBeNull();
......
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';
......@@ -7,14 +9,16 @@ 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 {
......@@ -22,7 +26,7 @@ export interface PythonSetup {
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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment