summaryrefslogtreecommitdiff
path: root/.github/scripts/release.py
blob: 0ac4af3ffde2bc3bbd9d0e1e472b275998bdbff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/usr/bin/env python3
import os, shutil
from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper
from argparse import ArgumentParser

# For interfacing Git REST API
import re
import datetime
from github import Github
from github.GithubException import *
from github.InputGitAuthor import InputGitAuthor

# Local interfacing of repo
from git import Repo
from git import PushInfo

import zipfile

from versioning import update_version_number_in_freertos_component
from versioning import update_freertos_version_macros

from packager import prune_result_tree, prune_result_tree_v2
from packager import RELATIVE_FILE_EXCLUDES as FREERTOS_RELATIVE_FILE_EXCLUDES

# PyGithub Git -  https://github.com/PyGithub/PyGithub
# PyGithub Docs - https://pygithub.readthedocs.io/en/latest/github_objects
# REST API used by PyGithub - https://developer.github.com/v3/

indent_level = 0

# Files/Directories not to be included in the FreeRTOS-Kernel release.
KERNEL_RELEASE_EXCLUDE_FILES = [
    '.git',
    '.github',
    '.gitignore',
    '.gitmodules'
]

def logIndentPush():
    global indent_level
    indent_level += 4

def logIndentPop():
    global indent_level
    indent_level -= 4

    if indent_level < 0:
        indent_level = 0

def info(msg, end='\n'):
    print('[INFO]: %s%s' % (' ' * indent_level, str(msg)), end=end, flush=True)

def warning(msg):
    print('[WARNING]: %s%s' % (' ' * indent_level, str(msg)), flush=True)

def error(msg):
    print('[ERROR]: %s%s' % (' ' * indent_level, str(msg)), flush=True)

def debug(msg):
    print('[DEBUG]: %s%s' % (' ' * indent_level, str(msg)), flush=True)

# Callback for progress updates. For long spanning gitpython commands
def printDot(op_code, cur_count, max_count=None, message=''):
    if max_count == None or cur_count == max_count:
        print('.', end='')

class BaseRelease:
    def __init__(self, mGit, version, commit='HEAD', git_ssh=False, git_org='FreeRTOS', repo_path=None, branch='main', do_not_push=False):
        self.version = version
        self.tag_msg = 'Autocreated by FreeRTOS Git Tools.'
        self.commit = commit
        self.git_ssh = git_ssh
        self.git_org = git_org
        self.repo_path = repo_path
        self.local_repo = None
        self.branch = branch
        self.commit_msg_prefix = '[AUTO][RELEASE]: '
        self.description = ''
        self.mGit = mGit # Save a handle to the authed git session
        self.do_not_push = do_not_push

        if self.repo_path:
            info('Sourcing "%s" to make local commits' % self.repo_path)
            self.local_repo = Repo(self.repo_path)

    def CheckRelease(self):
        '''
        Sanity checks for the release. Verify version number format. Check zip size.
        Ensure version numbers were updated, etc.
        '''
        assert False, 'Add release check'

    def hasTag(self, tag):
        remote_tags = self.repo.get_tags()
        for t in remote_tags:
            if t.name == tag:
                return True

        return False

    def commitChanges(self, msg):
        assert self.local_repo != None, 'Failed to commit. Git repo uninitialized.'

        info('Committing: "%s"' % msg)
        self.local_repo.git.add(update=True)
        commit = self.local_repo.index.commit(msg)

    def getRemoteEndpoint(self, repo_name):
        if self.git_ssh:
            return 'git@github.com:%s.git' % repo_name
        else:
            return 'https://github.com/%s.git' % repo_name

    def printReleases(self):
        releases = self.repo.get_releases()
        for r in releases:
            print(r)

    def pushLocalCommits(self, force=False):
        if self.do_not_push:
            info('Skipping to push local commits...')
        else:
            info('Pushing local commits...')
            push_infos = self.local_repo.remote('origin').push(force=force)

            # Check for any errors
            for push_info in push_infos:
                assert 0 == push_info.flags & PushInfo.ERROR, 'Failed to push changes to ' + str(push_info)

    def pushTag(self):
        if self.do_not_push:
            info('Skipping to push tag "%s"' % self.tag)
        else:
            # Overwrite existing tags
            info('Pushing tag "%s"' % self.tag)
            tag_info = self.local_repo.create_tag(self.tag, message=self.tag_msg, force=True)
            self.local_repo.git.push(tags=True, force=True)

    def deleteTag(self):
        # Remove from remote
        if self.tag in self.local_repo.tags:
            info('Deleting tag "%s"' % self.tag)
            self.local_repo.remote('origin').push(':%s' % self.tag)
        else:
            info('A tag does not exists for "%s". No need to delete.' % self.tag)

    def updateSubmodulePointer(self, rel_path, ref):
        submodule = Repo(rel_path)
        submodule.remote('origin').fetch()
        submodule.git.checkout(ref)

    def updateFileHeaderVersions(self, old_version_substrings, new_version_string):
        info('Updating file header versions for "%s"...' % self.version, end='')
        n_updated = 0
        n_updated += update_version_number_in_freertos_component(self.repo_path,
                                                                 '.',
                                                                 old_version_substrings,
                                                                 new_version_string,
                                                                 exclude_hidden=True)

        n_updated += update_version_number_in_freertos_component(os.path.join('.github', 'scripts'),
                                                                 self.repo_path,
                                                                 old_version_substrings,
                                                                 new_version_string,
                                                                 exclude_hidden=False)

        print('...%d Files updated.' % n_updated)

        self.commitChanges(self.commit_msg_prefix + 'Bump file header version to "%s"' % self.version)

    def deleteGitRelease(self):
        info('Deleting git release endpoint for "%s"' % self.tag)

        try:
            self.repo.get_release(self.tag).delete_release()
        except UnknownObjectException:
            info('A release endpoint does not exist for "%s". No need to erase.' % self.tag)
        except:
            assert False, 'Encountered error while trying to delete git release endpoint'

    def rollbackAutoCommits(self, n_autocommits=2, n_search=25):
        info('Rolling back "%s" autocommits' % self.tag)

        if self.tag not in self.local_repo.tags:
            error('Could not find a SHA to rollback to for tag "%s"' % self.tag)
            return False

        # Search for auto release SHAs that match the release tag SHA
        tag_commit = self.local_repo.tag('refs/tags/%s' % self.tag).commit
        prior_commit = self.local_repo.commit(tag_commit.hexsha + '~%d' % n_autocommits)
        n_commits_searched = 0
        for commit in self.local_repo.iter_commits():
            if n_commits_searched > n_search:
                error('Exhaustively searched but could not find tag commit to rollback')
                return False

            if (self.commit_msg_prefix in commit.message
                    and commit.hexsha == tag_commit.hexsha
                    and self.version in commit.message):

                info('Found matching tag commit %s. Reverting to prior commit %s'
                        % (tag_commit.hexsha, prior_commit.hexsha))

                # Found the commit prior to this autorelease. Revert back to it then push
                self.local_repo.git.reset(prior_commit.hexsha, hard=True)
                self.pushLocalCommits(force=True)
                return True

            n_commits_searched += 1

        return False

    def restorePriorToRelease(self):
        info('Restoring "main" to just before autorelease:%s' % self.version)

        self.deleteGitRelease()
        self.rollbackAutoCommits()
        self.deleteTag()
        self.pushLocalCommits(force=True)


class KernelRelease(BaseRelease):
    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):
        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)

        self.repo_name = '%s/FreeRTOS-Kernel' % self.git_org
        self.repo = mGit.get_repo(self.repo_name)
        self.tag = 'V%s' % version
        self.description = 'Contains source code for the FreeRTOS Kernel.'
        self.zip_path = 'FreeRTOS-KernelV%s.zip' % self.version
        self.main_br_version = main_br_version

        # Parent ctor configures local_repo if caller chooses to source local repo from repo_path.
        if self.repo_path is None:
            self.repo_path = 'tmp-release-freertos-kernel'
            if os.path.exists(self.repo_path):
                shutil.rmtree(self.repo_path)

            # Clone the target repo for creating the release autocommits
            remote_name = self.getRemoteEndpoint(self.repo_name)
            info('Downloading %s@%s to baseline auto-commits...' % (remote_name, commit), end='')
            self.local_repo = Repo.clone_from(remote_name, self.repo_path, progress=printDot, branch=self.branch)

        # In case user gave non-HEAD commit to baseline
        self.local_repo.git.checkout(commit)

        print()

    def updateVersionMacros(self, version_str):
        info('Updating version macros in task.h for "%s"' % version_str)

        # Strip out any non-numeric or '.' characters before setting major / minor / build
        (major, minor, build) = re.sub("[^0-9.]", "", version_str).split('.')
        update_freertos_version_macros(os.path.join(self.repo_path, 'include', 'task.h'), version_str, major, minor, build)

        self.commitChanges(self.commit_msg_prefix + 'Bump task.h version macros to "%s"' % version_str)

    def createReleaseZip(self):
        '''
        At the moment, the only asset we upload is the source code.
        '''
        zip_name = 'FreeRTOS-KernelV%s' % self.version
        info('Packaging "%s"' % zip_name)
        logIndentPush()

        # This path name is retained in zip, so we don't name it 'tmp-*' but
        # rather keep it consistent.
        rel_repo_path = zip_name

        # Clean up any old work from previous runs.
        if os.path.exists(rel_repo_path):
            shutil.rmtree(rel_repo_path)

        # Download a fresh copy for packaging.
        info('Downloading fresh copy of %s for packing...' % zip_name, end='')
        packaged_repo = Repo.clone_from(self.getRemoteEndpoint(self.repo_name),
                                        rel_repo_path,
                                        multi_options=['--depth=1', '-b%s' % self.tag, '--recurse-submodules'],
                                        progress=printDot,
                                        branch=self.branch)
        print()

        # Prune then zip package.
        info('Pruning from release zip...', end='')
        files_pruned = prune_result_tree_v2(rel_repo_path, KERNEL_RELEASE_EXCLUDE_FILES)
        print('...%d Files Removed.' % len(files_pruned))

        info('Compressing "%s"...' % self.zip_path)
        with zipfile.ZipFile(self.zip_path, 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zip:
            for root, dirs, files in os.walk(rel_repo_path):
                for file in files:
                    # For some strange reason, we have broken symlinks...avoid these.
                    file_path = os.path.join(root, file)
                    if os.path.islink(file_path) and not os.path.exists(file_path):
                        warning('Skipping over broken symlink "%s"' % file_path)
                    else:
                        zip.write(file_path)

        logIndentPop()

    def createGitRelease(self):
        '''
        Creates/Overwrites release identified by target tag
        '''
        if self.do_not_push:
            info('Skipping creating git release endpoint for "%s"...' % self.tag)
        else:
            # If this release already exists, delete it
            try:
                release_queried = self.repo.get_release(self.tag)

                info('Overwriting existing git release endpoint for "%s"...' % self.tag)
                release_queried.delete_release()
            except UnknownObjectException:
                info('Creating git release endpoint for "%s"...' % self.tag)

            # Create the release asset to upload.
            self.createReleaseZip()

            # Create the new release endpoint at upload assets
            release = self.repo.create_git_release(tag = self.tag,
                                                name = 'V%s' % (self.version),
                                                message = self.description,
                                                draft = False,
                                                prerelease = False)
            info('Uploading release asssets...')
            release.upload_asset(self.zip_path, name='FreeRTOS-KernelV%s.zip' % self.version, content_type='application/zip')

    def autoRelease(self):
        info('Auto-releasing FreeRTOS Kernel V%s' % self.version)

        # Determine if we need to set a separate version macros for the main branch
        if (self.commit == 'HEAD') and len(self.main_br_version) > 0 and (self.main_br_version != self.version):
            # Update version macros for main branch
            self.updateVersionMacros(self.main_br_version)

            # Push the branch
            self.pushLocalCommits()

            # Revert the last commit in our working git repo
            self.local_repo.git.reset('--hard','HEAD^')

        # Update the version macros
        self.updateVersionMacros(self.version)

        if (self.commit == 'HEAD') and (self.main_br_version == self.version):
            # Share a task.h version number commit for main branch and release tag)
            self.pushLocalCommits()

        # When baselining off a non-HEAD commit, main is left unchanged by tagging a detached HEAD,
        # applying the autocommits, tagging, and pushing the new tag data to remote.
        # However in the detached HEAD state we don't have a branch to push to, so we skip

        # Update the header in each c/assembly file
        self.updateFileHeaderVersions(['FreeRTOS Kernel V','FreeRTOS Kernel <DEVELOPMENT BRANCH>'], 'FreeRTOS Kernel V%s' % self.version)

        self.pushTag()

        self.createGitRelease()

        info('Kernel release done.')



class FreertosRelease(BaseRelease):
    def __init__(self, mGit, version, commit, git_ssh=False, git_org='FreeRTOS', repo_path=None, branch='main', do_not_push=False):
        super().__init__(mGit, version, commit, git_ssh=git_ssh, git_org=git_org, repo_path=repo_path, branch=branch, do_not_push=do_not_push)

        self.repo_name = '%s/FreeRTOS' % self.git_org
        self.repo = mGit.get_repo(self.repo_name)
        self.tag = self.version
        self.description = 'Contains source code and example projects for the FreeRTOS Kernel and FreeRTOS+ libraries.'
        self.zip_path = 'FreeRTOSv%s.zip' % self.version

        # Download a fresh copy of local repo for making autocommits, if necessary
        if self.repo_path is None:
            self.repo_path = 'tmp-release-freertos'

            # Clean up any old work from previous runs
            if os.path.exists(self.repo_path):
                shutil.rmtree(self.repo_path)

            # Clone the target repo for creating the release autocommits
            remote_name = self.getRemoteEndpoint(self.repo_name)
            info('Downloading %s@%s to baseline auto-commits...' % (remote_name, commit), end='')
            self.local_repo = Repo.clone_from(remote_name, self.repo_path, progress=printDot, branch=self.branch)

        # In support of non-HEAD baselines
        self.local_repo.git.checkout(commit)
        print()

    def isValidManifestYML(self, path_yml):
        assert False, 'Unimplemented'

    def updateSubmodulePointers(self):
        '''
        Reads the 'manifest.yml' file from the local FreeRTOS clone that is being used to stage the commits
        '''

        info('Initializing first level of submodules...')
        self.local_repo.submodule_update(init=True, recursive=False)

        # Read YML file
        path_manifest = os.path.join(self.repo_path, 'manifest.yml')
        assert os.path.exists(path_manifest), 'Missing manifest.yml'
        with open(path_manifest, 'r') as fp:
            manifest_data = fp.read()
        yml = load(manifest_data, Loader=Loader)
        assert 'dependencies' in yml, 'Manifest YML parsing error'

        # Update all the submodules per yml
        logIndentPush()
        for dep in yml['dependencies']:
            assert 'version' in dep, 'Failed to parse submodule tag from manifest'
            assert 'repository' in dep and 'path' in dep['repository'], 'Failed to parse submodule path from manifest'
            submodule_path = dep['repository']['path']
            submodule_tag  = dep['version']

            # Update the submodule to point to version noted in manifest file
            info('%-20s : %s' % (dep['name'], submodule_tag))
            self.updateSubmodulePointer(os.path.join(self.repo_path, submodule_path), submodule_tag)
        logIndentPop()

        self.commitChanges(self.commit_msg_prefix + 'Bump submodules per manifest.yml for V%s' % self.version)

    def createReleaseZip(self):
        '''
        At the moment, the only asset we upload is the
        '''
        zip_name = 'FreeRTOSv%s' % self.version
        info('Packaging "%s"' % zip_name)
        logIndentPush()

        # This path name is retained in zip, so we don't name it 'tmp-*' but rather keep it consistent with previous
        # packaging
        rel_repo_path = zip_name

        # Clean up any old work from previous runs
        if os.path.exists(rel_repo_path):
            shutil.rmtree(rel_repo_path)

        # Download a fresh copy for packaging
        info('Downloading fresh copy of %s for packing...' % zip_name, end='')
        packaged_repo = Repo.clone_from(self.getRemoteEndpoint(self.repo_name),
                                        rel_repo_path,
                                        multi_options=['--depth=1', '-b%s' % self.tag, '--recurse-submodules'],
                                        progress=printDot,
                                        branch=self.branch)
        print()

        # Prune then zip package
        info('Pruning from release zip...', end='')
        files_pruned = prune_result_tree(rel_repo_path, FREERTOS_RELATIVE_FILE_EXCLUDES)
        print('...%d Files Removed.' % len(files_pruned))

        info('Compressing "%s"...' % self.zip_path)
        with zipfile.ZipFile(self.zip_path, 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zip:
            for root, dirs, files in os.walk(rel_repo_path):
                for file in files:
                    # For some strange reason, we have broken symlinks...avoid these
                    file_path = os.path.join(root, file)
                    if os.path.islink(file_path) and not os.path.exists(file_path):
                        warning('Skipping over broken symlink "%s"' % file_path)
                    else:
                        zip.write(file_path)

        logIndentPop()

    def createGitRelease(self):
        '''
        Creates/Overwrites release identified by target tag
        '''
        if self.do_not_push:
            info('Skipping creating git release endpoint for "%s"...' % self.tag)
        else:
            # If this release already exists, delete it
            try:
                release_queried = self.repo.get_release(self.tag)

                info('Overwriting existing git release endpoint for "%s"...' % self.tag)
                release_queried.delete_release()
            except UnknownObjectException:
                info('Creating git release endpoint for "%s"...' % self.tag)

            # Create the new release endpoind at upload assets
            release = self.repo.create_git_release(tag = self.tag,
                                                name = 'FreeRTOSv%s' % (self.version),
                                                message = self.description,
                                                draft = False,
                                                prerelease = False)

            info('Uploading release asssets...')
            release.upload_asset(self.zip_path, name='FreeRTOSv%s.zip' % self.version, content_type='application/zip')

    def autoRelease(self):
        info('Auto-releasing FreeRTOS V%s' % self.version)

        self.updateFileHeaderVersions(['FreeRTOS Kernel V', 'FreeRTOS V', 'FreeRTOS Kernel <DEVELOPMENT BRANCH>', 'FreeRTOS <DEVELOPMENT BRANCH>'], 'FreeRTOS V%s' % self.version)
        self.updateSubmodulePointers()
        # When baselining off a non-HEAD commit, main is left unchanged by tagging a detached HEAD,
        # applying the autocommits, tagging, and pushing the new tag data to remote.
        # However in the detached HEAD state we don't have a branch to push to, so we skip
        if self.commit == 'HEAD':
            self.pushLocalCommits()

        self.pushTag()
        self.createReleaseZip()
        self.createGitRelease()

        info('Core release done.')

def configure_argparser():
    parser = ArgumentParser(description='FreeRTOS Release tool')

    parser.add_argument('git_org',
                        type=str,
                        metavar='GITHUB_ORG',
                        help='Git organization owner for FreeRTOS and FreeRTOS-Kernel. (i.e. "<git-org>/FreeRTOS.git")')

    parser.add_argument('--new-core-version',
                        default=None,
                        required=False,
                        help='FreeRTOS Standard Distribution Version to replace old version. (Ex. "FreeRTOS V202012.00")')

    parser.add_argument('--core-commit',
                        default='HEAD',
                        required=False,
                        metavar='GITHUB_SHA',
                        help='Github SHA to baseline autorelease')

    parser.add_argument('--rollback-core-version',
                        default=None,
                        required=False,
                        help='Reset "main" to state prior to autorelease of given core version')

    parser.add_argument('--core-repo-path',
                        type=str,
                        default=None,
                        required=False,
                        help='Instead of downloading from git, use existing local repos for autocommits')

    parser.add_argument('--core-repo-branch',
                        type=str,
                        default='main',
                        required=False,
                        help='Branch of FreeRTOS hub repository to release.')

    parser.add_argument('--new-kernel-version',
                        default=None,
                        required=False,
                        help='Reset "main" to just before the autorelease for the specified kernel version")')

    parser.add_argument('--new-kernel-main-br-version',
                        default='',
                        required=False,
                        help='Set the version in task.h on the kernel main branch to the specified value.')

    parser.add_argument('--kernel-commit',
                        default='HEAD',
                        required=False,
                        metavar='GITHUB_SHA',
                        help='Github SHA to baseline autorelease')

    parser.add_argument('--rollback-kernel-version',
                        default=None,
                        required=False,
                        help='Reset "main" to state prior to autorelease of the given kernel version')

    parser.add_argument('--kernel-repo-path',
                        type=str,
                        default=None,
                        required=False,
                        help='Instead of downloading from git, use existing local repos for autocommits')

    parser.add_argument('--kernel-repo-branch',
                        type=str,
                        default='main',
                        required=False,
                        help='Branch of FreeRTOS Kernel repository to release.')

    parser.add_argument('--use-git-ssh',
                        default=False,
                        action='store_true',
                        help='Use SSH endpoints to interface git remotes, instead of HTTPS')

    parser.add_argument('--unit-test',
                        action='store_true',
                        default=False,
                        help='Run unit tests.')

    parser.add_argument('--do-not-push',
                        action='store_true',
                        default=False,
                        help='Do not push the changes but only make local commits.')

    return parser

def main():
    cmd = configure_argparser()
    args = cmd.parse_args()

    # Auth
    if not args.do_not_push:
        assert 'GITHUB_TOKEN' in os.environ, 'Set env{GITHUB_TOKEN} to an authorized git PAT'
    mGit = Github(os.environ.get('GITHUB_TOKEN'))

    # Unit tests
    if args.unit_test:
        return

    # Create Releases
    if args.new_kernel_version:
        info('Starting kernel release...')
        logIndentPush()
        rel_kernel = KernelRelease(mGit, args.new_kernel_version, args.kernel_commit, git_ssh=args.use_git_ssh,
                                   git_org=args.git_org, repo_path=args.kernel_repo_path, branch=args.kernel_repo_branch,
                                   main_br_version=args.new_kernel_main_br_version, do_not_push=args.do_not_push)
        rel_kernel.autoRelease()
        logIndentPop()

    if args.new_core_version:
        info('Starting core release...')
        logIndentPush()
        rel_freertos = FreertosRelease(mGit, args.new_core_version, args.core_commit, git_ssh=args.use_git_ssh,
                                       git_org=args.git_org, repo_path=args.core_repo_path, branch=args.core_repo_branch,
                                       do_not_push=args.do_not_push)
        rel_freertos.autoRelease()
        logIndentPop()

    # Undo autoreleases
    if args.rollback_kernel_version:
        info('Starting kernel rollback...')
        rel_kernel = KernelRelease(mGit, args.rollback_kernel_version, args.kernel_commit, git_ssh=args.use_git_ssh,
                                   git_org=args.git_org, repo_path=args.kernel_repo_path, branch=args.kernel_repo_branch,
                                   do_not_push=args.do_not_push)
        logIndentPush()
        rel_kernel.restorePriorToRelease()
        logIndentPop()

    if args.rollback_core_version:
        info('Starting core rollback...')
        logIndentPush()
        rel_freertos = FreertosRelease(mGit, args.rollback_core_version, args.core_commit, git_ssh=args.use_git_ssh,
                                       git_org=args.git_org, repo_path=args.core_repo_path, branch=args.core_repo_branch,
                                       do_not_push=args.do_not_push)
        rel_freertos.restorePriorToRelease()
        logIndentPop()

    info('Review script output for any unexpected behaviour.')
    info('Done.')

if __name__ == '__main__':
    main()