renovate/lib/manager/pipenv/artifacts.ts

103 lines
2.8 KiB
TypeScript
Raw Normal View History

import { logger } from '../../logger';
2020-05-01 16:03:48 +00:00
import { ExecOptions, exec } from '../../util/exec';
import {
deleteLocalFile,
ensureCacheDir,
readLocalFile,
writeLocalFile,
} from '../../util/fs';
import { getRepoStatus } from '../../util/git';
import {
UpdateArtifact,
UpdateArtifactsConfig,
2020-05-01 16:03:48 +00:00
UpdateArtifactsResult,
} from '../common';
function getPythonConstraint(
existingLockFileContent: string,
config: UpdateArtifactsConfig
): string | undefined | null {
const { compatibility = {} } = config;
const { python } = compatibility;
if (python) {
logger.debug('Using python constraint from config');
return python;
}
try {
const pipfileLock = JSON.parse(existingLockFileContent);
if (pipfileLock?._meta?.requires?.python_version) {
return '== ' + pipfileLock._meta.requires.python_version + '.*';
}
if (pipfileLock?._meta?.requires?.python_full_version) {
return '== ' + pipfileLock._meta.requires.python_full_version;
}
} catch (err) {
// Do nothing
}
return undefined;
}
export async function updateArtifacts({
packageFileName: pipfileName,
newPackageFileContent: newPipfileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`pipenv.updateArtifacts(${pipfileName})`);
const cacheDir = await ensureCacheDir('./others/pipenv', 'PIPENV_CACHE_DIR');
logger.debug('Using pipenv cache ' + cacheDir);
const lockFileName = pipfileName + '.lock';
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug('No Pipfile.lock found');
return null;
}
try {
await writeLocalFile(pipfileName, newPipfileContent);
if (config.isLockFileMaintenance) {
await deleteLocalFile(lockFileName);
}
const cmd = 'pipenv lock';
const tagConstraint = getPythonConstraint(existingLockFileContent, config);
const execOptions: ExecOptions = {
extraEnv: {
PIPENV_CACHE_DIR: cacheDir,
},
docker: {
image: 'renovate/python',
tagConstraint,
tagScheme: 'pep440',
preCommands: ['pip install --user pipenv'],
volumes: [cacheDir],
},
};
logger.debug({ cmd }, 'pipenv lock command');
await exec(cmd, execOptions);
const status = await getRepoStatus();
if (!(status && status.modified.includes(lockFileName))) {
return null;
}
logger.debug('Returning updated Pipfile.lock');
return [
{
file: {
name: lockFileName,
contents: await readLocalFile(lockFileName, 'utf8'),
},
},
];
} catch (err) {
2020-04-28 08:47:30 +00:00
logger.debug({ err }, 'Failed to update Pipfile.lock');
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.message,
},
},
];
}
}