|
| 1 | +"""This file is specific to Azure SDK for Python and should be split somewhere else.""" |
| 2 | +import logging |
| 3 | +from pathlib import Path |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | + |
| 7 | +from github import Github |
| 8 | + |
| 9 | +from azure_devtools.ci_tools.github_tools import ( |
| 10 | + manage_git_folder, |
| 11 | + DashboardCommentableObject |
| 12 | +) |
| 13 | + |
| 14 | +_LOGGER = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +_STORAGE_ACCOUNT = "http://azuresdkinfrajobstore1.blob.core.windows.net/azure/azure-sdk-for-python/pullrequests/{prnumber}/dist/{file}" |
| 18 | + |
| 19 | +def execute_simple_command(cmd_line, cwd=None, shell=False, env=None): |
| 20 | + try: |
| 21 | + process = subprocess.Popen(cmd_line, |
| 22 | + stderr=subprocess.STDOUT, |
| 23 | + stdout=subprocess.PIPE, |
| 24 | + universal_newlines=True, |
| 25 | + cwd=cwd, |
| 26 | + shell=shell, |
| 27 | + env=env) |
| 28 | + output_buffer = [] |
| 29 | + for line in process.stdout: |
| 30 | + output_buffer.append(line.rstrip()) |
| 31 | + _LOGGER.info(output_buffer[-1]) |
| 32 | + process.wait() |
| 33 | + output = "\n".join(output_buffer) |
| 34 | + if process.returncode: |
| 35 | + raise subprocess.CalledProcessError( |
| 36 | + process.returncode, |
| 37 | + cmd_line, |
| 38 | + output |
| 39 | + ) |
| 40 | + return output |
| 41 | + except Exception as err: |
| 42 | + _LOGGER.error(err) |
| 43 | + raise |
| 44 | + else: |
| 45 | + _LOGGER.info("Return code: %s", process.returncode) |
| 46 | + |
| 47 | +def build_package_from_pr_number(gh_token, sdk_id, pr_number, output_folder, *, with_comment=False): |
| 48 | + """Will clone the given PR branch and vuild the package with the given name.""" |
| 49 | + |
| 50 | + con = Github(gh_token) |
| 51 | + repo = con.get_repo(sdk_id) |
| 52 | + sdk_pr = repo.get_pull(pr_number) |
| 53 | + # "get_files" of Github only download the first 300 files. Might not be enough. |
| 54 | + package_names = {f.filename.split('/')[0] for f in sdk_pr.get_files() if f.filename.startswith("azure")} |
| 55 | + absolute_output_folder = Path(output_folder).resolve() |
| 56 | + |
| 57 | + with tempfile.TemporaryDirectory() as temp_dir, \ |
| 58 | + manage_git_folder(gh_token, Path(temp_dir) / Path("sdk"), sdk_id, pr_number=pr_number) as sdk_folder: |
| 59 | + |
| 60 | + for package_name in package_names: |
| 61 | + _LOGGER.debug("Build {}".format(package_name)) |
| 62 | + execute_simple_command( |
| 63 | + ["python", "./build_package.py", "--dest", str(absolute_output_folder), package_name], |
| 64 | + cwd=sdk_folder |
| 65 | + ) |
| 66 | + _LOGGER.debug("Build finished: {}".format(package_name)) |
| 67 | + |
| 68 | + if with_comment: |
| 69 | + files = [f.name for f in absolute_output_folder.iterdir()] |
| 70 | + comment_message = None |
| 71 | + dashboard = DashboardCommentableObject(sdk_pr, "(message created by the CI based on PR content)") |
| 72 | + try: |
| 73 | + installation_message = build_installation_message(sdk_pr) |
| 74 | + download_message = build_download_message(sdk_pr, files) |
| 75 | + comment_message = installation_message + "\n\n" + download_message |
| 76 | + dashboard.create_comment(comment_message) |
| 77 | + except Exception: |
| 78 | + _LOGGER.critical("Unable to do PR comment:\n%s", comment_message) |
| 79 | + |
| 80 | +def build_download_message(sdk_pr, files): |
| 81 | + if not files: |
| 82 | + return "" |
| 83 | + message = "# Direct download\n\nYour files can be directly downloaded here:\n\n" |
| 84 | + for filename in files: |
| 85 | + message += "- [{}]({})\n".format( |
| 86 | + filename, |
| 87 | + _STORAGE_ACCOUNT.format(prnumber=sdk_pr.number, file=filename) |
| 88 | + ) |
| 89 | + return message |
| 90 | + |
| 91 | +def build_installation_message(sdk_pr): |
| 92 | + # Package starts with "azure" and is at the root of the repo |
| 93 | + package_names = {f.filename.split('/')[0] for f in sdk_pr.get_files() if f.filename.startswith("azure")} |
| 94 | + |
| 95 | + result = ["# Installation instruction"] |
| 96 | + for package in package_names: |
| 97 | + result.append("## Package {}".format(package)) |
| 98 | + result.append(pr_message_for_package(sdk_pr, package)) |
| 99 | + return "\n".join(result) |
| 100 | + |
| 101 | + |
| 102 | +def pr_message_for_package(sdk_pr, package_name): |
| 103 | + git_path = '"git+{}@{}#egg={}&subdirectory={}"'.format( |
| 104 | + sdk_pr.head.repo.html_url, |
| 105 | + sdk_pr.head.ref, |
| 106 | + package_name, |
| 107 | + package_name |
| 108 | + ) |
| 109 | + |
| 110 | + pip_install = 'pip install {}' |
| 111 | + pip_wheel = 'pip wheel --no-deps {}' |
| 112 | + |
| 113 | + pr_body = "You can install the package `{}` of this PR using the following command:\n\t`{}`".format( |
| 114 | + package_name, |
| 115 | + pip_install.format(git_path) |
| 116 | + ) |
| 117 | + |
| 118 | + pr_body += "\n\n" |
| 119 | + |
| 120 | + pr_body += "You can build a wheel to distribute for test using the following command:\n\t`{}`".format( |
| 121 | + pip_wheel.format(git_path) |
| 122 | + ) |
| 123 | + |
| 124 | + pr_body += "\n\n" |
| 125 | + pr_body += "If you have a local clone of this repository, you can also do:\n\n" |
| 126 | + pr_body += "- `git checkout {}`\n".format(sdk_pr.head.ref) |
| 127 | + pr_body += "- `pip install -e ./{}`\n".format(package_name) |
| 128 | + pr_body += "\n\n" |
| 129 | + pr_body += "Or build a wheel file to distribute for testing:\n\n" |
| 130 | + pr_body += "- `git checkout {}`\n".format(sdk_pr.head.ref) |
| 131 | + pr_body += "- `pip wheel --no-deps ./{}`\n".format(package_name) |
| 132 | + return pr_body |
0 commit comments