Skip to content

Commit

Permalink
python update
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Oct 12, 2023
1 parent 5f14fff commit 238b9a8
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions .autofix/fixers/update-lang-python.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
const os = require('os');
const util = require('util');
const exec = util.promisify(require('child_process').exec);

function sortSemVer(arr, reverse = false) {
let semVerArr = arr.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 { stdout, stderr } = await exec('pyenv update 2>&1 && pyenv install --list');
if (stderr) {
throw stderr;
}
const response = await fetch("https://raw.githubusercontent.com/endoflife-date/release-data/main/releases/python.json");
const data = await response.json();

const versions = sortSemVer(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

0 comments on commit 238b9a8

Please sign in to comment.