Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python autofix update #1201

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions .autofix/fixers/update-lang-python.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
const os = require('os');
const util = require('util');
const exec = util.promisify(require('child_process').exec);

exports.register = async (fixers) => {
const { stdout, stderr } = await exec('pyenv update 2>&1 && pyenv install --list');
if (stderr) {
throw stderr;
// Adapted from https://stackoverflow.com/a/71331716/10199319 (CC BY-SA 4.0)
function sortVersions(versionArray, reverse = false) {
let semVerArr = versionArray.map(i => i.replace(/(\d+)/g, m => +m + 100000)).sort(); // +m is just a short way of converting the match to int
if (reverse) {
semVerArr = semVerArr.reverse();
}

return semVerArr.map(i => i.replace(/(\d+)/g, m => +m - 100000))
}

exports.register = async (fixers) => {
const response = await fetch("https://raw.githubusercontent.com/endoflife-date/release-data/main/releases/python.json");
const data = await response.json();

const versions = sortVersions(Object.keys(data));

const patchVersionReplacements = {};
for (const line of stdout.split('\n').slice(1,-1)) {
const match = line.trim().match(/^([a-z1-9\.]+-)?(\d+\.\d+\.)(\d+)$/);
for (const version of versions) {
const match = version.match(/^(\d+\.\d+)\.(\d+)$/);
if (!match) {
continue;
}

const pattern = ((match[1] || '') + match[2]).replace(/\./g, '\\.') + '[0-9][0-9]*';
patchVersionReplacements[pattern] = match[0];
// Only capturing major and minor versions in the pattern
const segments = version.split('.');
const prefix = segments.slice(0, -1).join('\\.');
const pattern = prefix + '\\.[0-9][0-9]*';
patchVersionReplacements[pattern] = version;
}

fixers[0].push({
id: 'update-lang-python',
cmd: Object.keys(patchVersionReplacements).map(pattern => {
return `sed ${os.type() === 'Darwin' ? '-i "" -E' : '-i -e'} "s/\\(PYTHON_VERSION.*\\)${pattern}/\\1${patchVersionReplacements[pattern]}/g" chunks/lang-python/chunk.yaml`;
return `sed ${os.type() === 'Darwin' ? '-i "" -E' : '-i -e'} "s/\\(PYTHON_VERSION.*\\)${pattern}/\\1${patchVersionReplacements[pattern]}/g" chunks/lang-python/chunk.yaml`;
}).join('\n'),
description: 'update lang python',
});
Expand Down
Loading