|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os, shutil |
| 3 | +from yaml import load, dump |
| 4 | +try: |
| 5 | + from yaml import CLoader as Loader, CDumper as Dumper |
| 6 | +except ImportError: |
| 7 | + from yaml import Loader, Dumper |
| 8 | +from argparse import ArgumentParser |
| 9 | + |
| 10 | +# For interfacing Git REST API |
| 11 | +import re |
| 12 | +import datetime |
| 13 | +from github import Github |
| 14 | +from github.GithubException import * |
| 15 | +from github.InputGitAuthor import InputGitAuthor |
| 16 | + |
| 17 | +# Local interfacing of repo |
| 18 | +from git import Repo |
| 19 | + |
| 20 | +from versioning import update_version_number_in_freertos_component |
| 21 | +from versioning import update_freertos_version_macros |
| 22 | + |
| 23 | +from release import BaseRelease |
| 24 | +from release import info |
| 25 | +from release import indent_level |
| 26 | +from release import logIndentPush |
| 27 | +from release import logIndentPop |
| 28 | + |
| 29 | +class UpdateSourceVersionKernel(BaseRelease): |
| 30 | + def __init__(self, mGit, version, commit='HEAD', git_ssh=False, git_org='FreeRTOS', repo_path=None, branch='main', main_br_version='', do_not_push=False): |
| 31 | + super().__init__(mGit, version, commit=commit, git_ssh=git_ssh, git_org=git_org, repo_path=repo_path, branch=branch, do_not_push=do_not_push) |
| 32 | + |
| 33 | + self.repo_name = '%s/FreeRTOS-Kernel' % self.git_org |
| 34 | + self.repo = mGit.get_repo(self.repo_name) |
| 35 | + self.tag = 'V%s' % version |
| 36 | + self.description = 'Contains source code for the FreeRTOS Kernel.' |
| 37 | + self.zip_path = 'FreeRTOS-KernelV%s.zip' % self.version |
| 38 | + self.main_br_version = main_br_version |
| 39 | + |
| 40 | + # Parent ctor configures local_repo if caller chooses to source local repo from repo_path. |
| 41 | + if self.repo_path is None: |
| 42 | + self.repo_path = 'tmp-release-freertos-kernel' |
| 43 | + if os.path.exists(self.repo_path): |
| 44 | + shutil.rmtree(self.repo_path) |
| 45 | + |
| 46 | + # Clone the target repo for creating the release autocommits |
| 47 | + remote_name = self.getRemoteEndpoint(self.repo_name) |
| 48 | + info('Downloading %s@%s to baseline auto-commits...' % (remote_name, commit), end='') |
| 49 | + self.local_repo = Repo.clone_from(remote_name, self.repo_path, progress=printDot, branch=self.branch) |
| 50 | + |
| 51 | + # In case user gave non-HEAD commit to baseline |
| 52 | + self.local_repo.git.checkout(commit) |
| 53 | + |
| 54 | + print() |
| 55 | + |
| 56 | + def updateVersionMacros(self, version_str): |
| 57 | + info('Updating version macros in task.h for "%s"' % version_str) |
| 58 | + |
| 59 | + # Extract major / minor / build from the version string. |
| 60 | + ver = re.search(r'([\d.]+)', version_str).group(1) |
| 61 | + (major, minor, build) = ver.split('.') |
| 62 | + update_freertos_version_macros(os.path.join(self.repo_path, 'include', 'task.h'), version_str, major, minor, build) |
| 63 | + |
| 64 | + self.commitChanges(self.commit_msg_prefix + 'Bump task.h version macros to "%s"' % version_str) |
| 65 | + |
| 66 | + def UpdateSourceVersionKernel(self): |
| 67 | + |
| 68 | + # Determine if we need to set a separate version macros for the main branch |
| 69 | + if (self.commit == 'HEAD') and len(self.main_br_version) > 0 and (self.main_br_version != self.version): |
| 70 | + # Update version macros for main branch |
| 71 | + self.updateVersionMacros(self.main_br_version) |
| 72 | + |
| 73 | + # Push the branch |
| 74 | + self.pushLocalCommits() |
| 75 | + |
| 76 | + # Revert the last commit in our working git repo |
| 77 | + self.local_repo.git.reset('--hard','HEAD^') |
| 78 | + |
| 79 | + # Update the version macros |
| 80 | + self.updateVersionMacros(self.version) |
| 81 | + |
| 82 | + if (self.commit == 'HEAD') and (self.main_br_version == self.version): |
| 83 | + # Share a task.h version number commit for main branch and release tag) |
| 84 | + self.pushLocalCommits() |
| 85 | + |
| 86 | + # When baselining off a non-HEAD commit, main is left unchanged by tagging a detached HEAD, |
| 87 | + # applying the autocommits, tagging, and pushing the new tag data to remote. |
| 88 | + # However in the detached HEAD state we don't have a branch to push to, so we skip |
| 89 | + |
| 90 | + # Update the header in each c/assembly file |
| 91 | + self.updateFileHeaderVersions(['FreeRTOS Kernel V','FreeRTOS Kernel <DEVELOPMENT BRANCH>'], 'FreeRTOS Kernel V%s' % self.version) |
| 92 | + |
| 93 | +def configure_argparser(): |
| 94 | + parser = ArgumentParser(description='FreeRTOS Release tool') |
| 95 | + |
| 96 | + parser.add_argument('git_org', |
| 97 | + type=str, |
| 98 | + metavar='GITHUB_ORG', |
| 99 | + help='Git organization owner for FreeRTOS and FreeRTOS-Kernel. (i.e. "<git-org>/FreeRTOS.git")') |
| 100 | + |
| 101 | + parser.add_argument('--new-kernel-version', |
| 102 | + default=None, |
| 103 | + required=False, |
| 104 | + help='Reset "main" to just before the autorelease for the specified kernel version")') |
| 105 | + |
| 106 | + parser.add_argument('--new-kernel-main-br-version', |
| 107 | + default='', |
| 108 | + required=False, |
| 109 | + help='Set the version in task.h on the kernel main branch to the specified value.') |
| 110 | + |
| 111 | + parser.add_argument('--kernel-commit', |
| 112 | + default='HEAD', |
| 113 | + required=False, |
| 114 | + metavar='GITHUB_SHA', |
| 115 | + help='Github SHA to baseline autorelease') |
| 116 | + |
| 117 | + parser.add_argument('--kernel-repo-path', |
| 118 | + type=str, |
| 119 | + default=None, |
| 120 | + required=False, |
| 121 | + help='Instead of downloading from git, use existing local repos for autocommits') |
| 122 | + |
| 123 | + parser.add_argument('--kernel-repo-branch', |
| 124 | + type=str, |
| 125 | + default='main', |
| 126 | + required=False, |
| 127 | + help='Branch of FreeRTOS Kernel repository to release.') |
| 128 | + |
| 129 | + parser.add_argument('--use-git-ssh', |
| 130 | + default=False, |
| 131 | + action='store_true', |
| 132 | + help='Use SSH endpoints to interface git remotes, instead of HTTPS') |
| 133 | + |
| 134 | + parser.add_argument('--unit-test', |
| 135 | + action='store_true', |
| 136 | + default=False, |
| 137 | + help='Run unit tests.') |
| 138 | + |
| 139 | + parser.add_argument('--do-not-push', |
| 140 | + action='store_true', |
| 141 | + default=False, |
| 142 | + help='Do not push the changes but only make local commits.') |
| 143 | + |
| 144 | + return parser |
| 145 | + |
| 146 | +def main(): |
| 147 | + cmd = configure_argparser() |
| 148 | + args = cmd.parse_args() |
| 149 | + |
| 150 | + # Auth |
| 151 | + if not args.do_not_push: |
| 152 | + assert 'GITHUB_TOKEN' in os.environ, 'Set env{GITHUB_TOKEN} to an authorized git PAT' |
| 153 | + mGit = Github(os.environ.get('GITHUB_TOKEN')) |
| 154 | + |
| 155 | + # Unit tests |
| 156 | + if args.unit_test: |
| 157 | + return |
| 158 | + |
| 159 | + # Update the source files |
| 160 | + if args.new_kernel_version: |
| 161 | + info('Starting to update source files...') |
| 162 | + logIndentPush() |
| 163 | + update_kernel_src_handler = UpdateSourceVersionKernel(mGit, args.new_kernel_version, args.kernel_commit, git_ssh=args.use_git_ssh, |
| 164 | + git_org=args.git_org, repo_path=args.kernel_repo_path, branch=args.kernel_repo_branch, |
| 165 | + main_br_version=args.new_kernel_main_br_version, do_not_push=args.do_not_push) |
| 166 | + update_kernel_src_handler.UpdateSourceVersionKernel() |
| 167 | + logIndentPop() |
| 168 | + |
| 169 | + info('Review script output for any unexpected behaviour.') |
| 170 | + info('Done.') |
| 171 | + |
| 172 | +if __name__ == '__main__': |
| 173 | + main() |
0 commit comments