renovate/lib/manager/pip_setup/extract.ts

152 lines
4.1 KiB
TypeScript
Raw Normal View History

import { join } from 'upath';
import { exec } from '../../util/exec';
import { logger } from '../../logger';
import { isSkipComment } from '../../util/ignore';
import { dependencyPattern } from '../pip_requirements/extract';
import { ExtractConfig, PackageFile, PackageDependency } from '../common';
2018-11-15 17:42:01 +00:00
export const pythonVersions = ['python', 'python3', 'python3.8'];
let pythonAlias: string | null = null;
2018-11-15 17:42:01 +00:00
export function parsePythonVersion(str: string): number[] {
const arr = str.split(' ')[1].split('.');
return [parseInt(arr[0], 10), parseInt(arr[1], 10)];
}
export async function getPythonAlias(): Promise<string> {
if (pythonAlias) {
return pythonAlias;
}
pythonAlias = pythonVersions[0]; // fallback to 'python'
for (const pythonVersion of pythonVersions) {
try {
const { stdout, stderr } = await exec(`${pythonVersion} --version`);
const version = parsePythonVersion(stdout || stderr);
if (version[0] >= 3 && version[1] >= 7) {
pythonAlias = pythonVersion;
}
2019-03-17 15:56:40 +00:00
} catch (err) /* istanbul ignore next */ {
logger.debug(`${pythonVersion} alias not found`);
}
}
return pythonAlias;
}
interface PythonSetup {
extras_require: string[];
install_requires: string[];
}
export async function extractSetupFile(
_content: string,
packageFile: string,
config: ExtractConfig
): Promise<PythonSetup> {
2018-11-15 17:42:01 +00:00
const cwd = config.localDir;
let cmd: string;
const args = [`"${join(__dirname, 'extract.py')}"`, `"${packageFile}"`];
2018-11-15 17:42:01 +00:00
// istanbul ignore if
if (config.binarySource === 'docker') {
logger.info('Running python via docker');
await exec(`docker pull renovate/pip`);
2018-11-15 17:42:01 +00:00
cmd = 'docker';
args.unshift(
'run',
'-i',
'--rm',
// volume
'-v',
`${cwd}:${cwd}`,
'-v',
`${__dirname}:${__dirname}`,
// cwd
'-w',
cwd,
// image
'renovate/pip',
'python'
);
} else {
logger.info('Running python via global command');
cmd = await getPythonAlias();
2018-11-15 17:42:01 +00:00
}
logger.debug({ cmd, args }, 'python command');
const res = await exec(`${cmd} ${args.join(' ')}`, {
cwd,
timeout: 5000,
});
2019-05-25 05:07:16 +00:00
// istanbul ignore if
if (res.stderr) {
const stderr = res.stderr.replace(/.*\n\s*import imp/, '').trim();
if (stderr.length) {
logger.warn(
{ stdout: res.stdout, stderr: res.stderr },
'Error in read setup file'
);
}
2018-11-15 17:42:01 +00:00
}
return JSON.parse(res.stdout);
2018-11-15 17:42:01 +00:00
}
export async function extractPackageFile(
content: string,
packageFile: string,
config: ExtractConfig
): Promise<PackageFile | null> {
2018-11-15 17:42:01 +00:00
logger.debug('pip_setup.extractPackageFile()');
let setup: PythonSetup;
2018-11-15 17:42:01 +00:00
try {
setup = await extractSetupFile(content, packageFile, config);
} catch (err) {
logger.warn({ err, content, packageFile }, 'Failed to read setup.py file');
2018-11-15 17:42:01 +00:00
return null;
}
const requires: string[] = [];
2018-11-15 17:42:01 +00:00
if (setup.install_requires) {
requires.push(...setup.install_requires);
}
if (setup.extras_require) {
for (const req of Object.values(setup.extras_require)) {
requires.push(...req);
}
}
const regex = new RegExp(`^${dependencyPattern}`);
const lines = content.split('\n');
const deps = requires
.map(req => {
const lineNumber = lines.findIndex(l => l.includes(req));
if (lineNumber === -1) {
return null;
}
const rawline = lines[lineNumber];
let dep: PackageDependency = {};
2018-11-15 17:42:01 +00:00
const [, comment] = rawline.split('#').map(part => part.trim());
if (isSkipComment(comment)) {
dep.skipReason = 'ignored';
}
regex.lastIndex = 0;
const matches = regex.exec(req);
if (!matches) {
return null;
}
const [, depName, , currentValue] = matches;
dep = {
...dep,
depName,
currentValue,
2019-07-29 06:07:34 +00:00
managerData: { lineNumber },
datasource: 'pypi',
2018-11-15 17:42:01 +00:00
};
return dep;
})
.filter(Boolean)
.sort((a, b) =>
2019-07-29 06:07:34 +00:00
a.managerData.lineNumber === b.managerData.lineNumber
2019-08-28 04:46:48 +00:00
? a.depName.localeCompare(b.depName)
2019-07-29 06:07:34 +00:00
: a.managerData.lineNumber - b.managerData.lineNumber
);
// istanbul ignore if
2018-11-15 17:42:01 +00:00
if (!deps.length) {
return null;
}
return { deps };
}