1#!/usr/bin/env python3 2# ===-- github-upload-release.py ------------------------------------------===# 3# 4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5# See https://llvm.org/LICENSE.txt for license information. 6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7# 8# ===------------------------------------------------------------------------===# 9# 10# Create and manage releases in the llvm github project. 11# 12# This script requires python3 and the PyGithub module. 13# 14# Example Usage: 15# 16# You will need to obtain a personal access token for your github account in 17# order to use this script. Instructions for doing this can be found here: 18# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line 19# 20# Create a new release from an existing tag: 21# ./github-upload-release.py --token $github_token --release 8.0.1-rc4 create 22# 23# Upload files for a release 24# ./github-upload-release.py --token $github_token --release 8.0.1-rc4 upload --files llvm-8.0.1rc4.src.tar.xz 25# 26# You can upload as many files as you want at a time and use wildcards e.g. 27# ./github-upload-release.py --token $github_token --release 8.0.1-rc4 upload --files *.src.* 28# ===------------------------------------------------------------------------===# 29 30 31import argparse 32import github 33import sys 34from textwrap import dedent 35 36 37def create_release(repo, release, tag=None, name=None, message=None): 38 if not tag: 39 tag = "llvmorg-{}".format(release) 40 41 if not name: 42 name = "LLVM {}".format(release) 43 44 if not message: 45 message = dedent( 46 """\ 47 LLVM {} Release 48 49 # A note on binaries 50 51 Volunteers make binaries for the LLVM project, which will be uploaded 52 when they have had time to test and build these binaries. They might 53 not be available directly or not at all for each release. We suggest 54 you use the binaries from your distribution or build your own if you 55 rely on a specific platform or configuration.""" 56 ).format(release) 57 58 prerelease = True if "rc" in release else False 59 60 repo.create_git_release(tag=tag, name=name, message=message, prerelease=prerelease) 61 62 63def upload_files(repo, release, files): 64 release = repo.get_release("llvmorg-{}".format(release)) 65 for f in files: 66 print("Uploading {}".format(f)) 67 release.upload_asset(f) 68 print("Done") 69 70 71parser = argparse.ArgumentParser() 72parser.add_argument( 73 "command", type=str, choices=["create", "upload", "check-permissions"] 74) 75 76# All args 77parser.add_argument("--token", type=str) 78parser.add_argument("--release", type=str) 79parser.add_argument("--user", type=str) 80parser.add_argument("--user-token", type=str) 81 82# Upload args 83parser.add_argument("--files", nargs="+", type=str) 84 85args = parser.parse_args() 86 87gh = github.Github(args.token) 88llvm_org = gh.get_organization("llvm") 89llvm_repo = llvm_org.get_repo("llvm-project") 90 91if args.user: 92 if not args.user_token: 93 print("--user-token option required when --user is used") 94 sys.exit(1) 95 # Validate that this user is allowed to modify releases. 96 user = gh.get_user(args.user) 97 team = ( 98 github.Github(args.user_token) 99 .get_organization("llvm") 100 .get_team_by_slug("llvm-release-managers") 101 ) 102 if not team.has_in_members(user): 103 print("User {} is not a allowed to modify releases".format(args.user)) 104 sys.exit(1) 105elif args.command == "check-permissions": 106 print("--user option required for check-permissions") 107 sys.exit(1) 108 109if args.command == "create": 110 create_release(llvm_repo, args.release) 111if args.command == "upload": 112 upload_files(llvm_repo, args.release, args.files) 113