|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +/** |
| 4 | + * Script to reverse Python versions JSON data |
| 5 | + * Transforms from version-centric to platform-arch-centric structure |
| 6 | + */ |
| 7 | + |
| 8 | +function reverseVersionsJson(inputJson) { |
| 9 | + // Initialize the result object |
| 10 | + const result = {}; |
| 11 | + |
| 12 | + // Process each version entry in the input array |
| 13 | + for (const versionEntry of inputJson) { |
| 14 | + const pythonVersion = versionEntry.version; |
| 15 | + |
| 16 | + // Process each file in the version's files array |
| 17 | + for (const file of versionEntry.files) { |
| 18 | + // Construct the key based on platform, platform_version (if exists), and arch |
| 19 | + let key = file.platform; |
| 20 | + if (file.platform_version) { |
| 21 | + key += `-${file.platform_version}`; |
| 22 | + } |
| 23 | + key += `-${file.arch}`; |
| 24 | + |
| 25 | + // If this key doesn't exist in the result yet, initialize it as an empty array |
| 26 | + if (!result[key]) { |
| 27 | + result[key] = []; |
| 28 | + } |
| 29 | + |
| 30 | + // Add the Python version to this platform/arch combination if not already present |
| 31 | + if (!result[key].includes(pythonVersion)) { |
| 32 | + result[key].push(pythonVersion); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return result; |
| 38 | +} |
| 39 | + |
| 40 | +// Fetch data from GitHub repo |
| 41 | +async function fetchVersionsManifest() { |
| 42 | + try { |
| 43 | + const response = await fetch('https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json'); |
| 44 | + if (!response.ok) { |
| 45 | + throw new Error(`Failed to fetch data: ${response.status} ${response.statusText}`); |
| 46 | + } |
| 47 | + const data = await response.json(); |
| 48 | + return data; |
| 49 | + } catch (error) { |
| 50 | + console.error('Error fetching versions manifest:', error); |
| 51 | + throw error; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Main function to process the data |
| 56 | +async function main() { |
| 57 | + try { |
| 58 | + const inputData = await fetchVersionsManifest(); |
| 59 | + const reversed = reverseVersionsJson(inputData); |
| 60 | + console.log(JSON.stringify(reversed, null, 2)); |
| 61 | + } catch (error) { |
| 62 | + console.error('Error in main function:', error); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Execute the main function |
| 67 | +main(); |
0 commit comments