Skip to content

Instantly share code, notes, and snippets.

@drGrove
Created March 19, 2025 23:18
Show Gist options
  • Save drGrove/9df322883b17bb267490b00891b42ea3 to your computer and use it in GitHub Desktop.
Save drGrove/9df322883b17bb267490b00891b42ea3 to your computer and use it in GitHub Desktop.
A script to run against the stagex.tools packages folder to get the current list of packages and their respective versions
#!/usr/bin/env python3
import os
import toml
import sys
def find_toml_files(directory):
"""Recursively find all TOML files in the given directory."""
toml_files = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.toml'):
toml_files.append(os.path.join(root, file))
return toml_files
def extract_package_info(toml_file):
"""Extract package name and version from TOML file."""
try:
data = toml.load(toml_file)
if 'package' in data and 'name' in data['package'] and 'version' in data['package']:
return data['package']['name'], data['package']['version']
except Exception as e:
print(f"Error processing {toml_file}: {e}", file=sys.stderr)
return None, None
def format_output(root_dir, toml_file, package_name, package_version):
"""Format the output string according to the specified format."""
# Get the relative path from root_dir
rel_path = os.path.relpath(toml_file, root_dir)
# Extract the subfolder name (first directory after root)
path_parts = rel_path.split(os.sep)
subfolder = path_parts[0] if len(path_parts) > 1 else ""
return f"stagex/{subfolder}-{package_name}:{package_version}"
def main():
if len(sys.argv) != 2:
print("Usage: stagex_current_version <directory>")
sys.exit(1)
directory = sys.argv[1]
if not os.path.isdir(directory):
print(f"Error: {directory} is not a directory", file=sys.stderr)
sys.exit(1)
toml_files = find_toml_files(directory)
for toml_file in toml_files:
package_name, package_version = extract_package_info(toml_file)
if package_name and package_version:
output = format_output(directory, toml_file, package_name, package_version)
print(output)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment