Skip to content

Commit

Permalink
WIP: ENH: Add all-repos remote module configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jun 19, 2024
1 parent f7f9bd6 commit 9e9d513
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions Utilities/Maintenance/all_repos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output/
3 changes: 3 additions & 0 deletions Utilities/Maintenance/all_repos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# itk-all-repos

Proprose changes across multiple ITK repositories, e.g. remote modules.
13 changes: 13 additions & 0 deletions Utilities/Maintenance/all_repos/all-repos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"output_dir": "output",
"source": "itk_all_repos.itk_remote_modules",
"source_settings": {
"itk_source_dir": "/home/matt/src/ITK"
},
"push": "all_repos.push.github_pull_request",
"push_settings": {
"api_key_env": "ALL_REPOS_KEY",
"fork": true,
"username": "thewtex"
}
}
1 change: 1 addition & 0 deletions Utilities/Maintenance/all_repos/itk_all_repos/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "0.1.0"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import collections
from typing import Dict
from pathlib import Path
import glob

Settings = collections.namedtuple('Settings', ('itk_source_dir'))
Settings.__new__.__defaults__ = ()

def list_repos(settings: Settings) -> Dict[str, str]:
# get the path to the current script
remotes_dir = Path(__file__).parent.absolute() / '..' / '..' / '..' / '..' / 'Modules' / 'Remote'
result = {}
print(remotes_dir)
remotes = glob.glob(str(remotes_dir / '*.remote.cmake'))
print(remotes)
for remote in remotes:
with open(remote, 'r') as f:
lines = f.readlines()
for line in lines:
if 'GIT_REPOSITORY' in line:
repo = line.split('GIT_REPOSITORY ')[-1].strip()
repo = repo.replace('https://github.com/', 'git@github.com:')
if repo.endswith('.git'):
repo = repo[:-4]
name = Path(remote).stem[:-7]
result[name] = repo

# print(result)
# return { "ITKMontage": "git@github.com:InsightSoftwareConsortium/ITKMontage" }
return result
35 changes: 35 additions & 0 deletions Utilities/Maintenance/all_repos/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "itk-all-repos"
dynamic = ["version"]
description = 'Propose changes across multiple ITK repositories.'
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = [
{ name = "Matt McCormick", email = "matt.mccormick@kitware.com" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"all-repos",
]

[project.urls]
Documentation = "https://github.com/InsightSoftwareConsortium/ITK"

[tool.hatch.version]
path = "itk_all_repos/__about__.py"
86 changes: 86 additions & 0 deletions Utilities/Maintenance/all_repos/remote_ci_upgrade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from __future__ import annotations

import argparse
import sys
from typing import Sequence
from pathlib import Path

from all_repos import autofix_lib
from all_repos.config import Config
from all_repos.grep import repos_matching

ci_version = '5.4.0'

remote_action_config = f"""
name: Build, test, package
on: [push,pull_request]
jobs:
cxx-build-workflow:
uses: InsightSoftwareConsortium/ITKRemoteModuleBuildTestPackageAction/.github/workflows/build-test-cxx.yml@v{ci_version}
python-build-workflow:
uses: InsightSoftwareConsortium/ITKRemoteModuleBuildTestPackageAction/.github/workflows/build-test-package-python.yml@v{ci_version}
with:
test-notebooks: false
secrets:
pypi_password: ${{{{ secrets.pypi_password }}}}
"""

def find_repos(config: Config) -> set[str]:
return repos_matching(config, ('=', '--', '.github/workflows/build-test-package.yml'))

def apply_fix() -> None:
import yaml
with open('.github/workflows/build-test-package.yml', 'r') as fp:
ci_config = yaml.safe_load(fp)
# Upgrade to the new action configured in the repository
if not 'jobs' in ci_config or not 'cxx-build-workflow' in ci_config['jobs']:
with open('.github/workflows/build-test-package.yml', 'w') as fp:
fp.write(remote_action_config)
return
cxx_uses_old = ci_config['jobs']['cxx-build-workflow']['uses']
cxx_uses_new = '@'.join(cxx_uses_old.split('@')[:-1] + [f'v{ci_version}'])

python_uses_old = ci_config['jobs']['python-build-workflow']['uses']
python_uses_new = '@'.join(python_uses_old.split('@')[:-1] + [f'v{ci_version}'])

with open('.github/workflows/build-test-package.yml', 'r') as fp:
ci_config = fp.read()
with open('.github/workflows/build-test-package.yml', 'w') as fp:
ci_config = ci_config.replace(cxx_uses_old, cxx_uses_new)
ci_config = ci_config.replace(python_uses_old, python_uses_new)
fp.write(ci_config)

def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
autofix_lib.add_fixer_args(parser)
args = parser.parse_args(argv)

autofix_lib.assert_importable(
'yaml', install='pyyaml',
)

repos, config, commit, autofix_settings = autofix_lib.from_cli(
args,
find_repos=find_repos,
msg=f"""ENH: Upgrade CI for ITK {ci_version}
Upgrade CI testing to use the latest ITK {ci_version} version and CI configuration.
""",
branch_name=f'ci-itk-{ci_version}-secret-syntax',
)

autofix_lib.fix(
repos,
apply_fix=apply_fix,
config=config,
commit=commit,
autofix_settings=autofix_settings,
)
return 0


if __name__ == '__main__':
raise SystemExit(main())

0 comments on commit 9e9d513

Please sign in to comment.