2020-11-12 20:50:06 +00:00
|
|
|
import { quote } from 'shlex';
|
2021-03-07 20:40:40 +00:00
|
|
|
import { TEMPORARY_ERROR } from '../../constants/error-messages';
|
2019-07-25 06:17:19 +00:00
|
|
|
import { logger } from '../../logger';
|
2020-05-01 16:03:48 +00:00
|
|
|
import { ExecOptions, exec } from '../../util/exec';
|
2020-07-05 19:22:13 +00:00
|
|
|
import {
|
|
|
|
deleteLocalFile,
|
|
|
|
ensureCacheDir,
|
|
|
|
readLocalFile,
|
|
|
|
writeLocalFile,
|
|
|
|
} from '../../util/fs';
|
2020-07-04 11:52:33 +00:00
|
|
|
import { getRepoStatus } from '../../util/git';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type {
|
2020-04-30 05:12:20 +00:00
|
|
|
UpdateArtifact,
|
|
|
|
UpdateArtifactsConfig,
|
2020-05-01 16:03:48 +00:00
|
|
|
UpdateArtifactsResult,
|
2021-03-02 20:44:55 +00:00
|
|
|
} from '../types';
|
2018-11-23 06:58:25 +00:00
|
|
|
|
2020-04-30 05:12:20 +00:00
|
|
|
function getPythonConstraint(
|
|
|
|
existingLockFileContent: string,
|
|
|
|
config: UpdateArtifactsConfig
|
|
|
|
): string | undefined | null {
|
2020-09-30 09:02:25 +00:00
|
|
|
const { constraints = {} } = config;
|
|
|
|
const { python } = constraints;
|
2020-04-30 05:12:20 +00:00
|
|
|
|
|
|
|
if (python) {
|
|
|
|
logger.debug('Using python constraint from config');
|
|
|
|
return python;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const pipfileLock = JSON.parse(existingLockFileContent);
|
|
|
|
if (pipfileLock?._meta?.requires?.python_version) {
|
2020-08-27 06:59:23 +00:00
|
|
|
const pythonVersion: string = pipfileLock._meta.requires.python_version;
|
|
|
|
return `== ${pythonVersion}.*`;
|
2020-04-30 05:12:20 +00:00
|
|
|
}
|
|
|
|
if (pipfileLock?._meta?.requires?.python_full_version) {
|
2020-08-27 06:59:23 +00:00
|
|
|
const pythonFullVersion: string =
|
|
|
|
pipfileLock._meta.requires.python_full_version;
|
|
|
|
return `== ${pythonFullVersion}`;
|
2020-04-30 05:12:20 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:50:06 +00:00
|
|
|
function getPipenvConstraint(
|
|
|
|
existingLockFileContent: string,
|
|
|
|
config: UpdateArtifactsConfig
|
|
|
|
): string | null {
|
|
|
|
const { constraints = {} } = config;
|
|
|
|
const { pipenv } = constraints;
|
|
|
|
|
|
|
|
if (pipenv) {
|
|
|
|
logger.debug('Using pipenv constraint from config');
|
|
|
|
return pipenv;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const pipfileLock = JSON.parse(existingLockFileContent);
|
|
|
|
if (pipfileLock?.default?.pipenv?.version) {
|
|
|
|
const pipenvVersion: string = pipfileLock.default.pipenv.version;
|
|
|
|
return pipenvVersion;
|
|
|
|
}
|
|
|
|
if (pipfileLock?.develop?.pipenv?.version) {
|
|
|
|
const pipenvVersion: string = pipfileLock.develop.pipenv.version;
|
|
|
|
return pipenvVersion;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:18:34 +00:00
|
|
|
export async function updateArtifacts({
|
|
|
|
packageFileName: pipfileName,
|
|
|
|
newPackageFileContent: newPipfileContent,
|
|
|
|
config,
|
|
|
|
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
|
2019-06-09 06:18:41 +00:00
|
|
|
logger.debug(`pipenv.updateArtifacts(${pipfileName})`);
|
2020-01-07 15:32:21 +00:00
|
|
|
|
2020-07-05 19:22:13 +00:00
|
|
|
const cacheDir = await ensureCacheDir('./others/pipenv', 'PIPENV_CACHE_DIR');
|
2020-01-07 15:32:21 +00:00
|
|
|
|
2018-11-23 06:58:25 +00:00
|
|
|
const lockFileName = pipfileName + '.lock';
|
2020-05-13 10:45:02 +00:00
|
|
|
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
|
2018-11-23 06:58:25 +00:00
|
|
|
if (!existingLockFileContent) {
|
|
|
|
logger.debug('No Pipfile.lock found');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
2020-07-03 19:29:04 +00:00
|
|
|
await writeLocalFile(pipfileName, newPipfileContent);
|
2020-04-30 10:06:37 +00:00
|
|
|
if (config.isLockFileMaintenance) {
|
2020-07-03 19:29:04 +00:00
|
|
|
await deleteLocalFile(lockFileName);
|
2020-04-30 10:06:37 +00:00
|
|
|
}
|
2020-02-09 12:26:48 +00:00
|
|
|
const cmd = 'pipenv lock';
|
2020-04-30 05:12:20 +00:00
|
|
|
const tagConstraint = getPythonConstraint(existingLockFileContent, config);
|
2020-11-12 20:50:06 +00:00
|
|
|
const pipenvConstraint = getPipenvConstraint(
|
|
|
|
existingLockFileContent,
|
|
|
|
config
|
|
|
|
);
|
2020-02-09 12:26:48 +00:00
|
|
|
const execOptions: ExecOptions = {
|
2020-12-28 14:20:49 +00:00
|
|
|
cwdFile: pipfileName,
|
2020-02-09 12:26:48 +00:00
|
|
|
extraEnv: {
|
|
|
|
PIPENV_CACHE_DIR: cacheDir,
|
|
|
|
},
|
|
|
|
docker: {
|
2021-03-15 13:23:38 +00:00
|
|
|
image: 'python',
|
2020-04-30 05:12:20 +00:00
|
|
|
tagConstraint,
|
|
|
|
tagScheme: 'pep440',
|
2020-11-12 20:50:06 +00:00
|
|
|
preCommands: [
|
|
|
|
`pip install --user ${quote(`pipenv${pipenvConstraint}`)}`,
|
|
|
|
],
|
2020-02-09 12:26:48 +00:00
|
|
|
volumes: [cacheDir],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
logger.debug({ cmd }, 'pipenv lock command');
|
|
|
|
await exec(cmd, execOptions);
|
2020-07-04 11:52:33 +00:00
|
|
|
const status = await getRepoStatus();
|
2020-07-18 06:42:32 +00:00
|
|
|
if (!status?.modified.includes(lockFileName)) {
|
2019-05-25 04:23:44 +00:00
|
|
|
return null;
|
2018-11-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
logger.debug('Returning updated Pipfile.lock');
|
2019-02-08 13:50:06 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
file: {
|
|
|
|
name: lockFileName,
|
2020-07-03 19:29:04 +00:00
|
|
|
contents: await readLocalFile(lockFileName, 'utf8'),
|
2019-02-08 13:50:06 +00:00
|
|
|
},
|
2018-11-23 06:58:25 +00:00
|
|
|
},
|
2019-02-08 13:50:06 +00:00
|
|
|
];
|
2018-11-23 06:58:25 +00:00
|
|
|
} catch (err) {
|
2021-03-04 21:27:21 +00:00
|
|
|
// istanbul ignore if
|
2021-03-07 20:40:40 +00:00
|
|
|
if (err.message === TEMPORARY_ERROR) {
|
2021-03-04 21:27:21 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2020-04-28 08:47:30 +00:00
|
|
|
logger.debug({ err }, 'Failed to update Pipfile.lock');
|
2019-02-08 13:50:06 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
artifactError: {
|
|
|
|
lockFile: lockFileName,
|
|
|
|
stderr: err.message,
|
|
|
|
},
|
2018-11-23 06:58:25 +00:00
|
|
|
},
|
2019-02-08 13:50:06 +00:00
|
|
|
];
|
2018-11-23 06:58:25 +00:00
|
|
|
}
|
|
|
|
}
|