/
artifacts.ts
114 lines (111 loc) · 3.34 KB
/
artifacts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { parse } from 'toml';
import { logger } from '../../logger';
import { ExecOptions, exec } from '../../util/exec';
import {
deleteLocalFile,
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../util/fs';
import {
UpdateArtifact,
UpdateArtifactsConfig,
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 data = parse(existingLockFileContent);
if (data?.metadata?.['python-versions']) {
return data?.metadata?.['python-versions'];
}
} catch (err) {
// Do nothing
}
return undefined;
}
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`poetry.updateArtifacts(${packageFileName})`);
if (!is.nonEmptyArray(updatedDeps) && !config.isLockFileMaintenance) {
logger.debug('No updated poetry deps - returning null');
return null;
}
// Try poetry.lock first
let lockFileName = getSiblingFileName(packageFileName, 'poetry.lock');
let existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
// Try pyproject.lock next
lockFileName = getSiblingFileName(packageFileName, 'pyproject.lock');
existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug(`No lock file found`);
return null;
}
}
logger.debug(`Updating ${lockFileName}`);
try {
await writeLocalFile(packageFileName, newPackageFileContent);
const cmd: string[] = [];
if (config.isLockFileMaintenance) {
await deleteLocalFile(lockFileName);
cmd.push('poetry update --lock --no-interaction');
} else {
for (let i = 0; i < updatedDeps.length; i += 1) {
const dep = updatedDeps[i];
cmd.push(`poetry update --lock --no-interaction ${quote(dep)}`);
}
}
const tagConstraint = getPythonConstraint(existingLockFileContent, config);
const poetryRequirement = config.compatibility?.poetry || 'poetry';
const poetryInstall = 'pip install ' + quote(poetryRequirement);
const execOptions: ExecOptions = {
cwdFile: packageFileName,
docker: {
image: 'renovate/python',
tagConstraint,
tagScheme: 'poetry',
preCommands: [poetryInstall],
},
};
await exec(cmd, execOptions);
const newPoetryLockContent = await readLocalFile(lockFileName, 'utf8');
if (existingLockFileContent === newPoetryLockContent) {
logger.debug(`${lockFileName} is unchanged`);
return null;
}
logger.debug(`Returning updated ${lockFileName}`);
return [
{
file: {
name: lockFileName,
contents: newPoetryLockContent,
},
},
];
} catch (err) {
logger.debug({ err }, `Failed to update ${lockFileName} file`);
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.stdout + '\n' + err.stderr,
},
},
];
}
}