2022-01-13 04:23:04 -07:00
|
|
|
import { RANGE_PATTERN } from '@renovatebot/pep440';
|
2024-08-19 07:15:27 -06:00
|
|
|
import type { lexer, parser } from 'good-enough-parser';
|
|
|
|
import { lang, query as q } from 'good-enough-parser';
|
2022-03-03 02:35:26 -07:00
|
|
|
import { regEx } from '../../../util/regex';
|
2021-08-03 05:38:46 -06:00
|
|
|
import { PypiDatasource } from '../../datasource/pypi';
|
2024-06-06 05:23:47 -06:00
|
|
|
import { normalizePythonDepName } from '../../datasource/pypi/common';
|
2023-02-19 05:43:48 -07:00
|
|
|
import type {
|
|
|
|
ExtractConfig,
|
|
|
|
PackageDependency,
|
|
|
|
PackageFileContent,
|
|
|
|
} from '../types';
|
2018-11-15 10:42:01 -07:00
|
|
|
|
2021-11-10 04:20:12 -07:00
|
|
|
interface ManagerData {
|
|
|
|
lineNumber: number;
|
2019-12-20 00:51:20 -07:00
|
|
|
}
|
|
|
|
|
2023-02-19 05:43:48 -07:00
|
|
|
type Context = PackageFileContent<ManagerData>;
|
2021-11-10 04:20:12 -07:00
|
|
|
|
|
|
|
const python = lang.createLang('python');
|
2019-03-17 08:54:31 -06:00
|
|
|
|
2021-11-10 04:20:12 -07:00
|
|
|
// Optimize regex memory usage when we don't need named groups
|
|
|
|
function cleanupNamedGroups(regexSource: string): string {
|
|
|
|
return regexSource.replace(/\(\?<\w+>/g, '(?:');
|
2019-03-17 08:54:31 -06:00
|
|
|
}
|
2020-04-24 08:17:45 -06:00
|
|
|
|
2021-11-10 04:20:12 -07:00
|
|
|
const rangePattern = cleanupNamedGroups(RANGE_PATTERN);
|
|
|
|
const versionPattern = `(?:${rangePattern}(?:\\s*,\\s*${rangePattern})*)`;
|
2023-01-09 05:34:13 -07:00
|
|
|
const depNamePattern = '(?:[a-zA-Z][-_a-zA-Z0-9\\.]*[a-zA-Z0-9])';
|
2021-11-10 04:20:12 -07:00
|
|
|
const depPattern = [
|
|
|
|
'^',
|
|
|
|
`(?<depName>${depNamePattern})`,
|
|
|
|
`(?<extra>(?:\\[\\s*(?:${depNamePattern}(?:\\s*,\\s*${depNamePattern})*\\s*)\\])?)`,
|
|
|
|
`(?<currentValue>${versionPattern})`,
|
|
|
|
].join('\\s*');
|
|
|
|
|
|
|
|
const extractRegex = regEx(depPattern);
|
|
|
|
|
|
|
|
// Extract dependency string
|
|
|
|
function depStringHandler(
|
|
|
|
ctx: Context,
|
2023-11-07 08:50:29 -07:00
|
|
|
token: lexer.StringValueToken,
|
2021-11-10 04:20:12 -07:00
|
|
|
): Context {
|
|
|
|
const depStr = token.value;
|
|
|
|
const match = extractRegex.exec(depStr);
|
2023-08-15 03:31:15 -06:00
|
|
|
// TODO #22198
|
2022-04-19 11:46:07 -06:00
|
|
|
const { depName, currentValue } = match!.groups!;
|
2021-11-10 04:20:12 -07:00
|
|
|
|
|
|
|
const dep: PackageDependency<ManagerData> = {
|
|
|
|
depName,
|
2024-06-06 05:23:47 -06:00
|
|
|
packageName: normalizePythonDepName(depName),
|
2021-11-10 04:20:12 -07:00
|
|
|
currentValue,
|
|
|
|
managerData: {
|
|
|
|
lineNumber: token.line - 1,
|
2020-07-18 02:03:05 -06:00
|
|
|
},
|
2021-11-10 04:20:12 -07:00
|
|
|
datasource: PypiDatasource.id,
|
|
|
|
};
|
|
|
|
|
2024-08-26 03:09:26 -06:00
|
|
|
if (currentValue?.startsWith('==')) {
|
|
|
|
dep.currentVersion = currentValue.replace(regEx(/^==\s*/), '');
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:20:12 -07:00
|
|
|
return { ...ctx, deps: [...ctx.deps, dep] };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add `skip-reason` for dependencies annotated
|
|
|
|
// with "# renovate: ignore" comment
|
|
|
|
function depSkipHandler(ctx: Context): Context {
|
|
|
|
const dep = ctx.deps[ctx.deps.length - 1];
|
|
|
|
const deps = ctx.deps.slice(0, -1);
|
2022-01-21 00:59:36 -07:00
|
|
|
deps.push({ ...dep, skipReason: 'ignored' });
|
2021-11-10 04:20:12 -07:00
|
|
|
return { ...ctx, deps };
|
2018-11-15 10:42:01 -07:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:20:12 -07:00
|
|
|
const incompleteDepString = q
|
|
|
|
.str<Context>(new RegExp(cleanupNamedGroups(depPattern)))
|
|
|
|
.op(/^\+|\*$/);
|
|
|
|
|
|
|
|
const depString = q
|
|
|
|
.str<Context>(new RegExp(cleanupNamedGroups(depPattern)), depStringHandler)
|
|
|
|
.opt(
|
|
|
|
q
|
|
|
|
.opt(q.op<Context>(','))
|
2023-11-07 08:50:29 -07:00
|
|
|
.comment(/^#\s*renovate\s*:\s*ignore\s*$/, depSkipHandler),
|
2021-11-10 04:20:12 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
const query = q.alt(incompleteDepString, depString);
|
|
|
|
|
|
|
|
export function extractPackageFile(
|
2019-07-25 00:17:19 -06:00
|
|
|
content: string,
|
2021-11-10 04:20:12 -07:00
|
|
|
_packageFile: string,
|
2023-11-07 08:50:29 -07:00
|
|
|
_config: ExtractConfig,
|
2023-02-19 05:43:48 -07:00
|
|
|
): PackageFileContent | null {
|
2023-01-04 11:08:56 -07:00
|
|
|
const res = python.query<Context, parser.Node>(content, query, { deps: [] });
|
2021-11-10 04:20:12 -07:00
|
|
|
return res?.deps?.length ? res : null;
|
2018-11-15 10:42:01 -07:00
|
|
|
}
|